// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x38, %rsp
movslq 0xb460(%rip), %rax      # 0xdbb0
movq %rax, -0x18(%rbp)
movq $0x0, 0xb441(%rip) # 0xdba0
movq $0x0, 0xb43e(%rip) # 0xdba8
cmpq $0x6b, -0x18(%rbp)
jae 0x2782
movl 0xb439(%rip), %eax      # 0xdbb0
movl %eax, 0xb347(%rip)      # 0xdac4
jmp 0x286c
cmpq $0x6b00, -0x18(%rbp)    # imm = 0x6B00
jbe 0x27b2
movq $0x0, 0xb3e9(%rip) # 0xdb80
movq $0x0, 0xb3e6(%rip) # 0xdb88
leaq 0xb3f7(%rip), %rax      # 0xdba0
movq %rax, -0x10(%rbp)
jmp 0x2aa4
movq -0x18(%rbp), %rax
andq $0xff, %rax
movslq 0xb301(%rip), %rcx      # 0xdac4
cmpq %rcx, %rax
je 0x2802
movq -0x18(%rbp), %rax
shrq $0x8, %rax
movslq 0xb2ed(%rip), %rcx      # 0xdac4
cmpq %rcx, %rax
je 0x2802
movq $0x0, 0xb399(%rip) # 0xdb80
movq $0x0, 0xb396(%rip) # 0xdb88
leaq 0xb3a7(%rip), %rax      # 0xdba0
movq %rax, -0x10(%rbp)
jmp 0x2aa4
movq -0x18(%rbp), %rax
shrq $0x8, %rax
movslq 0xb2b3(%rip), %rcx      # 0xdac4
cmpq %rcx, %rax
je 0x283c
movq $0x0, 0xb35f(%rip) # 0xdb80
movq $0x0, 0xb35c(%rip) # 0xdb88
leaq 0xb36d(%rip), %rax      # 0xdba0
movq %rax, -0x10(%rbp)
jmp 0x2aa4
movq -0x18(%rbp), %rax
andq $0xff, %rax
movslq 0xb277(%rip), %rcx      # 0xdac4
cmpq %rcx, %rax
jne 0x285e
movq -0x18(%rbp), %rax
shrq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
andq $0xff, %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x29c1
movq -0x18(%rbp), %rax
movslq 0xb23e(%rip), %rcx      # 0xdac0
cmpq %rcx, %rax
je 0x29c1
movq -0x18(%rbp), %rax
addq 0xb2ea(%rip), %rax      # 0xdb80
movq %rax, 0xb2e3(%rip)      # 0xdb80
movq -0x18(%rbp), %rax
shlq $0xb, %rax
addq 0xb2dc(%rip), %rax      # 0xdb88
movq %rax, 0xb2d5(%rip)      # 0xdb88
movq 0xb2ce(%rip), %rax      # 0xdb88
shrq $0x11, %rax
movq 0xb2c3(%rip), %rcx      # 0xdb88
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xb2b5(%rip)      # 0xdb88
movq 0xb2b6(%rip), %rax      # 0xdb90
shrq $0x1d, %rax
movq -0x18(%rbp), %rcx
imulq -0x18(%rbp), %rcx
addq %rcx, %rax
movq 0xb29f(%rip), %rcx      # 0xdb90
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xb291(%rip)      # 0xdb90
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0xb270(%rip), %rax      # 0xdb80
movq %rax, 0xb269(%rip)      # 0xdb80
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0xb260(%rip), %rax      # 0xdb88
movq %rax, 0xb259(%rip)      # 0xdb88
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0xb250(%rip), %rax      # 0xdb90
movq %rax, 0xb249(%rip)      # 0xdb90
movl $0x2, %eax
leaq 0xb4cd(%rip), %rcx      # 0xde20
cmpl (%rcx), %eax
jl 0x29ac
movq -0x18(%rbp), %rbx
movq 0xb21e(%rip), %r11      # 0xdb80
movq 0xb21f(%rip), %r10      # 0xdb88
movq 0xb220(%rip), %rax      # 0xdb90
leaq 0x7784(%rip), %rdi      # 0xa0fb
leaq 0x778b(%rip), %rsi      # 0xa109
movl $0x4e, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
leaq 0x7783(%rip), %r9       # 0xa115
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, %al
callq 0x3570
jmp 0x29ae
movb $0x0, %al
callq 0x33c0
movq %rax, 0xb1dc(%rip)      # 0xdb98
jmp 0x2a8f
movb $0x0, %al
callq 0x33c0
movq 0xb1c9(%rip), %rcx      # 0xdb98
addq $0x3e8, %rcx            # imm = 0x3E8
cmpq %rcx, %rax
jbe 0x2a8d
movq 0xb1a2(%rip), %rax      # 0xdb88
shrq $0xb, %rax
movq 0xb18f(%rip), %rcx      # 0xdb80
shlq $0x15, %rcx
addq %rcx, %rax
movabsq $0xffffffff, %rcx       # imm = 0xFFFFFFFF
andq %rcx, %rax
movq %rax, 0xb194(%rip)      # 0xdba0
movq 0xb17d(%rip), %rax      # 0xdb90
movq %rax, 0xb18e(%rip)      # 0xdba8
movl $0x2, %eax
leaq 0xb3fa(%rip), %rcx      # 0xde20
cmpl (%rcx), %eax
jl 0x2a6a
movq 0xb16f(%rip), %r10      # 0xdba0
movq 0xb170(%rip), %rax      # 0xdba8
leaq 0x76bc(%rip), %rdi      # 0xa0fb
leaq 0x76c3(%rip), %rsi      # 0xa109
movl $0x55, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
leaq 0x76ee(%rip), %r9       # 0xa148
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x3570
jmp 0x2a6c
movq $0x0, 0xb111(%rip) # 0xdb88
movq $0x0, 0xb10e(%rip) # 0xdb90
movq $0x0, 0xb0f3(%rip) # 0xdb80
jmp 0x2a8f
movq -0x18(%rbp), %rax
movl %eax, 0xb027(%rip)      # 0xdac0
leaq 0xb100(%rip), %rax      # 0xdba0
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x38, %rsp
popq %rbx
popq %rbp
retq
nop