#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
char toupper(char);
int hextoint(char x) {
  x = toupper(x);
  if (x >= 'A' && x <= 'F')
    return x - 'A' + 10;
  else if (x >= '0' && x <= '9')
    return x - '0';
  fprintf(stderr, "bad input.\n");
  exit(1);
}


x86 assembly (clang, -O0):
<hextoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%edi
call   1030 <toupper@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x41,%eax
jl     1176 <hextoint+0x46>
movsbl -0x5(%rbp),%eax
cmp    $0x46,%eax
jg     1176 <hextoint+0x46>
movsbl -0x5(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <hextoint+0x93>
movsbl -0x5(%rbp),%eax
cmp    $0x30,%eax
jl     119f <hextoint+0x6f>
movsbl -0x5(%rbp),%eax
cmp    $0x39,%eax
jg     119f <hextoint+0x6f>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <hextoint+0x93>
jmp    11a4 <hextoint+0x74>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hextoint>:
push   %rax
call   1030 <toupper@plt>
lea    -0x41(%rax),%edx
mov    $0xffffffc9,%ecx
cmp    $0x6,%dl
jb     1150 <hextoint+0x20>
lea    -0x30(%rax),%edx
mov    $0xffffffd0,%ecx
cmp    $0xa,%dl
jae    1157 <hextoint+0x27>
movsbl %al,%eax
add    %ecx,%eax
pop    %rcx
ret
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0xe99(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"bad input.\0A\00"
define i32 @hextoint(i8 signext %0) {
%2 = tail call signext i8 @toupper(i8 signext %0)
%3 = add i8 %2, -65
%4 = icmp ult i8 %3, 6
br i1 %4, label %12, label %5
5: ; preds = %1
%6 = add i8 %2, -48
%7 = icmp ult i8 %6, 10
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5, %1
%13 = phi i32 [ -55, %1 ], [ -48, %5 ]
%14 = sext i8 %2 to i32
%15 = add nsw i32 %13, %14
ret i32 %15
}