#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 */
int errno;
long strtol(char *, char **, int);
long str2long(char *num, int base) {
  long rlong;
  int p_errno;
  if (num != (char *)NULL) {
    p_errno = errno;
    errno = 0;
    rlong = strtol(num, (char **)NULL, base);
    if (errno == 0) {
      errno = p_errno;
      return rlong;
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<str2long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <str2long+0x7d>
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ea3(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e82(%rip),%rax
cmpl   $0x0,(%rax)
jne    1180 <str2long+0x70>
mov    -0x24(%rbp),%ecx
mov    0x2e6f(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1195 <str2long+0x85>
movq   $0x0,-0x8(%rbp)
jmp    1195 <str2long+0x85>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2long>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1143 <str2long+0x33>
mov    %esi,%edx
mov    0x2ebe(%rip),%r14
mov    (%r14),%ebp
movl   $0x0,(%r14)
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <strtol@plt>
cmpl   $0x0,(%r14)
jne    1145 <str2long+0x35>
mov    %ebp,(%r14)
mov    %rax,%rbx
jmp    1145 <str2long+0x35>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i64 @str2long(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load i32, i32* @errno
store i32 %11, i32* %7
store i32 0, i32* @errno
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = call i64 @strtol(i8* %12, i8** null, i32 %13)
store i64 %14, i64* %6
%15 = load i32, i32* @errno
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* %7
store i32 %18, i32* @errno
%19 = load i64, i64* %6
store i64 %19, i64* %3
br label %22
20: ; preds = %10
store i64 0, i64* %3
br label %22
21: ; preds = %2
store i64 0, i64* %3
br label %22
22: ; preds = %21, %20, %17
%23 = load i64, i64* %3
ret i64 %23
}

LLVM IR (-O2):
@errno = global i32 0
define i64 @str2long(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @errno
store i32 0, i32* @errno
%6 = tail call i64 @strtol(i8* nocapture %0, i8** null, i32 %1)
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %4
store i32 %5, i32* @errno
br label %10
10: ; preds = %2, %4, %9
%11 = phi i64 [ %6, %9 ], [ 0, %4 ], [ 0, %2 ]
ret i64 %11
}