#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
long strtoul(char const *, char **, int /*<<< orphan*/);
int tolower(char);
long to_kbytes(const char *string) {
  int exp = 0;
  long result;
  char *end;
  result = strtoul(string, &end, 0);
  switch (tolower(*end)) {
  case 'k':
  case '\0':
    exp = 0;
    break;
  case 'm':
    exp = 1;
    break;
  case 'g':
    exp = 2;
    break;
  default:
    return 0;
  }
  if (*end)
    end++;
  if (*end) {
    fprintf(stderr, "garbage after end of number\n");
    return 0;
  }
  if (exp == 0)
    return result;
  return result * (2 << ((10 * exp) - 1));
}


x86 assembly (clang, -O0):
<to_kbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1040 <tolower@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
test   %eax,%eax
je     11a7 <to_kbytes+0x77>
jmp    1174 <to_kbytes+0x44>
mov    -0x2c(%rbp),%eax
sub    $0x67,%eax
je     11bf <to_kbytes+0x8f>
jmp    1185 <to_kbytes+0x55>
mov    -0x2c(%rbp),%eax
sub    $0x6b,%eax
je     11a7 <to_kbytes+0x77>
jmp    1196 <to_kbytes+0x66>
mov    -0x2c(%rbp),%eax
sub    $0x6d,%eax
je     11b3 <to_kbytes+0x83>
jmp    11cb <to_kbytes+0x9b>
movl   $0x0,-0x14(%rbp)
jmp    11d8 <to_kbytes+0xa8>
movl   $0x1,-0x14(%rbp)
jmp    11d8 <to_kbytes+0xa8>
movl   $0x2,-0x14(%rbp)
jmp    11d8 <to_kbytes+0xa8>
movq   $0x0,-0x8(%rbp)
jmp    1256 <to_kbytes+0x126>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f1 <to_kbytes+0xc1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1220 <to_kbytes+0xf0>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1256 <to_kbytes+0x126>
cmpl   $0x0,-0x14(%rbp)
jne    1237 <to_kbytes+0x107>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1256 <to_kbytes+0x126>
mov    -0x20(%rbp),%rax
imul   $0xa,-0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x2,%edx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
imul   %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_kbytes>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
xor    %r15d,%r15d
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rbx
movsbl (%rbx),%edi
call   1040 <tolower@plt>
mov    $0xffffffff,%ecx
mov    $0x1,%dl
cmp    $0x6a,%eax
jg     1174 <to_kbytes+0x44>
test   %eax,%eax
je     1185 <to_kbytes+0x55>
cmp    $0x67,%eax
jne    11bf <to_kbytes+0x8f>
mov    $0x13,%ecx
jmp    1183 <to_kbytes+0x53>
cmp    $0x6b,%eax
je     1185 <to_kbytes+0x55>
cmp    $0x6d,%eax
jne    11bf <to_kbytes+0x8f>
mov    $0x9,%ecx
xor    %edx,%edx
cmpb   $0x0,(%rbx)
je     11aa <to_kbytes+0x7a>
cmpb   $0x0,0x1(%rbx)
je     11aa <to_kbytes+0x7a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1030 <fprintf@plt>
xor    %r15d,%r15d
jmp    11bf <to_kbytes+0x8f>
mov    %r14,%r15
test   %dl,%dl
jne    11bf <to_kbytes+0x8f>
mov    $0x2,%eax
shl    %cl,%eax
imul   %rax,%r14
mov    %r14,%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"garbage after end of number\0A\00"
define i64 @to_kbytes(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = call i64 @strtoul(i8* %7, i8** %6, i32 0)
store i64 %8, i64* %5
%9 = load i8*, i8** %6
%10 = load i8, i8* %9
%11 = call i32 @tolower(i8 signext %10)
switch i32 %11, label %15 [
i32 107, label %12
i32 0, label %12
i32 109, label %13
i32 103, label %14
]
12: ; preds = %1, %1
store i32 0, i32* %4
br label %16
13: ; preds = %1
store i32 1, i32* %4
br label %16
14: ; preds = %1
store i32 2, i32* %4
br label %16
15: ; preds = %1
store i64 0, i64* %2
br label %43
16: ; preds = %14, %13, %12
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
br label %23
23: ; preds = %20, %16
%24 = load i8*, i8** %6
%25 = load i8, i8* %24
%26 = icmp ne i8 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %2
br label %43
30: ; preds = %23
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load i64, i64* %5
store i64 %34, i64* %2
br label %43
35: ; preds = %30
%36 = load i64, i64* %5
%37 = load i32, i32* %4
%38 = mul nsw i32 10, %37
%39 = sub nsw i32 %38, 1
%40 = shl i32 2, %39
%41 = sext i32 %40 to i64
%42 = mul nsw i64 %36, %41
store i64 %42, i64* %2
br label %43
43: ; preds = %35, %33, %27, %15
%44 = load i64, i64* %2
ret i64 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"garbage after end of number\0A\00"
define i64 @to_kbytes(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtoul(i8* %0, i8** %2, i32 0)
%5 = load i8*, i8** %2
%6 = load i8, i8* %5
%7 = tail call i32 @tolower(i8 signext %6)
switch i32 %7, label %27 [
i32 107, label %10
i32 0, label %10
i32 109, label %8
i32 103, label %9
]
8: ; preds = %1
br label %10
9: ; preds = %1
br label %10
10: ; preds = %1, %1, %9, %8
%11 = phi i1 [ false, %9 ], [ false, %8 ], [ true, %1 ], [ true, %1 ]
%12 = phi i32 [ 19, %9 ], [ 9, %8 ], [ -1, %1 ], [ -1, %1 ]
%13 = load i8, i8* %5
%14 = icmp eq i8 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %5, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %27
22: ; preds = %10, %15
br i1 %11, label %27, label %23
23: ; preds = %22
%24 = shl nuw nsw i32 2, %12
%25 = zext i32 %24 to i64
%26 = mul nsw i64 %4, %25
br label %27
27: ; preds = %22, %1, %23, %19
%28 = phi i64 [ 0, %19 ], [ %26, %23 ], [ 0, %1 ], [ %4, %22 ]
ret i64 %28
}