#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 */
int /*<<< orphan*/ EOVERFLOW;
long INT_MAX;
long INT_MIN;
int /*<<< orphan*/ errno;
long strtol(char const *, char **, int);
int lstrtoi(const char *ptr, char **endptr, int base) {
  long ret = strtol(ptr, endptr, base);
  if (ret > INT_MAX || ret < INT_MIN) {
    errno = EOVERFLOW;
    *endptr = (char *)ptr;
    return 0;
  }
  return (int)ret;
}


x86 assembly (clang, -O0):
<lstrtoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%rax
jg     115f <lstrtoi+0x4f>
mov    -0x28(%rbp),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jge    1188 <lstrtoi+0x78>
mov    0x2e8a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e59(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    118f <lstrtoi+0x7f>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lstrtoi>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strtol@plt>
mov    0x2eba(%rip),%rcx
cmp    (%rcx),%rax
jg     1137 <lstrtoi+0x27>
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%rax
jge    114e <lstrtoi+0x3e>
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    0x2e81(%rip),%rcx
mov    %eax,(%rcx)
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i32 @lstrtoi(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i8**, i8*** %6
%11 = load i32, i32* %7
%12 = call i64 @strtol(i8* %9, i8** %10, i32 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* @INT_MAX
%15 = icmp sgt i64 %13, %14
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i64, i64* %8
%18 = load i64, i64* @INT_MIN
%19 = icmp slt i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16, %3
%21 = load i32, i32* @EOVERFLOW
store i32 %21, i32* @errno
%22 = load i8*, i8** %5
%23 = load i8**, i8*** %6
store i8* %22, i8** %23
store i32 0, i32* %4
br label %27
24: ; preds = %16
%25 = load i64, i64* %8
%26 = trunc i64 %25 to i32
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i32 @lstrtoi(i8* %0, i8** nocapture %1, i32 %2) {
%4 = tail call i64 @strtol(i8* %0, i8** %1, i32 %2)
%5 = load i64, i64* @INT_MAX
%6 = icmp sgt i64 %4, %5
%7 = load i64, i64* @INT_MIN
%8 = icmp slt i64 %4, %7
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = load i32, i32* @EOVERFLOW
store i32 %11, i32* @errno
store i8* %0, i8** %1
br label %14
12: ; preds = %3
%13 = trunc i64 %4 to i32
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ 0, %10 ], [ %13, %12 ]
ret i32 %15
}