#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 long readhex(char *cptr) {
  unsigned long value = 0x00000000;
  int loop;
  if ((cptr == NULL) || (*cptr == '\0')) {
    fprintf(stderr, "%s: NULL hex number given\n", "mwr");
    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 HEX digit given\n", "mwr");
      exit(2);
    }
    value = ((value << 4) |
             ((*cptr <= '9') ? (unsigned long)(*cptr - '0')
                             : ((unsigned long)(*cptr - 'A') + 10L)));
    cptr++;
  }
  if (*cptr != '\0') {
    fprintf(stderr, "%s: Too many HEX digits given\n", "mwr");
    exit(2);
  }
  return (value);
}


x86 assembly (clang, -O0):
<readhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     115f <readhex+0x2f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1185 <readhex+0x55>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
lea    0xea5(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    1293 <readhex+0x163>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ab <readhex+0x7b>
jmp    1293 <readhex+0x163>
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     11df <readhex+0xaf>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jle    1225 <readhex+0xf5>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x41,%eax
jl     11ff <readhex+0xcf>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x46,%eax
jle    1225 <readhex+0xf5>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
lea    0xe05(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
shl    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x39,%eax
jg     1256 <readhex+0x126>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
cltq
mov    %rax,-0x28(%rbp)
jmp    126a <readhex+0x13a>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x41,%eax
cltq
add    $0xa,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
or     %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118c <readhex+0x5c>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12c9 <readhex+0x199>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
lea    0xd89(%rip),%rsi
lea    0xd61(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhex>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11e0 <readhex+0xb0>
mov    %rdi,%r14
mov    (%rdi),%al
test   %al,%al
je     11e0 <readhex+0xb0>
mov    $0x1,%r15d
xor    %ebx,%ebx
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    11ba <readhex+0x8a>
mov    %rbx,%rcx
shl    $0x4,%rcx
movzbl %al,%ebx
lea    -0x37(%rbx),%rdx
add    $0xffffffffffffffd0,%rbx
cmp    $0x3a,%al
cmovae %rdx,%rbx
or     %rcx,%rbx
movzbl (%r14,%r15,1),%eax
cmp    $0x8,%r15d
je     11ad <readhex+0x7d>
add    $0x1,%r15
test   %al,%al
jne    1160 <readhex+0x30>
jmp    11b1 <readhex+0x81>
test   %al,%al
jne    11f2 <readhex+0xc2>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
lea    0xe4a(%rip),%rdx
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
jmp    11ca <readhex+0x9a>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
jmp    11ca <readhex+0x9a>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"%s: NULL hex number given\0A\00"
@.str.1 = [4 x i8] c"mwr\00"
@.str.2 = [29 x i8] c"%s: invalid HEX digit given\0A\00"
@.str.3 = [31 x i8] c"%s: Too many HEX digits given\0A\00"
define i64 @readhex(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
store i8* %0, i8** %2
store i64 0, i64* %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 ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 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 = %79, %16
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 8
br i1 %19, label %20, label %82
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 %82
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 ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%53 = call i32 @exit(i32 2)
unreachable
54: ; preds = %45, %35
%55 = load i64, i64* %3
%56 = shl i64 %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 %67
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
%66 = sext i32 %65 to i64
br label %74
67: ; preds = %54
%68 = load i8*, i8** %2
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = sub nsw i32 %70, 65
%72 = sext i32 %71 to i64
%73 = add i64 %72, 10
br label %74
74: ; preds = %67, %61
%75 = phi i64 [ %66, %61 ], [ %73, %67 ]
%76 = or i64 %56, %75
store i64 %76, i64* %3
%77 = load i8*, i8** %2
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %2
br label %79
79: ; preds = %74
%80 = load i32, i32* %4
%81 = add nsw i32 %80, 1
store i32 %81, i32* %4
br label %17
82: ; preds = %25, %17
%83 = load i8*, i8** %2
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %91
87: ; preds = %82
%88 = load i32, i32* @stderr
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%90 = call i32 @exit(i32 2)
unreachable
91: ; preds = %82
%92 = load i64, i64* %3
ret i64 %92
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"%s: NULL hex number given\0A\00"
@.str.1 = [4 x i8] c"mwr\00"
@.str.2 = [29 x i8] c"%s: invalid HEX digit given\0A\00"
@.str.3 = [31 x i8] c"%s: Too many HEX digits given\0A\00"
define i64 @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 ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 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 i64 [ %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 ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %10
%26 = shl i64 %12, 4
%27 = icmp ult i8 %15, 58
%28 = zext i8 %15 to i64
%29 = select i1 %27, i64 -48, i64 -55
%30 = add nsw i64 %29, %28
%31 = or i64 %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 ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%42 = tail call i32 @exit(i32 2)
unreachable
43: ; preds = %37, %38
ret i64 %31
}