#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;
int /*<<< orphan*/ fmt_err_exit(char *, char const *, ...);
scalar_t__ is_blanks(char const *);
int /*<<< orphan*/ strerror(scalar_t__);
long strtol(char const *, char **, int const);
long str_to_long(const char *str) {
  const char MSG[] = "Number parsing error";
  const int BASE = 10;
  long res;
  char *tailptr;
  if (!str || *str == '\0')
    fmt_err_exit("%s: null or empty string", MSG);
  if (is_blanks(str))
    return 0;
  errno = 0;
  res = strtol(str, &tailptr, BASE);
  if (errno == ERANGE)
    fmt_err_exit("%s (%s): %s", MSG, str, strerror(errno));
  if (*tailptr)
    fmt_err_exit("%s (%s): not a base-10 integer", MSG, str);
  return res;
}


x86 assembly (clang, -O0):
<str_to_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xead(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeaa(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea8(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xea3(%rip),%al
mov    %al,-0x1c(%rbp)
movl   $0xa,-0x34(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1196 <str_to_long+0x56>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a8 <str_to_long+0x68>
lea    -0x30(%rbp),%rsi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1060 <fmt_err_exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <is_blanks@plt>
cmp    $0x0,%rax
je     11c8 <str_to_long+0x88>
movq   $0x0,-0x8(%rbp)
jmp    1268 <str_to_long+0x128>
mov    0x2e09(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x40(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    123d <str_to_long+0xfd>
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <fmt_err_exit@plt>
mov    -0x48(%rbp),%rax
cmpb   $0x0,(%rax)
je     1260 <str_to_long+0x120>
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <fmt_err_exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_long>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movaps 0xead(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x726f7272652067,%rax
mov    %rax,0x1d(%rsp)
test   %rdi,%rdi
je     1171 <str_to_long+0x31>
cmpb   $0x0,(%rbx)
jne    1184 <str_to_long+0x44>
lea    0xe9d(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <fmt_err_exit@plt>
mov    %rbx,%rdi
call   1030 <is_blanks@plt>
test   %rax,%rax
je     1196 <str_to_long+0x56>
xor    %r14d,%r14d
jmp    1205 <str_to_long+0xc5>
mov    0x2e3b(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r14
mov    (%r15),%rdi
mov    0x2e25(%rip),%rax
cmp    (%rax),%rdi
jne    11e5 <str_to_long+0xa5>
call   1050 <strerror@plt>
lea    0xe5a(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fmt_err_exit@plt>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     1205 <str_to_long+0xc5>
lea    0xe44(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fmt_err_exit@plt>
mov    %r14,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.str_to_long.MSG = [21 x i8] c"Number parsing error\00"
@.str = [25 x i8] c"%s: null or empty string\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.1 = [12 x i8] c"%s (%s): %s\00"
@.str.2 = [31 x i8] c"%s (%s): not a base-10 integer\00"
define i64 @str_to_long(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca [21 x i8]
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = bitcast [21 x i8]* %4 to i8*
store i32 10, i32* %5
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11, %1
%17 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
%18 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %11
%20 = load i8*, i8** %3
%21 = call i64 @is_blanks(i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i64 0, i64* %2
br label %46
24: ; preds = %19
store i64 0, i64* @errno
%25 = load i8*, i8** %3
%26 = call i64 @strtol(i8* %25, i8** %7, i32 10)
store i64 %26, i64* %6
%27 = load i64, i64* @errno
%28 = load i64, i64* @ERANGE
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %24
%31 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
%32 = load i8*, i8** %3
%33 = load i64, i64* @errno
%34 = call i32 @strerror(i64 %33)
%35 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %32, i32 %34)
br label %36
36: ; preds = %30, %24
%37 = load i8*, i8** %7
%38 = load i8, i8* %37
%39 = icmp ne i8 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
%42 = load i8*, i8** %3
%43 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %41, i8* %42)
br label %44
44: ; preds = %40, %36
%45 = load i64, i64* %6
store i64 %45, i64* %2
br label %46
46: ; preds = %44, %23
%47 = load i64, i64* %2
ret i64 %47
}

LLVM IR (-O2):
@__const.str_to_long.MSG = [21 x i8] c"Number parsing error\00"
@.str = [25 x i8] c"%s: null or empty string\00"
@errno = global i64 0
@ERANGE = global i64 0
@.str.1 = [12 x i8] c"%s (%s): %s\00"
@.str.2 = [31 x i8] c"%s (%s): not a base-10 integer\00"
define i64 @str_to_long(i8* %0) {
%2 = alloca [21 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %1
%11 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %12
12: ; preds = %10, %7
%13 = call i64 @is_blanks(i8* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %12
store i64 0, i64* @errno
%16 = call i64 @strtol(i8* %0, i8** %3, i32 10)
%17 = load i64, i64* @errno
%18 = load i64, i64* @ERANGE
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @strerror(i64 %17)
%22 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %0, i32 %21)
br label %23
23: ; preds = %20, %15
%24 = load i8*, i8** %3
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 (i8*, i8*, ...) @fmt_err_exit(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %0)
br label %29
29: ; preds = %23, %27, %12
%30 = phi i64 [ 0, %12 ], [ %16, %27 ], [ %16, %23 ]
ret i64 %30
}