#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;
long INT_MAX;
long INT_MIN;
scalar_t__ errno;
long strtol(char *, char **, int);
int str2int(char *str, int *i) {
  long l;
  char *pEnd;
  if (str == NULL) {
    return -1;
  }
  errno = 0;
  l = strtol(str, &pEnd, 10);
  if (pEnd == str || *pEnd != '\0' || errno == ERANGE) {
    return -1;
  }
  if (l > INT_MAX || l < INT_MIN) {
    errno = ERANGE;
    return -1;
  }
  *i = (int)l;
  return 0;
}


x86 assembly (clang, -O0):
<str2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1137 <str2int+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <str2int+0xea>
mov    0x2e8a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1193 <str2int+0x83>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1193 <str2int+0x83>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <str2int+0x8f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <str2int+0xea>
mov    -0x20(%rbp),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jg     11c7 <str2int+0xb7>
mov    -0x20(%rbp),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jge    11e7 <str2int+0xd7>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rcx
mov    0x2df0(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <str2int+0xea>
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1181 <str2int+0x71>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e9a(%rip),%r15
movq   $0x0,(%r15)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%rsp),%rcx
cmp    %rbx,%rcx
je     1181 <str2int+0x71>
cmpb   $0x0,(%rcx)
jne    1181 <str2int+0x71>
mov    0x2e81(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,(%r15)
je     1181 <str2int+0x71>
mov    0x2e82(%rip),%rdx
cmp    (%rdx),%rax
jg     117e <str2int+0x6e>
mov    0x2e6e(%rip),%rdx
cmp    (%rdx),%rax
jl     117e <str2int+0x6e>
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    1181 <str2int+0x71>
mov    %rcx,(%r15)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @str2int(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %41
11: ; preds = %2
store i64 0, i64* @errno
%12 = load i8*, i8** %4
%13 = call i64 @strtol(i8* %12, i8** %7, i32 10)
store i64 %13, i64* %6
%14 = load i8*, i8** %7
%15 = load i8*, i8** %4
%16 = icmp eq i8* %14, %15
br i1 %16, label %26, label %17
17: ; preds = %11
%18 = load i8*, i8** %7
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @ERANGE
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22, %17, %11
store i32 -1, i32* %3
br label %41
27: ; preds = %22
%28 = load i64, i64* %6
%29 = load i64, i64* @INT_MAX
%30 = icmp sgt i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i64, i64* %6
%33 = load i64, i64* @INT_MIN
%34 = icmp slt i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31, %27
%36 = load i64, i64* @ERANGE
store i64 %36, i64* @errno
store i32 -1, i32* %3
br label %41
37: ; preds = %31
%38 = load i64, i64* %6
%39 = trunc i64 %38 to i32
%40 = load i32*, i32** %5
store i32 %39, i32* %40
store i32 0, i32* %3
br label %41
41: ; preds = %37, %35, %26, %10
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @str2int(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %26, label %6
6: ; preds = %2
store i64 0, i64* @errno
%7 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, %0
br i1 %9, label %26, label %10
10: ; preds = %6
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @ERANGE
%16 = icmp eq i64 %14, %15
br i1 %16, label %26, label %17
17: ; preds = %13
%18 = load i64, i64* @INT_MAX
%19 = icmp sgt i64 %7, %18
%20 = load i64, i64* @INT_MIN
%21 = icmp slt i64 %7, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %24
23: ; preds = %17
store i64 %15, i64* @errno
br label %26
24: ; preds = %17
%25 = trunc i64 %7 to i32
store i32 %25, i32* %1
br label %26
26: ; preds = %6, %10, %13, %2, %24, %23
%27 = phi i32 [ -1, %23 ], [ 0, %24 ], [ -1, %2 ], [ -1, %13 ], [ -1, %10 ], [ -1, %6 ]
ret i32 %27
}