#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ error_and_exit(char *);
int fgetc(int /*<<< orphan*/ *);
int fgethex(FILE *fp) {
  int ch = fgetc(fp);
  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;
  error_and_exit("curious HEX format");
  return 0;
}


x86 assembly (clang, -O0):
<fgethex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x30,-0x14(%rbp)
jl     115a <fgethex+0x3a>
cmpl   $0x39,-0x14(%rbp)
jg     115a <fgethex+0x3a>
mov    -0x14(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <fgethex+0x97>
cmpl   $0x41,-0x14(%rbp)
jl     117f <fgethex+0x5f>
cmpl   $0x46,-0x14(%rbp)
jg     117f <fgethex+0x5f>
mov    -0x14(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <fgethex+0x97>
cmpl   $0x61,-0x14(%rbp)
jl     11a4 <fgethex+0x84>
cmpl   $0x66,-0x14(%rbp)
jg     11a4 <fgethex+0x84>
mov    -0x14(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <fgethex+0x97>
lea    0xe55(%rip),%rdi
call   1040 <error_and_exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgethex>:
push   %rax
call   1030 <fgetc@plt>
lea    -0x30(%rax),%ecx
cmp    $0xa,%ecx
jb     115a <fgethex+0x3a>
lea    -0x41(%rax),%ecx
cmp    $0x5,%ecx
ja     113d <fgethex+0x1d>
add    $0xffffffc9,%eax
mov    %eax,%ecx
jmp    115a <fgethex+0x3a>
lea    -0x61(%rax),%ecx
cmp    $0x5,%ecx
ja     114c <fgethex+0x2c>
add    $0xffffffa9,%eax
mov    %eax,%ecx
jmp    115a <fgethex+0x3a>
lea    0xead(%rip),%rdi
call   1040 <error_and_exit@plt>
xor    %ecx,%ecx
mov    %ecx,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@.str = [19 x i8] c"curious HEX format\00"
define i32 @fgethex(i32* nocapture %0) {
%2 = tail call i32 @fgetc(i32* %0)
%3 = add i32 %2, -48
%4 = icmp ult i32 %3, 10
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = add i32 %2, -65
%7 = icmp ult i32 %6, 6
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = add nsw i32 %2, -55
br label %17
10: ; preds = %5
%11 = add i32 %2, -97
%12 = icmp ult i32 %11, 6
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = add nsw i32 %2, -87
br label %17
15: ; preds = %10
%16 = tail call i32 @error_and_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %1, %15, %13, %8
%18 = phi i32 [ %9, %8 ], [ %14, %13 ], [ 0, %15 ], [ %3, %1 ]
ret i32 %18
}