#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 */
char *strchr(char const *, char);
int HexDigit(char hex) {
  const char *Digits = "0123456789ABCDEF";
  const char *digits = "0123456789abcdef";
  char *ofs = strchr(digits, hex);
  if (ofs) {
    return ofs - digits;
  }
  ofs = strchr(Digits, hex);
  if (ofs) {
    return ofs - Digits;
  }
  return 0;
}


x86 assembly (clang, -O0):
<HexDigit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
lea    0xedb(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xee1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x5(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1163 <HexDigit+0x53>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1199 <HexDigit+0x89>
mov    -0x10(%rbp),%rdi
movsbl -0x5(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1192 <HexDigit+0x82>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1199 <HexDigit+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HexDigit>:
push   %rbx
mov    %edi,%ebx
lea    0xef7(%rip),%rdi
mov    %ebx,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     112f <HexDigit+0x1f>
lea    0xee4(%rip),%rcx
jmp    114a <HexDigit+0x3a>
movsbl %bl,%esi
lea    0xec7(%rip),%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     114e <HexDigit+0x3e>
lea    0xeb6(%rip),%rcx
sub    %ecx,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
define i32 @HexDigit(i8 signext %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8 signext %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = trunc i64 %5 to i32
%7 = sub i32 %6, ptrtoint ([17 x i8]* @.str.1 to i32)
br label %15
8: ; preds = %1
%9 = tail call i8* @strchr(i8* dereferenceable(17) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 signext %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = ptrtoint i8* %9 to i64
%13 = trunc i64 %12 to i32
%14 = sub i32 %13, ptrtoint ([17 x i8]* @.str to i32)
br label %15
15: ; preds = %8, %11, %4
%16 = phi i32 [ %7, %4 ], [ %14, %11 ], [ 0, %8 ]
ret i32 %16
}