unsigned long int xstrtoul(nptr, base)
char *nptr;
const int base;
{
  char *endptr;
  unsigned long int retval;
  retval = strtoul(nptr, &endptr, base);
  switch (*endptr) {
  case 'k':
  case 'K':
    retval *= 1024;
    break;
  case 'm':
  case 'M':
    retval *= (1024 * 1024);
    break;
  case 'g':
  case 'G':
    retval *= ((1024 * 1024) * 1024);
    break;
  default:
    break;
  }
  return (retval);
}


x86 assembly (clang, -O0):
<xstrtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rsi
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x47,%eax
je     11c2 <xstrtoul+0xb2>
jmp    114b <xstrtoul+0x3b>
mov    -0x24(%rbp),%eax
sub    $0x4b,%eax
je     11a0 <xstrtoul+0x90>
jmp    115c <xstrtoul+0x4c>
mov    -0x24(%rbp),%eax
sub    $0x4d,%eax
je     11b1 <xstrtoul+0xa1>
jmp    116d <xstrtoul+0x5d>
mov    -0x24(%rbp),%eax
sub    $0x67,%eax
je     11c2 <xstrtoul+0xb2>
jmp    117e <xstrtoul+0x6e>
mov    -0x24(%rbp),%eax
sub    $0x6b,%eax
je     11a0 <xstrtoul+0x90>
jmp    118f <xstrtoul+0x7f>
mov    -0x24(%rbp),%eax
sub    $0x6d,%eax
je     11b1 <xstrtoul+0xa1>
jmp    11d3 <xstrtoul+0xc3>
mov    -0x20(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x20(%rbp)
jmp    11d8 <xstrtoul+0xc8>
mov    -0x20(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x20(%rbp)
jmp    11d8 <xstrtoul+0xc8>
mov    -0x20(%rbp),%rax
shl    $0x1e,%rax
mov    %rax,-0x20(%rbp)
jmp    11d8 <xstrtoul+0xc8>
jmp    11d8 <xstrtoul+0xc8>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrtoul>:
push   %rax
mov    %esi,%edx
mov    %rsp,%rsi
call   1030 <strtoul@plt>
mov    (%rsp),%rcx
movsbl (%rcx),%ecx
add    $0xffffffb9,%ecx
cmp    $0x26,%ecx
ja     1164 <xstrtoul+0x54>
movabs $0x100000001,%rdx
bt     %rcx,%rdx
jb     1160 <xstrtoul+0x50>
movabs $0x1000000010,%rdx
bt     %rcx,%rdx
jb     1166 <xstrtoul+0x56>
movabs $0x4000000040,%rdx
bt     %rcx,%rdx
jae    1164 <xstrtoul+0x54>
shl    $0x14,%rax
pop    %rcx
ret
shl    $0x1e,%rax
pop    %rcx
ret
shl    $0xa,%rax
pop    %rcx
ret

LLVM IR (-O0):
define i64 @xstrtoul(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = call i64 @strtoul(i8* %7, i8** %5, i32 %8)
store i64 %9, i64* %6
%10 = load i8*, i8** %5
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
switch i32 %12, label %22 [
i32 107, label %13
i32 75, label %13
i32 109, label %16
i32 77, label %16
i32 103, label %19
i32 71, label %19
]
13: ; preds = %2, %2
%14 = load i64, i64* %6
%15 = mul i64 %14, 1024
store i64 %15, i64* %6
br label %23
16: ; preds = %2, %2
%17 = load i64, i64* %6
%18 = mul i64 %17, 1048576
store i64 %18, i64* %6
br label %23
19: ; preds = %2, %2
%20 = load i64, i64* %6
%21 = mul i64 %20, 1073741824
store i64 %21, i64* %6
br label %23
22: ; preds = %2
br label %23
23: ; preds = %22, %19, %16, %13
%24 = load i64, i64* %6
ret i64 %24
}

LLVM IR (-O2):
define i64 @xstrtoul(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @strtoul(i8* %0, i8** %3, i32 %1)
%6 = load i8*, i8** %3
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
switch i32 %8, label %15 [
i32 107, label %9
i32 75, label %9
i32 109, label %11
i32 77, label %11
i32 103, label %13
i32 71, label %13
]
9: ; preds = %2, %2
%10 = shl i64 %5, 10
br label %15
11: ; preds = %2, %2
%12 = shl i64 %5, 20
br label %15
13: ; preds = %2, %2
%14 = shl i64 %5, 30
br label %15
15: ; preds = %2, %13, %11, %9
%16 = phi i64 [ %5, %2 ], [ %14, %13 ], [ %12, %11 ], [ %10, %9 ]
ret i64 %16
}