#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 __u8;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char, char);
int /*<<< orphan*/ stderr;
int fromhex(__u8 c) {
  if (c >= '0' && c <= '9')
    return (c - '0');
  if (c >= 'a' && c <= 'f')
    return 10 + (c - 'a');
  if (c >= 'A' && c <= 'F')
    return 10 + (c - 'A');
  fprintf(stderr, "bad char: '%c' 0x%02x\n", c, c);
  exit(EINVAL);
}


x86 assembly (clang, -O0):
<fromhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x30,%eax
jl     1157 <fromhex+0x37>
movsbl -0x5(%rbp),%eax
cmp    $0x39,%eax
jg     1157 <fromhex+0x37>
movsbl -0x5(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <fromhex+0xbc>
movsbl -0x5(%rbp),%eax
cmp    $0x61,%eax
jl     1183 <fromhex+0x63>
movsbl -0x5(%rbp),%eax
cmp    $0x66,%eax
jg     1183 <fromhex+0x63>
movsbl -0x5(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <fromhex+0xbc>
movsbl -0x5(%rbp),%eax
cmp    $0x41,%eax
jl     11af <fromhex+0x8f>
movsbl -0x5(%rbp),%eax
cmp    $0x46,%eax
jg     11af <fromhex+0x8f>
movsbl -0x5(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <fromhex+0xbc>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x5(%rbp),%al
lea    0xe3e(%rip),%rsi
movsbl %al,%edx
movsbl -0x5(%rbp),%ecx
call   1030 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fromhex>:
lea    -0x30(%rdi),%eax
mov    $0xffffffd0,%ecx
cmp    $0xa,%al
jb     1144 <fromhex+0x24>
lea    -0x61(%rdi),%eax
mov    $0xffffffa9,%ecx
cmp    $0x6,%al
jb     1144 <fromhex+0x24>
lea    -0x41(%rdi),%eax
mov    $0xffffffc9,%ecx
cmp    $0x6,%al
jae    114b <fromhex+0x2b>
movsbl %dil,%eax
add    %ecx,%eax
ret
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
movsbl %dil,%ecx
lea    0xea0(%rip),%rsi
mov    %eax,%edi
mov    %ecx,%edx
call   1030 <fprintf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

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

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