#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 long int32_t;

/* Variables and functions */
scalar_t__ ERANGE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ isspace(unsigned char);
long strtol(char const *, char **, int);
bool safe_strtol(const char *str, int32_t *out) {
  assert(out != NULL);
  errno = 0;
  *out = 0;
  char *endptr;
  long l = strtol(str, &endptr, 10);
  if ((errno == ERANGE) || (str == endptr)) {
    return false;
  }
  if (isspace((unsigned char)*endptr) || (*endptr == '\0' && endptr != str)) {
    *out = l;
    return true;
  }
  return false;
}


x86 assembly (clang, -O0):
<safe_strtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e7f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <safe_strtol+0x79>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11b5 <safe_strtol+0x85>
movl   $0x0,-0x4(%rbp)
jmp    1207 <safe_strtol+0xd7>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
jne    11e9 <safe_strtol+0xb9>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1200 <safe_strtol+0xd0>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1200 <safe_strtol+0xd0>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1207 <safe_strtol+0xd7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_strtol>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1040 <assert@plt>
mov    0x2e81(%rip),%r14
movq   $0x0,(%r14)
movq   $0x0,(%rbx)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%r15
mov    (%r14),%rax
mov    0x2e64(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
je     11b5 <safe_strtol+0x85>
mov    0x8(%rsp),%r12
cmp    %rbp,%r12
je     11b5 <safe_strtol+0x85>
movzbl (%r12),%edi
call   1030 <isspace@plt>
test   %rax,%rax
jne    11ac <safe_strtol+0x7c>
cmpb   $0x0,(%r12)
jne    11b5 <safe_strtol+0x85>
mov    %r15,(%rbx)
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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