#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 *, char *);
int /*<<< orphan*/ stderr;
char toupper(char);
unsigned int readhex(char *cptr) {
  unsigned int value = 0x00000000;
  int loop;
  if ((cptr == NULL) || (*cptr == '\0')) {
    fprintf(stderr, "%s: NULL number given\n", "romsplit");
    exit(2);
  }
  for (loop = 0; (loop < 8); loop++) {
    if (*cptr == '\0')
      break;
    *cptr = toupper(*cptr);
    if (((*cptr < '0') || (*cptr > '9')) && ((*cptr < 'A') || (*cptr > 'F'))) {
      fprintf(stderr, "%s: invalid digit given\n", "romsplit");
      exit(2);
    }
    value = ((value << 4) |
             ((*cptr <= '9') ? (*cptr - '0') : ((*cptr - 'A') + 10)));
    cptr++;
  }
  if (*cptr != '\0') {
    fprintf(stderr, "%s: Too many digits given\n", "romsplit");
    exit(2);
  }
  return (value);
}


x86 assembly (clang, -O0):
<readhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115e <readhex+0x2e>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1184 <readhex+0x54>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
lea    0xea2(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jge    1284 <readhex+0x154>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11aa <readhex+0x7a>
jmp    1284 <readhex+0x154>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     11de <readhex+0xae>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1224 <readhex+0xf4>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x41,%eax
jl     11fe <readhex+0xce>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x46,%eax
jle    1224 <readhex+0xf4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
lea    0xe02(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     124f <readhex+0x11f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
mov    %eax,-0x18(%rbp)
jmp    125f <readhex+0x12f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118b <readhex+0x5b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ba <readhex+0x18a>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
lea    0xd6c(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhex>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11d9 <readhex+0xa9>
mov    %rdi,%r14
mov    (%rdi),%al
test   %al,%al
je     11d9 <readhex+0xa9>
xor    %ebx,%ebx
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%edi
call   1030 <toupper@plt>
mov    %al,-0x1(%r14,%r15,1)
lea    -0x3a(%rax),%ecx
cmp    $0xf5,%cl
ja     117d <readhex+0x4d>
lea    -0x47(%rax),%ecx
cmp    $0xf9,%cl
jbe    11b3 <readhex+0x83>
mov    %ebx,%ecx
shl    $0x4,%ecx
movzbl %al,%ebx
lea    -0x37(%rbx),%edx
add    $0xffffffd0,%ebx
cmp    $0x3a,%al
cmovae %edx,%ebx
or     %ecx,%ebx
movzbl (%r14,%r15,1),%eax
cmp    $0x8,%r15d
je     11a7 <readhex+0x77>
add    $0x1,%r15
test   %al,%al
jne    1160 <readhex+0x30>
jmp    11ab <readhex+0x7b>
test   %al,%al
jne    11eb <readhex+0xbb>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
lea    0xe4d(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
jmp    11c3 <readhex+0x93>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
jmp    11c3 <readhex+0x93>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"%s: NULL number given\0A\00"
@.str.1 = [9 x i8] c"romsplit\00"
@.str.2 = [25 x i8] c"%s: invalid digit given\0A\00"
@.str.3 = [27 x i8] c"%s: Too many digits given\0A\00"
define i32 @readhex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i8*, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %7, %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 2)
unreachable
16: ; preds = %7
store i32 0, i32* %4
br label %17
17: ; preds = %77, %16
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 8
br i1 %19, label %20, label %80
20: ; preds = %17
%21 = load i8*, i8** %2
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
br label %80
26: ; preds = %20
%27 = load i8*, i8** %2
%28 = load i8, i8* %27
%29 = call signext i8 @toupper(i8 signext %28)
%30 = load i8*, i8** %2
store i8 %29, i8* %30
%31 = load i8*, i8** %2
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp slt i32 %33, 48
br i1 %34, label %40, label %35
35: ; preds = %26
%36 = load i8*, i8** %2
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp sgt i32 %38, 57
br i1 %39, label %40, label %54
40: ; preds = %35, %26
%41 = load i8*, i8** %2
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp slt i32 %43, 65
br i1 %44, label %50, label %45
45: ; preds = %40
%46 = load i8*, i8** %2
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp sgt i32 %48, 70
br i1 %49, label %50, label %54
50: ; preds = %45, %40
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 2)
unreachable
54: ; preds = %45, %35
%55 = load i32, i32* %3
%56 = shl i32 %55, 4
%57 = load i8*, i8** %2
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp sle i32 %59, 57
br i1 %60, label %61, label %66
61: ; preds = %54
%62 = load i8*, i8** %2
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = sub nsw i32 %64, 48
br label %72
66: ; preds = %54
%67 = load i8*, i8** %2
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = sub nsw i32 %69, 65
%71 = add nsw i32 %70, 10
br label %72
72: ; preds = %66, %61
%73 = phi i32 [ %65, %61 ], [ %71, %66 ]
%74 = or i32 %56, %73
store i32 %74, i32* %3
%75 = load i8*, i8** %2
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %2
br label %77
77: ; preds = %72
%78 = load i32, i32* %4
%79 = add nsw i32 %78, 1
store i32 %79, i32* %4
br label %17
80: ; preds = %25, %17
%81 = load i8*, i8** %2
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = icmp ne i32 %83, 0
br i1 %84, label %85, label %89
85: ; preds = %80
%86 = load i32, i32* @stderr
%87 = call i32 @fprintf(i32 %86, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%88 = call i32 @exit(i32 2)
unreachable
89: ; preds = %80
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"%s: NULL number given\0A\00"
@.str.1 = [9 x i8] c"romsplit\00"
@.str.2 = [25 x i8] c"%s: invalid digit given\0A\00"
@.str.3 = [27 x i8] c"%s: Too many digits given\0A\00"
define i32 @readhex(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3, %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %3, %37
%11 = phi i8* [ %32, %37 ], [ %0, %3 ]
%12 = phi i32 [ %31, %37 ], [ 0, %3 ]
%13 = phi i32 [ %33, %37 ], [ 0, %3 ]
%14 = phi i8 [ %35, %37 ], [ %4, %3 ]
%15 = tail call signext i8 @toupper(i8 signext %14)
store i8 %15, i8* %11
%16 = add i8 %15, -58
%17 = icmp ult i8 %16, -10
%18 = add i8 %15, -71
%19 = icmp ult i8 %18, -6
%20 = and i1 %17, %19
br i1 %20, label %21, label %25
21: ; preds = %10
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %10
%26 = shl i32 %12, 4
%27 = zext i8 %15 to i32
%28 = icmp ult i8 %15, 58
%29 = select i1 %28, i32 -48, i32 -55
%30 = add nsw i32 %29, %27
%31 = or i32 %30, %26
%32 = getelementptr inbounds i8, i8* %11, i64 1
%33 = add nuw nsw i32 %13, 1
%34 = icmp eq i32 %33, 8
%35 = load i8, i8* %32
%36 = icmp eq i8 %35, 0
br i1 %34, label %38, label %37
37: ; preds = %25
br i1 %36, label %43, label %10
38: ; preds = %25
br i1 %36, label %43, label %39
39: ; preds = %38
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%42 = tail call i32 @exit(i32 2)
unreachable
43: ; preds = %37, %38
ret i32 %31
}