#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 */
double HUGE_VAL;
double INT64_MAX;
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 || val == HUGE_VAL || *tp != '\0')
    fatal(errstr);
  val *= mult;
  if (val > INT64_MAX)
    fatal(errstr);
  return (int64_t)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
ja     128a <parse_size+0x13a>
movsd  -0x30(%rbp),%xmm0
mov    0x2d83(%rip),%rax
ucomisd (%rax),%xmm0
jne    127a <parse_size+0x12a>
jp     127a <parse_size+0x12a>
jmp    128a <parse_size+0x13a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1295 <parse_size+0x145>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
cvtsi2sdl -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
mov    0x2d30(%rip),%rax
ucomisd (%rax),%xmm0
jbe    12c5 <parse_size+0x175>
mov    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1070 <fatal@plt>
cvttsd2si -0x30(%rbp),%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
ja     1207 <parse_size+0xb7>
mov    0x2df3(%rip),%rax
ucomisd (%rax),%xmm0
jne    11fd <parse_size+0xad>
jnp    1207 <parse_size+0xb7>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     121f <parse_size+0xcf>
lea    0xdf2(%rip),%rdi
xor    %eax,%eax
movsd  %xmm0,(%rsp)
call   1070 <fatal@plt>
movsd  (%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
mulsd  %xmm1,%xmm0
mov    0x2daf(%rip),%rax
ucomisd (%rax),%xmm0
jbe    124f <parse_size+0xff>
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
movsd  %xmm0,(%rsp)
call   1070 <fatal@plt>
movsd  (%rsp),%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"
@HUGE_VAL = global double 0.000000e+00
@INT64_MAX = global double 0.000000e+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 olt double %46, 0.000000e+00
br i1 %47, label %57, label %48
48: ; preds = %43
%49 = load double, double* %8
%50 = load double, double* @HUGE_VAL
%51 = fcmp oeq double %49, %50
br i1 %51, label %57, label %52
52: ; preds = %48
%53 = load i8*, i8** %3
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %52, %48, %43
%58 = load i8*, i8** %5
%59 = call i32 (i8*, ...) @fatal(i8* %58)
br label %60
60: ; preds = %57, %52
%61 = load i32, i32* %7
%62 = sitofp i32 %61 to double
%63 = load double, double* %8
%64 = fmul double %63, %62
store double %64, double* %8
%65 = load double, double* %8
%66 = load double, double* @INT64_MAX
%67 = fcmp ogt double %65, %66
br i1 %67, label %68, label %71
68: ; preds = %60
%69 = load i8*, i8** %5
%70 = call i32 (i8*, ...) @fatal(i8* %69)
br label %71
71: ; preds = %68, %60
%72 = load double, double* %8
%73 = fptosi double %72 to i32
ret i32 %73
}

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"
@HUGE_VAL = global double 0.000000e+00
@INT64_MAX = global double 0.000000e+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 olt double %26, 0.000000e+00
%28 = load double, double* @HUGE_VAL
%29 = fcmp oeq double %26, %28
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %35, label %31
31: ; preds = %24
%32 = load i8*, i8** %2
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31, %24
%36 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = sitofp i32 %25 to double
%39 = fmul double %26, %38
%40 = load double, double* @INT64_MAX
%41 = fcmp ogt double %39, %40
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = fptosi double %39 to i32
ret i32 %45
}