#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__ calculate_value(char *, int, int);
scalar_t__ find_base_and_sign(char const *, int *, int *);
unsigned long strtoul(const char *src, char **endptr, int base) {
  int sign = 1;
  unsigned long value = 0;
  src = (char *)find_base_and_sign(src, &sign, &base);
  value = (unsigned long)calculate_value((char *)src, sign, base);
  if (endptr)
    *endptr = (char *)src;
  return (sign * value);
}


x86 assembly (clang, -O0):
<strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <find_base_and_sign@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <calculate_value@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1180 <strtoul+0x60>
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoul>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edx,0x4(%rsp)
movl   $0x1,(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
call   1040 <find_base_and_sign@plt>
mov    %rax,%rbx
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    %rax,%rdi
call   1030 <calculate_value@plt>
test   %r14,%r14
je     1159 <strtoul+0x39>
mov    %rbx,(%r14)
movslq (%rsp),%rcx
imul   %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @strtoul(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
store i32 1, i32* %7
store i64 0, i64* %8
%9 = load i8*, i8** %4
%10 = call i64 @find_base_and_sign(i8* %9, i32* %7, i32* %6)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = call i64 @calculate_value(i8* %12, i32 %13, i32 %14)
store i64 %15, i64* %8
%16 = load i8**, i8*** %5
%17 = icmp ne i8** %16, null
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %4
%20 = load i8**, i8*** %5
store i8* %19, i8** %20
br label %21
21: ; preds = %18, %3
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = load i64, i64* %8
%25 = mul i64 %23, %24
ret i64 %25
}

LLVM IR (-O2):
define i64 @strtoul(i8* %0, i8** writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
store i32 %2, i32* %4
%6 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%7 = call i64 @find_base_and_sign(i8* %0, i32* %5, i32* %4)
%8 = inttoptr i64 %7 to i8*
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = call i64 @calculate_value(i8* %8, i32 %9, i32 %10)
%12 = icmp eq i8** %1, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i8* %8, i8** %1
br label %14
14: ; preds = %13, %3
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = mul i64 %11, %16
ret i64 %17
}