#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 */
scalar_t__ digit_from_char(char, long, char *);
unsigned long long strtoull(const char *nptr) {
  char c;
  char digit;
  long base;
  unsigned long long l;
  while (*nptr == ' ') {
    nptr++;
  };
  if (nptr[0] == '0' && nptr[1] == 'x') {
    base = 16;
    nptr += 2;
  } else if (nptr[0] == '0') {
    base = 8;
    nptr += 1;
  } else {
    base = 10;
  }
  l = 0;
  while (digit_from_char(c = *nptr, base, &digit)) {
    l = l * base + digit;
    nptr++;
  }
  return l;
}


x86 assembly (clang, -O0):
<strtoull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    113d <strtoull+0x2d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    111c <strtoull+0xc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1177 <strtoull+0x67>
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x78,%eax
jne    1177 <strtoull+0x67>
movq   $0x10,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    11ad <strtoull+0x9d>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    11a0 <strtoull+0x90>
movq   $0x8,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <strtoull+0x98>
movq   $0xa,-0x18(%rbp)
jmp    11ad <strtoull+0x9d>
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x9(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0xa(%rbp),%rdx
movsbl %al,%edi
call   1030 <digit_from_char@plt>
cmp    $0x0,%rax
je     11fe <strtoull+0xee>
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
movsbq -0xa(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <strtoull+0xa5>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoull>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15
movzbl -0x1(%r15),%eax
cmp    $0x20,%al
je     1120 <strtoull+0x10>
cmp    $0x30,%al
jne    1149 <strtoull+0x39>
mov    $0x8,%r14d
cmpb   $0x78,(%r15)
jne    1153 <strtoull+0x43>
add    $0x1,%r15
mov    $0x10,%r14d
jmp    1153 <strtoull+0x43>
add    $0xffffffffffffffff,%r15
mov    $0xa,%r14d
movsbl (%r15),%edi
lea    0x7(%rsp),%rdx
mov    %r14,%rsi
call   1030 <digit_from_char@plt>
test   %rax,%rax
je     11a7 <strtoull+0x97>
add    $0x1,%r15
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
imul   %r14,%rbx
movsbq 0x7(%rsp),%rax
add    %rax,%rbx
movsbl (%r15),%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <digit_from_char@plt>
add    $0x1,%r15
test   %rax,%rax
jne    1180 <strtoull+0x70>
jmp    11a9 <strtoull+0x99>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @strtoull(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i8
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
br label %7
7: ; preds = %12, %1
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 32
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %2
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %2
br label %7
15: ; preds = %7
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 48
br i1 %20, label %21, label %30
21: ; preds = %15
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 120
br i1 %26, label %27, label %30
27: ; preds = %21
store i64 16, i64* %5
%28 = load i8*, i8** %2
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8* %29, i8** %2
br label %41
30: ; preds = %21, %15
%31 = load i8*, i8** %2
%32 = getelementptr inbounds i8, i8* %31, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 48
br i1 %35, label %36, label %39
36: ; preds = %30
store i64 8, i64* %5
%37 = load i8*, i8** %2
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8* %38, i8** %2
br label %40
39: ; preds = %30
store i64 10, i64* %5
br label %40
40: ; preds = %39, %36
br label %41
41: ; preds = %40, %27
store i64 0, i64* %6
br label %42
42: ; preds = %48, %41
%43 = load i8*, i8** %2
%44 = load i8, i8* %43
store i8 %44, i8* %3
%45 = load i64, i64* %5
%46 = call i64 @digit_from_char(i8 signext %44, i64 %45, i8* %4)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %42
%49 = load i64, i64* %6
%50 = load i64, i64* %5
%51 = mul i64 %49, %50
%52 = load i8, i8* %4
%53 = sext i8 %52 to i64
%54 = add i64 %51, %53
store i64 %54, i64* %6
%55 = load i8*, i8** %2
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %2
br label %42
57: ; preds = %42
%58 = load i64, i64* %6
ret i64 %58
}

LLVM IR (-O2):
define i64 @strtoull(i8* nocapture %0) {
%2 = alloca i8
br label %3
3: ; preds = %6, %1
%4 = phi i8* [ %0, %1 ], [ %7, %6 ]
%5 = load i8, i8* %4
switch i8 %5, label %16 [
i8 32, label %6
i8 48, label %8
]
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %4, i64 1
br label %3
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %4, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 120
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %4, i64 2
br label %16
14: ; preds = %8
%15 = getelementptr i8, i8* %4, i64 1
br label %16
16: ; preds = %3, %14, %12
%17 = phi i8* [ %13, %12 ], [ %15, %14 ], [ %4, %3 ]
%18 = phi i64 [ 16, %12 ], [ 8, %14 ], [ 10, %3 ]
%19 = load i8, i8* %17
%20 = call i64 @digit_from_char(i8 signext %19, i64 %18, i8* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %33, label %22
22: ; preds = %16, %22
%23 = phi i64 [ %28, %22 ], [ 0, %16 ]
%24 = phi i8* [ %29, %22 ], [ %17, %16 ]
%25 = mul i64 %23, %18
%26 = load i8, i8* %2
%27 = sext i8 %26 to i64
%28 = add i64 %25, %27
%29 = getelementptr inbounds i8, i8* %24, i64 1
%30 = load i8, i8* %29
%31 = call i64 @digit_from_char(i8 signext %30, i64 %18, i8* %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %22
33: ; preds = %22, %16
%34 = phi i64 [ 0, %16 ], [ %28, %22 ]
ret i64 %34
}