#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 */
typedef char uint8_t;

/* Variables and functions */
char toupper(char);
bool hex_to_int(char hex, uint8_t *val) {
  hex = toupper(hex);
  switch (hex) {
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
    *val = (hex - '0');
    return (true);
  case 'A':
  case 'B':
  case 'C':
  case 'D':
  case 'E':
  case 'F':
    *val = (hex - 55);
    return (true);
  default:
    return (false);
  }
}


x86 assembly (clang, -O0):
<hex_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
movsbl -0x5(%rbp),%edi
call   1030 <toupper@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x14(%rbp)
add    $0xffffffd0,%eax
sub    $0xa,%eax
jb     115a <hex_to_int+0x4a>
jmp    1146 <hex_to_int+0x36>
mov    -0x14(%rbp),%eax
add    $0xffffffbf,%eax
sub    $0x6,%eax
jb     1175 <hex_to_int+0x65>
jmp    1190 <hex_to_int+0x80>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1197 <hex_to_int+0x87>
movsbl -0x5(%rbp),%eax
sub    $0x37,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1197 <hex_to_int+0x87>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_int>:
push   %rbx
mov    %rsi,%rbx
call   1030 <toupper@plt>
movsbq %al,%rdx
add    $0xffffffffffffffd0,%rdx
xor    %ecx,%ecx
cmp    $0x16,%edx
ja     1143 <hex_to_int+0x33>
mov    $0x7e03ff,%esi
bt     %edx,%esi
jae    1143 <hex_to_int+0x33>
lea    0xec7(%rip),%rcx
add    (%rdx,%rcx,1),%al
mov    %al,(%rbx)
mov    $0x1,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @hex_to_int(i8 signext %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
store i8 %0, i8* %4
store i8* %1, i8** %5
%6 = load i8, i8* %4
%7 = call signext i8 @toupper(i8 signext %6)
store i8 %7, i8* %4
%8 = load i8, i8* %4
%9 = sext i8 %8 to i32
switch i32 %9, label %22 [
i32 48, label %10
i32 49, label %10
i32 50, label %10
i32 51, label %10
i32 52, label %10
i32 53, label %10
i32 54, label %10
i32 55, label %10
i32 56, label %10
i32 57, label %10
i32 65, label %16
i32 66, label %16
i32 67, label %16
i32 68, label %16
i32 69, label %16
i32 70, label %16
]
10: ; preds = %2, %2, %2, %2, %2, %2, %2, %2, %2, %2
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = sub nsw i32 %12, 48
%14 = trunc i32 %13 to i8
%15 = load i8*, i8** %5
store i8 %14, i8* %15
store i32 1, i32* %3
br label %23
16: ; preds = %2, %2, %2, %2, %2, %2
%17 = load i8, i8* %4
%18 = sext i8 %17 to i32
%19 = sub nsw i32 %18, 55
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %5
store i8 %20, i8* %21
store i32 1, i32* %3
br label %23
22: ; preds = %2
store i32 0, i32* %3
br label %23
23: ; preds = %22, %16, %10
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@switch.table.hex_to_int = [23 x i8] c"\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\D0\C9\C9\C9\C9\C9\C9"
define i32 @hex_to_int(i8 signext %0, i8* nocapture writeonly %1) {
%3 = tail call signext i8 @toupper(i8 signext %0)
%4 = sext i8 %3 to i32
%5 = add nsw i32 %4, -48
%6 = icmp ult i32 %5, 23
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = lshr i32 8258559, %5
%9 = and i32 %8, 1
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %7
%12 = sext i32 %5 to i64
%13 = getelementptr inbounds [23 x i8], [23 x i8]* @switch.table.hex_to_int, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = add i8 %3, %14
store i8 %15, i8* %1
br label %16
16: ; preds = %7, %2, %11
%17 = phi i32 [ 0, %2 ], [ 1, %11 ], [ 0, %7 ]
ret i32 %17
}