#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 */
scalar_t__ isdigit(char);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long long *);
int strlen(char *);
long long unsigned parse_arg_size(char *str) {
  long long unsigned result = 0;
  int len = strlen(str);
  sscanf(str, "%llu", &result);
  char suffix = *(str + len - 1);
  if (!isdigit(suffix) && (suffix == 'k' || suffix == 'm' || suffix == 'g')) {
    len -= 1;
  }
  for (int i = 0; i < len; i++) {
    if (!isdigit(*(str + i))) {
      return 0;
    }
  }
  if (isdigit(suffix))
    return result;
  switch (suffix) {
  case 'k':
    result *= 1024;
    break;
  case 'm':
    result *= 1024 * 1024;
    break;
  case 'g':
    result *= 1024 * 1024 * 1024;
    break;
  default:
    result = 0;
    break;
  }
  return result;
}


x86 assembly (clang, -O0):
<parse_arg_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    -0x1(%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    11b6 <parse_arg_size+0x86>
movsbl -0x1d(%rbp),%eax
cmp    $0x6b,%eax
je     11ad <parse_arg_size+0x7d>
movsbl -0x1d(%rbp),%eax
cmp    $0x6d,%eax
je     11ad <parse_arg_size+0x7d>
movsbl -0x1d(%rbp),%eax
cmp    $0x67,%eax
jne    11b6 <parse_arg_size+0x86>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1204 <parse_arg_size+0xd4>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    11f1 <parse_arg_size+0xc1>
movq   $0x0,-0x8(%rbp)
jmp    129e <parse_arg_size+0x16e>
jmp    11f6 <parse_arg_size+0xc6>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bd <parse_arg_size+0x8d>
movsbl -0x1d(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1224 <parse_arg_size+0xf4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    129e <parse_arg_size+0x16e>
movsbl -0x1d(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x67,%eax
je     127d <parse_arg_size+0x14d>
jmp    1239 <parse_arg_size+0x109>
mov    -0x28(%rbp),%eax
sub    $0x6b,%eax
je     125b <parse_arg_size+0x12b>
jmp    124a <parse_arg_size+0x11a>
mov    -0x28(%rbp),%eax
sub    $0x6d,%eax
je     126c <parse_arg_size+0x13c>
jmp    128e <parse_arg_size+0x15e>
mov    -0x18(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x18(%rbp)
jmp    1296 <parse_arg_size+0x166>
mov    -0x18(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x18(%rbp)
jmp    1296 <parse_arg_size+0x166>
mov    -0x18(%rbp),%rax
shl    $0x1e,%rax
mov    %rax,-0x18(%rbp)
jmp    1296 <parse_arg_size+0x166>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_arg_size>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xeaa(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
call   1050 <sscanf@plt>
movslq %ebp,%rax
movsbl -0x1(%r12,%rax,1),%r14d
mov    %r14d,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    1191 <parse_arg_size+0x61>
lea    -0x67(%r14),%eax
cmp    $0x6,%al
ja     1191 <parse_arg_size+0x61>
movzbl %al,%eax
mov    $0x51,%ecx
bt     %eax,%ecx
jae    1191 <parse_arg_size+0x61>
add    $0xffffffff,%ebp
test   %ebp,%ebp
jle    11b8 <parse_arg_size+0x88>
mov    %ebp,%ebp
xor    %r15d,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r12,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11ca <parse_arg_size+0x9a>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <parse_arg_size+0x70>
mov    %r14d,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11da <parse_arg_size+0xaa>
mov    0x8(%rsp),%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x67,%r14d
je     11f7 <parse_arg_size+0xc7>
cmp    $0x6d,%r14d
je     1202 <parse_arg_size+0xd2>
cmp    $0x6b,%r14d
jne    120d <parse_arg_size+0xdd>
mov    0x8(%rsp),%r15
shl    $0xa,%r15
jmp    11ca <parse_arg_size+0x9a>
mov    0x8(%rsp),%r15
shl    $0x1e,%r15
jmp    11ca <parse_arg_size+0x9a>
mov    0x8(%rsp),%r15
shl    $0x14,%r15
jmp    11ca <parse_arg_size+0x9a>
xor    %r15d,%r15d
jmp    11ca <parse_arg_size+0x9a>

LLVM IR (-O0):
@.str = [5 x i8] c"%llu\00"
define i64 @parse_arg_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
store i64 0, i64* %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i8*, i8** %3
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %4)
%12 = load i8*, i8** %3
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = getelementptr inbounds i8, i8* %15, i64 -1
%17 = load i8, i8* %16
store i8 %17, i8* %6
%18 = load i8, i8* %6
%19 = call i64 @isdigit(i8 signext %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %36, label %21
21: ; preds = %1
%22 = load i8, i8* %6
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 107
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = load i8, i8* %6
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 109
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i8, i8* %6
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 103
br i1 %32, label %33, label %36
33: ; preds = %29, %25, %21
%34 = load i32, i32* %5
%35 = sub nsw i32 %34, 1
store i32 %35, i32* %5
br label %36
36: ; preds = %33, %29, %1
store i32 0, i32* %7
br label %37
37: ; preds = %51, %36
%38 = load i32, i32* %7
%39 = load i32, i32* %5
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %54
41: ; preds = %37
%42 = load i8*, i8** %3
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = call i64 @isdigit(i8 signext %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %41
store i64 0, i64* %2
br label %75
50: ; preds = %41
br label %51
51: ; preds = %50
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %37
54: ; preds = %37
%55 = load i8, i8* %6
%56 = call i64 @isdigit(i8 signext %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = load i64, i64* %4
store i64 %59, i64* %2
br label %75
60: ; preds = %54
%61 = load i8, i8* %6
%62 = sext i8 %61 to i32
switch i32 %62, label %72 [
i32 107, label %63
i32 109, label %66
i32 103, label %69
]
63: ; preds = %60
%64 = load i64, i64* %4
%65 = mul i64 %64, 1024
store i64 %65, i64* %4
br label %73
66: ; preds = %60
%67 = load i64, i64* %4
%68 = mul i64 %67, 1048576
store i64 %68, i64* %4
br label %73
69: ; preds = %60
%70 = load i64, i64* %4
%71 = mul i64 %70, 1073741824
store i64 %71, i64* %4
br label %73
72: ; preds = %60
store i64 0, i64* %4
br label %73
73: ; preds = %72, %69, %66, %63
%74 = load i64, i64* %4
store i64 %74, i64* %2
br label %75
75: ; preds = %73, %58, %49
%76 = load i64, i64* %2
ret i64 %76
}

LLVM IR (-O2):
@.str = [5 x i8] c"%llu\00"
define i64 @parse_arg_size(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%4 = tail call i32 @strlen(i8* %0)
%5 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
%6 = sext i32 %4 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 -1
%9 = load i8, i8* %8
%10 = call i64 @isdigit(i8 signext %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
switch i8 %9, label %15 [
i8 109, label %13
i8 107, label %13
i8 103, label %13
]
13: ; preds = %12, %12, %12
%14 = add nsw i32 %4, -1
br label %15
15: ; preds = %12, %13, %1
%16 = phi i32 [ %4, %1 ], [ %14, %13 ], [ %4, %12 ]
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = zext i32 %16 to i64
br label %22
20: ; preds = %22
%21 = icmp eq i64 %28, %19
br i1 %21, label %29, label %22
22: ; preds = %18, %20
%23 = phi i64 [ 0, %18 ], [ %28, %20 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = call i64 @isdigit(i8 signext %25)
%27 = icmp eq i64 %26, 0
%28 = add nuw nsw i64 %23, 1
br i1 %27, label %45, label %20
29: ; preds = %20, %15
%30 = call i64 @isdigit(i8 signext %9)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = load i64, i64* %2
br label %45
34: ; preds = %29
%35 = sext i8 %9 to i32
switch i32 %35, label %45 [
i32 107, label %36
i32 109, label %39
i32 103, label %42
]
36: ; preds = %34
%37 = load i64, i64* %2
%38 = shl i64 %37, 10
br label %45
39: ; preds = %34
%40 = load i64, i64* %2
%41 = shl i64 %40, 20
br label %45
42: ; preds = %34
%43 = load i64, i64* %2
%44 = shl i64 %43, 30
br label %45
45: ; preds = %22, %34, %36, %39, %42, %32
%46 = phi i64 [ %33, %32 ], [ %44, %42 ], [ %41, %39 ], [ %38, %36 ], [ 0, %34 ], [ 0, %22 ]
ret i64 %46
}