#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
short KEY_A1;
short KEY_A3;
short KEY_B2;
short KEY_C1;
short KEY_C3;
int kb_toascii(const unsigned char *key) {
  short numpad[10] = {0, KEY_C1, 0, KEY_C3, 0, KEY_B2, 0, KEY_A1, 0, KEY_A3};
  int ch = key[0] | key[1] << 8;
  int i;
  for (i = 0; i < 10; i++)
    if (numpad[i] == ch)
      return i + '0';
  return '\033' + 1;
}


x86 assembly (clang, -O0):
<kb_toascii>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
movw   $0x0,-0x30(%rbp)
mov    0x2eb3(%rip),%rax
mov    (%rax),%ax
mov    %ax,-0x2e(%rbp)
movw   $0x0,-0x2c(%rbp)
mov    0x2ec7(%rip),%rax
mov    (%rax),%ax
mov    %ax,-0x2a(%rbp)
movw   $0x0,-0x28(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%ax
mov    %ax,-0x26(%rbp)
movw   $0x0,-0x24(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%ax
mov    %ax,-0x22(%rbp)
movw   $0x0,-0x20(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%ax
mov    %ax,-0x1e(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0xa,-0x38(%rbp)
jge    11c7 <kb_toascii+0xc7>
movslq -0x38(%rbp),%rax
movswl -0x30(%rbp,%rax,2),%eax
cmp    -0x34(%rbp),%eax
jne    11b4 <kb_toascii+0xb4>
mov    -0x38(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11ce <kb_toascii+0xce>
jmp    11b9 <kb_toascii+0xb9>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    118a <kb_toascii+0x8a>
movl   $0x1c,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<kb_toascii>:
movzwl (%rdi),%ecx
test   %ecx,%ecx
je     116d <kb_toascii+0x6d>
mov    0x2eba(%rip),%rax
movswl (%rax),%edx
mov    $0x31,%eax
cmp    %edx,%ecx
je     116c <kb_toascii+0x6c>
mov    0x2ecf(%rip),%rax
movswl (%rax),%edx
mov    $0x33,%eax
cmp    %edx,%ecx
je     116c <kb_toascii+0x6c>
mov    0x2eac(%rip),%rax
movswl (%rax),%edx
mov    $0x35,%eax
cmp    %edx,%ecx
je     116c <kb_toascii+0x6c>
mov    0x2e89(%rip),%rax
movswl (%rax),%edx
mov    $0x37,%eax
cmp    %edx,%ecx
je     116c <kb_toascii+0x6c>
mov    0x2e7e(%rip),%rax
movswl (%rax),%eax
cmp    %eax,%ecx
mov    $0x39,%ecx
mov    $0x1c,%eax
cmove  %ecx,%eax
ret
mov    $0x30,%eax
ret

LLVM IR (-O0):
@KEY_C1 = global i16 0
@KEY_C3 = global i16 0
@KEY_B2 = global i16 0
@KEY_A1 = global i16 0
@KEY_A3 = global i16 0
define i32 @kb_toascii(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [10 x i16]
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = getelementptr inbounds [10 x i16], [10 x i16]* %4, i64 0, i64 0
store i16 0, i16* %7
%8 = getelementptr inbounds i16, i16* %7, i64 1
%9 = load i16, i16* @KEY_C1
store i16 %9, i16* %8
%10 = getelementptr inbounds i16, i16* %8, i64 1
store i16 0, i16* %10
%11 = getelementptr inbounds i16, i16* %10, i64 1
%12 = load i16, i16* @KEY_C3
store i16 %12, i16* %11
%13 = getelementptr inbounds i16, i16* %11, i64 1
store i16 0, i16* %13
%14 = getelementptr inbounds i16, i16* %13, i64 1
%15 = load i16, i16* @KEY_B2
store i16 %15, i16* %14
%16 = getelementptr inbounds i16, i16* %14, i64 1
store i16 0, i16* %16
%17 = getelementptr inbounds i16, i16* %16, i64 1
%18 = load i16, i16* @KEY_A1
store i16 %18, i16* %17
%19 = getelementptr inbounds i16, i16* %17, i64 1
store i16 0, i16* %19
%20 = getelementptr inbounds i16, i16* %19, i64 1
%21 = load i16, i16* @KEY_A3
store i16 %21, i16* %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl i32 %29, 8
%31 = or i32 %25, %30
store i32 %31, i32* %5
store i32 0, i32* %6
br label %32
32: ; preds = %47, %1
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 10
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [10 x i16], [10 x i16]* %4, i64 0, i64 %37
%39 = load i16, i16* %38
%40 = sext i16 %39 to i32
%41 = load i32, i32* %5
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 48
store i32 %45, i32* %2
br label %51
46: ; preds = %35
br label %47
47: ; preds = %46
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %32
50: ; preds = %32
store i32 28, i32* %2
br label %51
51: ; preds = %50, %43
%52 = load i32, i32* %2
ret i32 %52
}

LLVM IR (-O2):
@KEY_C1 = global i16 0
@KEY_C3 = global i16 0
@KEY_B2 = global i16 0
@KEY_A1 = global i16 0
@KEY_A3 = global i16 0
define i32 @kb_toascii(i8* nocapture %0) {
%2 = load i16, i16* @KEY_C3
%3 = load i16, i16* @KEY_B2
%4 = load i16, i16* @KEY_A1
%5 = load i16, i16* @KEY_A3
%6 = load i8, i8* %0
%7 = zext i8 %6 to i32
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = shl nuw nsw i32 %10, 8
%12 = or i32 %11, %7
%13 = icmp eq i32 %12, 0
br i1 %13, label %31, label %14
14: ; preds = %1
%15 = load i16, i16* @KEY_C1
%16 = sext i16 %15 to i32
%17 = icmp eq i32 %12, %16
br i1 %17, label %31, label %18
18: ; preds = %14
%19 = sext i16 %2 to i32
%20 = icmp eq i32 %12, %19
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = sext i16 %3 to i32
%23 = icmp eq i32 %12, %22
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = sext i16 %4 to i32
%26 = icmp eq i32 %12, %25
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = sext i16 %5 to i32
%29 = icmp eq i32 %12, %28
%30 = select i1 %29, i32 57, i32 28
br label %31
31: ; preds = %27, %1, %14, %18, %21, %24
%32 = phi i32 [ 48, %1 ], [ 49, %14 ], [ 51, %18 ], [ 53, %21 ], [ 55, %24 ], [ %30, %27 ]
ret i32 %32
}