#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__ EINVAL;
long INT_MAX;
scalar_t__ errno;
long strtol(char *, char **, int);
int mustStrtoi(char *str) {
  char *endp;
  long result = strtol(str, &endp, 10);
  if (!errno)
    if (endp == str || result < 0 || result > INT_MAX)
      errno = EINVAL;
  return result;
}


x86 assembly (clang, -O0):
<mustStrtoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e9f(%rip),%rax
cmpq   $0x0,(%rax)
jne    1189 <mustStrtoi+0x79>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1170 <mustStrtoi+0x60>
cmpq   $0x0,-0x18(%rbp)
jl     1170 <mustStrtoi+0x60>
mov    -0x18(%rbp),%rax
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
jle    1184 <mustStrtoi+0x74>
mov    0x2e59(%rip),%rax
mov    (%rax),%rcx
mov    0x2e57(%rip),%rax
mov    %rcx,(%rax)
jmp    1189 <mustStrtoi+0x79>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mustStrtoi>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    0x2eaa(%rip),%rcx
cmpq   $0x0,(%rcx)
jne    1159 <mustStrtoi+0x49>
cmp    %rbx,0x8(%rsp)
je     114c <mustStrtoi+0x3c>
test   %rax,%rax
js     114c <mustStrtoi+0x3c>
mov    0x2ea1(%rip),%rdx
cmp    (%rdx),%rax
jle    1159 <mustStrtoi+0x49>
mov    0x2e7d(%rip),%rdx
mov    (%rdx),%rdx
mov    %rdx,(%rcx)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@errno = global i64 0
@INT_MAX = global i64 0
@EINVAL = global i64 0
define i32 @mustStrtoi(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 10)
store i64 %6, i64* %4
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = load i8*, i8** %2
%12 = icmp eq i8* %10, %11
br i1 %12, label %20, label %13
13: ; preds = %9
%14 = load i64, i64* %4
%15 = icmp slt i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i64, i64* %4
%18 = load i64, i64* @INT_MAX
%19 = icmp sgt i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16, %13, %9
%21 = load i64, i64* @EINVAL
store i64 %21, i64* @errno
br label %22
22: ; preds = %20, %16
br label %23
23: ; preds = %22, %1
%24 = load i64, i64* %4
%25 = trunc i64 %24 to i32
ret i32 %25
}

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