#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 EINVAL;
int ERANGE;
unsigned long LONG_MAX;
int errno;
unsigned long strtoull(char const *const, char **, int /*<<< orphan*/);
int mystrtoul(unsigned long *const dest, const char *const str,
              unsigned long max) {
  unsigned long val;
  char *endptr;
  errno = 0;
  val = strtoull(str, &endptr, 0);
  if ((val == 0 || val == LONG_MAX) && errno == ERANGE)
    return -ERANGE;
  if (val > max)
    return -ERANGE;
  if (*endptr != '\0')
    return -EINVAL;
  *dest = val;
  return 0;
}


x86 assembly (clang, -O0):
<mystrtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2ea5(%rip),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1163 <mystrtoul+0x53>
mov    -0x28(%rbp),%rax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <mystrtoul+0x7e>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%eax
jne    118e <mystrtoul+0x7e>
xor    %eax,%eax
mov    0x2e64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <mystrtoul+0xd4>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11af <mystrtoul+0x9f>
xor    %eax,%eax
mov    0x2e43(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <mystrtoul+0xd4>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d2 <mystrtoul+0xc2>
xor    %eax,%eax
mov    0x2e00(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <mystrtoul+0xd4>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mystrtoul>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2ea7(%rip),%r15
movl   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1030 <strtoull@plt>
test   %rax,%rax
je     1150 <mystrtoul+0x40>
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jne    1162 <mystrtoul+0x52>
mov    (%r15),%ecx
mov    0x2e8e(%rip),%rdx
cmp    (%rdx),%ecx
jne    1162 <mystrtoul+0x52>
neg    %ecx
jmp    118c <mystrtoul+0x7c>
cmp    %rbx,%rax
jbe    1170 <mystrtoul+0x60>
mov    0x2e7a(%rip),%rax
jmp    1181 <mystrtoul+0x71>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1187 <mystrtoul+0x77>
mov    0x2e47(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
jmp    118c <mystrtoul+0x7c>
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
@LONG_MAX = global i64 0
@ERANGE = global i32 0
@EINVAL = global i32 0
define i32 @mystrtoul(i64* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i64* %0, i64** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* @errno
%10 = load i8*, i8** %6
%11 = call i64 @strtoull(i8* %10, i8** %9, i32 0)
store i64 %11, i64* %8
%12 = load i64, i64* %8
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i64, i64* %8
%16 = load i64, i64* @LONG_MAX
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %14, %3
%19 = load i32, i32* @errno
%20 = load i32, i32* @ERANGE
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @ERANGE
%24 = sub nsw i32 0, %23
store i32 %24, i32* %4
br label %43
25: ; preds = %18, %14
%26 = load i64, i64* %8
%27 = load i64, i64* %7
%28 = icmp ugt i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @ERANGE
%31 = sub nsw i32 0, %30
store i32 %31, i32* %4
br label %43
32: ; preds = %25
%33 = load i8*, i8** %9
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @EINVAL
%39 = sub nsw i32 0, %38
store i32 %39, i32* %4
br label %43
40: ; preds = %32
%41 = load i64, i64* %8
%42 = load i64*, i64** %5
store i64 %41, i64* %42
store i32 0, i32* %4
br label %43
43: ; preds = %40, %37, %29, %22
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@errno = global i32 0
@LONG_MAX = global i64 0
@ERANGE = global i32 0
@EINVAL = global i32 0
define i32 @mystrtoul(i64* nocapture writeonly %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
store i32 0, i32* @errno
%6 = call i64 @strtoull(i8* %1, i8** %4, i32 0)
%7 = icmp eq i64 %6, 0
%8 = load i64, i64* @LONG_MAX
%9 = icmp eq i64 %6, %8
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* @errno
%13 = load i32, i32* @ERANGE
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = sub nsw i32 0, %12
br label %30
17: ; preds = %3, %11
%18 = icmp ugt i64 %6, %2
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i32, i32* @ERANGE
%21 = sub nsw i32 0, %20
br label %30
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i32, i32* @EINVAL
%28 = sub nsw i32 0, %27
br label %30
29: ; preds = %22
store i64 %6, i64* %0
br label %30
30: ; preds = %29, %26, %19, %15
%31 = phi i32 [ %16, %15 ], [ %21, %19 ], [ %28, %26 ], [ 0, %29 ]
ret i32 %31
}