#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;
  }
  return result * ((2 << ((10 * exp) - 1)) ?: 1);
}


x86 assembly (clang, -O0):
<to_kbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    126c <to_kbytes+0x13c>
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    126c <to_kbytes+0x13c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
imul   $0xa,-0x14(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x2,%eax
shl    %cl,%eax
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     124d <to_kbytes+0x11d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    125a <to_kbytes+0x12a>
mov    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    125a <to_kbytes+0x12a>
mov    -0x38(%rbp),%rax
mov    -0x3c(%rbp),%ecx
movslq %ecx,%rcx
imul   %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_kbytes>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
xor    %ebx,%ebx
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,%r14
mov    0x8(%rsp),%r15
movsbl (%r15),%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    11c3 <to_kbytes+0x93>
mov    $0x13,%ecx
jmp    1183 <to_kbytes+0x53>
cmp    $0x6b,%eax
je     1185 <to_kbytes+0x55>
cmp    $0x6d,%eax
jne    11c3 <to_kbytes+0x93>
mov    $0x9,%ecx
xor    %edx,%edx
cmpb   $0x0,(%r15)
je     11ab <to_kbytes+0x7b>
cmpb   $0x0,0x1(%r15)
je     11ab <to_kbytes+0x7b>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1030 <fprintf@plt>
xor    %ebx,%ebx
jmp    11c3 <to_kbytes+0x93>
mov    $0x2,%eax
shl    %cl,%eax
cltq
test   %dl,%dl
mov    $0x1,%ebx
cmove  %rax,%rbx
imul   %r14,%rbx
mov    %rbx,%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 i64, i64* %5
%32 = load i32, i32* %4
%33 = mul nsw i32 10, %32
%34 = sub nsw i32 %33, 1
%35 = shl i32 2, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
br label %39
38: ; preds = %30
br label %39
39: ; preds = %38, %37
%40 = phi i32 [ %35, %37 ], [ 1, %38 ]
%41 = sext i32 %40 to i64
%42 = mul nsw i64 %31, %41
store i64 %42, i64* %2
br label %43
43: ; preds = %39, %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
%23 = shl nuw nsw i32 2, %12
%24 = sext i32 %23 to i64
%25 = select i1 %11, i64 1, i64 %24
%26 = mul nsw i64 %25, %4
br label %27
27: ; preds = %1, %22, %19
%28 = phi i64 [ 0, %19 ], [ %26, %22 ], [ 0, %1 ]
ret i64 %28
}