// File: /workspace/github_cpp_makefile_star2_O1/nicoeps[P]NicoGB/src/main.cpp
// Function: run(NicoGB&)
void run(NicoGB& nicogb) {
    const int SCALE = 4;
    const int WIDTH = 160;
    const int HEIGHT = 144;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("NicoGB",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        WIDTH*SCALE, HEIGHT*SCALE, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Texture *texture = SDL_CreateTexture(renderer,
    SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 160, 144);
    SDL_UpdateTexture(texture, NULL, nicogb.framebuffer.data(), 160 * sizeof(uint32_t));

    SDL_Event event;
    SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
    std::string path;
    Key key;

    auto last = millis();
    bool run = true;
    while (run) {
        nicogb.tick();

        if (millis() - last >= 1000/60) {
            last = millis();

            SDL_UpdateTexture(texture, NULL, nicogb.framebuffer.data(), 160 * sizeof(uint32_t));
            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, texture, NULL, NULL);
            SDL_RenderPresent(renderer);

            while (SDL_PollEvent(&event)) {
                switch (event.type) {
                    case SDL_QUIT:
                        run = false;
                        break;

                    case SDL_DROPFILE:
                        path = event.drop.file;
                        nicogb.load(path);
                        SDL_SetWindowTitle(window, (std::string("NicoGB - ") + nicogb.title).c_str());
                        break;

                    case SDL_KEYDOWN:
                        switch (event.key.keysym.sym) {
                            case SDLK_q:
                                run = false;
                                break;
                            case SDLK_r:
                                nicogb.init();
                                break;
                            case SDLK_SPACE:
                                nicogb.speed = !nicogb.speed;
                                break;
                            default:
                                key = getKey(event.key.keysym.sym);
                                nicogb.keyDown(key);
                                break;
                        }
                        break;

                    case SDL_KEYUP:
                        key = getKey(event.key.keysym.sym);
                        nicogb.keyUp(key);
                        break;

                    default: break;
                }
            }
        }
    }
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %r14
movl $0x20, %edi
callq 0x2190
leaq 0x48d3(%rip), %rdi      # 0xa944
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
movl $0x280, %ecx            # imm = 0x280
movl $0x240, %r8d            # imm = 0x240
xorl %r9d, %r9d
callq 0x2050
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x2040
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl $0x16362004, %esi       # imm = 0x16362004
movl $0x2, %edx
movl $0xa0, %ecx
movl $0x90, %r8d
callq 0x2290
movq %rax, %rdi
movq 0x2c0(%r14), %rax
movq (%rax), %rdx
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2160
movl $0x1000, %edi           # imm = 0x1000
movl $0x1, %esi
callq 0x2100
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
callq 0x2060
subq 0x805f(%rip), %rax      # 0xe170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x18(%rsp)
leaq 0x80(%rsp), %rbp
leaq 0xa0(%rsp), %r15
movb $0x1, %bl
movq %r14, 0x68(%rsp)
movq %r14, %rdi
callq 0x777e
callq 0x2060
subq 0x8014(%rip), %rax      # 0xe170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
subq 0x18(%rsp), %rdx
cmpq $0x10, %rdx
jl 0x63ff
callq 0x2060
subq 0x7fde(%rip), %rax      # 0xe170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r12
movq 0x2c0(%r14), %rax
movq (%rax), %rdx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x21a0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21d0
movq 0x8(%rsp), %rdi
callq 0x2080
movq %r12, %rax
shrq $0x3f, %rax
sarq $0x12, %r12
addq %rax, %r12
movq %r12, 0x18(%rsp)
movl %ebx, %r12d
leaq 0xb0(%rsp), %rdi
callq 0x2130
testl %eax, %eax
je 0x6402
movl 0xb0(%rsp), %eax
cmpl $0x300, %eax            # imm = 0x300
jg 0x625f
cmpl $0x100, %eax            # imm = 0x100
je 0x6350
movl %r12d, %ebx
cmpl $0x300, %eax            # imm = 0x300
jne 0x61f7
movl 0xc4(%rsp), %edi
cmpl $0x20, %edi
je 0x63d8
xorl %ebx, %ebx
cmpl $0x71, %edi
je 0x61f7
cmpl $0x72, %edi
jne 0x63e8
movq %r14, %rdi
callq 0x76b6
movl %r12d, %ebx
jmp 0x61f7
cmpl $0x301, %eax            # imm = 0x301
je 0x6357
movl %r12d, %ebx
cmpl $0x1000, %eax           # imm = 0x1000
jne 0x61f7
movq 0xb8(%rsp), %rbx
movq %r15, %r13
movq 0x30(%rsp), %r15
movq %rbx, %rdi
callq 0x20f0
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r13, %r15
movq 0x68(%rsp), %r14
movq %rbx, %rcx
movq %rax, %r8
callq 0x2210
movq %rbp, 0x70(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x667e
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
callq 0x76fc
movq 0x70(%rsp), %rdi
cmpq %rbp, %rdi
leaq 0x58(%rsp), %rbx
je 0x62f1
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %r15, 0x90(%rsp)
leaq 0x90(%rsp), %r13
movq %r13, %rdi
leaq 0x4640(%rip), %rsi      # 0xa94b
leaq 0x4642(%rip), %rdx      # 0xa954
callq 0x670e
movq 0x2b8(%r14), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x20c0
movq %rbx, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x6375
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x637b
xorl %ebx, %ebx
jmp 0x61f7
movl 0xc4(%rsp), %edi
callq 0x5fd7
movq %r14, %rdi
movl %eax, %esi
callq 0x783c
movl %r12d, %ebx
jmp 0x61f7
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2260
movq 0x48(%rsp), %rdi
cmpq %rbx, %rdi
je 0x63b9
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x63e0
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x63e0
xorb $0x1, 0x2a8(%r14)
movl %r12d, %ebx
jmp 0x61f7
callq 0x5fd7
movq %r14, %rdi
movl %eax, %esi
callq 0x7832
movl %r12d, %ebx
jmp 0x61f7
movl %ebx, %r12d
movl %r12d, %ebx
testb $0x1, %r12b
jne 0x6148
movq 0x10(%rsp), %rdi
callq 0x2150
movq 0x8(%rsp), %rdi
callq 0x20e0
movq 0x20(%rsp), %rdi
callq 0x2030
callq 0x2070
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x644e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x64be
jmp 0x64be
movq %rax, %rbx
jmp 0x6486
movq %rbx, %rcx
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %rcx, %rdi
je 0x6486
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x64c1
movq 0xa0(%rsp), %rsi
jmp 0x64b4
jmp 0x64be
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %rbp, %rdi
je 0x64c1
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x64c1
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x64dd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2270