#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 */
scalar_t__ ERANGE;
int /*<<< orphan*/ EXIT_FAILURE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long strtoul(char *, char **, int);
unsigned long str2long(char *str, char *opt) {
  int base;
  char *endptr;
  long val;
  base = (str[1] == 'x' || str[1] == 'X') ? 16 : 10;
  errno = 0;
  val = strtoul(str, &endptr, base);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0)) {
    fprintf(stderr, "error when parsing -%s\n", opt);
    perror("strtol");
    exit(EXIT_FAILURE);
  }
  if (endptr == str) {
    fprintf(stderr, "No digits were found when parseing -%s\n", opt);
    exit(EXIT_FAILURE);
  }
  return val;
}


x86 assembly (clang, -O0):
<str2long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    $0x1,%al
cmp    $0x78,%ecx
mov    %al,-0x29(%rbp)
je     1177 <str2long+0x37>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x58,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%dl
mov    $0xa,%eax
mov    $0x10,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2c(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1050 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
jne    11f1 <str2long+0xb1>
mov    -0x28(%rbp),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
je     120d <str2long+0xcd>
mov    -0x28(%rbp),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
je     120d <str2long+0xcd>
mov    0x2dc8(%rip),%rax
cmpq   $0x0,(%rax)
je     1240 <str2long+0x100>
cmpq   $0x0,-0x28(%rbp)
jne    1240 <str2long+0x100>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddf(%rip),%rsi
call   1030 <fprintf@plt>
lea    0xdeb(%rip),%rdi
call   1040 <perror@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1275 <str2long+0x135>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbd(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2long>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x1(%rdi),%al
and    $0xdf,%al
cmp    $0x58,%al
mov    $0x10,%eax
mov    $0xa,%edx
cmove  %eax,%edx
mov    0x2e56(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
call   1050 <strtoul@plt>
mov    (%r15),%rcx
mov    0x2e5b(%rip),%rdx
cmp    (%rdx),%rcx
jne    11a2 <str2long+0x62>
mov    0x2e37(%rip),%rdx
cmp    (%rdx),%rax
je     11bd <str2long+0x7d>
mov    0x2e4b(%rip),%rdx
cmp    (%rdx),%rax
je     11bd <str2long+0x7d>
test   %rcx,%rcx
je     11ac <str2long+0x6c>
test   %rax,%rax
je     11bd <str2long+0x7d>
cmp    %rbx,0x8(%rsp)
je     11e3 <str2long+0xa3>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
lea    0xe3c(%rip),%rdi
call   1040 <perror@plt>
jmp    11fb <str2long+0xbb>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    %r14,%rdx
call   1030 <fprintf@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"error when parsing -%s\0A\00"
@.str.1 = [7 x i8] c"strtol\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [40 x i8] c"No digits were found when parseing -%s\0A\00"
define i64 @str2long(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 120
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 88
br label %19
19: ; preds = %13, %2
%20 = phi i1 [ true, %2 ], [ %18, %13 ]
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 16, i32 10
store i32 %22, i32* %5
store i64 0, i64* @errno
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = call i64 @strtoul(i8* %23, i8** %6, i32 %24)
store i64 %25, i64* %7
%26 = load i64, i64* @errno
%27 = load i64, i64* @ERANGE
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %37
29: ; preds = %19
%30 = load i64, i64* %7
%31 = load i64, i64* @LONG_MAX
%32 = icmp eq i64 %30, %31
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = load i64, i64* %7
%35 = load i64, i64* @LONG_MIN
%36 = icmp eq i64 %34, %35
br i1 %36, label %43, label %37
37: ; preds = %33, %19
%38 = load i64, i64* @errno
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %37
%41 = load i64, i64* %7
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %40, %33, %29
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %4
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40, %37
%51 = load i8*, i8** %6
%52 = load i8*, i8** %3
%53 = icmp eq i8* %51, %52
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %4
%57 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %56)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %50
%61 = load i64, i64* %7
ret i64 %61
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"error when parsing -%s\0A\00"
@.str.1 = [7 x i8] c"strtol\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [40 x i8] c"No digits were found when parseing -%s\0A\00"
define i64 @str2long(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = and i8 %6, -33
%8 = icmp eq i8 %7, 88
%9 = select i1 %8, i32 16, i32 10
store i64 0, i64* @errno
%10 = call i64 @strtoul(i8* %0, i8** %3, i32 %9)
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i64, i64* @LONG_MAX
%16 = icmp eq i64 %10, %15
%17 = load i64, i64* @LONG_MIN
%18 = icmp eq i64 %10, %17
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %24, label %20
20: ; preds = %14, %2
%21 = icmp ne i64 %11, 0
%22 = icmp eq i64 %10, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %30
24: ; preds = %20, %14
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %1)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = icmp eq i8* %31, %0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %30
ret i64 %10
}