#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 */
typedef int uint64_t;

/* Variables and functions */
scalar_t__ strcmp(char *, char *);
int strtoull(char *, char **, int /*<<< orphan*/);
uint64_t str_to_num(char *str) {
  char *s = str;
  uint64_t num = strtoull(s, &s, 0);
  if (*s == '\0')
    return num;
  if (strcmp(s, "KiB") == 0)
    num *= 1024;
  else if (strcmp(s, "MiB") == 0)
    num *= 1024 * 1024;
  else if (strcmp(s, "GiB") == 0)
    num *= 1024 * 1024 * 1024;
  return num;
}


x86 assembly (clang, -O0):
<str_to_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1161 <str_to_num+0x41>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e4 <str_to_num+0xc4>
mov    -0x18(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1189 <str_to_num+0x69>
mov    -0x1c(%rbp),%eax
shl    $0xa,%eax
mov    %eax,-0x1c(%rbp)
jmp    11de <str_to_num+0xbe>
mov    -0x18(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11b1 <str_to_num+0x91>
mov    -0x1c(%rbp),%eax
shl    $0x14,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d9 <str_to_num+0xb9>
mov    -0x18(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11d4 <str_to_num+0xb4>
mov    -0x1c(%rbp),%eax
shl    $0x1e,%eax
mov    %eax,-0x1c(%rbp)
jmp    11d9 <str_to_num+0xb9>
jmp    11de <str_to_num+0xbe>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_num>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,(%rsp)
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <strtoull@plt>
mov    %eax,%ebp
mov    (%rsp),%rbx
cmpb   $0x0,(%rbx)
je     1188 <str_to_num+0x68>
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1180 <str_to_num+0x60>
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     1185 <str_to_num+0x65>
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
mov    %ebp,%ecx
shl    $0x1e,%ecx
test   %rax,%rax
cmove  %ecx,%ebp
jmp    1188 <str_to_num+0x68>
shl    $0xa,%ebp
jmp    1188 <str_to_num+0x68>
shl    $0x14,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"KiB\00"
@.str.1 = [4 x i8] c"MiB\00"
@.str.2 = [4 x i8] c"GiB\00"
define i32 @str_to_num(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strtoull(i8* %7, i8** %4, i32 0)
store i32 %8, i32* %5
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %5
store i32 %14, i32* %2
br label %40
15: ; preds = %1
%16 = load i8*, i8** %4
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %5
%21 = mul nsw i32 %20, 1024
store i32 %21, i32* %5
br label %38
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %5
%28 = mul nsw i32 %27, 1048576
store i32 %28, i32* %5
br label %37
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* %5
%35 = mul nsw i32 %34, 1073741824
store i32 %35, i32* %5
br label %36
36: ; preds = %33, %29
br label %37
37: ; preds = %36, %26
br label %38
38: ; preds = %37, %19
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %13
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [4 x i8] c"KiB\00"
@.str.1 = [4 x i8] c"MiB\00"
@.str.2 = [4 x i8] c"GiB\00"
define i32 @str_to_num(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i8* %0, i8** %2
%4 = call i32 @strtoull(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %1
%9 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = shl nsw i32 %4, 10
br label %23
13: ; preds = %8
%14 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = shl nsw i32 %4, 20
br label %23
18: ; preds = %13
%19 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
%21 = shl nsw i32 %4, 30
%22 = select i1 %20, i32 %21, i32 %4
br label %23
23: ; preds = %18, %11, %16, %1
%24 = phi i32 [ %4, %1 ], [ %12, %11 ], [ %17, %16 ], [ %22, %18 ]
ret i32 %24
}