#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;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
long strtol(char const *, char **, int /*<<< orphan*/);
int parse_int(const char *str) {
  char *endptr;
  long ret = strtol(str, &endptr, 0);
  if (*endptr != '\0') {
    fprintf(stderr, "[error] strtol() failed: parse error: %s\n", endptr);
    exit(1);
  }
  if (errno == ERANGE)
    fprintf(stderr, "[warning] strtol() out of range\n");
  if (ret > INT_MAX || ret < INT_MIN) {
    fprintf(stderr, "[warning] parse_int() out of range\n");
    ret = (ret > 0) ? INT_MAX : INT_MIN;
  }
  return (int)ret;
}


x86 assembly (clang, -O0):
<parse_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1184 <parse_int+0x54>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe8d(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <parse_int+0x85>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
jg     11dd <parse_int+0xad>
mov    -0x18(%rbp),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jge    1228 <parse_int+0xf8>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1212 <parse_int+0xe2>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1220 <parse_int+0xf0>
mov    0x2dc7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_int>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    0x8(%rsp),%rdx
cmpb   $0x0,(%rdx)
jne    11c5 <parse_int+0x95>
mov    %rax,%rbx
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
jne    117f <parse_int+0x4f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xeb2(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2e62(%rip),%r14
mov    0x2e53(%rip),%r15
cmp    (%r14),%rbx
jg     1197 <parse_int+0x67>
cmp    (%r15),%rbx
jge    11b9 <parse_int+0x89>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xea4(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %rbx,%rbx
jle    11b6 <parse_int+0x86>
mov    %r14,%r15
mov    (%r15),%rbx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"[error] strtol() failed: parse error: %s\0A\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.1 = [33 x i8] c"[warning] strtol() out of range\0A\00"
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str.2 = [36 x i8] c"[warning] parse_int() out of range\0A\00"
define i32 @parse_int(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** %3, i32 0)
store i64 %6, i64* %4
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i64, i64* @errno
%18 = load i64, i64* @ERANGE
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %20, %16
%24 = load i64, i64* %4
%25 = load i64, i64* @INT_MAX
%26 = icmp sgt i64 %24, %25
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i64, i64* %4
%29 = load i64, i64* @INT_MIN
%30 = icmp slt i64 %28, %29
br i1 %30, label %31, label %42
31: ; preds = %27, %23
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%34 = load i64, i64* %4
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = load i64, i64* @INT_MAX
br label %40
38: ; preds = %31
%39 = load i64, i64* @INT_MIN
br label %40
40: ; preds = %38, %36
%41 = phi i64 [ %37, %36 ], [ %39, %38 ]
store i64 %41, i64* %4
br label %42
42: ; preds = %40, %27
%43 = load i64, i64* %4
%44 = trunc i64 %43 to i32
ret i32 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"[error] strtol() failed: parse error: %s\0A\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.1 = [33 x i8] c"[warning] strtol() out of range\0A\00"
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str.2 = [36 x i8] c"[warning] parse_int() out of range\0A\00"
define i32 @parse_int(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtol(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %5)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %12
%20 = load i64, i64* @INT_MAX
%21 = icmp sgt i64 %4, %20
%22 = load i64, i64* @INT_MIN
%23 = icmp slt i64 %4, %22
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %25, label %32
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp sgt i64 %4, 0
%29 = load i64, i64* @INT_MAX
%30 = load i64, i64* @INT_MIN
%31 = select i1 %28, i64 %29, i64 %30
br label %32
32: ; preds = %19, %25
%33 = phi i64 [ %31, %25 ], [ %4, %19 ]
%34 = trunc i64 %33 to i32
ret i32 %34
}