// File: /workspace/github_c_makefile_star2_O1/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 0x95dd(%rip), %rbx      # 0xbb80
xorps %xmm0, %xmm0
movaps %xmm0, 0x95c3(%rip)     # 0xbb70
cmpq $0x6a, %rbx
ja 0x267c
movl %ebx, 0x94f7(%rip)      # 0xbab4
testq %rbx, %rbx
je 0x26d1
movslq 0x94e3(%rip), %rax      # 0xbab0
cmpq %rax, %rbx
je 0x26d1
movl 0x9573(%rip), %r10d     # 0xbb50
addl %ebx, %r10d
movq %rbx, %rax
shlq $0xb, %rax
addq 0x956a(%rip), %rax      # 0xbb58
movq %rax, %r11
shrq $0x11, %r11
shll $0x4, %eax
addl %eax, %r11d
movq 0x955e(%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, 0x9535(%rip)      # 0xbb50
movq %r11, 0x9536(%rip)      # 0xbb58
movq %r14, 0x9537(%rip)      # 0xbb60
leaq 0x97c0(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x2669
leaq 0x5abf(%rip), %rdi      # 0x80fb
leaq 0x5ac6(%rip), %rsi      # 0x8109
leaq 0x5acb(%rip), %r9       # 0x8115
movl $0x4e, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r10
pushq %rbx
callq 0x2d72
addq $0x20, %rsp
xorl %eax, %eax
callq 0x2cc8
movq %rax, 0x94f1(%rip)      # 0xbb68
jmp 0x276d
cmpl $0x6b01, %ebx           # imm = 0x6B01
jae 0x26bc
movzbl %bl, %eax
movslq 0x9426(%rip), %rcx      # 0xbab4
cmpq %rcx, %rax
sete %sil
movq %rbx, %rdx
shrq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
je 0x26bc
cmpq %rcx, %rdx
jne 0x26bc
cmpq %rcx, %rax
cmoveq %rdx, %rbx
movzbl %bl, %ebx
jmp 0x25bd
xorl %eax, %eax
movq %rax, 0x948b(%rip)      # 0xbb50
movq %rax, 0x948c(%rip)      # 0xbb58
jmp 0x2773
xorl %eax, %eax
callq 0x2cc8
movl $0x3e8, %ecx            # imm = 0x3E8
addq 0x9484(%rip), %rcx      # 0xbb68
cmpq %rcx, %rax
jbe 0x276d
movq 0x9464(%rip), %r10      # 0xbb58
shrq $0xb, %r10
movl 0x9452(%rip), %eax      # 0xbb50
shll $0x15, %eax
addl %eax, %r10d
movq %r10, 0x9465(%rip)      # 0xbb70
movq 0x944e(%rip), %r11      # 0xbb60
movq %r11, 0x945f(%rip)      # 0xbb78
leaq 0x96d0(%rip), %rax      # 0xbdf0
cmpl $0x2, (%rax)
jg 0x2756
leaq 0x59cf(%rip), %rdi      # 0x80fb
leaq 0x59d6(%rip), %rsi      # 0x8109
leaq 0x5a0e(%rip), %r9       # 0x8148
movl $0x55, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2d72
addq $0x10, %rsp
xorl %eax, %eax
movq %rax, 0x93f9(%rip)      # 0xbb58
movq %rax, 0x93fa(%rip)      # 0xbb60
movq %rax, 0x93e3(%rip)      # 0xbb50
movl %ebx, 0x933d(%rip)      # 0xbab0
leaq 0x93f6(%rip), %rax      # 0xbb70
addq $0x8, %rsp
popq %rbx
popq %r14
retq
nop