#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__ ERANGE;
char *LLONG_MAX;
long long checked_multiply(long long, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcasecmp(char *, char *);
long long strtoll(char const *, char **, int /*<<< orphan*/);
long long parse_size(const char *str) {
  char *ptr;
  long long size;
  errno = 0;
  size = strtoll(str, &ptr, 0);
  if (errno == ERANGE || size < 0) {
    fprintf(stderr, "Size out of range; it must be within 0 and %lld bytes.\n",
            LLONG_MAX);
    return -1;
  }
  if (errno || ptr == str) {
    fprintf(stderr, "Invalid size specified.\n");
    return -1;
  }
  if (!strcasecmp(ptr, "k"))
    size = checked_multiply(size, 1);
  else if (!strcasecmp(ptr, "m"))
    size = checked_multiply(size, 2);
  else if (!strcasecmp(ptr, "g"))
    size = checked_multiply(size, 3);
  else if (*ptr) {
    fprintf(stderr, "Invalid suffix \"%s\" specified.\n", ptr);
    return -1;
  }
  if (size == -1) {
    fprintf(stderr, "Size out of range; it must be within 0 and %lld bytes.\n",
            LLONG_MAX);
    return -1;
  }
  return size;
}


x86 assembly (clang, -O0):
<parse_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoll@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
je     1192 <parse_size+0x52>
cmpq   $0x0,-0x20(%rbp)
jge    11c0 <parse_size+0x80>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%rdx
lea    0xe54(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1318 <parse_size+0x1d8>
mov    0x2e01(%rip),%rax
cmpq   $0x0,(%rax)
jne    11df <parse_size+0x9f>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1203 <parse_size+0xc3>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1318 <parse_size+0x1d8>
mov    -0x18(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1233 <parse_size+0xf3>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1040 <checked_multiply@plt>
mov    %rax,-0x20(%rbp)
jmp    12d7 <parse_size+0x197>
mov    -0x18(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1263 <parse_size+0x123>
mov    -0x20(%rbp),%rdi
mov    $0x2,%esi
call   1040 <checked_multiply@plt>
mov    %rax,-0x20(%rbp)
jmp    12d2 <parse_size+0x192>
mov    -0x18(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1293 <parse_size+0x153>
mov    -0x20(%rbp),%rdi
mov    $0x3,%esi
call   1040 <checked_multiply@plt>
mov    %rax,-0x20(%rbp)
jmp    12cd <parse_size+0x18d>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     12c8 <parse_size+0x188>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1318 <parse_size+0x1d8>
jmp    12cd <parse_size+0x18d>
jmp    12d2 <parse_size+0x192>
jmp    12d7 <parse_size+0x197>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1310 <parse_size+0x1d0>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%rdx
lea    0xd04(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1318 <parse_size+0x1d8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_size>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e75(%rip),%r14
movq   $0x0,(%r14)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoll@plt>
mov    %rax,%r15
mov    (%r14),%rax
mov    0x2e75(%rip),%rcx
cmp    (%rcx),%rax
je     1228 <parse_size+0xe8>
test   %r15,%r15
js     1228 <parse_size+0xe8>
test   %rax,%rax
je     11a6 <parse_size+0x66>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1249 <parse_size+0x109>
mov    0x8(%rsp),%r14
cmp    %rbx,%r14
je     118a <parse_size+0x4a>
lea    0xe9a(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     1204 <parse_size+0xc4>
lea    0xe89(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     120b <parse_size+0xcb>
lea    0xe78(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     1212 <parse_size+0xd2>
cmpb   $0x0,(%r14)
je     1250 <parse_size+0x110>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    %r14,%rdx
jmp    1242 <parse_size+0x102>
mov    $0x1,%esi
jmp    1217 <parse_size+0xd7>
mov    $0x2,%esi
jmp    1217 <parse_size+0xd7>
mov    $0x3,%esi
mov    %r15,%rdi
call   1040 <checked_multiply@plt>
mov    %rax,%r15
cmp    $0xffffffffffffffff,%rax
jne    1250 <parse_size+0x110>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%rdx
lea    0xdbe(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [56 x i8] c"Size out of range; it must be within 0 and %lld bytes.\0A\00"
@LLONG_MAX = global i8* null
@.str.1 = [25 x i8] c"Invalid size specified.\0A\00"
@.str.2 = [2 x i8] c"k\00"
@.str.3 = [2 x i8] c"m\00"
@.str.4 = [2 x i8] c"g\00"
@.str.5 = [32 x i8] c"Invalid suffix \22%s\22 specified.\0A\00"
define i64 @parse_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* @errno
%6 = load i8*, i8** %3
%7 = call i64 @strtoll(i8* %6, i8** %4, i32 0)
store i64 %7, i64* %5
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i64, i64* %5
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11, %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** @LLONG_MAX
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %16)
store i64 -1, i64* %2
br label %69
18: ; preds = %11
%19 = load i64, i64* @errno
%20 = icmp ne i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21, %18
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %2
br label %69
28: ; preds = %21
%29 = load i8*, i8** %4
%30 = call i32 @strcasecmp(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i64, i64* %5
%34 = call i64 @checked_multiply(i64 %33, i32 1)
store i64 %34, i64* %5
br label %60
35: ; preds = %28
%36 = load i8*, i8** %4
%37 = call i32 @strcasecmp(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = icmp ne i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = load i64, i64* %5
%41 = call i64 @checked_multiply(i64 %40, i32 2)
store i64 %41, i64* %5
br label %59
42: ; preds = %35
%43 = load i8*, i8** %4
%44 = call i32 @strcasecmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp ne i32 %44, 0
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = load i64, i64* %5
%48 = call i64 @checked_multiply(i64 %47, i32 3)
store i64 %48, i64* %5
br label %58
49: ; preds = %42
%50 = load i8*, i8** %4
%51 = load i8, i8* %50
%52 = icmp ne i8 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %4
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %55)
store i64 -1, i64* %2
br label %69
57: ; preds = %49
br label %58
58: ; preds = %57, %46
br label %59
59: ; preds = %58, %39
br label %60
60: ; preds = %59, %32
%61 = load i64, i64* %5
%62 = icmp eq i64 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = load i8*, i8** @LLONG_MAX
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %65)
store i64 -1, i64* %2
br label %69
67: ; preds = %60
%68 = load i64, i64* %5
store i64 %68, i64* %2
br label %69
69: ; preds = %67, %63, %53, %25, %14
%70 = load i64, i64* %2
ret i64 %70
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@stderr = global i32 0
@.str = [56 x i8] c"Size out of range; it must be within 0 and %lld bytes.\0A\00"
@LLONG_MAX = global i8* null
@.str.1 = [25 x i8] c"Invalid size specified.\0A\00"
@.str.2 = [2 x i8] c"k\00"
@.str.3 = [2 x i8] c"m\00"
@.str.4 = [2 x i8] c"g\00"
@.str.5 = [32 x i8] c"Invalid suffix \22%s\22 specified.\0A\00"
define i64 @parse_size(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
store i64 0, i64* @errno
%4 = call i64 @strtoll(i8* %0, i8** %2, i32 0)
%5 = load i64, i64* @errno
%6 = load i64, i64* @ERANGE
%7 = icmp eq i64 %5, %6
%8 = icmp slt i64 %4, 0
%9 = select i1 %7, i1 true, i1 %8
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** @LLONG_MAX
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %45
14: ; preds = %1
%15 = icmp eq i64 %5, 0
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = load i8*, i8** %2
%18 = icmp eq i8* %17, %0
br i1 %18, label %19, label %22
19: ; preds = %16, %14
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %45
22: ; preds = %16
%23 = tail call i32 @strcasecmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = tail call i32 @strcasecmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %37, label %28
28: ; preds = %25
%29 = tail call i32 @strcasecmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load i8, i8* %17
%33 = icmp eq i8 %32, 0
br i1 %33, label %45, label %34
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i8* %17)
br label %45
37: ; preds = %28, %25, %22
%38 = phi i32 [ 1, %22 ], [ 2, %25 ], [ 3, %28 ]
%39 = tail call i64 @checked_multiply(i64 %4, i32 %38)
%40 = icmp eq i64 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i8*, i8** @LLONG_MAX
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %31, %37, %41, %34, %19, %10
%46 = phi i64 [ -1, %10 ], [ -1, %19 ], [ -1, %34 ], [ -1, %41 ], [ %39, %37 ], [ %4, %31 ]
ret i64 %46
}