#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 /*<<< orphan*/ ERANGE;
long long LONG_MAX;
long long LONG_MIN;
int /*<<< orphan*/ errno;
long long strtoll(char const *, char **, int);
long strtol(const char *s, char **endptr, int base) {
  long long result = strtoll(s, endptr, base);
  if (result < LONG_MIN) {
    errno = ERANGE;
    return LONG_MIN;
  }
  if (result > LONG_MAX) {
    errno = ERANGE;
    return LONG_MAX;
  }
  return (long)result;
}


x86 assembly (clang, -O0):
<strtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strtoll@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ea6(%rip),%rcx
cmp    (%rcx),%rax
jge    1170 <strtol+0x60>
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6d(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <strtol+0xa1>
mov    -0x28(%rbp),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jle    11a9 <strtol+0x99>
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <strtol+0xa1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtol>:
push   %rax
call   1030 <strtoll@plt>
mov    0x2ecb(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jl     1134 <strtol+0x24>
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jle    1149 <strtol+0x39>
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    0x2e84(%rip),%rdx
mov    %eax,(%rdx)
mov    %rcx,%rax
pop    %rcx
ret

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

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