#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 const *, char **, int);
int asciitoint(const char *s, int *val, int base) {
  char *end;
  long longval;
  errno = 0;
  longval = strtol(s, &end, base);
  if (errno || *end != '\0')
    return (-1);
  if (longval > INT_MAX || longval < INT_MIN) {
    errno = ERANGE;
    return (-1);
  }
  *val = longval;
  return (0);
}


x86 assembly (clang, -O0):
<asciitoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e9e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1030 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e7c(%rip),%rax
cmpq   $0x0,(%rax)
jne    1166 <asciitoint+0x56>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1172 <asciitoint+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <asciitoint+0xbd>
mov    -0x30(%rbp),%rax
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%rax
jg     119a <asciitoint+0x8a>
mov    -0x30(%rbp),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jge    11ba <asciitoint+0xaa>
mov    0x2e37(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1d(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <asciitoint+0xbd>
mov    -0x30(%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):
<asciitoint>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2eaa(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rsi
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpq   $0x0,(%rbx)
jne    1170 <asciitoint+0x60>
mov    (%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    1170 <asciitoint+0x60>
mov    0x2e9d(%rip),%rdx
cmp    (%rdx),%rcx
jg     1163 <asciitoint+0x53>
mov    0x2e89(%rip),%rdx
cmp    (%rdx),%rcx
jl     1163 <asciitoint+0x53>
mov    %ecx,(%r14)
xor    %eax,%eax
jmp    1170 <asciitoint+0x60>
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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