#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 int /*<<< orphan*/ uintmax_t;

/* Variables and functions */
int ULONG_MAX;
scalar_t__ _PDCLIB_strtox_main(char const **, unsigned int, int /*<<< orphan*/,
                               int /*<<< orphan*/, int, char *);
char *_PDCLIB_strtox_prelim(char const *, char *, int *);
unsigned long int strtoul(const char *s, char **endptr, int base) {
  unsigned long int rc;
  char sign = '+';
  const char *p = _PDCLIB_strtox_prelim(s, &sign, &base);
  if (base < 2 || base > 36)
    return 0;
  rc = (unsigned long int)_PDCLIB_strtox_main(
      &p, (unsigned)base, (uintmax_t)ULONG_MAX, (uintmax_t)(ULONG_MAX / base),
      (int)(ULONG_MAX % base), &sign);
  if (endptr != NULL)
    *endptr = (p != NULL) ? (char *)p : (char *)s;
  return (sign == '+') ? rc : -rc;
}


x86 assembly (clang, -O0):
<strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movb   $0x2b,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x29(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <_PDCLIB_strtox_prelim@plt>
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     1160 <strtoul+0x40>
cmpl   $0x24,-0x1c(%rbp)
jle    116d <strtoul+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    1211 <strtoul+0xf1>
mov    -0x1c(%rbp),%esi
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x1c(%rbp)
mov    %eax,%ecx
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
cltd
idivl  -0x1c(%rbp)
mov    %edx,%r8d
mov    -0x3c(%rbp),%edx
lea    -0x38(%rbp),%rdi
lea    -0x29(%rbp),%r9
call   1040 <_PDCLIB_strtox_main@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e5 <strtoul+0xc5>
cmpq   $0x0,-0x38(%rbp)
je     11d2 <strtoul+0xb2>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11da <strtoul+0xba>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0x2b,%eax
jne    11ff <strtoul+0xdf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1209 <strtoul+0xe9>
xor    %eax,%eax
sub    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoul>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %edx,0xc(%rsp)
movb   $0x2b,0xb(%rsp)
lea    0xb(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1030 <_PDCLIB_strtox_prelim@plt>
mov    %rax,0x10(%rsp)
mov    0xc(%rsp),%esi
lea    -0x25(%rsi),%eax
cmp    $0xffffffdd,%eax
jae    115a <strtoul+0x3a>
xor    %ecx,%ecx
jmp    11a1 <strtoul+0x81>
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    %ecx,%eax
cltd
idiv   %esi
mov    %edx,%r8d
lea    0x10(%rsp),%rdi
lea    0xb(%rsp),%r9
mov    %ecx,%edx
mov    %eax,%ecx
call   1040 <_PDCLIB_strtox_main@plt>
test   %r14,%r14
je     1192 <strtoul+0x72>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
cmovne %rcx,%rbx
mov    %rbx,(%r14)
mov    %rax,%rcx
neg    %rcx
cmpb   $0x2b,0xb(%rsp)
cmove  %rax,%rcx
mov    %rcx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ULONG_MAX = global i32 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
%9 = alloca i8
%10 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i8 43, i8* %9
%11 = load i8*, i8** %5
%12 = call i8* @_PDCLIB_strtox_prelim(i8* %11, i8* %9, i32* %7)
store i8* %12, i8** %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* %7
%17 = icmp sgt i32 %16, 36
br i1 %17, label %18, label %19
18: ; preds = %15, %3
store i64 0, i64* %4
br label %52
19: ; preds = %15
%20 = load i32, i32* %7
%21 = load i32, i32* @ULONG_MAX
%22 = load i32, i32* @ULONG_MAX
%23 = load i32, i32* %7
%24 = sdiv i32 %22, %23
%25 = load i32, i32* @ULONG_MAX
%26 = load i32, i32* %7
%27 = srem i32 %25, %26
%28 = call i64 @_PDCLIB_strtox_main(i8** %10, i32 %20, i32 %21, i32 %24, i32 %27, i8* %9)
store i64 %28, i64* %8
%29 = load i8**, i8*** %6
%30 = icmp ne i8** %29, null
br i1 %30, label %31, label %41
31: ; preds = %19
%32 = load i8*, i8** %10
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i8*, i8** %10
br label %38
36: ; preds = %31
%37 = load i8*, i8** %5
br label %38
38: ; preds = %36, %34
%39 = phi i8* [ %35, %34 ], [ %37, %36 ]
%40 = load i8**, i8*** %6
store i8* %39, i8** %40
br label %41
41: ; preds = %38, %19
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 43
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i64, i64* %8
br label %50
47: ; preds = %41
%48 = load i64, i64* %8
%49 = sub i64 0, %48
br label %50
50: ; preds = %47, %45
%51 = phi i64 [ %46, %45 ], [ %49, %47 ]
store i64 %51, i64* %4
br label %52
52: ; preds = %50, %18
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
@ULONG_MAX = global i32 0
define i64 @strtoul(i8* %0, i8** writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
store i32 %2, i32* %4
store i8 43, i8* %5
%7 = bitcast i8** %6 to i8*
%8 = call i8* @_PDCLIB_strtox_prelim(i8* %0, i8* %5, i32* %4)
store i8* %8, i8** %6
%9 = load i32, i32* %4
%10 = add i32 %9, -37
%11 = icmp ult i32 %10, -35
br i1 %11, label %27, label %12
12: ; preds = %3
%13 = load i32, i32* @ULONG_MAX
%14 = sdiv i32 %13, %9
%15 = srem i32 %13, %9
%16 = call i64 @_PDCLIB_strtox_main(i8** %6, i32 %9, i32 %13, i32 %14, i32 %15, i8* %5)
%17 = icmp eq i8** %1, null
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* %0, i8* %19
store i8* %21, i8** %1
br label %22
22: ; preds = %18, %12
%23 = load i8, i8* %5
%24 = icmp eq i8 %23, 43
%25 = sub i64 0, %16
%26 = select i1 %24, i64 %16, i64 %25
br label %27
27: ; preds = %3, %22
%28 = phi i64 [ %26, %22 ], [ 0, %3 ]
ret i64 %28
}