#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 */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
long INT_MAX;
long INT_MIN;
int /*<<< orphan*/ errno;
long strtol(char const *, char **, int);
int parse_int(const char *str) {
  char *end;
  long val = strtol(str, &end, 10);
  if (*str == '\0' || *end != '\0') {
    errno = EINVAL;
    return 0;
  } else if (val < INT_MIN || val > INT_MAX) {
    errno = ERANGE;
    return 0;
  } else {
    return (int)val;
  }
}


x86 assembly (clang, -O0):
<parse_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1152 <parse_int+0x42>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1170 <parse_int+0x60>
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    0x2e66(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11bd <parse_int+0xad>
mov    -0x20(%rbp),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jl     1198 <parse_int+0x88>
mov    -0x20(%rbp),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jle    11b6 <parse_int+0xa6>
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    0x2e20(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11bd <parse_int+0xad>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_int>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
cmpb   $0x0,(%rbx)
je     1136 <parse_int+0x26>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1150 <parse_int+0x40>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    0x2e82(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e89(%rip),%rcx
cmp    (%rcx),%rax
jl     1168 <parse_int+0x58>
mov    0x2e85(%rip),%rcx
cmp    (%rcx),%rax
jle    114a <parse_int+0x3a>
mov    0x2e69(%rip),%rax
jmp    113d <parse_int+0x2d>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@ERANGE = global i32 0
define i32 @parse_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12, %1
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
store i32 0, i32* %2
br label %32
19: ; preds = %12
%20 = load i64, i64* %5
%21 = load i64, i64* @INT_MIN
%22 = icmp slt i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* %5
%25 = load i64, i64* @INT_MAX
%26 = icmp sgt i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23, %19
%28 = load i32, i32* @ERANGE
store i32 %28, i32* @errno
store i32 0, i32* %2
br label %32
29: ; preds = %23
%30 = load i64, i64* %5
%31 = trunc i64 %30 to i32
store i32 %31, i32* %2
br label %32
32: ; preds = %29, %27, %17
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@INT_MIN = global i64 0
@INT_MAX = global i64 0
@ERANGE = global i32 0
define i32 @parse_int(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7, %1
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %23
13: ; preds = %7
%14 = load i64, i64* @INT_MIN
%15 = icmp slt i64 %4, %14
%16 = load i64, i64* @INT_MAX
%17 = icmp sgt i64 %4, %16
%18 = select i1 %15, i1 true, i1 %17
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* @ERANGE
store i32 %20, i32* @errno
br label %23
21: ; preds = %13
%22 = trunc i64 %4 to i32
br label %23
23: ; preds = %21, %19, %11
%24 = phi i32 [ 0, %11 ], [ 0, %19 ], [ %22, %21 ]
ret i32 %24
}