#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 EINVAL;
int errno;
long strtol(char const *, char **, int);
int lmsc_s_tol(const char *str, long *result, char **endp, int base) {
  int e = errno;
  errno = 0;
  *result = strtol(str, endp, base);
  if (errno) {
    return -1;
  }
  if (str == *endp) {
    errno = EINVAL;
    return -1;
  }
  errno = e;
  return 0;
}


x86 assembly (clang, -O0):
<lmsc_s_tol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2ea6(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e7f(%rip),%rax
cmpl   $0x0,(%rax)
je     1176 <lmsc_s_tol+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <lmsc_s_tol+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jne    11a5 <lmsc_s_tol+0x95>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e49(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <lmsc_s_tol+0xa8>
mov    -0x28(%rbp),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lmsc_s_tol>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2eb8(%rip),%r12
mov    (%r12),%ebp
movl   $0x0,(%r12)
mov    %rdx,%rsi
mov    %ecx,%edx
call   1030 <strtol@plt>
mov    %rax,(%r15)
mov    $0xffffffff,%eax
cmpl   $0x0,(%r12)
jne    1168 <lmsc_s_tol+0x58>
xor    %eax,%eax
cmp    %rbx,(%r14)
sete   %cl
jne    1160 <lmsc_s_tol+0x50>
mov    0x2e7a(%rip),%rdx
mov    (%rdx),%ebp
mov    %cl,%al
neg    %eax
mov    %ebp,(%r12)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i32 0
@EINVAL = global i32 0
define i32 @lmsc_s_tol(i8* %0, i64* nocapture writeonly %1, i8** nocapture %2, i32 %3) {
%5 = load i32, i32* @errno
store i32 0, i32* @errno
%6 = tail call i64 @strtol(i8* %0, i8** %2, i32 %3)
store i64 %6, i64* %1
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %4
%10 = load i8*, i8** %2
%11 = icmp eq i8* %10, %0
%12 = load i32, i32* @EINVAL
%13 = select i1 %11, i32 %12, i32 %5
%14 = sext i1 %11 to i32
store i32 %13, i32* @errno
br label %15
15: ; preds = %9, %4
%16 = phi i32 [ -1, %4 ], [ %14, %9 ]
ret i32 %16
}