#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 scalar_t__ int64_t;

/* Variables and functions */
scalar_t__ INT_MAX;
scalar_t__ INT_MIN;
int /*<<< orphan*/ snprintf(char *, size_t, char *);
scalar_t__ str_to_s64(char const *, char *, size_t);
int str_to_int(const char *str, char *err, size_t err_len) {
  int64_t ret;
  ret = str_to_s64(str, err, err_len);
  if (err[0])
    return 0;
  if (ret < INT_MIN) {
    snprintf(err, err_len,
             "number is too negative to fit in "
             "signed int");
    return 0;
  }
  if (ret > INT_MAX) {
    snprintf(err, err_len,
             "number is too large to fit in "
             "signed int");
    return 0;
  }
  return (int)ret;
}


x86 assembly (clang, -O0):
<str_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <str_to_s64@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     1162 <str_to_int+0x42>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <str_to_int+0xb1>
mov    -0x28(%rbp),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
jge    1196 <str_to_int+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xe7b(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <str_to_int+0xb1>
mov    -0x28(%rbp),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
jle    11ca <str_to_int+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    0xe73(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <str_to_int+0xb1>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_int>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%rbp
call   1030 <str_to_s64@plt>
xor    %r14d,%r14d
cmpb   $0x0,0x0(%rbp)
jne    1170 <str_to_int+0x50>
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%rax
jge    114d <str_to_int+0x2d>
lea    0xeb5(%rip),%rdx
jmp    1160 <str_to_int+0x40>
mov    0x2e94(%rip),%rcx
cmp    (%rcx),%rax
jle    116d <str_to_int+0x4d>
lea    0xecc(%rip),%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <snprintf@plt>
jmp    1170 <str_to_int+0x50>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@INT_MIN = global i64 0
@.str = [44 x i8] c"number is too negative to fit in signed int\00"
@INT_MAX = global i64 0
@.str.1 = [41 x i8] c"number is too large to fit in signed int\00"
define i32 @str_to_int(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = call i64 @str_to_s64(i8* %9, i8* %10, i64 %11)
store i64 %12, i64* %8
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 0, i32* %4
br label %37
18: ; preds = %3
%19 = load i64, i64* %8
%20 = load i64, i64* @INT_MIN
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = call i32 @snprintf(i8* %23, i64 %24, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %37
26: ; preds = %18
%27 = load i64, i64* %8
%28 = load i64, i64* @INT_MAX
%29 = icmp sgt i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = call i32 @snprintf(i8* %31, i64 %32, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %37
34: ; preds = %26
%35 = load i64, i64* %8
%36 = trunc i64 %35 to i32
store i32 %36, i32* %4
br label %37
37: ; preds = %34, %30, %22, %17
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@INT_MIN = global i64 0
@.str = [44 x i8] c"number is too negative to fit in signed int\00"
@INT_MAX = global i64 0
@.str.1 = [41 x i8] c"number is too large to fit in signed int\00"
define i32 @str_to_int(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @str_to_s64(i8* %0, i8* %1, i64 %2)
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %3
%8 = load i64, i64* @INT_MIN
%9 = icmp slt i64 %4, %8
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %19
12: ; preds = %7
%13 = load i64, i64* @INT_MAX
%14 = icmp sgt i64 %4, %13
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %19
17: ; preds = %12
%18 = trunc i64 %4 to i32
br label %19
19: ; preds = %3, %17, %15, %10
%20 = phi i32 [ 0, %10 ], [ 0, %15 ], [ %18, %17 ], [ 0, %3 ]
ret i32 %20
}