#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 LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
long strtol(char const *, char **, int);
void read_int_arg(const char *arg, int *result, const char **error) {
  char *endptr;
  errno = 0;
  long l = strtol(arg, &endptr, 10);
  if (*arg == '\0' || *endptr != '\0') {
    *error = "is not a number";
    return;
  }
  if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
    *error = "too high";
    return;
  }
  if ((errno == ERANGE && l == LONG_MIN) || l <= 0) {
    *error = "too low";
    return;
  }
  *result = l;
}


x86 assembly (clang, -O0):
<read_int_arg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1168 <read_int_arg+0x58>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     117b <read_int_arg+0x6b>
mov    -0x18(%rbp),%rax
lea    0xe8d(%rip),%rcx
mov    %rcx,(%rax)
jmp    1228 <read_int_arg+0x118>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    11a9 <read_int_arg+0x99>
mov    -0x28(%rbp),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
je     11bd <read_int_arg+0xad>
mov    -0x28(%rbp),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jle    11d0 <read_int_arg+0xc0>
mov    -0x18(%rbp),%rax
lea    0xe48(%rip),%rcx
mov    %rcx,(%rax)
jmp    1228 <read_int_arg+0x118>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <read_int_arg+0xee>
mov    -0x28(%rbp),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     1209 <read_int_arg+0xf9>
cmpq   $0x0,-0x28(%rbp)
jg     121c <read_int_arg+0x10c>
mov    -0x18(%rbp),%rax
lea    0xe05(%rip),%rcx
mov    %rcx,(%rax)
jmp    1228 <read_int_arg+0x118>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int_arg>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e98(%rip),%r12
movq   $0x0,(%r12)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
cmpb   $0x0,(%rbx)
je     114b <read_int_arg+0x3b>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1161 <read_int_arg+0x51>
lea    0xeae(%rip),%rax
mov    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r12),%rcx
mov    0x2e6c(%rip),%rdx
mov    (%rdx),%rdx
mov    %rcx,%rsi
xor    %rdx,%rsi
mov    0x2e4c(%rip),%rdi
mov    (%rdi),%rdi
xor    %rax,%rdi
or     %rsi,%rdi
je     11ae <read_int_arg+0x9e>
mov    0x2e5a(%rip),%rsi
cmp    (%rsi),%rax
jg     11ae <read_int_arg+0x9e>
cmp    %rdx,%rcx
jne    11b7 <read_int_arg+0xa7>
test   %rax,%rax
jle    11bc <read_int_arg+0xac>
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <read_int_arg+0xac>
mov    %eax,(%r14)
jmp    1155 <read_int_arg+0x45>
lea    0xe5b(%rip),%rax
jmp    1152 <read_int_arg+0x42>
test   %rax,%rax
jg     11a9 <read_int_arg+0x99>
lea    0xe56(%rip),%rax
jmp    1152 <read_int_arg+0x42>

LLVM IR (-O0):
@errno = global i64 0
@.str = [16 x i8] c"is not a number\00"
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@INT_MAX = global i64 0
@.str.1 = [9 x i8] c"too high\00"
@LONG_MIN = global i64 0
@.str.2 = [8 x i8] c"too low\00"
define void @read_int_arg(i8* %0, i32* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
store i64 0, i64* @errno
%9 = load i8*, i8** %4
%10 = call i64 @strtol(i8* %9, i8** %7, i32 10)
store i64 %10, i64* %8
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15, %3
%21 = load i8**, i8*** %6
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %21
br label %53
22: ; preds = %15
%23 = load i64, i64* @errno
%24 = load i64, i64* @ERANGE
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i64, i64* %8
%28 = load i64, i64* @LONG_MAX
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26, %22
%31 = load i64, i64* %8
%32 = load i64, i64* @INT_MAX
%33 = icmp sgt i64 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30, %26
%35 = load i8**, i8*** %6
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %35
br label %53
36: ; preds = %30
%37 = load i64, i64* @errno
%38 = load i64, i64* @ERANGE
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i64, i64* %8
%42 = load i64, i64* @LONG_MIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %47, label %44
44: ; preds = %40, %36
%45 = load i64, i64* %8
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44, %40
%48 = load i8**, i8*** %6
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %48
br label %53
49: ; preds = %44
%50 = load i64, i64* %8
%51 = trunc i64 %50 to i32
%52 = load i32*, i32** %5
store i32 %51, i32* %52
br label %53
53: ; preds = %49, %47, %34, %20
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [16 x i8] c"is not a number\00"
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@INT_MAX = global i64 0
@.str.1 = [9 x i8] c"too high\00"
@LONG_MIN = global i64 0
@.str.2 = [8 x i8] c"too low\00"
define void @read_int_arg(i8* %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i64 0, i64* @errno
%6 = call i64 @strtol(i8* %0, i8** %4, i32 10)
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %9, %3
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
br label %36
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @ERANGE
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @LONG_MAX
%19 = icmp eq i64 %6, %18
%20 = select i1 %17, i1 %19, i1 false
%21 = load i64, i64* @INT_MAX
%22 = icmp sgt i64 %6, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %25
24: ; preds = %14
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %36
25: ; preds = %14
br i1 %17, label %26, label %31
26: ; preds = %25
%27 = load i64, i64* @LONG_MIN
%28 = icmp eq i64 %6, %27
%29 = icmp slt i64 %6, 1
%30 = or i1 %29, %28
br i1 %30, label %33, label %34
31: ; preds = %25
%32 = icmp slt i64 %6, 1
br i1 %32, label %33, label %34
33: ; preds = %31, %26
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %36
34: ; preds = %26, %31
%35 = trunc i64 %6 to i32
store i32 %35, i32* %1
br label %36
36: ; preds = %34, %33, %24, %13
ret void
}