#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 */
scalar_t__ toupper(int);
int hex_to_int(int c) {
  static const char *hex_table = "0123456789ABCDEF";
  int i;
  char target = (char)toupper(c);
  for (i = 0; hex_table[i] != '\0'; i++) {
    if (target == hex_table[i])
      return i;
  }
  return -1;
}


x86 assembly (clang, -O0):
<hex_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <toupper@plt>
mov    %al,-0xd(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2ef4(%rip),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     117e <hex_to_int+0x6e>
movsbl -0xd(%rbp),%eax
mov    0x2ed8(%rip),%rcx
movslq -0xc(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    116b <hex_to_int+0x5b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1185 <hex_to_int+0x75>
jmp    1170 <hex_to_int+0x60>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112d <hex_to_int+0x1d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_int>:
push   %rax
call   1030 <toupper@plt>
add    $0xd0,%al
mov    $0xffffffff,%ecx
cmp    $0x16,%al
ja     112f <hex_to_int+0x1f>
movsbq %al,%rax
lea    0xed4(%rip),%rcx
mov    (%rcx,%rax,4),%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
@hex_to_int.hex_table = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @hex_to_int(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @toupper(i32 %6)
%8 = trunc i64 %7 to i8
store i8 %8, i8* %5
store i32 0, i32* %4
br label %9
9: ; preds = %30, %1
%10 = load i8*, i8** @hex_to_int.hex_table
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %9
%18 = load i8, i8* %5
%19 = sext i8 %18 to i32
%20 = load i8*, i8** @hex_to_int.hex_table
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %19, %25
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %34
29: ; preds = %17
br label %30
30: ; preds = %29
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %9
33: ; preds = %9
store i32 -1, i32* %2
br label %34
34: ; preds = %33, %27
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@switch.table.hex_to_int = [23 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15]
define i32 @hex_to_int(i32 %0) {
%2 = tail call i64 @toupper(i32 %0)
%3 = trunc i64 %2 to i8
%4 = add i8 %3, -48
%5 = icmp ult i8 %4, 23
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = sext i8 %4 to i64
%8 = getelementptr inbounds [23 x i32], [23 x i32]* @switch.table.hex_to_int, i64 0, i64 %7
%9 = load i32, i32* %8
br label %10
10: ; preds = %1, %6
%11 = phi i32 [ %9, %6 ], [ -1, %1 ]
ret i32 %11
}