#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 */
typedef scalar_t__ int32_t;

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ERANGE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
long strtol(char const *, char **, int);
bool safe_strtoint(const char *str, int32_t *value) {
  long ret;
  char *end;
  assert(str != NULL);
  errno = 0;
  ret = strtol(str, &end, 10);
  if (errno != 0) {
    return false;
  } else if (end == str || *end != '\0') {
    errno = EINVAL;
    return false;
  }
  if ((long)((int32_t)ret) != ret) {
    errno = ERANGE;
    return false;
  }
  *value = (int32_t)ret;
  return true;
}


x86 assembly (clang, -O0):
<safe_strtoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e8f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e6b(%rip),%rax
cmpq   $0x0,(%rax)
je     1183 <safe_strtoint+0x63>
movl   $0x0,-0x4(%rbp)
jmp    1206 <safe_strtoint+0xe6>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11a1 <safe_strtoint+0x81>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c1 <safe_strtoint+0xa1>
mov    0x2e28(%rip),%rax
mov    (%rax),%rcx
mov    0x2e26(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1206 <safe_strtoint+0xe6>
jmp    11c6 <safe_strtoint+0xa6>
mov    -0x20(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11f4 <safe_strtoint+0xd4>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rcx
mov    0x2df3(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1206 <safe_strtoint+0xe6>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_strtoint>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
mov    0x2e9c(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmpq   $0x0,(%r15)
jne    118e <safe_strtoint+0x6e>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     1177 <safe_strtoint+0x57>
cmpb   $0x0,(%rdx)
je     1186 <safe_strtoint+0x66>
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%r15)
jmp    118e <safe_strtoint+0x6e>
mov    %rcx,(%r14)
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@ERANGE = global i64 0
define i32 @safe_strtoint(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp ne i8* %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
store i64 0, i64* @errno
%8 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, %0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14, %11
%18 = load i64, i64* @EINVAL
store i64 %18, i64* @errno
br label %20
19: ; preds = %14
store i64 %8, i64* %1
br label %20
20: ; preds = %2, %19, %17
%21 = phi i32 [ 0, %17 ], [ 1, %19 ], [ 0, %2 ]
ret i32 %21
}