#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*/ snprintf(char *, size_t, char *);
int /*<<< orphan*/ str_to_ll(char const *, int, long long *, char *, size_t);
void str_to_int(const char *str, int base, int *i, char *err, size_t err_len) {
  long long ll;
  str_to_ll(str, base, &ll, err, err_len);
  if (err[0])
    return;
  if (ll > INT_MAX) {
    snprintf(err, err_len, "overflow");
    return;
  }
  if (ll < INT_MIN) {
    snprintf(err, err_len, "underflow");
    return;
  }
  *i = (int)ll;
}


x86 assembly (clang, -O0):
<str_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x30(%rbp),%rdx
call   1040 <str_to_ll@plt>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1165 <str_to_int+0x45>
jmp    11cb <str_to_int+0xab>
mov    -0x30(%rbp),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jle    1192 <str_to_int+0x72>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xe78(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11cb <str_to_int+0xab>
mov    -0x30(%rbp),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jge    11bf <str_to_int+0x9f>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xe54(%rip),%rdx
call   1030 <snprintf@plt>
jmp    11cb <str_to_int+0xab>
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_int>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rdx,%r15
lea    0x8(%rsp),%rdx
call   1040 <str_to_ll@plt>
cmpb   $0x0,(%rbx)
jne    117e <str_to_int+0x5e>
mov    0x8(%rsp),%rax
mov    0x2e9b(%rip),%rcx
cmp    (%rcx),%rax
jle    115b <str_to_int+0x3b>
lea    0xea7(%rip),%rdx
jmp    116e <str_to_int+0x4e>
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%rax
jge    117b <str_to_int+0x5b>
lea    0xe9b(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
jmp    117e <str_to_int+0x5e>
mov    %eax,(%r15)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@INT_MAX = global i64 0
@.str = [9 x i8] c"overflow\00"
@INT_MIN = global i64 0
@.str.1 = [10 x i8] c"underflow\00"
define void @str_to_int(i8* %0, i32 %1, i32* nocapture writeonly %2, i8* %3, i64 %4) {
%6 = alloca i64
%7 = bitcast i64* %6 to i8*
%8 = call i32 @str_to_ll(i8* %0, i32 %1, i64* %6, i8* %3, i64 %4)
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %5
%12 = load i64, i64* %6
%13 = load i64, i64* @INT_MAX
%14 = icmp sgt i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %24
17: ; preds = %11
%18 = load i64, i64* @INT_MIN
%19 = icmp slt i64 %12, %18
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @snprintf(i8* %3, i64 %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %17
%23 = trunc i64 %12 to i32
store i32 %23, i32* %2
br label %24
24: ; preds = %5, %22, %20, %15
ret void
}