// File: /workspace/github_c_makefile_star2_O2/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 0x964d(%rip), %rbx      # 0xbb80
xorps %xmm0, %xmm0
movaps %xmm0, 0x9633(%rip)     # 0xbb70
cmpq $0x6a, %rbx
ja 0x2608
movl %ebx, 0x9567(%rip)      # 0xbab4
testq %rbx, %rbx
je 0x265d
movslq 0x9553(%rip), %rax      # 0xbab0
cmpq %rax, %rbx
je 0x265d
movl 0x95e3(%rip), %r10d     # 0xbb50
addl %ebx, %r10d
movq %rbx, %rax
shlq $0xb, %rax
addq 0x95da(%rip), %rax      # 0xbb58
movq %rax, %r11
shrq $0x11, %r11
shll $0x4, %eax
addl %eax, %r11d
movq 0x95ce(%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, 0x95a5(%rip)      # 0xbb50
movq %r11, 0x95a6(%rip)      # 0xbb58
movq %r14, 0x95a7(%rip)      # 0xbb60
leaq 0x9830(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x25f5
leaq 0x4b2f(%rip), %rdi      # 0x70fb
leaq 0x4b36(%rip), %rsi      # 0x7109
leaq 0x4b3b(%rip), %r9       # 0x7115
pushq $0x4e
popq %rdx
pushq $0x2
popq %rcx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
pushq %rbx
callq 0x2cd0
addq $0x20, %rsp
xorl %eax, %eax
callq 0x2c10
movq %rax, 0x9565(%rip)      # 0xbb68
jmp 0x26f0
cmpl $0x6b01, %ebx           # imm = 0x6B01
jae 0x2648
movzbl %bl, %eax
movslq 0x949a(%rip), %rcx      # 0xbab4
cmpq %rcx, %rax
sete %sil
movq %rbx, %rdx
shrq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
je 0x2648
cmpq %rcx, %rdx
jne 0x2648
cmpq %rcx, %rax
cmoveq %rcx, %rbx
movzbl %bl, %ebx
jmp 0x254d
andq $0x0, 0x9500(%rip)      # 0xbb50
andq $0x0, 0x9500(%rip)      # 0xbb58
jmp 0x26f6
xorl %eax, %eax
callq 0x2c10
movl $0x3e8, %ecx            # imm = 0x3E8
addq 0x94f8(%rip), %rcx      # 0xbb68
cmpq %rcx, %rax
jbe 0x26f0
movq 0x94dc(%rip), %r10      # 0xbb58
shrq $0xb, %r10
movl 0x94ca(%rip), %eax      # 0xbb50
shll $0x15, %eax
addl %eax, %r10d
movq %r10, 0x94dd(%rip)      # 0xbb70
movq 0x94c6(%rip), %r11      # 0xbb60
movq %r11, 0x94d7(%rip)      # 0xbb78
leaq 0x9748(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x26d8
leaq 0x4a47(%rip), %rdi      # 0x70fb
leaq 0x4a4e(%rip), %rsi      # 0x7109
leaq 0x4a86(%rip), %r9       # 0x7148
pushq $0x55
popq %rdx
pushq $0x2
popq %rcx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2cd0
popq %rax
popq %rcx
andq $0x0, 0x9478(%rip)      # 0xbb58
andq $0x0, 0x9478(%rip)      # 0xbb60
andq $0x0, 0x9460(%rip)      # 0xbb50
movl %ebx, 0x93ba(%rip)      # 0xbab0
leaq 0x9473(%rip), %rax      # 0xbb70
addq $0x8, %rsp
popq %rbx
popq %r14
retq
nopl (%rax)