// File: donfreiday[P]gb/joypad.cpp
// Function: Joypad::handleEvent(SDL_Event)
void Joypad::handleEvent(SDL_Event e) {
  if (e.type == SDL_KEYDOWN) {
    switch (e.key.keysym.sym) {
      case SDLK_DOWN:
        bitClear(directions, DOWN);
        break;

      case SDLK_UP:
        bitClear(directions, UP);
        break;

      case SDLK_LEFT:
        bitClear(directions, LEFT);
        break;

      case SDLK_RIGHT:
        bitClear(directions, RIGHT);
        break;

      case SDLK_RETURN:
        bitClear(buttons, START);
        break;

      case SDLK_SPACE:
        bitClear(buttons, SELECT);
        break;

      case SDLK_z:
        bitClear(buttons, A);
        break;

      case SDLK_x:
        bitClear(buttons, B);
        break;

      default:
        break;
    }
  } else if (e.type == SDL_CONTROLLERBUTTONDOWN) {
    switch (e.cbutton.button) {
      case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
        bitClear(directions, DOWN);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_UP:
        bitClear(directions, UP);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
        bitClear(directions, LEFT);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
        bitClear(directions, RIGHT);
        break;

      case SDL_CONTROLLER_BUTTON_START:
        bitClear(buttons, START);
        break;

      case SDL_CONTROLLER_BUTTON_BACK:
        bitClear(buttons, SELECT);
        break;

      case SDL_CONTROLLER_BUTTON_A:
        bitClear(buttons, A);
        break;

      case SDL_CONTROLLER_BUTTON_B:
        bitClear(buttons, B);
        break;

      // I prefer square button for B :-D
      case 2:
        bitClear(buttons, B);
        break;

      default:
        break;
    }
  } else if (e.type == SDL_KEYUP) {
    switch (e.key.keysym.sym) {
      case SDLK_DOWN:
        bitSet(directions, DOWN);
        break;

      case SDLK_UP:
        bitSet(directions, UP);
        break;

      case SDLK_LEFT:
        bitSet(directions, LEFT);
        break;

      case SDLK_RIGHT:
        bitSet(directions, RIGHT);
        break;

      case SDLK_RETURN:
        bitSet(buttons, START);
        break;

      case SDLK_SPACE:
        bitSet(buttons, SELECT);
        break;

      case SDLK_z:
        bitSet(buttons, A);
        break;

      case SDLK_x:
        bitSet(buttons, B);
        break;

      default:
        break;
    }
  } else if (e.type == SDL_CONTROLLERBUTTONUP) {
    switch (e.cbutton.button) {
      case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
        bitSet(directions, DOWN);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_UP:
        bitSet(directions, UP);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
        bitSet(directions, LEFT);
        break;

      case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
        bitSet(directions, RIGHT);
        break;

      case SDL_CONTROLLER_BUTTON_START:
        bitSet(buttons, START);
        break;

      case SDL_CONTROLLER_BUTTON_BACK:
        bitSet(buttons, SELECT);
        break;

      case SDL_CONTROLLER_BUTTON_A:
        bitSet(buttons, A);
        break;

      case SDL_CONTROLLER_BUTTON_B:
        bitSet(buttons, B);
        break;

        // I prefer square button for B :-D
      case 2:
        bitSet(buttons, B);
        break;

      default:
        break;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x10(%rbp)
cmpl $0x300, (%rax)          # imm = 0x300
jne 0x4e55b
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x1c(%rbp)
subl $0xd, %eax
je 0x4e517
jmp 0x4e46d
movl -0x1c(%rbp), %eax
subl $0x20, %eax
je 0x4e527
jmp 0x4e47b
movl -0x1c(%rbp), %eax
subl $0x78, %eax
je 0x4e544
jmp 0x4e489
movl -0x1c(%rbp), %eax
subl $0x7a, %eax
je 0x4e537
jmp 0x4e497
movl -0x1c(%rbp), %eax
subl $0x4000004f, %eax       # imm = 0x4000004F
je 0x4e506
jmp 0x4e4a3
movl -0x1c(%rbp), %eax
subl $0x40000050, %eax       # imm = 0x40000050
je 0x4e4f2
jmp 0x4e4af
movl -0x1c(%rbp), %eax
subl $0x40000051, %eax       # imm = 0x40000051
je 0x4e4ca
jmp 0x4e4bb
movl -0x1c(%rbp), %eax
subl $0x40000052, %eax       # imm = 0x40000052
je 0x4e4de
jmp 0x4e554
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x3, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x2, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
xorl %esi, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x4e8b0
jmp 0x4e556
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8b0
jmp 0x4e556
jmp 0x4e556
jmp 0x4e82d
movq -0x18(%rbp), %rax
cmpl $0x651, (%rax)          # imm = 0x651
jne 0x4e639
movq -0x18(%rbp), %rax
movzbl 0xc(%rax), %eax
movq %rax, -0x28(%rbp)
subq $0xe, %rax
ja 0x4e632
movq -0x28(%rbp), %rax
leaq 0x17a54(%rip), %rcx     # 0x65fe0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x3, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x2, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
xorl %esi, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8b0
jmp 0x4e634
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8b0
jmp 0x4e634
jmp 0x4e634
jmp 0x4e82b
movq -0x18(%rbp), %rax
cmpl $0x301, (%rax)          # imm = 0x301
jne 0x4e74c
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0xd, %eax
je 0x4e708
jmp 0x4e65e
movl -0x2c(%rbp), %eax
subl $0x20, %eax
je 0x4e718
jmp 0x4e66c
movl -0x2c(%rbp), %eax
subl $0x78, %eax
je 0x4e735
jmp 0x4e67a
movl -0x2c(%rbp), %eax
subl $0x7a, %eax
je 0x4e728
jmp 0x4e688
movl -0x2c(%rbp), %eax
subl $0x4000004f, %eax       # imm = 0x4000004F
je 0x4e6f7
jmp 0x4e694
movl -0x2c(%rbp), %eax
subl $0x40000050, %eax       # imm = 0x40000050
je 0x4e6e3
jmp 0x4e6a0
movl -0x2c(%rbp), %eax
subl $0x40000051, %eax       # imm = 0x40000051
je 0x4e6bb
jmp 0x4e6ac
movl -0x2c(%rbp), %eax
subl $0x40000052, %eax       # imm = 0x40000052
je 0x4e6cf
jmp 0x4e745
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x3, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x2, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
xorl %esi, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x4e8e0
jmp 0x4e747
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8e0
jmp 0x4e747
jmp 0x4e747
jmp 0x4e829
movq -0x18(%rbp), %rax
cmpl $0x652, (%rax)          # imm = 0x652
jne 0x4e827
movq -0x18(%rbp), %rax
movzbl 0xc(%rax), %eax
movq %rax, -0x38(%rbp)
subq $0xe, %rax
ja 0x4e823
movq -0x38(%rbp), %rax
leaq 0x17827(%rip), %rcx     # 0x65fa4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x3, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x2, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
addq $0x1, %rdi
xorl %esi, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8e0
jmp 0x4e825
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0x4e8e0
jmp 0x4e825
jmp 0x4e825
jmp 0x4e827
jmp 0x4e829
jmp 0x4e82b
jmp 0x4e82d
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)