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


x86 assembly (clang, -O0):
<str_to_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e95(%rip),%rax
cmpq   $0x0,(%rax)
je     1161 <str_to_long+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <str_to_long+0x99>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    117b <str_to_long+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <str_to_long+0x99>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1197 <str_to_long+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <str_to_long+0x99>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_long>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eba(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
cmpq   $0x0,(%r15)
je     1154 <str_to_long+0x44>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     114a <str_to_long+0x3a>
cmpb   $0x0,(%rdx)
jne    114a <str_to_long+0x3a>
mov    %rcx,(%r14)
xor    %eax,%eax
jmp    114a <str_to_long+0x3a>

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

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