#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 strtoul_base(char const **, int);
unsigned int strtoul_charval(char const);
unsigned long long strtoull(const char *p, char **endp, int base) {
  unsigned long long ret = 0;
  unsigned int charval;
  base = strtoul_base(&p, base);
  while (1) {
    charval = strtoul_charval(*p);
    if (charval >= (unsigned int)base)
      break;
    ret = ((ret * base) + charval);
    p++;
  }
  if (endp)
    *endp = (char *)p;
  return (ret);
}


x86 assembly (clang, -O0):
<strtoull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x8(%rbp),%rdi
call   1030 <strtoul_base@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <strtoul_charval@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     116a <strtoull+0x4a>
jmp    1191 <strtoull+0x71>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
imul   %rcx,%rax
mov    -0x24(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    114a <strtoull+0x2a>
cmpq   $0x0,-0x10(%rbp)
je     11a7 <strtoull+0x87>
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoull>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,(%rsp)
mov    %rsp,%rdi
mov    %edx,%esi
call   1030 <strtoul_base@plt>
mov    %eax,%r15d
mov    (%rsp),%rax
movsbl (%rax),%edi
call   1040 <strtoul_charval@plt>
cmp    %r15d,%eax
jae    119d <strtoull+0x7d>
movslq %r15d,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
imul   %rbp,%rbx
mov    %eax,%eax
add    %rax,%rbx
mov    (%rsp),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%rsp)
movsbl 0x1(%rax),%edi
call   1040 <strtoul_charval@plt>
cmp    %r15d,%eax
jb     1160 <strtoull+0x40>
test   %r14,%r14
je     118f <strtoull+0x6f>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebx,%ebx
test   %r14,%r14
jne    1188 <strtoull+0x68>
jmp    118f <strtoull+0x6f>

LLVM IR (-O0):
define i64 @strtoull(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
store i64 0, i64* %7
%9 = load i32, i32* %6
%10 = call i32 @strtoul_base(i8** %4, i32 %9)
store i32 %10, i32* %6
br label %11
11: ; preds = %3, %19
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = call i32 @strtoul_charval(i8 signext %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = icmp uge i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %11
br label %29
19: ; preds = %11
%20 = load i64, i64* %7
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = mul i64 %20, %22
%24 = load i32, i32* %8
%25 = zext i32 %24 to i64
%26 = add i64 %23, %25
store i64 %26, i64* %7
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
br label %11
29: ; preds = %18
%30 = load i8**, i8*** %5
%31 = icmp ne i8** %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %4
%34 = load i8**, i8*** %5
store i8* %33, i8** %34
br label %35
35: ; preds = %32, %29
%36 = load i64, i64* %7
ret i64 %36
}

LLVM IR (-O2):
define i64 @strtoull(i8* %0, i8** writeonly %1, i32 %2) {
%4 = alloca i8*
store i8* %0, i8** %4
%5 = call i32 @strtoul_base(i8** %4, i32 %2)
%6 = load i8*, i8** %4
%7 = load i8, i8* %6
%8 = call i32 @strtoul_charval(i8 signext %7)
%9 = icmp ult i32 %8, %5
br i1 %9, label %10, label %23
10: ; preds = %3
%11 = sext i32 %5 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ %8, %10 ], [ %21, %12 ]
%14 = phi i64 [ 0, %10 ], [ %17, %12 ]
%15 = mul i64 %14, %11
%16 = zext i32 %13 to i64
%17 = add i64 %15, %16
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %19, i8** %4
%20 = load i8, i8* %19
%21 = call i32 @strtoul_charval(i8 signext %20)
%22 = icmp ult i32 %21, %5
br i1 %22, label %12, label %23
23: ; preds = %12, %3
%24 = phi i64 [ 0, %3 ], [ %17, %12 ]
%25 = icmp eq i8** %1, null
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = load i8*, i8** %4
store i8* %27, i8** %1
br label %28
28: ; preds = %26, %23
ret i64 %24
}