#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;
int /*<<< orphan*/ EXIT_FAILURE;
long INT_MAX;
long INT_MIN;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ stderr;
long strtol(int /*<<< orphan*/, char **, int /*<<< orphan*/);
int parse_intarg(const char msg[]) {
  char *end;
  long temp = strtol(optarg, &end, 0);
  if ((errno == ERANGE && (temp == LONG_MAX || temp == LONG_MIN)) ||
      (errno != 0 && temp == 0)) {
    perror(msg);
    exit(EXIT_FAILURE);
  }
  if (temp > INT_MAX || temp < INT_MIN) {
    fprintf(stderr, "range error: %ld\n", temp);
    exit(EXIT_FAILURE);
  }
  return (int)temp;
}


x86 assembly (clang, -O0):
<parse_intarg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11a6 <parse_intarg+0x66>
mov    -0x18(%rbp),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11c2 <parse_intarg+0x82>
mov    -0x18(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
je     11c2 <parse_intarg+0x82>
mov    0x2dfb(%rip),%rax
cmpq   $0x0,(%rax)
je     11d9 <parse_intarg+0x99>
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <parse_intarg+0x99>
mov    -0x8(%rbp),%rdi
call   1050 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
jg     1201 <parse_intarg+0xc1>
mov    -0x18(%rbp),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
jge    1228 <parse_intarg+0xe8>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdeb(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_intarg>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    0x2e44(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e5a(%rip),%rdx
cmp    (%rdx),%rcx
jne    118b <parse_intarg+0x4b>
mov    0x2e36(%rip),%rdx
cmp    (%rdx),%rax
je     11b3 <parse_intarg+0x73>
mov    0x2e52(%rip),%rdx
cmp    (%rdx),%rax
je     11b3 <parse_intarg+0x73>
test   %rcx,%rcx
je     1195 <parse_intarg+0x55>
test   %rax,%rax
je     11b3 <parse_intarg+0x73>
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jg     11bd <parse_intarg+0x7d>
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
jl     11bd <parse_intarg+0x7d>
add    $0x10,%rsp
pop    %rbx
ret
mov    %rbx,%rdi
call   1050 <perror@plt>
jmp    11d5 <parse_intarg+0x95>
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edi
lea    0xe33(%rip),%rsi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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