#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__ uintmax_t;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
scalar_t__ LONG_MAX;
long LONG_MIN;
scalar_t__ __strtomax(char const *, int, int *, int *, char **);
int /*<<< orphan*/ errno;
long int strtol(const char *restrict nptr, char **restrict endptr, int base) {
  bool overflow = false, sign = false;
  uintmax_t pval = __strtomax(nptr, base, &overflow, &sign, endptr);
  if (overflow || pval > LONG_MAX) {
    errno = ERANGE;
    if (sign) {
      return LONG_MIN;
    } else {
      return LONG_MAX;
    }
  } else {
    if (sign)
      return 0 - (long int)pval;
    else
      return (long int)pval;
  }
}


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)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%r8
lea    -0x20(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   1030 <__strtomax@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    116b <strtol+0x5b>
mov    -0x30(%rbp),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jle    11ad <strtol+0x9d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x24(%rbp)
je     119a <strtol+0x8a>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <strtol+0xbe>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <strtol+0xbe>
cmpl   $0x0,-0x24(%rbp)
je     11c6 <strtol+0xb6>
xor    %eax,%eax
sub    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ce <strtol+0xbe>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtol>:
push   %rax
mov    %rsi,%r8
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
lea    0x4(%rsp),%rax
mov    %rsp,%rcx
mov    %edx,%esi
mov    %rax,%rdx
call   1030 <__strtomax@plt>
mov    %rax,%rcx
cmpl   $0x0,0x4(%rsp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%rax
jne    115e <strtol+0x4e>
cmp    %rax,%rcx
jg     115e <strtol+0x4e>
mov    %rcx,%rax
neg    %rax
cmpl   $0x0,(%rsp)
cmove  %rcx,%rax
pop    %rcx
ret
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e5a(%rip),%rdx
mov    %ecx,(%rdx)
cmpl   $0x0,(%rsp)
je     115c <strtol+0x4c>
mov    0x2e6b(%rip),%rax
mov    (%rax),%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* noalias %0, i8** noalias %1, i32 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = load i32, i32* %7
%13 = load i8**, i8*** %6
%14 = call i64 @__strtomax(i8* %11, i32 %12, i32* %8, i32* %9, i8** %13)
store i64 %14, i64* %10
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %3
%18 = load i64, i64* %10
%19 = load i64, i64* @LONG_MAX
%20 = icmp sgt i64 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17, %3
%22 = load i32, i32* @ERANGE
store i32 %22, i32* @errno
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i64, i64* @LONG_MIN
store i64 %26, i64* %4
br label %37
27: ; preds = %21
%28 = load i64, i64* @LONG_MAX
store i64 %28, i64* %4
br label %37
29: ; preds = %17
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i64, i64* %10
%34 = sub nsw i64 0, %33
store i64 %34, i64* %4
br label %37
35: ; preds = %29
%36 = load i64, i64* %10
store i64 %36, i64* %4
br label %37
37: ; preds = %35, %32, %27, %25
%38 = load i64, i64* %4
ret i64 %38
}

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* noalias %0, i8** noalias %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = call i64 @__strtomax(i8* %0, i32 %2, i32* %4, i32* %5, i8** %1)
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
%11 = load i64, i64* @LONG_MAX
%12 = icmp sgt i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @ERANGE
store i32 %15, i32* @errno
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
%18 = load i64, i64* @LONG_MIN
%19 = select i1 %17, i64 %11, i64 %18
br label %25
20: ; preds = %3
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
%23 = sub nsw i64 0, %8
%24 = select i1 %22, i64 %8, i64 %23
br label %25
25: ; preds = %14, %20
%26 = phi i64 [ %24, %20 ], [ %19, %14 ]
ret i64 %26
}