#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(char);
long strtol(char const *, char **, int);
int gut_parse_long(const char *s, long *result) {
  char *endptr;
  long val;
  errno = 0;
  val = strtol(s, &endptr, 10);
  if (errno == ERANGE) {
    *result = val;
    return -3;
  } else if (errno != 0) {
    return -2;
  }
  if (endptr == s) {
    return -1;
  }
  while ((*endptr != '\0') && isspace(*endptr))
    endptr++;
  if (*endptr != '\0') {
    return -1;
  }
  *result = val;
  return 0;
}


x86 assembly (clang, -O0):
<gut_parse_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <gut_parse_long+0x65>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    1240 <gut_parse_long+0x120>
mov    0x2e4c(%rip),%rax
cmpq   $0x0,(%rax)
je     11a2 <gut_parse_long+0x82>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1240 <gut_parse_long+0x120>
jmp    11a7 <gut_parse_long+0x87>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c1 <gut_parse_long+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <gut_parse_long+0x120>
jmp    11c6 <gut_parse_long+0xa6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     11f1 <gut_parse_long+0xd1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1201 <gut_parse_long+0xe1>
jmp    1212 <gut_parse_long+0xf2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c6 <gut_parse_long+0xa6>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     122e <gut_parse_long+0x10e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <gut_parse_long+0x120>
mov    -0x28(%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):
<gut_parse_long>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea0(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r15
mov    0x0(%rbp),%rax
mov    0x2e8d(%rip),%rcx
mov    $0xfffffffd,%r12d
cmp    (%rcx),%rax
jne    116e <gut_parse_long+0x4e>
mov    %r15,(%r14)
mov    %r12d,%r13d
jmp    1179 <gut_parse_long+0x59>
mov    $0xfffffffe,%r13d
test   %rax,%rax
je     118b <gut_parse_long+0x6b>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rbp
mov    $0xffffffff,%r13d
cmp    %rbx,%rbp
je     1179 <gut_parse_long+0x59>
mov    0x0(%rbp),%al
xor    %r12d,%r12d
test   %al,%al
je     1166 <gut_parse_long+0x46>
add    $0x1,%rbp
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <isspace@plt>
test   %rax,%rax
je     11cb <gut_parse_long+0xab>
movzbl 0x0(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
jne    11b0 <gut_parse_long+0x90>
jmp    1166 <gut_parse_long+0x46>
cmpb   $0x0,-0x1(%rbp)
jne    1179 <gut_parse_long+0x59>
jmp    1166 <gut_parse_long+0x46>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i32 @gut_parse_long(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
store i64 0, i64* @errno
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 10)
store i64 %9, i64* %7
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i64, i64* %7
%15 = load i64*, i64** %5
store i64 %14, i64* %15
store i32 -3, i32* %3
br label %51
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -2, i32* %3
br label %51
20: ; preds = %16
br label %21
21: ; preds = %20
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %3
br label %51
26: ; preds = %21
br label %27
27: ; preds = %39, %26
%28 = load i8*, i8** %6
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = load i8, i8* %33
%35 = call i64 @isspace(i8 signext %34)
%36 = icmp ne i64 %35, 0
br label %37
37: ; preds = %32, %27
%38 = phi i1 [ false, %27 ], [ %36, %32 ]
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %6
br label %27
42: ; preds = %37
%43 = load i8*, i8** %6
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 -1, i32* %3
br label %51
48: ; preds = %42
%49 = load i64, i64* %7
%50 = load i64*, i64** %5
store i64 %49, i64* %50
store i32 0, i32* %3
br label %51
51: ; preds = %48, %47, %25, %19, %13
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i32 @gut_parse_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 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
br i1 %8, label %29, label %9
9: ; preds = %2
%10 = icmp eq i64 %6, 0
br i1 %10, label %11, label %31
11: ; preds = %9
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, %0
br i1 %13, label %31, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %14, %22
%18 = phi i8 [ %24, %22 ], [ %15, %14 ]
%19 = phi i8* [ %23, %22 ], [ %12, %14 ]
%20 = tail call i64 @isspace(i8 signext %18)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = getelementptr inbounds i8, i8* %19, i64 1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %29, label %17
26: ; preds = %17
%27 = load i8, i8* %19
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22, %26, %14, %2
%30 = phi i32 [ -3, %2 ], [ 0, %14 ], [ 0, %26 ], [ 0, %22 ]
store i64 %5, i64* %1
br label %31
31: ; preds = %29, %26, %11, %9
%32 = phi i32 [ -2, %9 ], [ -1, %11 ], [ -1, %26 ], [ %30, %29 ]
ret i32 %32
}