// File: /workspace/github_c_makefile_star2_O3/jeff-1amstudios[P]carmageddon-1-cheat-code-disassembly/src/input.c
// Function: KevKeyService
tU32 *KevKeyService()
{
  static tU32 sum = 0;
  static tU32 code = 0;
  static tU32 code2 = 0;
  static int last_key = -1;
  static int last_single_key = -1;
  static tU32 last_time = 0;
  static tU32 return_val[2];
  tU32 keys;
  
  keys = gLast_key_down;
  //printf("key: %d, %lx, %lx\n", sizeof(long), keys, code2);
  return_val[0] = 0;
  return_val[1] = 0;
  
  if (keys < 0x6B) {
    last_single_key = gLast_key_down;
  }
  else {
    if (keys > 0x6b00) {
      sum = 0;
      code = 0;
      return return_val;
    }
    if ((keys & 0xff) != last_single_key && keys >> 8 != last_single_key) {
      sum = 0;
      code = 0;
      return return_val;
    }
    if (keys >> 8 != last_single_key) {
      sum = 0;
      code = 0;
      return return_val;
    }
    if ((keys & 0xff) == last_single_key) {
      keys = keys >> 8;
    }
    
    keys = keys & 0xff;
  }

  if (keys && keys != last_key) {
    sum += keys;
    code += keys << 11;
    code = (code >> 17) + 16 * code;
    code2 = (code2 >> 29) + keys * keys + 8 * code2;
    // JeffH added to mask to 32 bit values
    sum &= 0xffffffff;
    code &= 0xffffffff;
    code2 &= 0xffffffff;
    // ------------------
    ZF_LOGD("accumulate: keys=%lx, sum=%lx, code=%lx, code2=%lx", keys, sum, code, code2);
    last_time = PDGetTotalTime();
    
  }
  else if (PDGetTotalTime() > (last_time + 1000)) {
    return_val[0] = ((code >> 11) + (sum << 21)) & 0xffffffff;
    return_val[1] = code2;
    ZF_LOGD("final value: code=%lx, code2=%lx", return_val[0], return_val[1]);
    code = 0;
    code2 = 0;
    sum = 0;
  }
  last_key = keys;
  return return_val;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movslq 0x95ed(%rip), %rbx      # 0xbb80
xorps %xmm0, %xmm0
movaps %xmm0, 0x95d3(%rip)     # 0xbb70
cmpq $0x6a, %rbx
ja 0x266c
movl %ebx, 0x9507(%rip)      # 0xbab4
testq %rbx, %rbx
je 0x26c1
movslq 0x94f3(%rip), %rax      # 0xbab0
cmpq %rax, %rbx
je 0x26c1
movl 0x9583(%rip), %r10d     # 0xbb50
addl %ebx, %r10d
movq %rbx, %rax
shlq $0xb, %rax
addq 0x957a(%rip), %rax      # 0xbb58
movq %rax, %r11
shrq $0x11, %r11
shll $0x4, %eax
addl %eax, %r11d
movq 0x956e(%rip), %rax      # 0xbb60
movq %rax, %rcx
shrq $0x1d, %rcx
movl %ebx, %edx
imull %ebx, %edx
addl %edx, %ecx
leal (%rcx,%rax,8), %r14d
movq %r10, 0x9545(%rip)      # 0xbb50
movq %r11, 0x9546(%rip)      # 0xbb58
movq %r14, 0x9547(%rip)      # 0xbb60
leaq 0x97d0(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x2659
leaq 0x5acf(%rip), %rdi      # 0x80fb
leaq 0x5ad6(%rip), %rsi      # 0x8109
leaq 0x5adb(%rip), %r9       # 0x8115
movl $0x4e, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
pushq %rbx
callq 0x2d56
addq $0x20, %rsp
xorl %eax, %eax
callq 0x2cac
movq %rax, 0x9501(%rip)      # 0xbb68
jmp 0x275d
cmpl $0x6b01, %ebx           # imm = 0x6B01
jae 0x26ac
movzbl %bl, %eax
movslq 0x9436(%rip), %rcx      # 0xbab4
cmpq %rcx, %rax
sete %sil
movq %rbx, %rdx
shrq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
je 0x26ac
cmpq %rcx, %rdx
jne 0x26ac
cmpq %rcx, %rax
cmoveq %rcx, %rbx
movzbl %bl, %ebx
jmp 0x25ad
xorl %eax, %eax
movq %rax, 0x949b(%rip)      # 0xbb50
movq %rax, 0x949c(%rip)      # 0xbb58
jmp 0x2763
xorl %eax, %eax
callq 0x2cac
movl $0x3e8, %ecx            # imm = 0x3E8
addq 0x9494(%rip), %rcx      # 0xbb68
cmpq %rcx, %rax
jbe 0x275d
movq 0x9474(%rip), %r10      # 0xbb58
shrq $0xb, %r10
movl 0x9462(%rip), %eax      # 0xbb50
shll $0x15, %eax
addl %eax, %r10d
movq %r10, 0x9475(%rip)      # 0xbb70
movq 0x945e(%rip), %r11      # 0xbb60
movq %r11, 0x946f(%rip)      # 0xbb78
leaq 0x96e0(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x2746
leaq 0x59df(%rip), %rdi      # 0x80fb
leaq 0x59e6(%rip), %rsi      # 0x8109
leaq 0x5a1e(%rip), %r9       # 0x8148
movl $0x55, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2d56
addq $0x10, %rsp
xorl %eax, %eax
movq %rax, 0x9409(%rip)      # 0xbb58
movq %rax, 0x940a(%rip)      # 0xbb60
movq %rax, 0x93f3(%rip)      # 0xbb50
movl %ebx, 0x934d(%rip)      # 0xbab0
leaq 0x9406(%rip), %rax      # 0xbb70
addq $0x8, %rsp
popq %rbx
popq %r14
retq
nop