#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;
long LONG_MAX;
long LONG_MIN;
scalar_t__ errno;
long strtol(char const *, char **, int);
int str_to_long(const char *str, long *val) {
  char *endptr;
  *val = 0;
  errno = 0;
  *val = strtol(str, &endptr, 10);
  if ((errno == ERANGE && (*val == LONG_MAX || *val == LONG_MIN)) ||
      (errno != 0 && *val == 0)) {
    return -1;
  }
  if (endptr == str) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<str_to_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e96(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jne    119d <str_to_long+0x8d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <str_to_long+0xac>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <str_to_long+0xac>
mov    0x2e24(%rip),%rax
cmpq   $0x0,(%rax)
je     11c8 <str_to_long+0xb8>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c8 <str_to_long+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <str_to_long+0xd9>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11e2 <str_to_long+0xd2>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e9 <str_to_long+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_long>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,(%rsi)
mov    0x2e9b(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,(%rbx)
mov    (%r15),%rdx
mov    0x2e90(%rip),%rcx
cmp    (%rcx),%rdx
jne    1172 <str_to_long+0x62>
mov    0x2e74(%rip),%rsi
mov    $0xffffffff,%ecx
cmp    (%rsi),%rax
je     1191 <str_to_long+0x81>
mov    0x2e7b(%rip),%rsi
cmp    (%rsi),%rax
je     1191 <str_to_long+0x81>
test   %rdx,%rdx
je     1181 <str_to_long+0x71>
mov    $0xffffffff,%ecx
test   %rax,%rax
je     1191 <str_to_long+0x81>
xor    %eax,%eax
cmp    %r14,0x8(%rsp)
setne  %al
lea    (%rax,%rax,1),%ecx
add    $0xfffffffe,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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