// File: /workspace/github_cpp_makefile_star2_O2/khedoros[P]khednes/main.cpp
// Function: main
int main(int argc, char ** argv) {
    opterr = 0;
    char option;
    int res=1;
    int mapper=0;
    //cout<<"Argc: "<<argc<<endl;
    if(argc==1) {
        cout<<"Usage:\n\n./"<<argv[0]<<" [-d #] [-m #] <filename>\n\n\tOptions:\n\t-d Enables debug output. Options for # are:\n\t\t1\tCPU\n\t\t2\tMAIN\n\t\t3\tMEM\n\t\t4\tPPU\n\t\t5\tROM\n\t\t6\tUTIL\n\t\t7\tWAV\n\t\t7\tAPU\n";
        cout<<"\n\t-m manually specifies an iNES mapper number to use.\n";
    }

    while(argc>3&&(option=getopt(argc,argv,"d:x:m:"))!=-1) {
        switch(option) {
        case 'd':
            util::toggle_log(atoi(optarg));
            break;
        case 'x':
            res=atoi(optarg);
            break;
        case 'm':
            mapper=atoi(optarg);
            break;
        default:
            cerr<<"Unkown option '"<<optopt<<".\n";
            return 1;
        }
    }
    if(res==0)
        res=1; 
    std::string filename;
    if(optind<argc||argc==2) {
        filename=std::string(argv[optind]);
        cout<<"Loading "<<filename<<"..."<<endl;
    }
    else {
        cout<<"Give a single filename, like this: \n\n\t"<<argv[0]<<" rom.nes"<<endl;        
        return 1;
    }
        
//        if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 ) {
    bool headless = false;
    if( SDL_Init(SDL_INIT_EVERYTHING|SDL_INIT_JOYSTICK|SDL_INIT_GAMECONTROLLER|SDL_INIT_NOPARACHUTE) < 0 ) {
        if( SDL_Init(SDL_INIT_NOPARACHUTE|SDL_INIT_AUDIO) < 0) {
            fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
            exit(1);
        }
        headless = true;
        cout<<"Running in headless mode due to difficulty initializing some SDL subsystems."<<endl;
    }

    //cout<<"Trying to open the joystick (if any)"<<endl;

    SDL_Joystick * js = NULL;
    int jscount = SDL_NumJoysticks();
    if(jscount > 0) {
        for(int i=0;i<jscount;++i) {
            const char * name = SDL_JoystickNameForIndex(i);
            cout<<"Found joystick: "<<name<<endl;
        }
        cout<<"Just opening joystick 0 though"<<endl;
        js = SDL_JoystickOpen(0);
    }

    //cout<<"Done with joystick stuff"<<endl;

    //cout<<"Going to load cartridge"<<endl;
    rom cart(filename, mapper);
    //cout<<"Cart loaded. Going to start ppu"<<endl;
    ppu ppui(cart,res);
    //cout<<"PPU started. Going to start apu."<<endl;
    if(ppui.dummy) {
        headless = true;
        cout<<"PPU started in dummy mode, so audio will too."<<endl;
    }
    apu apui(headless);
    SDL_PauseAudioDevice(apui.get_id(), true);
    //cout<<"APU started. Going to bring up memory map"<<endl;
    mem memi(cart,ppui,apui);
    apui.setmem(&memi);
    //cout<<"Memory started. Going to start CPU"<<endl;
    cpu cpui(&memi,&apui,memi.get_rst_addr(), cart.isNSF());
    //cout<<"CPU started."<<endl;

    if(cart.isNSF()) {
        cout<<"Starting NSF player instead of main emulator"<<endl;
        return run_nsf(cart, apui, memi, cpui);
    }

    bool paused=false;
    //int time_stop;
    //long total_time;
    int pstart = SDL_GetTicks();
    //int delay = 0;
    //long delay_count = 0;
    //long delay_total = 0;
    SDL_PauseAudioDevice(apui.get_id(), false);
    while (1==1) {
        //cout<<"Start of event loop."<<endl;
        //int frame_start = SDL_GetTicks();
        SDL_Event event;
        //int curx,cury;
        int newx,newy;
        //bool changed;
        //cout<<"Polling and handling events"<<endl;
        while(SDL_PollEvent(&event)){  /* Loop until there are no events left on the queue */
            switch(event.type){  /* Process the appropiate event type */
            case SDL_JOYBUTTONDOWN:
            case SDL_JOYBUTTONUP:
                {
                SDL_Scancode sc;
                int button = int(event.jbutton.button);
                int state = int(event.jbutton.state);
                //cout<<"Button: "<<state<<" state: "<<state<<endl;
                cout<<"Saw button: "<<button<<endl;
                switch(button) {
                case 0: sc = SDL_SCANCODE_L; break;
                case 1: sc = SDL_SCANCODE_K; break;
                case 4: sc = SDL_SCANCODE_G; break;
                case 5: sc = SDL_SCANCODE_H; break;
                case 8: sc = SDL_SCANCODE_W; break;
                case 9: sc = SDL_SCANCODE_S; break;
                case 10: sc = SDL_SCANCODE_A; break;
                case 11: sc = SDL_SCANCODE_D; break;
                default: sc = SDL_SCANCODE_UNKNOWN;
                }
                if(state == 1)
                    memi.sendkeydown(sc);
                else
                    memi.sendkeyup(sc);
                }
                break;
            case SDL_JOYDEVICEADDED:
            case SDL_CONTROLLERDEVICEADDED:
                cout<<"Added joystick "<<event.jdevice.which<<": "<<SDL_JoystickNameForIndex(event.jdevice.which)<<endl;
                if(js) {
                    SDL_JoystickClose(js);
                    js = NULL;
                }
                if(!js) {
                    cout<<"No joystick previously connected, so I'm using that one."<<endl;
                    js = SDL_JoystickOpen(event.jdevice.which);
                }
                break;
            case SDL_JOYDEVICEREMOVED:
            case SDL_CONTROLLERDEVICEREMOVED:
                cout<<"Removed joystick: "<<SDL_JoystickNameForIndex(event.jdevice.which)<<endl;
                if(js) {
                    SDL_JoystickClose(js);
                    js = NULL;
                }
                break;
            case SDL_KEYDOWN:  /* Handle a KEYDOWN event */         
                if(event.key.keysym.scancode==SDL_SCANCODE_Q||
                  (event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_RCTRL))||
                  (event.key.keysym.scancode==SDL_SCANCODE_C&&(event.key.keysym.mod==KMOD_LCTRL))) {
                    printf("You pressed q or ctrl-c. Exiting.\n");
                    cpui.print_details(filename);
                    printf("%d frames rendered in %f seconds. (%f FPS)\n",ppui.get_frame(),float(clock())/float(CLOCKS_PER_SEC),float(ppui.get_frame())/(float(clock())/float(CLOCKS_PER_SEC)));
                    //SDL_PauseAudio(true);
                    cout<<"Calling SDL_Quit()"<<endl;
                    SDL_Quit();
                    cout<<"Called SDL_Quit()"<<endl;
                    return 0;
                }
                else if(event.key.keysym.scancode==SDL_SCANCODE_R) {
                    cpui.reset(memi.get_rst_addr());
                }
                else if(event.key.keysym.scancode==SDL_SCANCODE_P) {
                    paused=!paused;
                    if(paused) {
                        SDL_PauseAudioDevice(apui.get_id(), true);
                        printf("Paused emulation.\n");
                    }
                    else {
                        SDL_PauseAudioDevice(apui.get_id(), false);
                        printf("Unpaused emulation.\n");
                    }
                }
                else if(event.key.keysym.scancode >= SDL_SCANCODE_1 &&
                        event.key.keysym.scancode <= SDL_SCANCODE_0) {

                    std::cout<<"Saw scancode "<<event.key.keysym.scancode<<endl;
                    printf(" debug output #%d",int(event.key.keysym.scancode- (SDL_SCANCODE_1 - 1)));
                    //util::toggle_log(event.key.keysym.scancode-48);
                    printf("\n");
                }
                else if(event.key.keysym.scancode==SDL_SCANCODE_TAB) {
                    ppui.toggle_table_view();
                }
                else {
                    //printf("Keydown\n");
                    //printf("Time: %d\n",SDL_GetTicks());
                    memi.sendkeydown(event.key.keysym.scancode);
                }
                break;
            case SDL_KEYUP: /* Handle a KEYUP event*/
                //printf("Keyup\n");
                memi.sendkeyup(event.key.keysym.scancode);
                break;
            case SDL_MOUSEMOTION:
                memi.sendmousepos(event.motion.x, event.motion.y);
                break;
            case SDL_MOUSEBUTTONDOWN:
                memi.sendmousedown(event.button.x,event.button.y);
                break;
            case SDL_MOUSEBUTTONUP:
                memi.sendmouseup(event.button.x,event.button.y);
                break;
            //case SDL_VIDEORESIZE:
            case SDL_WINDOWEVENT:
                switch(event.window.event) {
                case SDL_WINDOWEVENT_SIZE_CHANGED:
                    newx=event.window.data1;
                    newy=event.window.data2;
                    if(newx > 0 && newy > 0) {
                        //changed=true;
                    }
                    //ppui.resize(newx,newy);
                    break;
                case SDL_WINDOWEVENT_EXPOSED:
                    //ppui.resize(1,1);
                    break;
                }
                break;
            case SDL_QUIT:
                //SDL_PauseAudio(true);
                SDL_Quit();
                cpui.print_details(filename);
                printf("%d frames rendered in %f seconds. (%f FPS)\n",ppui.get_frame(),float(clock())/float(CLOCKS_PER_SEC),float(ppui.get_frame())/(float(clock())/float(CLOCKS_PER_SEC)));
                return 0;
                break;
            default: /* Report an unhandled event */
                //printf("I don't know what this event is! Flushing it.\n");
                SDL_FlushEvent(event.type);
                break;
            }
        }
        if(!paused) {
            //cout<<"Running the CPU"<<endl;
            int cpu_ret=cpui.run_ops();
            //cout<<"Ran the CPU"<<endl;
            if(cpu_ret < 0) {
                return 1;
            }
            else {
                //cout<<"Frame "<<ppui.get_frame()<<endl;

                int apu_id = apui.get_id();
                if(apu_id > 0) {
                    //cout<<"Locking audio...";
                    SDL_LockAudioDevice(apu_id);
                    //cout<<"done."<<endl;
                    apui.set_frame(ppui.get_frame());
                    apui.gen_audio();
                    //apui.dat.buffered += 735; Dude, do this in gen_audio, not here =/
                    //cout<<"Unlocking audio...";
                    SDL_UnlockAudioDevice(apu_id);
                    //cout<<"done."<<endl;
                }
                //cout<<SDL_GetTicks()<<": Frame: "<<ppui.get_frame()<<" Write pos: "<<apui.write_pos<<endl;
                //cout<<"Running the PPU"<<endl;
                int ppu_ret=ppui.calc();
                //cout<<"Ran the PPU"<<endl;
                if(ppu_ret>0) {
                    if(ppu_ret==1) {
                        //cout<<"Triggering NMI in CPU"<<endl;
                        cpui.trigger_nmi();
                    }
                }
            }
        }

        //Calculates the time that the frame should end at, and delays until that time
        //cout<<"Timing two"<<endl;
        int frames = ppui.get_frame();
        int now = SDL_GetTicks();
        int delay = pstart + int(double(frames) * double(1000) / double(60)) - now;
        //cout<<"Frame: "<<frames<<" now: "<<now<<" delay: "<<delay<<endl;       
        if(delay > 0) {
            SDL_Delay(delay);
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x221e8, %rsp          # imm = 0x221E8
movq %rsi, %rbx
movl %edi, %r15d
movq 0x1420e(%rip), %rax     # 0x1afc8
andl $0x0, (%rax)
cmpl $0x1, %edi
jne 0x6e01
movq 0x141e7(%rip), %r14     # 0x1afb0
leaq 0xb248(%rip), %rsi      # 0x12018
movq %r14, %rdi
callq 0x4390
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x4390
leaq 0xb239(%rip), %rsi      # 0x12023
movq %rax, %rdi
callq 0x4390
leaq 0xb2bf(%rip), %rsi      # 0x120b8
movq %r14, %rdi
callq 0x4390
pushq $0x1
popq %rbp
movl $0x0, (%rsp)
leaq 0xb2dd(%rip), %r12      # 0x120ef
movq 0x1417f(%rip), %r14     # 0x1af98
movl %ebp, %r13d
cmpl $0x4, %r15d
jl 0x6e7b
movl %r15d, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x40c0
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x6e84
sarl $0x18, %eax
cmpl $0x64, %eax
jne 0x6e53
movq (%r14), %rdi
callq 0x4550
movl %eax, %edi
callq 0xe244
jmp 0x6e1c
cmpl $0x6d, %eax
je 0x6e6e
cmpl $0x78, %eax
jne 0x7001
movq (%r14), %rdi
callq 0x4550
movl %eax, %r13d
jmp 0x6e1c
movq (%r14), %rdi
callq 0x4550
movl %eax, (%rsp)
jmp 0x6e1c
movl $0x0, (%rsp)
jmp 0x6e87
movl %r13d, %ebp
cmpl $0x1, %ebp
adcl $0x0, %ebp
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x1411b(%rip), %rax     # 0x1afc0
movslq (%rax), %rax
cmpl %r15d, %eax
setge %cl
cmpl $0x2, %r15d
setne %dl
testb %cl, %dl
jne 0x6fc1
movq (%rbx,%rax,8), %rsi
leaq 0x980(%rsp), %rdi
leaq 0x458(%rsp), %rdx
callq 0x4150
leaq 0x50(%rsp), %rdi
leaq 0x980(%rsp), %rbx
movq %rbx, %rsi
callq 0x4410
movq %rbx, %rdi
callq 0x41b0
movq 0x140b6(%rip), %rdi     # 0x1afb0
leaq 0xb205(%rip), %rsi      # 0x12106
callq 0x4390
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x4330
leaq 0xb1f5(%rip), %rsi      # 0x1210f
movq %rax, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movl $0x10f231, %edi         # imm = 0x10F231
callq 0x4340
movl %eax, 0x8(%rsp)
cmpl $0x0, 0x8(%rsp)
jns 0x6f6c
movl $0x100010, %edi         # imm = 0x100010
callq 0x4340
testl %eax, %eax
js 0x7833
movq 0x14058(%rip), %rdi     # 0x1afb0
leaq 0xb201(%rip), %rsi      # 0x12160
callq 0x4390
movq %rax, %rdi
callq 0x41c0
callq 0x4240
movl %eax, %r12d
xorl %ebx, %ebx
testl %eax, %eax
jle 0x7064
movq 0x1402b(%rip), %r13     # 0x1afb0
leaq 0xb221(%rip), %r14      # 0x121ad
cmpl %ebx, %r12d
je 0x703f
movl %ebx, %edi
callq 0x4470
movq %rax, %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x4390
movq %rax, %rdi
movq %r15, %rsi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
incl %ebx
jmp 0x6f8c
movq 0x13fe8(%rip), %rdi     # 0x1afb0
leaq 0xb144(%rip), %rsi      # 0x12113
callq 0x4390
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x4390
leaq 0xb154(%rip), %rsi      # 0x1213a
movq %rax, %rdi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
pushq $0x1
popq %rax
movl %eax, (%rsp)
jmp 0x7814
movq 0x13fe8(%rip), %rdi     # 0x1aff0
leaq 0xb0e7(%rip), %rsi      # 0x120f6
callq 0x4390
movq 0x13f8d(%rip), %rcx     # 0x1afa8
movl (%rcx), %esi
movq %rax, %rdi
callq 0x4610
leaq 0xb537(%rip), %rsi      # 0x12563
movq %rax, %rdi
callq 0x4390
pushq $0x1
popq %rax
movl %eax, (%rsp)
jmp 0x781e
movq 0x13f6a(%rip), %rdi     # 0x1afb0
leaq 0xb171(%rip), %rsi      # 0x121be
callq 0x4390
movq %rax, %rdi
callq 0x41c0
xorl %edi, %edi
callq 0x4190
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4490
leaq 0x140(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl (%rsp), %edx
callq 0x7f30
leaq 0x70(%rsp), %rdi
callq 0x41b0
leaq 0x980(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movl %ebp, %edx
callq 0xc3bc
cmpb $0x1, 0x221e1(%rsp)
movl 0x8(%rsp), %ebp
jne 0x70d7
movq 0x13ef2(%rip), %rdi     # 0x1afb0
leaq 0xb118(%rip), %rsi      # 0x121dd
callq 0x4390
movb $0x1, %bpl
movq %rax, %rdi
callq 0x41c0
jmp 0x70da
shrl $0x1f, %ebp
movzbl %bpl, %esi
leaq 0x210(%rsp), %rdi
callq 0xede2
leaq 0x210(%rsp), %rdi
callq 0xe90c
pushq $0x1
popq %rsi
movl %eax, %edi
movl %esi, (%rsp)
callq 0x4440
leaq 0x90(%rsp), %rdi
leaq 0x140(%rsp), %rsi
leaq 0x980(%rsp), %rdx
leaq 0x210(%rsp), %rcx
callq 0x947c
leaq 0x210(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xef5a
leaq 0x90(%rsp), %rdi
callq 0x9b80
movl %eax, %r14d
leaq 0x140(%rsp), %rdi
callq 0x8e62
movzbl %al, %r8d
leaq 0x458(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x210(%rsp), %rdx
movl %r14d, %ecx
callq 0x9e54
leaq 0x140(%rsp), %rdi
callq 0x8e62
testb %al, %al
je 0x71d9
movq 0x13e18(%rip), %rdi     # 0x1afb0
leaq 0xb06c(%rip), %rsi      # 0x1220b
callq 0x4390
movq %rax, %rdi
callq 0x41c0
leaq 0x140(%rsp), %rdi
leaq 0x210(%rsp), %rsi
leaq 0x90(%rsp), %rdx
leaq 0x458(%rsp), %rcx
callq 0x7900
movl %eax, (%rsp)
jmp 0x77d3
callq 0x43c0
movl %eax, 0x14(%rsp)
leaq 0x210(%rsp), %rdi
callq 0xe90c
movl %eax, %edi
xorl %esi, %esi
callq 0x4440
movq $0x0, 0x8(%rsp)
leaq 0x18(%rsp), %r15
movq 0x13da3(%rip), %r13     # 0x1afb0
leaq 0x90(%rsp), %r14
movq %rbx, %rax
movq %rax, %rbx
movq %r15, %rdi
callq 0x4220
testl %eax, %eax
je 0x74e3
movl 0x18(%rsp), %edi
leal -0x603(%rdi), %eax
cmpl $0x2, %eax
jb 0x7341
movq %rbx, %rax
cmpl $0x200, %edi            # imm = 0x200
je 0x7218
cmpl $0x300, %edi            # imm = 0x300
je 0x73e8
cmpl $0x301, %edi            # imm = 0x301
je 0x7489
cmpl $0x400, %edi            # imm = 0x400
je 0x7441
cmpl $0x401, %edi            # imm = 0x401
je 0x7471
cmpl $0x402, %edi            # imm = 0x402
je 0x7459
cmpl $0x605, %edi            # imm = 0x605
je 0x72c6
cmpl $0x606, %edi            # imm = 0x606
je 0x739d
cmpl $0x653, %edi            # imm = 0x653
je 0x72c6
cmpl $0x654, %edi            # imm = 0x654
je 0x739d
cmpl $0x100, %edi            # imm = 0x100
je 0x773c
callq 0x4690
movq %rbx, %rax
jmp 0x7218
movq %r13, %rdi
leaq 0xaf75(%rip), %rsi      # 0x12245
callq 0x4390
movl 0x20(%rsp), %esi
movq %rax, %rdi
callq 0x4610
movq %rax, %rdi
leaq 0xc88a(%rip), %rsi      # 0x13b75
callq 0x4390
movq %rax, %r12
movl 0x20(%rsp), %edi
callq 0x4470
movq %r12, %rdi
movq %rax, %rsi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
testq %rbx, %rbx
je 0x731c
movq %rbx, %rdi
callq 0x4180
movq %r13, %rdi
leaq 0xaf2f(%rip), %rsi      # 0x12255
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movl 0x20(%rsp), %edi
callq 0x4190
jmp 0x7218
movzbl 0x24(%rsp), %r12d
movb 0x25(%rsp), %bpl
movq %r13, %rdi
leaq 0xaee2(%rip), %rsi      # 0x12238
callq 0x4390
movq %rax, %rdi
movl %r12d, %esi
callq 0x4610
movq %rax, %rdi
callq 0x41c0
xorl %esi, %esi
cmpb $0xc, %r12b
jae 0x7383
movl %r12d, %eax
leaq 0xb054(%rip), %rcx      # 0x123d4
movl (%rcx,%rax,4), %esi
cmpb $0x1, %bpl
jne 0x749d
movq %r14, %rdi
callq 0x95b4
movq %rbx, %rax
jmp 0x7218
movq %r13, %rdi
leaq 0xaee7(%rip), %rsi      # 0x1228e
callq 0x4390
movq %rax, %r12
movl 0x20(%rsp), %edi
callq 0x4470
movq %r12, %rdi
movq %rax, %rsi
callq 0x4390
movq %rax, %rdi
callq 0x41c0
movl $0x0, %eax
testq %rbx, %rbx
je 0x7218
movq %rbx, %rdi
callq 0x4180
xorl %eax, %eax
jmp 0x7218
movl 0x28(%rsp), %esi
cmpl $0x14, %esi
je 0x765e
movl 0x30(%rsp), %eax
addl $-0x40, %eax
testw $0xffbf, %ax            # imm = 0xFFBF
sete %al
cmpl $0x6, %esi
jne 0x7410
testb %al, %al
jne 0x765e
cmpl $0x13, %esi
je 0x75ba
cmpl $0x15, %esi
jne 0x74ad
movq %r14, %rdi
callq 0x9b80
leaq 0x458(%rsp), %rdi
movl %eax, %esi
callq 0xb3b4
movq %rbx, %rax
jmp 0x7218
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
movq %r14, %rdi
callq 0x96c6
movq %rbx, %rax
jmp 0x7218
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
movq %r14, %rdi
callq 0x96ce
movq %rbx, %rax
jmp 0x7218
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
movq %r14, %rdi
callq 0x96d4
movq %rbx, %rax
jmp 0x7218
movl 0x28(%rsp), %esi
movq %r14, %rdi
callq 0x9650
movq %rbx, %rax
jmp 0x7218
movq %r14, %rdi
callq 0x9650
movq %rbx, %rax
jmp 0x7218
leal -0x1e(%rsi), %eax
cmpl $0x9, %eax
jbe 0x7619
cmpl $0x2b, %esi
jne 0x74d3
leaq 0x980(%rsp), %rdi
callq 0xdf3e
movq %rbx, %rax
jmp 0x7218
movq %r14, %rdi
callq 0x95b4
movq %rbx, %rax
jmp 0x7218
testb $0x1, 0x8(%rsp)
jne 0x7574
leaq 0x458(%rsp), %rdi
callq 0xb150
testl %eax, %eax
js 0x77d3
leaq 0x210(%rsp), %rdi
callq 0xe90c
movl %eax, %r12d
testl %eax, %eax
jle 0x7555
movl %r12d, %edi
callq 0x4380
leaq 0x980(%rsp), %rdi
callq 0xdf2a
leaq 0x210(%rsp), %rdi
movl %eax, %esi
callq 0xe910
leaq 0x210(%rsp), %rdi
movl $0x2df, %esi            # imm = 0x2DF
callq 0xf246
movl %r12d, %edi
callq 0x4600
leaq 0x980(%rsp), %rdi
callq 0xd4ee
cmpl $0x1, %eax
jne 0x7574
leaq 0x458(%rsp), %rdi
callq 0xb354
leaq 0x980(%rsp), %rdi
callq 0xdf2a
movl %eax, %r12d
callq 0x43c0
cvtsi2sd %r12d, %xmm0
mulsd 0xaa72(%rip), %xmm0     # 0x12008
divsd 0xaa72(%rip), %xmm0     # 0x12010
cvttsd2si %xmm0, %edi
addl 0x14(%rsp), %edi
subl %eax, %edi
testl %edi, %edi
jle 0x7215
callq 0x42b0
jmp 0x7215
movq 0x8(%rsp), %rax
movl %eax, %r12d
xorb $0x1, %r12b
testb $0x1, %al
je 0x75e9
leaq 0x210(%rsp), %rdi
callq 0xe90c
movl %eax, %edi
xorl %esi, %esi
callq 0x4440
leaq 0xada2(%rip), %rdi      # 0x12389
jmp 0x7607
leaq 0x210(%rsp), %rdi
callq 0xe90c
movl %eax, %edi
pushq $0x1
popq %rsi
callq 0x4440
leaq 0xad96(%rip), %rdi      # 0x1239d
callq 0x4540
movl %r12d, %eax
movq %rax, 0x8(%rsp)
jmp 0x7215
movq %r13, %rdi
leaq 0xaccf(%rip), %rsi      # 0x122f2
callq 0x4390
movl 0x28(%rsp), %esi
movq %rax, %rdi
callq 0x4610
movq %rax, %rdi
callq 0x41c0
movl 0x28(%rsp), %esi
addl $-0x1d, %esi
leaq 0xacb6(%rip), %rdi      # 0x12300
xorl %eax, %eax
callq 0x4040
pushq $0xa
popq %rdi
callq 0x44b0
jmp 0x7215
leaq 0xad4a(%rip), %rdi      # 0x123af
callq 0x4540
leaq 0x458(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x9f50
leaq 0x980(%rsp), %rdi
callq 0xdf2a
movl %eax, %ebx
callq 0x4260
movq %rax, %r14
leaq 0x980(%rsp), %rdi
callq 0xdf2a
cvtsi2ss %r14, %xmm0
divss 0xa957(%rip), %xmm0     # 0x12004
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x8(%rsp)
callq 0x4260
cvtsi2ss %rax, %xmm0
divss 0xa932(%rip), %xmm0     # 0x12004
movss 0x8(%rsp), %xmm1
divss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0xabba(%rip), %rdi      # 0x122a1
movl %ebx, %esi
movsd (%rsp), %xmm0
movb $0x2, %al
callq 0x4040
movq 0x138b4(%rip), %rdi     # 0x1afb0
leaq 0xabca(%rip), %rsi      # 0x122cd
callq 0x4390
movq %rax, %rdi
callq 0x41c0
callq 0x4080
movq 0x13894(%rip), %rdi     # 0x1afb0
leaq 0xabbd(%rip), %rsi      # 0x122e0
callq 0x4390
movl $0x0, (%rsp)
movq %rax, %rdi
callq 0x41c0
jmp 0x77d3
callq 0x4080
leaq 0x458(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x9f50
leaq 0x980(%rsp), %rdi
callq 0xdf2a
movl %eax, %ebx
callq 0x4260
movq %rax, %r14
leaq 0x980(%rsp), %rdi
callq 0xdf2a
cvtsi2ss %r14, %xmm0
divss 0xa880(%rip), %xmm0     # 0x12004
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x8(%rsp)
callq 0x4260
cvtsi2ss %rax, %xmm0
divss 0xa85b(%rip), %xmm0     # 0x12004
movss 0x8(%rsp), %xmm1
divss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0xaae3(%rip), %rdi      # 0x122a1
movl %ebx, %esi
movsd (%rsp), %xmm0
movb $0x2, %al
callq 0x4040
movl $0x0, (%rsp)
leaq 0x458(%rsp), %rdi
callq 0xa3ba
leaq 0x90(%rsp), %rdi
callq 0x9582
leaq 0x210(%rsp), %rdi
callq 0xfb2c
leaq 0x980(%rsp), %rdi
callq 0xc786
leaq 0x140(%rsp), %rdi
callq 0x8eb6
leaq 0x50(%rsp), %rdi
callq 0x41b0
movl (%rsp), %eax
addq $0x221e8, %rsp          # imm = 0x221E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1377e(%rip), %rax     # 0x1afb8
movq (%rax), %rbx
callq 0x42c0
leaq 0xa8fa(%rip), %rsi      # 0x12143
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4500
pushq $0x1
popq %rdi
callq 0x43f0
jmp 0x7898
jmp 0x7889
jmp 0x7864
movq %rax, %rbx
jmp 0x78d4
movq %rax, %rbx
jmp 0x78e1
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x41b0
jmp 0x78ee
jmp 0x78a5
jmp 0x78a5
jmp 0x78aa
jmp 0x78aa
jmp 0x78aa
jmp 0x78aa
movq %rax, %rbx
jmp 0x78c7
jmp 0x78aa
jmp 0x78aa
jmp 0x78a5
jmp 0x78aa
jmp 0x78a5
movq %rax, %rbx
jmp 0x78ba
jmp 0x78aa
jmp 0x78aa
jmp 0x78a5
jmp 0x78aa
movq %rax, %rbx
jmp 0x78ee
movq %rax, %rbx
leaq 0x458(%rsp), %rdi
callq 0xa3ba
leaq 0x90(%rsp), %rdi
callq 0x9582
leaq 0x210(%rsp), %rdi
callq 0xfb2c
leaq 0x980(%rsp), %rdi
callq 0xc786
leaq 0x140(%rsp), %rdi
callq 0x8eb6
leaq 0x50(%rsp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x4640