#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 */
typedef scalar_t__ int64_t;
typedef scalar_t__ int32_t;

/* Variables and functions */
scalar_t__ INT_MAX;
scalar_t__ INT_MIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ strlen(char *);
scalar_t__ strtol(char *, char **, int);
bool parseInteger(char *str, int32_t *result) {
  char *endToken = NULL;
  int64_t longResult = strtol(str, &endToken, 10);
  if (errno != 0) {
    fprintf(stderr, "Invalid integer: %s (%s)\n", str, strerror(errno));
    return false;
  }
  if ((endToken != NULL && strlen(endToken) > 0) || longResult >= INT_MAX ||
      longResult <= INT_MIN) {
    fprintf(stderr, "Invalid integer: %s\n", str);
    return false;
  }
  *result = (int32_t)longResult;
  return true;
}


x86 assembly (clang, -O0):
<parseInteger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e53(%rip),%rax
cmpq   $0x0,(%rax)
je     11c6 <parseInteger+0x86>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <parseInteger+0x105>
cmpq   $0x0,-0x20(%rbp)
je     11e4 <parseInteger+0xa4>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jg     120c <parseInteger+0xcc>
mov    -0x28(%rbp),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jge    120c <parseInteger+0xcc>
mov    -0x28(%rbp),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
jg     1233 <parseInteger+0xf3>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <parseInteger+0x105>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parseInteger>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%r15
mov    0x2e54(%rip),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     11a4 <parseInteger+0x64>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe6f(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11f6 <parseInteger+0xb6>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11b8 <parseInteger+0x78>
call   1030 <strlen@plt>
test   %rax,%rax
jg     11da <parseInteger+0x9a>
mov    0x2e29(%rip),%rax
cmp    (%rax),%r15
jge    11da <parseInteger+0x9a>
mov    0x2e15(%rip),%rax
cmp    (%rax),%r15
jle    11da <parseInteger+0x9a>
mov    %r15,(%r14)
mov    $0x1,%ebp
jmp    11f6 <parseInteger+0xb6>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
xor    %ebp,%ebp
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@stderr = global i32 0
@.str = [26 x i8] c"Invalid integer: %s (%s)\0A\00"
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str.1 = [21 x i8] c"Invalid integer: %s\0A\00"
define i32 @parseInteger(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %6
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 10)
store i64 %9, i64* %7
%10 = load i64, i64* @errno
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = load i64, i64* @errno
%16 = call i8* @strerror(i64 %15)
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %16)
store i32 0, i32* %3
br label %40
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %21, %18
%26 = load i64, i64* %7
%27 = load i64, i64* @INT_MAX
%28 = icmp sge i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* %7
%31 = load i64, i64* @INT_MIN
%32 = icmp sle i64 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29, %25, %21
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %4
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 0, i32* %3
br label %40
37: ; preds = %29
%38 = load i64, i64* %7
%39 = load i64*, i64** %5
store i64 %38, i64* %39
store i32 1, i32* %3
br label %40
40: ; preds = %37, %33, %12
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@errno = global i64 0
@stderr = global i32 0
@.str = [26 x i8] c"Invalid integer: %s (%s)\0A\00"
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str.1 = [21 x i8] c"Invalid integer: %s\0A\00"
define i32 @parseInteger(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%6 = load i64, i64* @errno
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i8* @strerror(i64 %6)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %31
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = tail call i64 @strlen(i8* %13)
%17 = icmp slt i64 %16, 1
%18 = load i64, i64* @INT_MAX
%19 = icmp slt i64 %5, %18
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %24, label %27
21: ; preds = %12
%22 = load i64, i64* @INT_MAX
%23 = icmp slt i64 %5, %22
br i1 %23, label %24, label %27
24: ; preds = %15, %21
%25 = load i64, i64* @INT_MIN
%26 = icmp sgt i64 %5, %25
br i1 %26, label %30, label %27
27: ; preds = %24, %21, %15
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
30: ; preds = %24
store i64 %5, i64* %1
br label %31
31: ; preds = %30, %27, %8
%32 = phi i32 [ 0, %8 ], [ 0, %27 ], [ 1, %30 ]
ret i32 %32
}