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


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)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strtoll@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1161 <strtoul+0x51>
mov    0x2e9f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e86(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11a2 <strtoul+0x92>
mov    -0x28(%rbp),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jle    119a <strtoul+0x8a>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e53(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11a2 <strtoul+0x92>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoul>:
push   %rax
call   1030 <strtoll@plt>
test   %rax,%rax
js     112c <strtoul+0x1c>
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jg     112e <strtoul+0x1e>
pop    %rcx
ret
xor    %ecx,%ecx
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    0x2e9a(%rip),%rdx
mov    %eax,(%rdx)
mov    %rcx,%rax
pop    %rcx
ret

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

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