#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*/ ERANGE;
long long INT_MAX;
long long INT_MIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ panic_err(char *, int, char const *, int);
long long strtoll(char const *, char **, int);
int safe_atoi(const char *s, size_t size, int base) {
  char *endptr;
  long long int val = strtoll(s, &endptr, base);
  if (val < INT_MIN || val > INT_MAX || endptr < s + size) {
    errno = ERANGE;
    panic_err("could not convert %.*s to integer, result = %d", (int)size, s,
              (int)val);
  }
  return (int)val;
}


x86 assembly (clang, -O0):
<safe_atoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1030 <strtoll@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
jl     1184 <safe_atoi+0x64>
mov    -0x28(%rbp),%rax
mov    0x2e82(%rip),%rcx
cmp    (%rcx),%rax
jg     1184 <safe_atoi+0x64>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11b2 <safe_atoi+0x92>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
lea    0xe53(%rip),%rdi
call   1040 <panic_err@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_atoi>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1030 <strtoll@plt>
mov    %rax,%rbx
mov    0x2e9d(%rip),%rax
cmp    (%rax),%rbx
jl     115f <safe_atoi+0x3f>
mov    0x2e99(%rip),%rax
cmp    (%rax),%rbx
jg     115f <safe_atoi+0x3f>
lea    (%r14,%r15,1),%rax
cmp    %rax,0x8(%rsp)
jae    1185 <safe_atoi+0x65>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    0x2e59(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe88(%rip),%rdi
mov    %r15d,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1040 <panic_err@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
@.str = [47 x i8] c"could not convert %.*s to integer, result = %d\00"
define i32 @safe_atoi(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* %6
%11 = call i64 @strtoll(i8* %9, i8** %7, i32 %10)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = load i64, i64* @INT_MIN
%14 = icmp slt i64 %12, %13
br i1 %14, label %25, label %15
15: ; preds = %3
%16 = load i64, i64* %8
%17 = load i64, i64* @INT_MAX
%18 = icmp sgt i64 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = load i8*, i8** %7
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = icmp ult i8* %20, %23
br i1 %24, label %25, label %33
25: ; preds = %19, %15, %3
%26 = load i32, i32* @ERANGE
store i32 %26, i32* @errno
%27 = load i64, i64* %5
%28 = trunc i64 %27 to i32
%29 = load i8*, i8** %4
%30 = load i64, i64* %8
%31 = trunc i64 %30 to i32
%32 = call i32 @panic_err(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %28, i8* %29, i32 %31)
br label %33
33: ; preds = %25, %19
%34 = load i64, i64* %8
%35 = trunc i64 %34 to i32
ret i32 %35
}

LLVM IR (-O2):
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
@.str = [47 x i8] c"could not convert %.*s to integer, result = %d\00"
define i32 @safe_atoi(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i64 @strtoll(i8* %0, i8** %4, i32 %2)
%7 = load i64, i64* @INT_MIN
%8 = icmp slt i64 %6, %7
%9 = load i64, i64* @INT_MAX
%10 = icmp sgt i64 %6, %9
%11 = select i1 %8, i1 true, i1 %10
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %0, i64 %1
%14 = icmp ult i8* %12, %13
%15 = select i1 %11, i1 true, i1 %14
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = trunc i64 %6 to i32
br label %23
18: ; preds = %3
%19 = load i32, i32* @ERANGE
store i32 %19, i32* @errno
%20 = trunc i64 %1 to i32
%21 = trunc i64 %6 to i32
%22 = tail call i32 @panic_err(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %0, i32 %21)
br label %23
23: ; preds = %16, %18
%24 = phi i32 [ %17, %16 ], [ %21, %18 ]
ret i32 %24
}