// File: /workspace/github_cpp_makefile_star2_O3/0x6D70[P]chip8_emu/src/cpu.cpp
// Function: Cpu::run(SDL_Renderer*)
void Cpu::run(SDL_Renderer *renderer) {

    bool isFinished = false;
    bool update_video = false;
    uint64_t instruction_count = 0;

    SDL_Event event;

    // variable for keyboard
    bool keys[0x10] = {false};
    

    while (!isFinished) {
        instruction_count++;

        uint16_t opcode = this->mem_read_u16(this->state.pc);
        this->state.pc += 2;

        //  uncomment for single stepping
        // std::cout << "0x" << std::hex << opcode;
        // getchar();

        uint8_t opcode_n3 = opcode & 0xf;
        uint8_t opcode_n2 = (opcode >> 4) & 0xf;
        uint8_t opcode_n1 = (opcode >> 8) & 0xf;
        uint8_t opcode_n0 = (opcode >> 12) & 0xf;

        switch (opcode_n0) {
            case 0x0: {
                if (opcode == 0x00e0) {
                    // cls
                    update_video = true;
                    for (int y = 0; y < VIDEO_HEIGHT; y++) {
                        for (int x = 0; x < VIDEO_WIDTH; x++) {
                            this->state.video[x][y] = false;
                        }
                    }
                } else if (opcode == 0x00ee) {
                    // ret
                    this->state.pc = this->state.call_stack.top();
                    this->state.call_stack.pop();
                } else {
                    // sys addr
                    // this instruction is ignored
                    // http://devernay.free.fr/hacks/chip8/C8TECH10.HTM#0nnn
                }
                break;
            }
            case 0x1: {
                // JP addr
                this->state.pc = opcode & 0xfff;
                break;
            }
            case 0x2: {
                // CALL addr
                this->state.call_stack.push(this->state.pc);
                this->state.pc = opcode & 0xfff;
                break;
            }
            case 0x3: {
                // SE Vx, byte
                if (this->state.regs[opcode_n1] == (uint8_t)(opcode & 0xff)) {
                    // skip instruction
                    this->state.pc += 2;
                }
                break;
            }
            case 0x4: {
                // SNE Vx, byte
                if (this->state.regs[opcode_n1] != (uint8_t)(opcode & 0xff)) {
                    // skip instruction
                    this->state.pc += 2;
                }
                break;
            }
            case 0x5: {
                // SE Vx, Vy
                if (this->state.regs[opcode_n1] == this->state.regs[opcode_n2]) {
                    // skip instruction
                    this->state.pc += 2;
                }
                break;
            }
            case 0x6: {
                // ld vx,byte
                this->state.regs[opcode_n1] = (uint8_t)(opcode & 0xff);
                break;
            }
            case 0x7: {
                // ADD Vx, byte
                this->state.regs[opcode_n1] += (opcode & 0xff);
                break;
            }
            case 0x8: {
                // logical and arithmetic instruction
                switch (opcode_n3) {
                    case 0x0: {
                        this->state.regs[opcode_n1] = this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x1: {
                        this->state.regs[opcode_n1] |= this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x2: {
                        this->state.regs[opcode_n1] &= this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x3: {
                        this->state.regs[opcode_n1] ^= this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x4: {
                        // check overflow
                        if ((uint16_t)this->state.regs[opcode_n1] + (uint16_t)this->state.regs[opcode_n2] > 0xff) {
                            this->state.regs[0xf] = 1;
                        } else {
                            this->state.regs[0xf] = 0;
                        }

                        this->state.regs[opcode_n1] += this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x5: {
                        if (this->state.regs[opcode_n1] > this->state.regs[opcode_n2]) {
                            this->state.regs[0xf] = 1;
                        } else {
                            this->state.regs[0xf] = 0;
                        }

                        this->state.regs[opcode_n1] -= this->state.regs[opcode_n2];
                        break;
                    }
                    case 0x6: {
                        this->state.regs[opcode_n1] = this->state.regs[opcode_n2];
                        // shift right

                        this->state.regs[0xf] = this->state.regs[opcode_n1] & 1;
                        this->state.regs[opcode_n1] >>= 1;

                        break;
                    }
                    case 0x7: {
                        if (this->state.regs[opcode_n2] > this->state.regs[opcode_n1]) {
                            this->state.regs[0xf] = 1;
                        } else {
                            this->state.regs[0xf] = 0;
                        }
                        
                        this->state.regs[opcode_n1] = this->state.regs[opcode_n2] - this->state.regs[opcode_n1];
                        break;
                    }
                    case 0xe: {
                        this->state.regs[opcode_n1] = this->state.regs[opcode_n2];
                        // shift left

                        this->state.regs[0xf] = (this->state.regs[opcode_n1] >> 7) & 1;
                        this->state.regs[opcode_n1] <<= 1;

                        break;
                    }
                }
                break;
            }
            case 0x9: {
                // SNE Vx, Vy
                if (this->state.regs[opcode_n1] != this->state.regs[opcode_n2]) {
                    // skip instruction
                    this->state.pc += 2;
                }
                break;
            }
            case 0xa: {
                // ld l, addr
                this->state.l = opcode & 0xfff;
                break;
            }
            case 0xb: {
                // Bnnn - JP V0, addr
                this->state.pc = (opcode & 0xfff) + this->state.regs[0x0];
                break;
            }
            case 0xc: {
                // Cxkk - RND Vx, byte
                this->state.regs[opcode_n1] = (uint8_t)(rand() & (opcode & 0xff));
                break;
            }
            case 0xd: {
                // DXYN
                uint8_t x_base = this->state.regs[opcode_n1] % 64;
                uint8_t y_base = this->state.regs[opcode_n2] % 32;
                this->state.regs[0xf] = 0;
                update_video = true;

                for (int n = 0; n < opcode_n3; n++) {
                    uint8_t pixels = this->state.ram[this->state.l + n];

                    for (int x = 0; x < 8; x++) {
                        if (pixels & (0x80 >> x)) {
                            // current bit is one

                            if (this->state.video[x_base + x][y_base + n]) {
                                this->state.regs[0xf] = 1;
                                // flip bit
                                this->state.video[x_base + x][y_base + n] = false;
                            } else {
                                this->state.video[x_base + x][y_base + n] = true;
                            }
                        }
                    }

                }

                break;
            }
            case 0xe: {
                switch (opcode & 0xff) {
                    case 0x9e: {
                        // Ex9E - SKP Vx
                        uint8_t index = this->state.regs[opcode_n1] & 0xf;
                        if (keys[index])
                            this->state.pc += 2;
                        break;
                    }
                    case 0xa1: {
                        // ExA1 - SKNP Vx
                        uint8_t index = this->state.regs[opcode_n1] & 0xf;
                        if (!keys[index])
                            this->state.pc += 2;
                        break;
                    }
                }
                break;
            }
            case 0xf: {

                switch (opcode & 0xff) { 
                    case 0x7: {
                        // LD Vx, DT
                        this->state.regs[opcode_n1] = this->state.delay_timer;
                        break;
                    }
                    case 0xa: {
                        // LD Vx, K
                        // wait until key is pressed
                        // when the key is pressed, put it into Vx

                        if (isKeyPressed(keys)) {
                            for (int i = 0; i < 0x10; i++) {
                                if (keys[i]) {
                                    this->state.regs[opcode_n1] = i;
                                }
                            }
                        } else {
                            // decrement pc to hit this instruction again
                            this->state.pc -= 2;
                        }
                        break;
                    }
                    case 0x15: {
                        // LD DT, Vx
                        this->state.delay_timer = this->state.regs[opcode_n1];
                        break;
                    }
                    case 0x18: {
                        // LD ST, Vx
                        this->state.sound_timer = this->state.regs[opcode_n1];
                        break;
                    }
                    case 0x1e: {
                        // ADD I, Vx
                        this->state.l += this->state.regs[opcode_n1];

                        if (this->state.l > 0xfff) {
                            this->state.regs[0xf] = 1;
                        } else {
                            this->state.regs[0xf] = 0;
                        }

                        this->state.l &= 0xfff;
                        break;
                    }
                    case 0x29: {
                        // LD F, Vx
                        this->state.l = 0x50 + (opcode & 0xff) * 5;
                        break;
                    }
                    case 0x33: {
                        // Fx33 - LD B, Vx
                        // Binary-coded decimal conversion

                        this->state.ram[this->state.l + 0] = this->state.regs[opcode_n1] / 100;
                        this->state.ram[this->state.l + 1] = (this->state.regs[opcode_n1] / 10) % 10;
                        this->state.ram[this->state.l + 2] = this->state.regs[opcode_n1] % 10;

                        break;
                    }
                    case 0x55: {
                        // LD [I], Vx

                        for (int i = 0; i <= opcode_n1; i++) {
                            this->state.ram[this->state.l + i] = this->state.regs[i];
                        }

                        break;
                    }
                    case 0x65: {
                        // LD Vx, [I]

                        for (int i = 0; i <= opcode_n1; i++) {
                            this->state.regs[i] = this->state.ram[this->state.l + i];
                        }

                        break;
                    }
                }
                break;
            }
            default:
                std::cout << "missing instruction 0x" << std::hex << (int)opcode_n0 << std::endl;
                isFinished = true;
        }

        // update timers

        if (instruction_count % 10  == 0) {
            // ownly decrease every 10 instructions which should be a rate of about ~60 times a second

            if (this->state.delay_timer > 0)
                this->state.delay_timer--;
            if (this->state.sound_timer > 0)
                this->state.sound_timer--;
        }

        if (update_video || instruction_count % 0x100 == 0) {
            // video array has changed => update window

            SDL_RenderClear(renderer);

            for (int y = 0; y < VIDEO_HEIGHT; y++) {
                for (int x = 0; x < VIDEO_WIDTH; x++) {
                    if (this->state.video[x][y]) {
                        // white
                        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
                    } else {
                        // black
                        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                    }

                    SDL_Rect rect;
                    rect.h = VIDEO_MULTI;
                    rect.w = VIDEO_MULTI;
                    rect.x = x * VIDEO_MULTI;
                    rect.y = y * VIDEO_MULTI;

                    SDL_RenderFillRect(renderer, &rect);
                }
            }

            SDL_RenderPresent(renderer);

            update_video = false;
        }

        while(SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_QUIT:
                    isFinished = true;
                    break;
                case SDL_KEYDOWN: {
                    uint8_t index = keysym_to_index(event.key.keysym);

                    if (index < 0x10) {
                        keys[index] = true;
                    }
                    
                    break;
                }
                case SDL_KEYUP: {
                    uint8_t index = keysym_to_index(event.key.keysym);

                    if (index < 0x10) {
                        keys[index] = false;
                    }

                    break;
                }
            }
        }

        SDL_Delay(1);
    }

    std::cout << "Exited after 0x" << std::hex << (long)instruction_count << " instructions\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r14
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
leaq 0x1860(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0x1000(%rdi), %rax
movq %rax, -0x30(%rbp)
leaq 0x1010(%rdi), %rax
movq %rax, -0x88(%rbp)
leaq 0x1810(%rdi), %rax
movq %rax, -0x90(%rbp)
xorl %ebx, %ebx
movq %rdi, -0x98(%rbp)
movzwl 0x1860(%r13), %ecx
movzbl (%r13,%rcx), %eax
movl %eax, %r15d
shll $0x8, %r15d
leal 0x1(%rcx), %edx
movzwl %dx, %edx
movzbl (%r13,%rdx), %r12d
orl %r12d, %r15d
leal 0x2(%rcx), %esi
movw %si, 0x1860(%r13)
movl %r12d, %edx
andl $0xf, %edx
movl %r12d, %edi
shrl $0x4, %edi
movl %eax, %r9d
andl $0xf, %r9d
shrl $0x4, %eax
leaq 0x195a(%rip), %r8       # 0x4088
movslq (%r8,%rax,4), %rax
addq %r8, %rax
jmpq *%rax
cmpl $0xee, %r15d
je 0x2c37
cmpl $0xe0, %r15d
leaq -0x70(%rbp), %r12
jne 0x2f0d
movq -0x88(%rbp), %rcx
xorl %edx, %edx
xorl %eax, %eax
movb $0x0, (%rcx,%rax)
addq $0x20, %rax
cmpq $0x800, %rax            # imm = 0x800
jne 0x2760
incq %rdx
incq %rcx
movb $0x1, %al
cmpq $0x20, %rdx
jne 0x275e
jmp 0x2a41
cmpl $0xa1, %r12d
je 0x2c0d
cmpl $0x9e, %r12d
jne 0x2a3b
movq -0x30(%rbp), %rax
movzbl (%rax,%r9), %eax
andl $0xf, %eax
cmpb $0x1, -0x80(%rbp,%rax)
jmp 0x2c1e
andl $0xfff, %r15d           # imm = 0xFFF
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
addl %r15d, %eax
jmp 0x28e3
movq -0x30(%rbp), %rax
cmpb %r12b, (%rax,%r9)
jne 0x28e0
jmp 0x2a3b
movq %r9, %r15
callq 0x20b0
andb %r12b, %al
movq -0x30(%rbp), %rcx
movb %al, (%rcx,%r15)
jmp 0x2a3b
movq -0x30(%rbp), %rdx
movb (%rdx,%r9), %al
cmpb (%rdx,%rdi), %al
jne 0x28e0
jmp 0x2a3b
movq 0x1840(%r13), %rax
movq 0x1850(%r13), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
je 0x2a1c
movw %si, (%rax)
addq $0x2, %rax
movq %rax, 0x1840(%r13)
movq -0x38(%rbp), %r12
jmp 0x2a2f
xorl %eax, %eax
cmpl $0x1d, %r12d
jle 0x29d6
cmpl $0x32, %r12d
jle 0x2ca2
cmpl $0x33, %r12d
je 0x2d2e
cmpl $0x55, %r12d
je 0x2e4a
cmpl $0x65, %r12d
leaq -0x70(%rbp), %r12
jne 0x2a41
incb %r9b
movzbl %r9b, %ecx
movq -0x30(%rbp), %rdx
movzwl 0x1862(%r13), %eax
movb -0x1000(%rdx,%rax), %al
movb %al, (%rdx)
incq %rdx
xorl %eax, %eax
decq %rcx
jne 0x287f
jmp 0x2a41
movq -0x30(%rbp), %rax
cmpb %r12b, (%rax,%r9)
je 0x28e0
jmp 0x2a3b
movq -0x30(%rbp), %rax
addb %r12b, (%rax,%r9)
jmp 0x2a3b
andl $0xfff, %r15d           # imm = 0xFFF
movq -0x38(%rbp), %rax
movw %r15w, (%rax)
jmp 0x2a3b
movq -0x30(%rbp), %rdx
movb (%rdx,%r9), %al
cmpb (%rdx,%rdi), %al
jne 0x2a3b
leal 0x4(%rcx), %eax
movq -0x38(%rbp), %rcx
movw %ax, (%rcx)
jmp 0x2a3b
movzbl 0x1000(%r13,%r9), %esi
movzbl 0x1000(%r13,%rdi), %ecx
movb $0x0, 0x100f(%r13)
movb $0x1, %al
testb %dl, %dl
je 0x2a3d
andl $0x1f, %ecx
andl $0x3f, %esi
movzwl 0x1862(%r13), %edi
shll $0x5, %esi
orq %rcx, %rsi
addq -0x88(%rbp), %rsi
xorl %r8d, %r8d
leaq -0x70(%rbp), %r12
leaq (%r8,%rdi), %rcx
movzbl (%r13,%rcx), %r9d
movq %rsi, %r10
xorl %ecx, %ecx
movl %r9d, %r11d
shll %cl, %r11d
testb %r11b, %r11b
jns 0x2966
movb $0x1, %r11b
cmpb $0x1, (%r10)
jne 0x2963
movb $0x1, 0x100f(%r13)
xorl %r11d, %r11d
movb %r11b, (%r10)
incq %rcx
addq $0x20, %r10
cmpq $0x8, %rcx
jne 0x2944
incq %r8
incq %rsi
cmpq %rdx, %r8
jne 0x2935
jmp 0x2a41
movq -0x30(%rbp), %rax
movb %r12b, (%rax,%r9)
jmp 0x2a3b
andl $0xfff, %r15d           # imm = 0xFFF
movw %r15w, 0x1862(%r13)
jmp 0x2a3b
xorl %eax, %eax
cmpb $0xe, %dl
ja 0x2a3d
movzbl %dl, %ecx
leaq 0x170f(%rip), %rdx      # 0x40c8
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
leaq -0x70(%rbp), %r12
jmpq *%rcx
movq -0x30(%rbp), %rcx
movb (%rcx,%rdi), %al
movb %al, (%rcx,%r9)
jmp 0x2f0d
cmpl $0x14, %r12d
jg 0x2c7a
cmpl $0x7, %r12d
je 0x2cd9
cmpl $0xa, %r12d
leaq -0x70(%rbp), %r12
jne 0x2a41
cmpb $0x0, -0x80(%rbp)
je 0x2ee1
xorl %ecx, %ecx
cmpb $0x1, -0x80(%rbp,%rcx)
jne 0x2a0f
movq -0x30(%rbp), %rax
movb %cl, (%rax,%r9)
incq %rcx
xorl %eax, %eax
cmpq $0x10, %rcx
jne 0x2a00
jmp 0x2a41
movq -0x90(%rbp), %rdi
movq -0x38(%rbp), %r12
movq %r12, %rsi
callq 0x32d2
andl $0xfff, %r15d           # imm = 0xFFF
movw %r15w, (%r12)
xorl %eax, %eax
leaq -0x70(%rbp), %r12
incq %rbx
movq %rbx, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
rorq %rcx
movabsq $0x1999999999999999, %rdx # imm = 0x1999999999999999
cmpq %rdx, %rcx
ja 0x2a8f
movb 0x1864(%r13), %cl
testb %cl, %cl
je 0x2a7b
decb %cl
movb %cl, 0x1864(%r13)
movb 0x1865(%r13), %cl
testb %cl, %cl
je 0x2a8f
decb %cl
movb %cl, 0x1865(%r13)
testb %bl, %bl
sete %cl
orb %cl, %al
cmpb $0x1, %al
jne 0x2b81
movq %rbx, -0xa0(%rbp)
movq %r14, %r12
movq %r14, %rdi
callq 0x21d0
movq -0x88(%rbp), %r15
xorl %eax, %eax
movq %rax, -0xa8(%rbp)
leal (,%rax,4), %eax
leal (%rax,%rax,4), %ebx
movq %r15, -0xb0(%rbp)
xorl %r14d, %r14d
cmpb $0x1, (%r15)
jne 0x2af1
movq %r12, %r13
movq %r12, %rdi
movl $0xff, %esi
movl $0xff, %edx
movl $0xff, %ecx
jmp 0x2afd
movq %r12, %r13
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0xff, %r8d
callq 0x2260
movabsq $0x1400000014, %rax     # imm = 0x1400000014
movq %rax, -0xb8(%rbp)
movl %r14d, -0xc0(%rbp)
movl %ebx, -0xbc(%rbp)
movq %r13, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2100
addq $0x14, %r14
addq $0x20, %r15
cmpq $0x500, %r14            # imm = 0x500
jne 0x2ad4
movq -0xa8(%rbp), %rax
incq %rax
movq -0xb0(%rbp), %r15
incq %r15
cmpq $0x20, %rax
jne 0x2ab9
movq %r12, %r14
movq %r12, %rdi
callq 0x2080
movq -0xa0(%rbp), %rbx
movq -0x98(%rbp), %r13
leaq -0x70(%rbp), %r12
movq %r12, %rdi
callq 0x2130
testl %eax, %eax
je 0x2bfe
xorl %r15d, %r15d
movl -0x70(%rbp), %eax
cmpl $0x301, %eax            # imm = 0x301
je 0x2bc4
cmpl $0x300, %eax            # imm = 0x300
je 0x2bad
cmpl $0x100, %eax            # imm = 0x100
jne 0x2bd9
movb $0x1, %r15b
jmp 0x2bd9
movq -0x60(%rbp), %rdi
callq 0x25e0
cmpb $0xf, %al
ja 0x2bd9
movzbl %al, %eax
movb $0x1, -0x80(%rbp,%rax)
jmp 0x2bd9
movq -0x60(%rbp), %rdi
callq 0x25e0
cmpb $0xf, %al
ja 0x2bd9
movzbl %al, %eax
movb $0x0, -0x80(%rbp,%rax)
movq %r12, %rdi
callq 0x2130
testl %eax, %eax
jne 0x2b90
movl $0x1, %edi
callq 0x2160
testb $0x1, %r15b
je 0x26e2
jmp 0x2f14
movl $0x1, %edi
callq 0x2160
jmp 0x26e2
movq -0x30(%rbp), %rax
movzbl (%rax,%r9), %eax
andl $0xf, %eax
cmpb $0x0, -0x80(%rbp,%rax)
leaq -0x70(%rbp), %r12
jne 0x2f0d
leal 0x4(%rcx), %eax
movq -0x38(%rbp), %rcx
movw %ax, (%rcx)
jmp 0x2f0d
movq 0x1840(%r13), %rax
cmpq 0x1848(%r13), %rax
leaq -0x70(%rbp), %r12
jne 0x2c5e
movq 0x1858(%r13), %rax
movq -0x8(%rax), %rax
movl $0x200, %ecx            # imm = 0x200
addq %rcx, %rax
movzwl -0x2(%rax), %eax
movq -0x38(%rbp), %rcx
movw %ax, (%rcx)
movq -0x90(%rbp), %rdi
callq 0x327c
jmp 0x2f0d
cmpl $0x15, %r12d
je 0x2ced
cmpl $0x18, %r12d
leaq -0x70(%rbp), %r12
jne 0x2a41
movb 0x1000(%r13,%r9), %al
movb %al, 0x1865(%r13)
jmp 0x2f0d
cmpl $0x1e, %r12d
je 0x2dab
cmpl $0x29, %r12d
leaq -0x70(%rbp), %r12
jne 0x2a41
movw $0x11d, 0x1862(%r13)    # imm = 0x11D
jmp 0x2f0d
movq -0x30(%rbp), %rcx
movb (%rcx,%rdi), %al
andb %al, (%rcx,%r9)
jmp 0x2f0d
movb 0x1864(%r13), %al
movb %al, 0x1000(%r13,%r9)
jmp 0x2a3b
movb 0x1000(%r13,%r9), %al
movb %al, 0x1864(%r13)
jmp 0x2a3b
movb 0x1000(%r13,%r9), %al
cmpb 0x1000(%r13,%rdi), %al
seta 0x100f(%r13)
movb 0x1000(%r13,%rdi), %al
subb %al, 0x1000(%r13,%r9)
jmp 0x2f0d
movzbl 0x1000(%r13,%r9), %eax
leal (%rax,%rax,4), %ecx
leal (%rax,%rcx,8), %eax
shrl $0xc, %eax
movzwl 0x1862(%r13), %ecx
movb %al, (%r13,%rcx)
movzbl 0x1000(%r13,%r9), %eax
imull $0xcd, %eax, %eax
shrl $0xb, %eax
leal (%rax,%rax,4), %ecx
leal (%rcx,%rcx,4), %ecx
addl %eax, %ecx
shrl $0x7, %ecx
andl $0x6, %ecx
leal (%rcx,%rcx,4), %ecx
subb %cl, %al
movzwl 0x1862(%r13), %ecx
movb %al, 0x1(%rcx,%r13)
movzbl 0x1000(%r13,%r9), %eax
imull $0xcd, %eax, %ecx
shrl $0xa, %ecx
andl $-0x2, %ecx
leal (%rcx,%rcx,4), %ecx
subb %cl, %al
movzwl 0x1862(%r13), %ecx
movb %al, 0x2(%rcx,%r13)
jmp 0x2a3b
movzbl 0x1000(%r13,%r9), %eax
addw 0x1862(%r13), %ax
movzwl %ax, %ecx
cmpl $0x1000, %ecx           # imm = 0x1000
setae 0x100f(%r13)
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, 0x1862(%r13)
jmp 0x2a3b
movq -0x30(%rbp), %rcx
movb (%rcx,%rdi), %al
orb %al, (%rcx,%r9)
jmp 0x2f0d
movb 0x1000(%r13,%rdi), %al
movb %al, 0x1000(%r13,%r9)
andb $0x1, %al
movb %al, 0x100f(%r13)
shrb 0x1000(%r13,%r9)
jmp 0x2f0d
movb 0x1000(%r13,%rdi), %al
cmpb 0x1000(%r13,%r9), %al
seta 0x100f(%r13)
movb 0x1000(%r13,%rdi), %al
subb 0x1000(%r13,%r9), %al
movb %al, 0x1000(%r13,%r9)
jmp 0x2f0d
incb %r9b
movzbl %r9b, %ecx
movq -0x30(%rbp), %rdx
leaq -0x70(%rbp), %r12
movb (%rdx), %al
movzwl 0x1862(%r13), %esi
movb %al, -0x1000(%rdx,%rsi)
incq %rdx
xorl %eax, %eax
decq %rcx
jne 0x2e59
jmp 0x2a41
movq -0x30(%rbp), %rcx
movb (%rcx,%rdi), %al
xorb %al, (%rcx,%r9)
jmp 0x2f0d
movzbl 0x1000(%r13,%r9), %eax
movzbl 0x1000(%r13,%rdi), %ecx
addl %eax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
setae 0x100f(%r13)
movb 0x1000(%r13,%rdi), %al
addb %al, 0x1000(%r13,%r9)
jmp 0x2f0d
movb 0x1000(%r13,%rdi), %al
movb %al, 0x1000(%r13,%r9)
shrb $0x7, %al
movb %al, 0x100f(%r13)
shlb 0x1000(%r13,%r9)
jmp 0x2f0d
movq $-0x1, %rax
cmpq $0xe, %rax
je 0x2f06
leaq 0x1(%rax), %rdx
cmpb $0x1, -0x7e(%rbp,%rax)
movq %rdx, %rax
jne 0x2ee8
cmpq $0xe, %rdx
jbe 0x29fe
movq -0x38(%rbp), %rax
movw %cx, (%rax)
xorl %eax, %eax
jmp 0x2a41
movq 0x30bd(%rip), %r14      # 0x5fd8
leaq 0x1241(%rip), %rsi      # 0x4163
movl $0xf, %edx
movq %r14, %rdi
callq 0x2230
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x121b(%rip), %rsi      # 0x4173
movl $0xe, %edx
movq %rax, %rdi
callq 0x2230
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop