#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 */
long INT_MAX;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
long strtol(char *, char **, int);
int str2int(char *str) {
  int num;
  errno = 0;
  char *p;
  long val = strtol(str, &p, 10);
  if (val > INT_MAX) {
    printf("\"%s\" is to large for int!\n", str);
    exit(1);
  }
  if (errno != 0 || *p != '\0') {
    printf("\"%s\" is not a valid int!\n", str);
    exit(1);
  }
  num = val;
  return num;
}


x86 assembly (clang, -O0):
<str2int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%rax
jle    118e <str2int+0x5e>
mov    -0x8(%rbp),%rsi
lea    0xe81(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e43(%rip),%rax
cmpq   $0x0,(%rax)
jne    11af <str2int+0x7f>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c9 <str2int+0x99>
mov    -0x8(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2int>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9a(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    0x2e8f(%rip),%rcx
cmp    (%rcx),%rax
jg     117e <str2int+0x4e>
cmpq   $0x0,(%rbx)
jne    1175 <str2int+0x45>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1175 <str2int+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe9f(%rip),%rdi
jmp    1185 <str2int+0x55>
lea    0xe7b(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@INT_MAX = global i64 0
@.str = [27 x i8] c"\22%s\22 is to large for int!\0A\00"
@.str.1 = [26 x i8] c"\22%s\22 is not a valid int!\0A\00"
define i32 @str2int(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* @errno
%6 = load i8*, i8** %2
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = load i64, i64* @INT_MAX
%10 = icmp sgt i64 %8, %9
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = icmp ne i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18, %15
%24 = load i8*, i8** %2
%25 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i64, i64* %5
%29 = trunc i64 %28 to i32
store i32 %29, i32* %3
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@errno = global i64 0
@INT_MAX = global i64 0
@.str = [27 x i8] c"\22%s\22 is to large for int!\0A\00"
@.str.1 = [26 x i8] c"\22%s\22 is not a valid int!\0A\00"
define i32 @str2int(i8* %0) {
%2 = alloca i8*
store i64 0, i64* @errno
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @INT_MAX
%6 = icmp sgt i64 %4, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13, %10
%18 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = trunc i64 %4 to i32
ret i32 %21
}