#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;
scalar_t__ __strtomax(char const *, int, int *, int *, char **);
int /*<<< orphan*/ errno;
unsigned long int strtoul(const char *restrict nptr, char **restrict endptr,
                          int base) {
  bool overflow = false, sign = false;
  uintmax_t pval = __strtomax(nptr, base, &overflow, &sign, endptr);
  if (sign) {
    errno = ERANGE;
    return 0;
  }
  if (overflow || pval > LONG_MAX) {
    errno = ERANGE;
    return LONG_MAX;
  } else {
    return (long int)pval;
  }
}


x86 assembly (clang, -O0):
<strtoul>:
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,-0x24(%rbp)
je     1176 <strtoul+0x66>
mov    0x2e8a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e69(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11c1 <strtoul+0xb1>
cmpl   $0x0,-0x20(%rbp)
jne    1194 <strtoul+0x84>
mov    -0x30(%rbp),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jle    11b9 <strtoul+0xa9>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2c(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11c1 <strtoul+0xb1>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoul>:
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>
cmpl   $0x0,(%rsp)
je     1154 <strtoul+0x44>
xor    %ecx,%ecx
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    0x2e83(%rip),%rdx
mov    %eax,(%rdx)
mov    %rcx,%rax
pop    %rcx
ret
cmpl   $0x0,0x4(%rsp)
mov    0x2e78(%rip),%rcx
mov    (%rcx),%rcx
jne    113d <strtoul+0x2d>
cmp    %rcx,%rax
jle    1152 <strtoul+0x42>
jmp    113d <strtoul+0x2d>

LLVM IR (-O0):
@ERANGE = global i32 0
@errno = global i32 0
@LONG_MAX = global i64 0
define i64 @strtoul(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* %9
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* @ERANGE
store i32 %18, i32* @errno
store i64 0, i64* %4
br label %31
19: ; preds = %3
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load i64, i64* %10
%24 = load i64, i64* @LONG_MAX
%25 = icmp sgt i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22, %19
%27 = load i32, i32* @ERANGE
store i32 %27, i32* @errno
%28 = load i64, i64* @LONG_MAX
store i64 %28, i64* %4
br label %31
29: ; preds = %22
%30 = load i64, i64* %10
store i64 %30, i64* %4
br label %31
31: ; preds = %29, %26, %17
%32 = load i64, i64* %4
ret i64 %32
}

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
@LONG_MAX = global i64 0
define i64 @strtoul(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* %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
%14 = load i64, i64* @LONG_MAX
%15 = icmp sgt i64 %8, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %20
17: ; preds = %11, %3
%18 = phi i64 [ 0, %3 ], [ %14, %11 ]
%19 = load i32, i32* @ERANGE
store i32 %19, i32* @errno
br label %20
20: ; preds = %17, %11
%21 = phi i64 [ %8, %11 ], [ %18, %17 ]
ret i64 %21
}