#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;
int /*<<< orphan*/ err(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ str(char *, char *);
long strtol(char *, char **, int /*<<< orphan*/);
int try_str_to_int(char *arg) {
  char *endptr;
  errno = 0;
  long result = strtol(arg, &endptr, 0);
  if (errno == ERANGE || result > INT_MAX || result < INT_MIN) {
    err(str("'%s' is out of range", arg));
  }
  if (*endptr != '\0') {
    err(str("cannot parse '%s' as an integer", arg));
  }
  return (int)result;
}


x86 assembly (clang, -O0):
<try_str_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
je     119f <try_str_to_int+0x6f>
mov    -0x18(%rbp),%rax
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
jg     119f <try_str_to_int+0x6f>
mov    -0x18(%rbp),%rax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
jge    11b6 <try_str_to_int+0x86>
mov    -0x8(%rbp),%rsi
lea    0xe56(%rip),%rdi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11dd <try_str_to_int+0xad>
mov    -0x8(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_str_to_int>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %rax,%rbx
mov    (%r15),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <try_str_to_int+0x50>
mov    0x2e79(%rip),%rax
cmp    (%rax),%rbx
jg     1180 <try_str_to_int+0x50>
mov    0x2e65(%rip),%rax
cmp    (%rax),%rbx
jge    1196 <try_str_to_int+0x66>
lea    0xe79(%rip),%rdi
mov    %r14,%rsi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11b6 <try_str_to_int+0x86>
lea    0xe6e(%rip),%rdi
mov    %r14,%rsi
call   1050 <str@plt>
mov    %eax,%edi
call   1040 <err@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str = [21 x i8] c"'%s' is out of range\00"
@.str.1 = [32 x i8] c"cannot parse '%s' as an integer\00"
define i32 @try_str_to_int(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** %3, i32 0)
store i64 %6, i64* %4
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
br i1 %9, label %18, label %10
10: ; preds = %1
%11 = load i64, i64* %4
%12 = load i64, i64* @INT_MAX
%13 = icmp sgt i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i64, i64* %4
%16 = load i64, i64* @INT_MIN
%17 = icmp slt i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14, %10, %1
%19 = load i8*, i8** %2
%20 = call i32 @str(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @err(i32 %20)
br label %22
22: ; preds = %18, %14
%23 = load i8*, i8** %3
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i8*, i8** %2
%29 = call i32 @str(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @err(i32 %29)
br label %31
31: ; preds = %27, %22
%32 = load i64, i64* %4
%33 = trunc i64 %32 to i32
ret i32 %33
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
@.str = [21 x i8] c"'%s' is out of range\00"
@.str.1 = [32 x i8] c"cannot parse '%s' as an integer\00"
define i32 @try_str_to_int(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 0)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
%8 = load i64, i64* @INT_MAX
%9 = icmp sgt i64 %4, %8
%10 = select i1 %7, i1 true, i1 %9
%11 = load i64, i64* @INT_MIN
%12 = icmp slt i64 %4, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = tail call i32 @str(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = tail call i32 @err(i32 %15)
br label %17
17: ; preds = %1, %14
%18 = load i8*, i8** %2
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = tail call i32 @str(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = tail call i32 @err(i32 %22)
br label %24
24: ; preds = %21, %17
%25 = trunc i64 %4 to i32
ret i32 %25
}