#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;
int /*<<< orphan*/ perror(char *);
long strtol(char *, char **, int);
long string_to_long(char *str) {
  long val;
  char *endptr;
  errno = 0;
  val = strtol(str, &endptr, 10);
  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
      (errno != 0 && val == 0) || (endptr == str)) {
    perror("Error while converting string to long value");
    val = -1;
  }
  return val;
}


x86 assembly (clang, -O0):
<string_to_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1192 <string_to_long+0x72>
mov    -0x10(%rbp),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <string_to_long+0x9c>
mov    -0x10(%rbp),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
je     11bc <string_to_long+0x9c>
mov    0x2e2f(%rip),%rax
cmpq   $0x0,(%rax)
je     11ae <string_to_long+0x8e>
cmpq   $0x0,-0x10(%rbp)
je     11bc <string_to_long+0x9c>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11d0 <string_to_long+0xb0>
lea    0xe3d(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x10(%rbp)
ff
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_long>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9a(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    (%r14),%rcx
mov    0x2e94(%rip),%rdx
cmp    (%rdx),%rcx
jne    1169 <string_to_long+0x49>
mov    0x2e78(%rip),%rdx
cmp    (%rdx),%rax
je     1187 <string_to_long+0x67>
mov    0x2e84(%rip),%rdx
cmp    (%rdx),%rax
je     1187 <string_to_long+0x67>
test   %rcx,%rcx
setne  %cl
test   %rax,%rax
sete   %dl
test   %dl,%cl
jne    1187 <string_to_long+0x67>
cmp    %rbx,(%rsp)
je     1187 <string_to_long+0x67>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    117f <string_to_long+0x5f>

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str = [44 x i8] c"Error while converting string to long value\00"
define i64 @string_to_long(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i8* %0, i8** %2
store i64 0, i64* @errno
%5 = load i8*, i8** %2
%6 = call i64 @strtol(i8* %5, i8** %4, i32 10)
store i64 %6, i64* %3
%7 = load i64, i64* @errno
%8 = load i64, i64* @ERANGE
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i64, i64* %3
%12 = load i64, i64* @LONG_MAX
%13 = icmp eq i64 %11, %12
br i1 %13, label %28, label %14
14: ; preds = %10
%15 = load i64, i64* %3
%16 = load i64, i64* @LONG_MIN
%17 = icmp eq i64 %15, %16
br i1 %17, label %28, label %18
18: ; preds = %14, %1
%19 = load i64, i64* @errno
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i64, i64* %3
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21, %18
%25 = load i8*, i8** %4
%26 = load i8*, i8** %2
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24, %21, %14, %10
%29 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %3
br label %30
30: ; preds = %28, %24
%31 = load i64, i64* %3
ret i64 %31
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@.str = [44 x i8] c"Error while converting string to long value\00"
define i64 @string_to_long(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i64, i64* @LONG_MAX
%10 = icmp eq i64 %4, %9
%11 = load i64, i64* @LONG_MIN
%12 = icmp eq i64 %4, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %21, label %14
14: ; preds = %8, %1
%15 = icmp ne i64 %5, 0
%16 = icmp eq i64 %4, 0
%17 = select i1 %15, i1 %16, i1 false
%18 = load i8*, i8** %2
%19 = icmp eq i8* %18, %0
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %23
21: ; preds = %14, %8
%22 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %14, %21
%24 = phi i64 [ -1, %21 ], [ %4, %14 ]
ret i64 %24
}