#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 */
typedef scalar_t__ intmax_t;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
scalar_t__ LONG_MAX;
scalar_t__ LONG_MIN;
int /*<<< orphan*/ errno;
scalar_t__ strtoimax(char const *, char **, int);
long strtol(const char *s, char **p, int base) {
  intmax_t x = strtoimax(s, p, base);
  if (x > LONG_MAX) {
    errno = ERANGE;
    return LONG_MAX;
  } else if (x < LONG_MIN) {
    errno = ERANGE;
    return LONG_MIN;
  }
  return x;
}


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 <strtoimax@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
jle    1170 <strtol+0x60>
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6d(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <strtol+0xa6>
mov    -0x28(%rbp),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jge    11a9 <strtol+0x99>
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    0x2e34(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <strtol+0xa6>
jmp    11ae <strtol+0x9e>
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 <strtoimax@plt>
mov    0x2eb3(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jg     1134 <strtol+0x24>
mov    0x2ebc(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jge    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_MAX = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
@LONG_MIN = 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 @strtoimax(i8* %9, i8** %10, i32 %11)
store i64 %12, i64* %8
%13 = load i64, i64* %8
%14 = load i64, i64* @LONG_MAX
%15 = icmp sgt 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_MAX
store i64 %18, i64* %4
br label %29
19: ; preds = %3
%20 = load i64, i64* %8
%21 = load i64, i64* @LONG_MIN
%22 = icmp slt 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_MIN
store i64 %25, i64* %4
br label %29
26: ; preds = %19
br label %27
27: ; preds = %26
%28 = load i64, i64* %8
store i64 %28, i64* %4
br label %29
29: ; preds = %27, %23, %16
%30 = load i64, i64* %4
ret i64 %30
}

LLVM IR (-O2):
@LONG_MAX = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
@LONG_MIN = global i64 0
define i64 @strtol(i8* %0, i8** %1, i32 %2) {
%4 = tail call i64 @strtoimax(i8* %0, i8** %1, i32 %2)
%5 = load i64, i64* @LONG_MAX
%6 = icmp sgt i64 %4, %5
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i64, i64* @LONG_MIN
%9 = icmp slt 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
}