#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 int32_t;

/* Variables and functions */
scalar_t__ ERANGE;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long strtol(char *, char **, int);
int32_t string_to_int(char *str, int32_t base) {
  int num;
  char *ep;
  long val;
  errno = 0;
  val = strtol(str, &ep, base);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0)) {
    perror("strtol");
    return -1;
  }
  if (ep == optarg) {
    fprintf(stderr, "No digits were found.\n");
    return -1;
  }
  return (num = val);
}


x86 assembly (clang, -O0):
<string_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <string_to_int+0x73>
mov    -0x28(%rbp),%rax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <string_to_int+0x8f>
mov    -0x28(%rbp),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     11bf <string_to_int+0x8f>
mov    0x2e0e(%rip),%rax
cmpq   $0x0,(%rax)
je     11d7 <string_to_int+0xa7>
cmpq   $0x0,-0x28(%rbp)
jne    11d7 <string_to_int+0xa7>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <string_to_int+0xe6>
mov    -0x20(%rbp),%rax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <string_to_int+0xdc>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <string_to_int+0xe6>
mov    -0x28(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_int>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%edx
mov    0x2e7a(%rip),%rbx
movq   $0x0,(%rbx)
lea    0x8(%rsp),%rsi
call   1040 <strtol@plt>
mov    (%rbx),%rcx
mov    0x2e7f(%rip),%rdx
cmp    (%rdx),%rcx
jne    1176 <string_to_int+0x46>
mov    0x2e5b(%rip),%rdx
cmp    (%rdx),%rax
je     11b1 <string_to_int+0x81>
mov    0x2e6f(%rip),%rdx
cmp    (%rdx),%rax
je     11b1 <string_to_int+0x81>
test   %rcx,%rcx
je     1180 <string_to_int+0x50>
test   %rax,%rax
je     11b1 <string_to_int+0x81>
mov    0x8(%rsp),%rcx
mov    0x2e6c(%rip),%rdx
cmp    (%rdx),%rcx
jne    11ab <string_to_int+0x7b>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
jmp    11a6 <string_to_int+0x76>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str = [7 x i8] c"strtol\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [23 x i8] c"No digits were found.\0A\00"
define i32 @string_to_int(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 0, i64* @errno
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i64 @strtol(i8* %9, i8** %7, i32 %10)
store i64 %11, i64* %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @ERANGE
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i64, i64* %8
%17 = load i64, i64* @LONG_MAX
%18 = icmp eq i64 %16, %17
br i1 %18, label %29, label %19
19: ; preds = %15
%20 = load i64, i64* %8
%21 = load i64, i64* @LONG_MIN
%22 = icmp eq i64 %20, %21
br i1 %22, label %29, label %23
23: ; preds = %19, %2
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %19, %15
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
31: ; preds = %26, %23
%32 = load i8*, i8** %7
%33 = load i8*, i8** @optarg
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
38: ; preds = %31
%39 = load i64, i64* %8
%40 = trunc i64 %39 to i32
store i32 %40, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %38, %35, %29
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str = [7 x i8] c"strtol\00"
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [23 x i8] c"No digits were found.\0A\00"
define i32 @string_to_int(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%5 = call i64 @strtol(i8* %0, i8** %3, i32 %1)
%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 %21
19: ; preds = %15, %9
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = load i8*, i8** @optarg
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %30
28: ; preds = %21
%29 = trunc i64 %5 to i32
br label %30
30: ; preds = %28, %25, %19
%31 = phi i32 [ -1, %19 ], [ -1, %25 ], [ %29, %28 ]
ret i32 %31
}