// File: /workspace/github_c_makefile_star2_O0/leiradel[P]zx48k-libretro/src/main.c
// Function: retro_run
void retro_run(void) {
    zx48k.input_poll_cb();

    /* Update the joystick */
    static struct {unsigned id; uint8_t mask;} const joy_map[] = {
        {RETRO_DEVICE_ID_JOYPAD_UP, ZX_JOYSTICK_UP},
        {RETRO_DEVICE_ID_JOYPAD_DOWN, ZX_JOYSTICK_DOWN},
        {RETRO_DEVICE_ID_JOYPAD_LEFT, ZX_JOYSTICK_LEFT},
        {RETRO_DEVICE_ID_JOYPAD_RIGHT, ZX_JOYSTICK_RIGHT},
        {RETRO_DEVICE_ID_JOYPAD_B, ZX_JOYSTICK_BTN}
    };

    uint8_t joy_mask = 0;

    for (int i = 0; i < sizeof(joy_map) / sizeof(joy_map[0]); i++) {
        int16_t const pressed = zx48k.input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, joy_map[i].id);

        if (pressed != 0) {
            joy_mask |= joy_map[i].mask;
        }
    }

    zx_joystick(&zx48k.zx, joy_mask);

    /* Update the keyboard */
    static unsigned const kbd_map[8][5] = {
        {RETROK_LSHIFT, RETROK_z,     RETROK_x, RETROK_c, RETROK_v},
        {RETROK_a,      RETROK_s,     RETROK_d, RETROK_f, RETROK_g},
        {RETROK_q,      RETROK_w,     RETROK_e, RETROK_r, RETROK_t},
        {RETROK_1,      RETROK_2,     RETROK_3, RETROK_4, RETROK_5},
        {RETROK_0,      RETROK_9,     RETROK_8, RETROK_7, RETROK_6},
        {RETROK_p,      RETROK_o,     RETROK_i, RETROK_u, RETROK_y},
        {RETROK_RETURN, RETROK_l,     RETROK_k, RETROK_j, RETROK_h},
        {RETROK_SPACE,  RETROK_LCTRL, RETROK_m, RETROK_n, RETROK_b}
    };

    uint64_t current_key_states = 0;
    uint64_t key = 1;

    for (int col = 0; col < 8; col++) {
        for (int row = 0; row < 5; row++, key <<= 1) {
            uint64_t const pressed = -(zx48k.input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, kbd_map[col][row]) != 0);
            current_key_states |= pressed & key;
        }
    }

    uint64_t const changed_keys = current_key_states ^ zx48k.key_states;
    key = 1;

    for (int code = 128; code < 168; code++, key <<= 1) {
        if ((changed_keys & key) != 0) {
            if ((current_key_states & key) != 0) {
                zx_key_down(&zx48k.zx, code);
            }
            else {
                zx_key_up(&zx48k.zx, code);
            }
        }
    }

    zx48k.key_states = current_key_states;

    zx_exec(&zx48k.zx, ZX48K_US_PER_FRAME);

    uint32_t pixel_buffer[320 * 256];

    for (size_t i = 0; i < sizeof(pixel_buffer) / sizeof(pixel_buffer[0]); i++) {
        uint32_t const pixel = zx48k.pixel_buffer[i];
        pixel_buffer[i] = (pixel & 0xff00ff00) | ((pixel & 0x00ff0000) >> 16) | ((pixel & 0x000000ff) << 16);
    }

    zx48k.video_cb(pixel_buffer, zx48k.width, zx48k.height, zx48k.width * 4);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50050, %rsp          # imm = 0x50050
callq *0x8aacf(%rip)          # 0xad680
movb $0x0, -0x1(%rbp)
movl $0x0, -0x8(%rbp)
movslq -0x8(%rbp), %rax
cmpq $0x5, %rax
jae 0x22c19
movq 0x8aabb(%rip), %rax     # 0xad688
movslq -0x8(%rbp), %rdx
leaq 0x1de8(%rip), %rcx      # 0x249c0
movl (%rcx,%rdx,8), %ecx
xorl %edx, %edx
movl $0x1, %esi
movl %edx, %edi
callq *%rax
movw %ax, -0xa(%rbp)
movswl -0xa(%rbp), %eax
cmpl $0x0, %eax
je 0x22c0c
movslq -0x8(%rbp), %rcx
leaq 0x1dc2(%rip), %rax      # 0x249c0
movzbl 0x4(%rax,%rcx,8), %ecx
movzbl -0x1(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1(%rbp)
jmp 0x22c0e
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x22bbc
leaq 0xa7f0(%rip), %rdi      # 0x2d410
movzbl -0x1(%rbp), %esi
callq 0x3510
movq $0x0, -0x18(%rbp)
movq $0x1, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x8, -0x24(%rbp)
jge 0x22cce
movl $0x0, -0x28(%rbp)
cmpl $0x5, -0x28(%rbp)
jge 0x22cbe
movq 0x8aa2a(%rip), %rax     # 0xad688
movslq -0x24(%rbp), %rdx
leaq 0x1d87(%rip), %rcx      # 0x249f0
imulq $0x14, %rdx, %rdx
addq %rdx, %rcx
movslq -0x28(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
xorl %edx, %edx
movl $0x3, %esi
movl %edx, %edi
callq *%rax
cwtl
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
subl %ecx, %eax
cltq
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
andq -0x20(%rbp), %rax
orq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
shlq %rax
movq %rax, -0x20(%rbp)
jmp 0x22c51
jmp 0x22cc0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x22c40
movq -0x18(%rbp), %rax
xorq 0x3a967(%rip), %rax     # 0x5d640
movq %rax, -0x38(%rbp)
movq $0x1, -0x20(%rbp)
movl $0x80, -0x3c(%rbp)
cmpl $0xa8, -0x3c(%rbp)
jge 0x22d4b
movq -0x38(%rbp), %rax
andq -0x20(%rbp), %rax
cmpq $0x0, %rax
je 0x22d33
movq -0x18(%rbp), %rax
andq -0x20(%rbp), %rax
cmpq $0x0, %rax
je 0x22d22
movl -0x3c(%rbp), %esi
leaq 0xa6f5(%rip), %rdi      # 0x2d410
callq 0x3430
jmp 0x22d31
movl -0x3c(%rbp), %esi
leaq 0xa6e4(%rip), %rdi      # 0x2d410
callq 0x3210
jmp 0x22d33
jmp 0x22d35
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
shlq %rax
movq %rax, -0x20(%rbp)
jmp 0x22cec
movq -0x18(%rbp), %rax
movq %rax, 0x3a8ea(%rip)     # 0x5d640
leaq 0xa6b3(%rip), %rdi      # 0x2d410
movl $0x4e20, %esi           # imm = 0x4E20
callq 0x3280
movq $0x0, -0x50048(%rbp)
cmpq $0x14000, -0x50048(%rbp) # imm = 0x14000
jae 0x22de8
movq -0x50048(%rbp), %rcx
leaq 0xa683(%rip), %rax      # 0x2d410
movl 0x30238(%rax,%rcx,4), %eax
movl %eax, -0x5004c(%rbp)
movl -0x5004c(%rbp), %ecx
andl $0xff00ff00, %ecx       # imm = 0xFF00FF00
movl -0x5004c(%rbp), %eax
andl $0xff0000, %eax         # imm = 0xFF0000
shrl $0x10, %eax
orl %eax, %ecx
movl -0x5004c(%rbp), %eax
andl $0xff, %eax
shll $0x10, %eax
orl %eax, %ecx
movq -0x50048(%rbp), %rax
movl %ecx, -0x50040(%rbp,%rax,4)
movq -0x50048(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50048(%rbp)
jmp 0x22d72
movq 0x8a881(%rip), %rax     # 0xad670
leaq -0x50040(%rbp), %rdi
movl 0x8a84c(%rip), %esi     # 0xad648
movl 0x8a84a(%rip), %edx     # 0xad64c
movl 0x8a840(%rip), %ecx     # 0xad648
shll $0x2, %ecx
movl %ecx, %ecx
callq *%rax
addq $0x50050, %rsp          # imm = 0x50050
popq %rbp
retq
nopl (%rax,%rax)