#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;
int LONG_MAX;
int LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ error_exit(int /*<<< orphan*/, char *);
int strtol(char *, char **, int);
int string_to_int(char *input) {
  errno = 0;
  int value = -1;
  char *endptr;
  value = strtol(input, &endptr, 10);
  if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
      (errno != 0 && value == 0)) {
    error_exit(EXIT_FAILURE,
               "Error while trying to convert number with strtol");
  }
  if (endptr == input) {
    error_exit(EXIT_FAILURE, "No digits were found");
  }
  return value;
}


x86 assembly (clang, -O0):
<string_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <string_to_int+0x74>
mov    -0xc(%rbp),%eax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
je     11af <string_to_int+0x8f>
mov    -0xc(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
je     11af <string_to_int+0x8f>
mov    0x2e2d(%rip),%rax
cmpq   $0x0,(%rax)
je     11c4 <string_to_int+0xa4>
cmpl   $0x0,-0xc(%rbp)
jne    11c4 <string_to_int+0xa4>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1030 <error_exit@plt>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11e7 <string_to_int+0xc7>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1030 <error_exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_int>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e95(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,%ebx
mov    (%r15),%rax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1170 <string_to_int+0x50>
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%ebx
je     1179 <string_to_int+0x59>
mov    0x2e7c(%rip),%rcx
cmp    (%rcx),%ebx
je     1179 <string_to_int+0x59>
test   %rax,%rax
je     118e <string_to_int+0x6e>
test   %ebx,%ebx
jne    118e <string_to_int+0x6e>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1030 <error_exit@plt>
cmp    %r14,0x8(%rsp)
jne    11aa <string_to_int+0x8a>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1030 <error_exit@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i32 0
@LONG_MIN = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [49 x i8] c"Error while trying to convert number with strtol\00"
@.str.1 = [21 x i8] c"No digits were found\00"
define i32 @string_to_int(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
store i32 -1, i32* %3
%5 = load i8*, i8** %2
%6 = call i32 @strtol(i8* %5, i8** %4, i32 10)
store i32 %6, i32* %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i32, i32* @LONG_MAX
%13 = icmp eq i32 %11, %12
br i1 %13, label %24, label %14
14: ; preds = %10
%15 = load i32, i32* %3
%16 = load i32, i32* @LONG_MIN
%17 = icmp eq i32 %15, %16
br i1 %17, label %24, label %18
18: ; preds = %14, %1
%19 = load i64, i64* @errno
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21, %14, %10
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @error_exit(i32 %25, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %24, %21, %18
%28 = load i8*, i8** %4
%29 = load i8*, i8** %2
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @error_exit(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %27
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i32 0
@LONG_MIN = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [49 x i8] c"Error while trying to convert number with strtol\00"
@.str.1 = [21 x i8] c"No digits were found\00"
define i32 @string_to_int(i8* %0) {
%2 = alloca i8*
store i64 0, i64* @errno
%3 = bitcast i8** %2 to i8*
%4 = call i32 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @LONG_MAX
%10 = icmp eq i32 %4, %9
%11 = load i32, i32* @LONG_MIN
%12 = icmp eq i32 %4, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %18, label %14
14: ; preds = %8, %1
%15 = icmp ne i64 %5, 0
%16 = icmp eq i32 %4, 0
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %21
18: ; preds = %14, %8
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @error_exit(i32 %19, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %18, %14
%22 = load i8*, i8** %2
%23 = icmp eq i8* %22, %0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @error_exit(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %21
ret i32 %4
}