#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;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
int str_to_int(char *str, char *argname) {
  char *endptr;
  long val = -1;
  int base = 10;
  errno = 0;
  val = strtol(str, &endptr, base);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0)) {
    fprintf(stderr, "Error when parsing option %s: ", argname);
    perror("strtol");
  }
  if (endptr == str) {
    fprintf(stderr, "No digits were found when parsing option %s\n", argname);
  }
  if (*endptr != '\0') {
    fprintf(stderr,
            "Excess characters after integer argument of option %s: %s\n",
            argname, endptr);
  }
  return (int)val;
}


x86 assembly (clang, -O0):
<str_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
movl   $0xa,-0x24(%rbp)
mov    0x2e6a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <str_to_int+0x83>
mov    -0x20(%rbp),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
je     11cf <str_to_int+0x9f>
mov    -0x20(%rbp),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
je     11cf <str_to_int+0x9f>
mov    0x2e06(%rip),%rax
cmpq   $0x0,(%rax)
je     11f6 <str_to_int+0xc6>
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <str_to_int+0xc6>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    121f <str_to_int+0xef>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     124e <str_to_int+0x11e>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_int>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e7b(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r15
mov    (%rbx),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1183 <str_to_int+0x53>
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%r15
je     11e8 <str_to_int+0xb8>
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%r15
je     11e8 <str_to_int+0xb8>
test   %rax,%rax
je     118d <str_to_int+0x5d>
test   %r15,%r15
je     11e8 <str_to_int+0xb8>
mov    (%rsp),%rbx
cmp    %r12,%rbx
je     11c7 <str_to_int+0x97>
cmpb   $0x0,(%rbx)
je     11b8 <str_to_int+0x88>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xea8(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
cmpb   $0x0,(%rbx)
jne    119b <str_to_int+0x6b>
jmp    11b8 <str_to_int+0x88>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
lea    0xe16(%rip),%rdi
call   1050 <perror@plt>
jmp    118d <str_to_int+0x5d>

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 = [31 x i8] c"Error when parsing option %s: \00"
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [45 x i8] c"No digits were found when parsing option %s\0A\00"
@.str.3 = [59 x i8] c"Excess characters after integer argument of option %s: %s\0A\00"
define i32 @str_to_int(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 -1, i64* %6
store i32 10, i32* %7
store i64 0, i64* @errno
%8 = load i8*, i8** %3
%9 = load i32, i32* %7
%10 = call i64 @strtol(i8* %8, i8** %5, i32 %9)
store i64 %10, i64* %6
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i64, i64* %6
%16 = load i64, i64* @LONG_MAX
%17 = icmp eq i64 %15, %16
br i1 %17, label %28, label %18
18: ; preds = %14
%19 = load i64, i64* %6
%20 = load i64, i64* @LONG_MIN
%21 = icmp eq i64 %19, %20
br i1 %21, label %28, label %22
22: ; preds = %18, %2
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i64, i64* %6
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %25, %18, %14
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %4
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %30)
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %28, %25, %22
%34 = load i8*, i8** %5
%35 = load i8*, i8** %3
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %4
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37, %33
%42 = load i8*, i8** %5
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %4
%49 = load i8*, i8** %5
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i8* %48, i8* %49)
br label %51
51: ; preds = %46, %41
%52 = load i64, i64* %6
%53 = trunc i64 %52 to i32
ret i32 %53
}

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 = [31 x i8] c"Error when parsing option %s: \00"
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [45 x i8] c"No digits were found when parsing option %s\0A\00"
@.str.3 = [59 x i8] c"Excess characters after integer argument of option %s: %s\0A\00"
define i32 @str_to_int(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%6 = load i64, i64* @errno
%7 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i64, i64* @LONG_MAX
%11 = icmp eq i64 %5, %10
%12 = load i64, i64* @LONG_MIN
%13 = icmp eq i64 %5, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %19, label %15
15: ; preds = %9, %2
%16 = icmp ne i64 %6, 0
%17 = icmp eq i64 %5, 0
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %19, label %23
19: ; preds = %15, %9
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %1)
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %19, %15
%24 = load i8*, i8** %3
%25 = icmp eq i8* %24, %0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %29
29: ; preds = %26, %23
%30 = load i8, i8* %24
%31 = icmp eq i8 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %24)
br label %35
35: ; preds = %32, %29
%36 = trunc i64 %5 to i32
ret i32 %36
}