#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 /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ fatal(char *, ...);
scalar_t__ isalpha(char);
int strlen(char *);
double strtod(char *, char **);
char tolower(char);
int64_t parse_size(char *inp) {
  char *tp;
  int len = strlen(inp);
  char *errstr = "bad size. examples: 4096, 4k, 100M, 1.5G";
  char mult_char;
  int mult = 1;
  double val;
  if (len < 1)
    fatal(errstr);
  mult_char = tolower(inp[len - 1]);
  if (isalpha(mult_char)) {
    switch (mult_char) {
    case 'k':
      mult = 1024;
      break;
    case 'm':
      mult = 1024 * 1024;
      break;
    case 'g':
      mult = 1024 * 1024 * 1024;
      break;
    default:
      fatal("unknown size multiplier: %c", mult_char);
    }
    inp[len - 1] = '\0';
  }
  val = strtod(inp, &tp);
  if (val <= 0 || *tp != '\0')
    fatal(errstr);
  return (int64_t)(mult * val);
}


x86 assembly (clang, -O0):
<parse_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    118f <parse_size+0x3f>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%edi
call   1060 <isalpha@plt>
cmp    $0x0,%rax
je     1239 <parse_size+0xe9>
movsbl -0x21(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x67,%eax
je     120a <parse_size+0xba>
jmp    11d0 <parse_size+0x80>
mov    -0x34(%rbp),%eax
sub    $0x6b,%eax
je     11f2 <parse_size+0xa2>
jmp    11e1 <parse_size+0x91>
mov    -0x34(%rbp),%eax
sub    $0x6d,%eax
je     11fe <parse_size+0xae>
jmp    1216 <parse_size+0xc6>
movl   $0x400,-0x28(%rbp)
jmp    1228 <parse_size+0xd8>
movl   $0x100000,-0x28(%rbp)
jmp    1228 <parse_size+0xd8>
movl   $0x40000000,-0x28(%rbp)
jmp    1228 <parse_size+0xd8>
movsbl -0x21(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strtod@plt>
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x30(%rbp),%xmm0
jae    1269 <parse_size+0x119>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1274 <parse_size+0x124>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
cvtsi2sdl -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1174 <parse_size+0x24>
lea    0xe93(%rip),%rdi
xor    %eax,%eax
call   1070 <fatal@plt>
movslq %ebp,%r14
movsbl -0x1(%r14,%rbx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%ebp
mov    %ebp,%edi
call   1060 <isalpha@plt>
test   %rax,%rax
je     11a7 <parse_size+0x57>
cmp    $0x67,%ebp
je     11ae <parse_size+0x5e>
cmp    $0x6b,%ebp
je     11b5 <parse_size+0x65>
cmp    $0x6d,%ebp
jne    11bc <parse_size+0x6c>
mov    $0x100000,%ebp
jmp    11d1 <parse_size+0x81>
mov    $0x1,%ebp
jmp    11d7 <parse_size+0x87>
mov    $0x40000000,%ebp
jmp    11d1 <parse_size+0x81>
mov    $0x400,%ebp
jmp    11d1 <parse_size+0x81>
lea    0xe66(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <fatal@plt>
mov    $0x1,%ebp
movb   $0x0,-0x1(%rbx,%r14,1)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <strtod@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jae    11f8 <parse_size+0xa8>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     1210 <parse_size+0xc0>
lea    0xe01(%rip),%rdi
xor    %eax,%eax
movsd  %xmm0,(%rsp)
call   1070 <fatal@plt>
movsd  (%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"bad size. examples: 4096, 4k, 100M, 1.5G\00"
@.str.1 = [28 x i8] c"unknown size multiplier: %c\00"
define i32 @parse_size(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca double
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %4
store i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8** %5
store i32 1, i32* %7
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %5
%15 = call i32 (i8*, ...) @fatal(i8* %14)
br label %16
16: ; preds = %13, %1
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i8, i8* %21
%23 = call signext i8 @tolower(i8 signext %22)
store i8 %23, i8* %6
%24 = load i8, i8* %6
%25 = call i64 @isalpha(i8 signext %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %43
27: ; preds = %16
%28 = load i8, i8* %6
%29 = sext i8 %28 to i32
switch i32 %29, label %33 [
i32 107, label %30
i32 109, label %31
i32 103, label %32
]
30: ; preds = %27
store i32 1024, i32* %7
br label %37
31: ; preds = %27
store i32 1048576, i32* %7
br label %37
32: ; preds = %27
store i32 1073741824, i32* %7
br label %37
33: ; preds = %27
%34 = load i8, i8* %6
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %32, %31, %30
%38 = load i8*, i8** %2
%39 = load i32, i32* %4
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %37, %16
%44 = load i8*, i8** %2
%45 = call double @strtod(i8* %44, i8** %3)
store double %45, double* %8
%46 = load double, double* %8
%47 = fcmp ole double %46, 0.000000e+00
br i1 %47, label %53, label %48
48: ; preds = %43
%49 = load i8*, i8** %3
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48, %43
%54 = load i8*, i8** %5
%55 = call i32 (i8*, ...) @fatal(i8* %54)
br label %56
56: ; preds = %53, %48
%57 = load i32, i32* %7
%58 = sitofp i32 %57 to double
%59 = load double, double* %8
%60 = fmul double %58, %59
%61 = fptosi double %60 to i32
ret i32 %61
}

LLVM IR (-O2):
@.str = [41 x i8] c"bad size. examples: 4096, 4k, 100M, 1.5G\00"
@.str.1 = [28 x i8] c"unknown size multiplier: %c\00"
define i32 @parse_size(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = add nsw i32 %4, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call signext i8 @tolower(i8 signext %12)
%14 = tail call i64 @isalpha(i8 signext %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %8
%17 = sext i8 %13 to i32
switch i32 %17, label %20 [
i32 107, label %22
i32 109, label %18
i32 103, label %19
]
18: ; preds = %16
br label %22
19: ; preds = %16
br label %22
20: ; preds = %16
%21 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %22
22: ; preds = %16, %20, %19, %18
%23 = phi i32 [ 1, %20 ], [ 1073741824, %19 ], [ 1048576, %18 ], [ 1024, %16 ]
store i8 0, i8* %11
br label %24
24: ; preds = %22, %8
%25 = phi i32 [ %23, %22 ], [ 1, %8 ]
%26 = call double @strtod(i8* %0, i8** %2)
%27 = fcmp ugt double %26, 0.000000e+00
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i8*, i8** %2
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28, %24
%33 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = sitofp i32 %25 to double
%36 = fmul double %26, %35
%37 = fptosi double %36 to i32
ret i32 %37
}