#if 1 /* WILY BOSS FIGHTER */
    static const unsigned framelimit = 56950;
    /* Note: loop and local variables need to be "static"
     * in this function because of the co-routine mechanism.
     * RunFrameWith actually returns from this function, and
     * goto's to the same spot after the frame has been run.
     */
    #define NXT { if(BqtDamaged) \
                    { fail_line = __LINE__; goto WalkFail; } \
                  if(framecount >= framelimit) \
                    { fail_line = __LINE__; goto NextFail; } }

    #define RunFramesWhile(condition, input, maxframes, if_fail) \
       ({ static signed long cnt, maxv; \
          for(maxv=(maxframes), cnt=0; (condition); ++cnt) { \
            if(cnt >= maxv) { fail_line = __LINE__; if_fail; } \
            static unsigned in; \
            in = (input); \
            /* This hack adds walking (in the beginning of the clip) \
             * to the input. The XS comparison checks if Rockman is \
             * deaccelerating, and pushes Right once to instantly \
             * decelerate to almost halt. \
             */ \
            if(walk_done < walk_length) { in |= K_R; } \
            else if(BqtRockXS == 0x0080) { in |= K_R; } \
            ++walk_done; \
            RunFrameWith(in); \
            /* Load updated coordinates */ \
            /* BqtBossHit, BqtBossRemHp, BqtSelPushed, BqtDamaged \
             * and BqtFiring are updated by emulation core (by tracking \
             * which ROM addresses are executed. They can't be read from RAM.) \
             */ \
            BqtRockY = RAM[0x600]; \
            BqtRockXS = RAM[0x4C0]*256+RAM[0x4E0]; \
            NXT; \
          } \
          cnt; /* return value */ \
        })

    if(!FCEUI_EmulationPaused()) FCEUI_ToggleEmulationPause();
    static unsigned prefix;
    static unsigned frames_best;
    static SaveState begin_anchor, best;
    static int fail_line;
    begin_anchor.Create();
    frames_best=9999999;
    fprintf(stderr, "Frame counter at start: %u\n", (unsigned)framecount);
    
    for(prefix=39; prefix<=40; ++prefix)
    {
        static unsigned walk_length;
        for(walk_length=26; walk_length<30/*prefix+18*/; ++walk_length)
        {
            __label__ NextFail;
            static std::string current_option;
            { char Buf[512];
              sprintf(Buf, "idle=%u[walk=%u]",
                prefix, walk_length);
              current_option = Buf;
            }

            begin_anchor.Load();
            BqtRockY=255;
            BqtFiring=0;
            BqtBossHit=0;
            BqtDamaged=0;
            
            /* Sit still (or walk), then jump to the height that works,
             * and shoot, and wait until the boss registered the damage.
             */
            static unsigned walk_done, jump_done;
            walk_done = 0; jump_done = 0;
            RunFramesWhile(true, 0,   prefix, break);
            RunFramesWhile(BqtRockY > 134, K_A, 26,   goto FailPrefix);
            RunFramesWhile(!BqtFiring, K_B, 3, goto FailPrefix);
            RunFramesWhile(!BqtBossHit, 0, 20, goto FailPrefix);
            /* If we requested more walking than there was action frames
             * useable, break the loop because the work was already done once. */
            if(walk_done < walk_length) break;
            
            static SaveState action_begin;
            action_begin.Create();
            
            static unsigned undu_delay; // number of loops to skip from the undulation cycle
            static unsigned begin_pausestart; // number of times to use Start in the beginning
            static unsigned end_pausestart;   // number of times to use Start in the end
            static unsigned end_skip;         // number of times to not use Start in the end, after the Starts
            static unsigned pausestart_hp;    // save this value so it can be restored in each loop
            pausestart_hp = BqtBossRemHp;
            for(undu_delay=1; undu_delay-->0; )
              for(begin_pausestart = 0; begin_pausestart < 4; ++begin_pausestart)
                for(end_pausestart = 0; end_pausestart < 8-begin_pausestart; ++end_pausestart)
                  for(end_skip=0; end_skip < 4; ++end_skip)
                  {
                      if(end_skip > 0 && end_pausestart == 0) break;
                      
                      __label__ NextFail;
                      static std::string pause_option;
                      { char Buf[512];
                        sprintf(Buf, ",start=[%u,%u,%u,%u]",
                          begin_pausestart, end_pausestart, end_skip, undu_delay);
                        pause_option = current_option + Buf;
                      }

                      BqtBossRemHp = pausestart_hp;
                      action_begin.Load();
              
                      static SaveState last_action; /* savestate for movie ending frame candidate */
                      
                      /* As long as boss has HP left, run this loop. */
                      /* At the beginning of each loop, the game is running
                       * with the boss just having suffered a hit.
                       */
                      static unsigned hit_counter, undu_counter;
                      undu_counter = 0;
                      for(hit_counter = 0; BqtBossRemHp; ++hit_counter)
                      {
                          if(framecount + (BqtBossRemHp-1)*7 > framelimit)
                              { fail_line = __LINE__;
                                if(BqtBossRemHp > end_pausestart+end_skip)
                                    goto FailEndPausePoint;
                                goto NextFail; }
                          
                          /* Hold Select until the game gets paused
                           * (may take longer than 1 frame due to lag).
                           */
                          BqtSelPushed=0;
                          RunFramesWhile(!BqtSelPushed, K_SE, 3, goto FailPauseMethod);
                          
                          /* Check which method we will use to unpause */
                          static bool UnpauseWithStart;
                          UnpauseWithStart = false;
                          if(hit_counter < begin_pausestart) UnpauseWithStart = true;
                          else if(end_skip && BqtBossRemHp == 1) UnpauseWithStart = false;
                          else if(BqtBossRemHp < end_pausestart + end_skip) UnpauseWithStart = true;
                          
                          if(!UnpauseWithStart)
                          {
                              /* Unpause with SELECT */
                              RunFrameWith(0);    NXT;
                              RunFrameWith(K_SE); NXT;
                              last_action.Create();

                              /* Wait until boss registered damage */
                              BqtBossHit=BqtBossRemHp=0;
                              if(undu_counter >= undu_delay)
                              {
                                  /* undulate up&down while pausing (Bisqwit's style) */
                                  static SaveState anc2;
                                  static long k;
                                  for(k=0; !BqtBossHit; ++k)
                                  {
                                      if(k >= 27) goto FailPauseMethod;
                                      anc2.Create();
                                      RunFrameWith(K_R);
                                  }
                                  anc2.Load();
                                  RunFrameWith(K_R | K_A);
                              }
                              else
                              {
                                  /* simple version of above */
                                  RunFramesWhile(!BqtBossHit, K_R, 27, goto FailPauseMethod);
                              }
                              ++undu_counter;
                          }
                          else
                          {
                              /* Unpause with START */
                              /* Check how many frames of idle are required before
                               * pressing Start will immediately yield damage.
                               * Immediateness is used to avoid lag.
                               */
                              static unsigned n;
                              for(n=0; ; )
                              {
                                  static SaveState anc2;
                                  anc2.Create();

                                  /* Try pressing start. */
                                  BqtBossHit=BqtBossRemHp=0;
                                  RunFrameWith(K_ST); NXT;
                                  if(BqtBossHit) { last_action.Create(); break; }
                                  
                                  /* If too many frames have elapsed, either the shot
                                   * won't hit the boss at all, or it will take too long.
                                   */
                                  if(++n >= 10) { fprintf(stderr, "Missed the boss! hitcounter=%u\n", hit_counter);
                                                  fail_line = __LINE__; goto FailEndPausePoint; }

                                  /* If the boss didn't suffer, undo Start, idle 1 frame, and try again. */
                                  anc2.Load();
                                  RunFrameWith(0); NXT;
                              }
                          }
                      } /* repeat until the boss's HP is exhausted */
                      
                      last_action.Load(); /* Shorten the movie to minimal length */
                      
                      fprintf(stderr, "With %s, it ends at frame %u!\n",
                          pause_option.c_str(), (unsigned)framecount);
                      if(framecount <= frames_best)
                      {
                          frames_best = framecount;
                          best.Create();
                      }
                      
                      continue;
                  FailEndPausePoint:
                      end_pausestart=100; end_skip=10;
                  FailPauseMethod:
                  NextFail:
                      fprintf(stderr, "%s is not acceptable (fail_line=%d,framecount=%u)\n",
                          pause_option.c_str(), fail_line, (unsigned)framecount);
                  }
            
            continue;
        WalkFail:
            walk_length=999;
        FailPrefix:
        NextFail:
            fprintf(stderr,
                "%s is not acceptable (fail_line=%d, Y=%d, frno=%u)\n",
                current_option.c_str(), fail_line, BqtRockY, (unsigned)framecount);
        }
    }
    fprintf(stderr, "Loading best savestate: %u frames\n", frames_best);
    best.Load();
    BotFrontEnd();
    FCEUSS_Save("botfront-end");
    goto Restart;
#endif