#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__ ERANGE;
scalar_t__ errno;
scalar_t__ isspace(unsigned char);
long strtol(char const *, char **, int);
int safe_strtol(const char *str, int *out) {
  errno = 0;
  *out = 0;
  char *endptr;
  long l = strtol(str, &endptr, 10);
  if ((errno == ERANGE) || (str == endptr)) {
    return -1;
  }
  if (isspace((unsigned char)*endptr) || (*endptr == '\0' && endptr != str)) {
    *out = l;
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<safe_strtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
je     1186 <safe_strtol+0x66>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1192 <safe_strtol+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <safe_strtol+0xc5>
mov    -0x20(%rbp),%rax
movzbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
jne    11c6 <safe_strtol+0xa6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11de <safe_strtol+0xbe>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11de <safe_strtol+0xbe>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e5 <safe_strtol+0xc5>
movl   $0xffffffff,-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,%r15
mov    %rdi,%rbx
mov    0x2e9f(%rip),%rbp
movq   $0x0,0x0(%rbp)
movl   $0x0,(%rsi)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r14
mov    0x0(%rbp),%rax
mov    0x2e84(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
je     1193 <safe_strtol+0x73>
mov    0x8(%rsp),%r12
cmp    %rbx,%r12
je     1193 <safe_strtol+0x73>
movzbl (%r12),%edi
call   1030 <isspace@plt>
test   %rax,%rax
jne    118e <safe_strtol+0x6e>
cmpb   $0x0,(%r12)
jne    1193 <safe_strtol+0x73>
mov    %r14d,(%r15)
xor    %ebp,%ebp
mov    %ebp,%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, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 0, i64* @errno
%8 = load i32*, i32** %5
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = call i64 @strtol(i8* %9, i8** %6, i32 10)
store i64 %10, i64* %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ERANGE
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %6
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14, %2
store i32 -1, i32* %3
br label %38
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = load i8, i8* %20
%22 = call i64 @isspace(i8 zeroext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = load i8*, i8** %4
%32 = icmp ne i8* %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29, %19
%34 = load i64, i64* %7
%35 = trunc i64 %34 to i32
%36 = load i32*, i32** %5
store i32 %35, i32* %36
store i32 0, i32* %3
br label %38
37: ; preds = %29, %24
store i32 -1, i32* %3
br label %38
38: ; preds = %37, %33, %18
%39 = load i32, i32* %3
ret i32 %39
}

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