#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 */
long LONG_MAX;
long LONG_MIN;
int /*<<< orphan*/ isspace(char);
long strtol(char const *, char **, int);
int read_long(const char *str, long *val_p) {
  char *end;
  long val = strtol(str, &end, 10);
  if ((*end != '\0' && !isspace(*end)) || val == LONG_MIN || val == LONG_MAX) {
    return -1;
  }
  *val_p = val;
  return 0;
}


x86 assembly (clang, -O0):
<read_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116b <read_long+0x4b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isspace@plt>
cmp    $0x0,%eax
je     1193 <read_long+0x73>
mov    -0x28(%rbp),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
je     1193 <read_long+0x73>
mov    -0x28(%rbp),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <read_long+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <read_long+0x91>
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):
<read_long>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%rbx
mov    (%rsp),%rax
mov    (%rax),%al
test   %al,%al
je     1162 <read_long+0x42>
movsbl %al,%edi
call   1030 <isspace@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
je     1189 <read_long+0x69>
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rbx
jne    1173 <read_long+0x53>
jmp    1189 <read_long+0x69>
mov    0x2e7f(%rip),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%rbx
je     1189 <read_long+0x69>
mov    0x2e5e(%rip),%rcx
mov    $0xffffffff,%eax
cmp    (%rcx),%rbx
je     1189 <read_long+0x69>
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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