#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__ atol(char *);
int /*<<< orphan*/ sscanf(char *, char *, long *);
unsigned long cstrtoul(char *str, int base) {
  long l = -1L;
  if (base == 10)
    return (unsigned long)atol(str);
  if (base == 16) {
    sscanf(str, "%lx", &l);
    return (unsigned long)l;
  }
  if (base == 8) {
    sscanf(str, "%lo", &l);
    return (unsigned long)l;
  }
  return (unsigned long)-1L;
}


x86 assembly (clang, -O0):
<cstrtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
cmpl   $0xa,-0x14(%rbp)
jne    1153 <cstrtoul+0x33>
mov    -0x10(%rbp),%rdi
call   1040 <atol@plt>
mov    %rax,-0x8(%rbp)
jmp    11b1 <cstrtoul+0x91>
cmpl   $0x10,-0x14(%rbp)
jne    117e <cstrtoul+0x5e>
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sscanf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <cstrtoul+0x91>
cmpl   $0x8,-0x14(%rbp)
jne    11a9 <cstrtoul+0x89>
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sscanf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <cstrtoul+0x91>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstrtoul>:
push   %rax
movq   $0xffffffffffffffff,(%rsp)
ff
cmp    $0x8,%esi
je     1145 <cstrtoul+0x25>
cmp    $0x10,%esi
je     114e <cstrtoul+0x2e>
mov    $0xffffffffffffffff,%rax
cmp    $0xa,%esi
jne    1161 <cstrtoul+0x41>
pop    %rax
jmp    1040 <atol@plt>
lea    0xeb8(%rip),%rsi
jmp    1155 <cstrtoul+0x35>
lea    0xeab(%rip),%rsi
mov    %rsp,%rdx
call   1030 <sscanf@plt>
mov    (%rsp),%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lx\00"
@.str.1 = [4 x i8] c"%lo\00"
define i64 @cstrtoul(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 -1, i64* %6
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 10
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i64 @atol(i8* %10)
store i64 %11, i64* %3
br label %27
12: ; preds = %2
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 16
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %6)
%18 = load i64, i64* %6
store i64 %18, i64* %3
br label %27
19: ; preds = %12
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 8
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8*, i8** %4
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%25 = load i64, i64* %6
store i64 %25, i64* %3
br label %27
26: ; preds = %19
store i64 -1, i64* %3
br label %27
27: ; preds = %26, %22, %15, %9
%28 = load i64, i64* %3
ret i64 %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lx\00"
@.str.1 = [4 x i8] c"%lo\00"
define i64 @cstrtoul(i8* nocapture %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 -1, i64* %3
switch i32 %1, label %13 [
i32 10, label %5
i32 16, label %7
i32 8, label %10
]
5: ; preds = %2
%6 = tail call i64 @atol(i8* %0)
br label %13
7: ; preds = %2
%8 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %3)
%9 = load i64, i64* %3
br label %13
10: ; preds = %2
%11 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%12 = load i64, i64* %3
br label %13
13: ; preds = %2, %10, %7, %5
%14 = phi i64 [ %6, %5 ], [ %9, %7 ], [ %12, %10 ], [ -1, %2 ]
ret i64 %14
}