#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int tohex(int ch) {
  if (ch >= '0' && ch <= '9') {
    return (ch - '0');
  }
  if (ch >= 'A' && ch <= 'F') {
    return (ch - 'A' + 10);
  }
  if (ch >= 'a' && ch <= 'f') {
    return (ch - 'a' + 10);
  }
  fprintf(stderr, "\nInvalid hex digit '%c'\n", ch);
  fflush(stderr);
  exit(1);
}


x86 assembly (clang, -O0):
<tohex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x30,-0x8(%rbp)
jl     115d <tohex+0x2d>
cmpl   $0x39,-0x8(%rbp)
jg     115d <tohex+0x2d>
mov    -0x8(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <tohex+0xa7>
cmpl   $0x41,-0x8(%rbp)
jl     1182 <tohex+0x52>
cmpl   $0x46,-0x8(%rbp)
jg     1182 <tohex+0x52>
mov    -0x8(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <tohex+0xa7>
cmpl   $0x61,-0x8(%rbp)
jl     11a7 <tohex+0x77>
cmpl   $0x66,-0x8(%rbp)
jg     11a7 <tohex+0x77>
mov    -0x8(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <tohex+0xa7>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tohex>:
mov    %edi,%edx
lea    -0x30(%rdx),%eax
cmp    $0xa,%eax
jb     1154 <tohex+0x24>
lea    -0x41(%rdx),%eax
cmp    $0x5,%eax
ja     1147 <tohex+0x17>
add    $0xffffffc9,%edx
jmp    1152 <tohex+0x22>
lea    -0x61(%rdx),%eax
cmp    $0x5,%eax
ja     1155 <tohex+0x25>
add    $0xffffffa9,%edx
mov    %edx,%eax
ret
push   %rbx
mov    0x2e83(%rip),%rbx
mov    (%rbx),%edi
lea    0xe9a(%rip),%rsi
call   1030 <fprintf@plt>
mov    (%rbx),%edi
call   1040 <fflush@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"\0AInvalid hex digit '%c'\0A\00"
define i32 @tohex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = icmp sge i32 %4, 48
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 57
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* %3
%11 = sub nsw i32 %10, 48
store i32 %11, i32* %2
br label %39
12: ; preds = %6, %1
%13 = load i32, i32* %3
%14 = icmp sge i32 %13, 65
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i32, i32* %3
%17 = icmp sle i32 %16, 70
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 65
%21 = add nsw i32 %20, 10
store i32 %21, i32* %2
br label %39
22: ; preds = %15, %12
%23 = load i32, i32* %3
%24 = icmp sge i32 %23, 97
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* %3
%27 = icmp sle i32 %26, 102
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 97
%31 = add nsw i32 %30, 10
store i32 %31, i32* %2
br label %39
32: ; preds = %25, %22
%33 = load i32, i32* @stderr
%34 = load i32, i32* %3
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* @stderr
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %28, %18, %9
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"\0AInvalid hex digit '%c'\0A\00"
define i32 @tohex(i32 %0) {
%2 = add i32 %0, -48
%3 = icmp ult i32 %2, 10
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = add i32 %0, -65
%6 = icmp ult i32 %5, 6
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = add nsw i32 %0, -55
br label %20
9: ; preds = %4
%10 = add i32 %0, -97
%11 = icmp ult i32 %10, 6
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = add nsw i32 %0, -87
br label %20
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = load i32, i32* @stderr
%18 = tail call i32 @fflush(i32 %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %1, %12, %7
%21 = phi i32 [ %8, %7 ], [ %13, %12 ], [ %2, %1 ]
ret i32 %21
}