#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*/ uint64_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
int /*<<< orphan*/ strtoull(char *, char **, int);
char *terror(int);
uint64_t parse_hex_range(const char *str, int start, int end, char *err,
                         size_t err_len) {
  char *endptr = NULL;
  uint64_t ret;
  char substr[32 + 1];
  err[0] = '\0';
  if (end - start >= 32) {
    snprintf(err, err_len, "parse_hex_range buffer too short.");
    return 0;
  }
  memcpy(substr, str + start, end - start);
  substr[end - start] = '\0';
  errno = 0;
  ret = strtoull(substr, &endptr, 16);
  if (errno) {
    int e = errno;
    snprintf(err, err_len, "parse_hex_range error: %s", terror(e));
    return 0;
  }
  while (1) {
    char c = *endptr;
    if (c == '\0') {
      break;
    }
    if ((c != ' ') || (c != '\t')) {
      snprintf(err, err_len,
               "parse_hex_range error: garbage at end "
               "of string.");
      return 0;
    }
    endptr++;
  }
  return ret;
}


x86 assembly (clang, -O0):
<parse_hex_range>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x20,%eax
jl     119d <parse_hex_range+0x5d>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xe76(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <parse_hex_range+0x172>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cltq
movb   $0x0,-0x60(%rbp,%rax,1)
mov    0x2e15(%rip),%rax
movl   $0x0,(%rax)
lea    -0x60(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtoull@plt>
mov    %eax,-0x34(%rbp)
mov    0x2df3(%rip),%rax
cmpl   $0x0,(%rax)
je     123f <parse_hex_range+0xff>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x64(%rbp),%edi
call   1060 <terror@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    %rax,%rcx
lea    0xdf6(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <parse_hex_range+0x172>
jmp    1244 <parse_hex_range+0x104>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x65(%rbp)
movsbl -0x65(%rbp),%eax
cmp    $0x0,%eax
jne    125f <parse_hex_range+0x11f>
jmp    12ac <parse_hex_range+0x16c>
movsbl -0x65(%rbp),%eax
cmp    $0x20,%eax
jne    1279 <parse_hex_range+0x139>
movsbl -0x65(%rbp),%eax
cmp    $0x9,%eax
je     129b <parse_hex_range+0x15b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xdb4(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <parse_hex_range+0x172>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1244 <parse_hex_range+0x104>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex_range>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %edx,%ebp
movq   $0x0,0x8(%rsp)
movb   $0x0,(%rcx)
sub    %esi,%ebp
cmp    $0x20,%ebp
jl     1170 <parse_hex_range+0x30>
lea    0xe92(%rip),%rdx
jmp    11ef <parse_hex_range+0xaf>
mov    %rdi,%rax
movslq %esi,%rcx
add    %rcx,%rax
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x2e46(%rip),%r12
movl   $0x0,(%r12)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    $0x10,%edx
call   1040 <strtoull@plt>
mov    (%r12),%edi
test   %edi,%edi
je     11dc <parse_hex_range+0x9c>
call   1060 <terror@plt>
lea    0xe5a(%rip),%rdx
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    11fe <parse_hex_range+0xbe>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11fe <parse_hex_range+0xbe>
lea    0xe4d(%rip),%rdx
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"parse_hex_range buffer too short.\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"parse_hex_range error: %s\00"
@.str.2 = [49 x i8] c"parse_hex_range error: garbage at end of string.\00"
define i32 @parse_hex_range(i8* %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca [33 x i8]
%15 = alloca i32
%16 = alloca i8
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i64 %4, i64* %11
store i8* null, i8** %12
%17 = load i8*, i8** %10
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
%19 = load i32, i32* %9
%20 = load i32, i32* %8
%21 = sub nsw i32 %19, %20
%22 = icmp sge i32 %21, 32
br i1 %22, label %23, label %27
23: ; preds = %5
%24 = load i8*, i8** %10
%25 = load i64, i64* %11
%26 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %24, i64 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %78
27: ; preds = %5
%28 = getelementptr inbounds [33 x i8], [33 x i8]* %14, i64 0, i64 0
%29 = load i8*, i8** %7
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = sub nsw i32 %33, %34
%36 = call i32 @memcpy(i8* %28, i8* %32, i32 %35)
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = sub nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [33 x i8], [33 x i8]* %14, i64 0, i64 %40
store i8 0, i8* %41
store i32 0, i32* @errno
%42 = getelementptr inbounds [33 x i8], [33 x i8]* %14, i64 0, i64 0
%43 = call i32 @strtoull(i8* %42, i8** %12, i32 16)
store i32 %43, i32* %13
%44 = load i32, i32* @errno
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %27
%47 = load i32, i32* @errno
store i32 %47, i32* %15
%48 = load i8*, i8** %10
%49 = load i64, i64* %11
%50 = load i32, i32* %15
%51 = call i8* @terror(i32 %50)
%52 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %48, i64 %49, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %51)
store i32 0, i32* %6
br label %78
53: ; preds = %27
br label %54
54: ; preds = %53, %73
%55 = load i8*, i8** %12
%56 = load i8, i8* %55
store i8 %56, i8* %16
%57 = load i8, i8* %16
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %54
br label %76
61: ; preds = %54
%62 = load i8, i8* %16
%63 = sext i8 %62 to i32
%64 = icmp ne i32 %63, 32
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i8, i8* %16
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 9
br i1 %68, label %69, label %73
69: ; preds = %65, %61
%70 = load i8*, i8** %10
%71 = load i64, i64* %11
%72 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %70, i64 %71, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %78
73: ; preds = %65
%74 = load i8*, i8** %12
%75 = getelementptr inbounds i8, i8* %74, i32 1
store i8* %75, i8** %12
br label %54
76: ; preds = %60
%77 = load i32, i32* %13
store i32 %77, i32* %6
br label %78
78: ; preds = %76, %69, %46, %23
%79 = load i32, i32* %6
ret i32 %79
}

LLVM IR (-O2):
@.str = [34 x i8] c"parse_hex_range buffer too short.\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"parse_hex_range error: %s\00"
@.str.2 = [49 x i8] c"parse_hex_range error: garbage at end of string.\00"
define i32 @parse_hex_range(i8* %0, i32 %1, i32 %2, i8* nocapture writeonly %3, i64 %4) {
%6 = alloca i8*
%7 = alloca [33 x i8]
%8 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%9 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
store i8 0, i8* %3
%10 = sub nsw i32 %2, %1
%11 = icmp sgt i32 %10, 31
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %32
14: ; preds = %5
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = call i32 @memcpy(i8* %9, i8* %16, i32 %10)
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 %18
store i8 0, i8* %19
store i32 0, i32* @errno
%20 = call i32 @strtoull(i8* %9, i8** %6, i32 16)
%21 = load i32, i32* @errno
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %14
%24 = call i8* @terror(i32 %21)
%25 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %32
26: ; preds = %14
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26, %23, %12
%33 = phi i32 [ 0, %12 ], [ 0, %23 ], [ %20, %26 ], [ 0, %30 ]
ret i32 %33
}