#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 */
long long INT_MAX;
long long INT_MIN;
int /*<<< orphan*/ LLONG_MAX;
int /*<<< orphan*/ LLONG_MIN;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
long long strtonum(char *, int /*<<< orphan*/, int /*<<< orphan*/,
                   char const **);
int stringtoint(char *string) {
  const char *err;
  long long ll;
  int answer;
  errno = 0;
  ll = strtonum(string, LLONG_MIN, LLONG_MAX, &err);
  if (err != NULL) {
    errno = -2;
    fprintf(stderr, "strtonum error: %s\n", err);
    return 0;
  }
  if (ll > INT_MAX) {
    errno = 1;
    fprintf(stderr, "stringtoint: number too big for int! (Number was %lld)\n",
            ll);
    return 0;
  } else if (ll < INT_MIN) {
    errno = -1;
    fprintf(stderr, "stringtoint: number too big for int! (Number was %lld)\n",
            ll);
    return 0;
  }
  answer = (int)ll;
  errno = 0;
  return answer;
}


x86 assembly (clang, -O0):
<stringtoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
call   1040 <strtonum@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119b <stringtoint+0x7b>
mov    0x2e4a(%rip),%rax
movl   $0xfffffffe,(%rax)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <stringtoint+0x12a>
mov    -0x20(%rbp),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jle    11e3 <stringtoint+0xc3>
mov    0x2e02(%rip),%rax
movl   $0x1,(%rax)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <stringtoint+0x12a>
mov    -0x20(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jge    122b <stringtoint+0x10b>
mov    0x2dba(%rip),%rax
movl   $0xffffffff,(%rax)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <stringtoint+0x12a>
jmp    1230 <stringtoint+0x110>
mov    -0x20(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    0x2d7a(%rip),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringtoint>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rbx
movl   $0x0,(%rbx)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
call   1040 <strtonum@plt>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     1172 <stringtoint+0x52>
movl   $0xfffffffe,(%rbx)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %ebx,%ebx
jmp    11ad <stringtoint+0x8d>
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
jle    1186 <stringtoint+0x66>
movl   $0x1,(%rbx)
jmp    1198 <stringtoint+0x78>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jge    11bc <stringtoint+0x9c>
movl   $0xffffffff,(%rbx)
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edi
lea    0xe6c(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
movl   $0x0,(%rbx)
mov    %eax,%ebx
jmp    11b4 <stringtoint+0x94>

LLVM IR (-O0):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@INT_MAX = global i64 0
@.str.1 = [56 x i8] c"stringtoint: number too big for int! (Number was %lld)\0A\00"
@INT_MIN = global i64 0
define i32 @stringtoint(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* @errno
%7 = load i8*, i8** %3
%8 = load i32, i32* @LLONG_MIN
%9 = load i32, i32* @LLONG_MAX
%10 = call i64 @strtonum(i8* %7, i32 %8, i32 %9, i8** %4)
store i64 %10, i64* %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
store i32 -2, i32* @errno
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 0, i32* %2
br label %38
17: ; preds = %1
%18 = load i64, i64* %5
%19 = load i64, i64* @INT_MAX
%20 = icmp sgt i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
store i32 1, i32* @errno
%22 = load i32, i32* @stderr
%23 = load i64, i64* %5
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %23)
store i32 0, i32* %2
br label %38
25: ; preds = %17
%26 = load i64, i64* %5
%27 = load i64, i64* @INT_MIN
%28 = icmp slt i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %25
store i32 -1, i32* @errno
%30 = load i32, i32* @stderr
%31 = load i64, i64* %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %31)
store i32 0, i32* %2
br label %38
33: ; preds = %25
br label %34
34: ; preds = %33
%35 = load i64, i64* %5
%36 = trunc i64 %35 to i32
store i32 %36, i32* %6
store i32 0, i32* @errno
%37 = load i32, i32* %6
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %29, %21, %13
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@errno = global i32 0
@LLONG_MIN = global i32 0
@LLONG_MAX = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"strtonum error: %s\0A\00"
@INT_MAX = global i64 0
@.str.1 = [56 x i8] c"stringtoint: number too big for int! (Number was %lld)\0A\00"
@INT_MIN = global i64 0
define i32 @stringtoint(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i32 0, i32* @errno
%4 = load i32, i32* @LLONG_MIN
%5 = load i32, i32* @LLONG_MAX
%6 = call i64 @strtonum(i8* %0, i32 %4, i32 %5, i8** %2)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
store i32 -2, i32* @errno
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %26
12: ; preds = %1
%13 = load i64, i64* @INT_MAX
%14 = icmp sgt i64 %6, %13
br i1 %14, label %15, label %18
15: ; preds = %12
store i32 1, i32* @errno
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
18: ; preds = %12
%19 = load i64, i64* @INT_MIN
%20 = icmp slt i64 %6, %19
br i1 %20, label %21, label %24
21: ; preds = %18
store i32 -1, i32* @errno
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i64 %6)
br label %26
24: ; preds = %18
%25 = trunc i64 %6 to i32
store i32 0, i32* @errno
br label %26
26: ; preds = %24, %21, %15, %9
%27 = phi i32 [ 0, %9 ], [ 0, %15 ], [ 0, %21 ], [ %25, %24 ]
ret i32 %27
}