#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int rm_so;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ errbuf;

/* Variables and functions */
int REG_EXTENDED;
int REG_ICASE;
int REG_NOMATCH;
scalar_t__ atol(char const *);
int /*<<< orphan*/ err(char *, char const *, char const *, ...);
int regcomp(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ regerror(int, int /*<<< orphan*/ *, char *, int);
int regexec(int /*<<< orphan*/ *, char const *, int, TYPE_1__ *,
            int /*<<< orphan*/);
char tolower(char const);
int parse_size(const char *str, size_t *r) {
  regex_t ex;
  int e;
  static const char *pattern =
      "^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$";
  if ((e = regcomp(&ex, pattern, REG_EXTENDED | REG_ICASE)) != 0) {
    char errbuf[128];
    regerror(e, &ex, errbuf, sizeof(errbuf) - 1);
    errbuf[sizeof(errbuf) - 1] = '\0';
    err("regcomp(%s): %s\n", pattern, errbuf);
    return -1;
  }
  regmatch_t matches[3];
  if ((e = regexec(&ex, str, 3, matches, 0)) != 0) {
    char errbuf[128];
    regerror(e, &ex, errbuf, sizeof(errbuf) - 1);
    errbuf[sizeof(errbuf) - 1] = '\0';
    if (e != REG_NOMATCH)
      err("regexec: Couldn't match '%s' in '%s': %s\n", pattern, str, errbuf);
    return -1;
  }
  *r = (size_t)atol(str + matches[1].rm_so) *
       (matches[2].rm_so == -1                      ? 1
        : tolower(*(str + matches[2].rm_so)) == 'g' ? 1024 * 1024 * 1024
        : tolower(*(str + matches[2].rm_so)) == 'm' ? 1024 * 1024
        : tolower(*(str + matches[2].rm_so)) == 'k' ? 1024
                                                    : 1);
  return 0;
}


x86 assembly (clang, -O0):
<parse_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ed6(%rip),%rsi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e5e(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rdi
call   1030 <regcomp@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     11e5 <parse_size+0x85>
mov    -0x20(%rbp),%edi
lea    -0xa0(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
mov    $0x7f,%ecx
call   1040 <regerror@plt>
movb   $0x0,-0x21(%rbp)
mov    0x2e8c(%rip),%rsi
lea    -0xa0(%rbp),%rdx
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <parse_size+0x207>
mov    -0x10(%rbp),%rsi
lea    -0xb0(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x3,%edx
xor    %r8d,%r8d
call   1070 <regexec@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     126a <parse_size+0x10a>
mov    -0x20(%rbp),%edi
lea    -0x130(%rbp),%rdx
lea    -0x1c(%rbp),%rsi
mov    $0x7f,%ecx
call   1040 <regerror@plt>
movb   $0x0,-0xb1(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%eax
je     125e <parse_size+0xfe>
mov    0x2e0b(%rip),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x130(%rbp),%rcx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1060 <err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1367 <parse_size+0x207>
mov    -0x10(%rbp),%rdi
movslq -0xac(%rbp),%rax
add    %rax,%rdi
call   1080 <atol@plt>
mov    %rax,-0x138(%rbp)
cmpl   $0xffffffff,-0xa8(%rbp)
jne    12a1 <parse_size+0x141>
mov    $0x1,%eax
mov    %eax,-0x13c(%rbp)
jmp    1346 <parse_size+0x1e6>
mov    -0x10(%rbp),%rax
movslq -0xa8(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%eax
cmp    $0x67,%eax
jne    12d1 <parse_size+0x171>
mov    $0x40000000,%eax
mov    %eax,-0x140(%rbp)
jmp    133a <parse_size+0x1da>
mov    -0x10(%rbp),%rax
movslq -0xa8(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%eax
cmp    $0x6d,%eax
jne    1301 <parse_size+0x1a1>
mov    $0x100000,%eax
mov    %eax,-0x144(%rbp)
jmp    132e <parse_size+0x1ce>
mov    -0x10(%rbp),%rax
movslq -0xa8(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
movsbl %al,%edx
mov    $0x1,%eax
mov    $0x400,%ecx
cmp    $0x6b,%edx
cmove  %ecx,%eax
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%eax
mov    %eax,-0x140(%rbp)
mov    -0x140(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x138(%rbp),%rcx
mov    -0x13c(%rbp),%eax
cltq
imul   %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_size>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e56(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe74(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1030 <regcomp@plt>
test   %eax,%eax
je     11dd <parse_size+0x7d>
lea    0xc(%rsp),%rsi
lea    0x20(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
mov    $0x7f,%ecx
call   1040 <regerror@plt>
movb   $0x0,0x9f(%rsp)
lea    0xe71(%rip),%rdi
lea    0xe37(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    $0xffffffff,%eax
jmp    12bc <parse_size+0x15c>
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %rbx,%rsi
mov    $0x3,%edx
xor    %r8d,%r8d
call   1070 <regexec@plt>
test   %eax,%eax
je     124a <parse_size+0xea>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    %eax,%edi
mov    $0x7f,%ecx
call   1040 <regerror@plt>
movb   $0x0,0x9f(%rsp)
mov    0x2dbe(%rip),%rax
cmp    (%rax),%ebp
je     1243 <parse_size+0xe3>
lea    0xe17(%rip),%rdi
lea    0xdcc(%rip),%rsi
lea    0x20(%rsp),%rcx
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    $0xffffffff,%eax
jmp    12bc <parse_size+0x15c>
movslq 0x14(%rsp),%rdi
add    %rbx,%rdi
call   1080 <atol@plt>
mov    %rax,%r15
movslq 0x18(%rsp),%rax
cmp    $0xffffffffffffffff,%rax
je     12ae <parse_size+0x14e>
movsbl (%rbx,%rax,1),%edi
call   1050 <tolower@plt>
mov    $0x40000000,%ecx
cmp    $0x67,%al
je     12b3 <parse_size+0x153>
movslq 0x18(%rsp),%rax
movsbl (%rbx,%rax,1),%edi
call   1050 <tolower@plt>
mov    $0x100000,%ecx
cmp    $0x6d,%al
je     12b3 <parse_size+0x153>
movslq 0x18(%rsp),%rax
movsbl (%rbx,%rax,1),%edi
call   1050 <tolower@plt>
cmp    $0x6b,%al
mov    $0x400,%eax
mov    $0x1,%ecx
cmove  %rax,%rcx
jmp    12b3 <parse_size+0x153>
mov    $0x1,%ecx
imul   %r15,%rcx
mov    %rcx,(%r14)
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@parse_size.pattern = internal global i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i32 0, i32 0)
@.str = [51 x i8] c"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$\00"
@REG_EXTENDED = global i32 0
@REG_ICASE = global i32 0
@.str.1 = [17 x i8] c"regcomp(%s): %s\0A\00"
@REG_NOMATCH = global i32 0
@.str.2 = [42 x i8] c"regexec: Couldn't match '%s' in '%s': %s\0A\00"
define i32 @parse_size(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
%9 = alloca [3 x %struct.TYPE_3__]
%10 = alloca [128 x i8]
store i8* %0, i8** %4
store i64* %1, i64** %5
%11 = load i8*, i8** @parse_size.pattern
%12 = load i32, i32* @REG_EXTENDED
%13 = load i32, i32* @REG_ICASE
%14 = or i32 %12, %13
%15 = call i32 @regcomp(i32* %6, i8* %11, i32 %14)
store i32 %15, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %2
%18 = load i32, i32* %7
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%20 = call i32 @regerror(i32 %18, i32* %6, i8* %19, i32 127)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 127
store i8 0, i8* %21
%22 = load i8*, i8** @parse_size.pattern
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%24 = call i32 (i8*, i8*, i8*, ...) @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %23)
store i32 -1, i32* %3
br label %103
25: ; preds = %2
%26 = load i8*, i8** %4
%27 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 0
%28 = call i32 @regexec(i32* %6, i8* %26, i32 3, %struct.TYPE_3__* %27, i32 0)
store i32 %28, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %25
%31 = load i32, i32* %7
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%33 = call i32 @regerror(i32 %31, i32* %6, i8* %32, i32 127)
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 127
store i8 0, i8* %34
%35 = load i32, i32* %7
%36 = load i32, i32* @REG_NOMATCH
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = load i8*, i8** @parse_size.pattern
%40 = load i8*, i8** %4
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%42 = call i32 (i8*, i8*, i8*, ...) @err(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %40, i8* %41)
br label %43
43: ; preds = %38, %30
store i32 -1, i32* %3
br label %103
44: ; preds = %25
%45 = load i8*, i8** %4
%46 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 1
%47 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %45, i64 %49
%51 = call i64 @atol(i8* %50)
%52 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 2
%53 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %57
56: ; preds = %44
br label %98
57: ; preds = %44
%58 = load i8*, i8** %4
%59 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 2
%60 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %58, i64 %62
%64 = load i8, i8* %63
%65 = call signext i8 @tolower(i8 signext %64)
%66 = sext i8 %65 to i32
%67 = icmp eq i32 %66, 103
br i1 %67, label %68, label %69
68: ; preds = %57
br label %96
69: ; preds = %57
%70 = load i8*, i8** %4
%71 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 2
%72 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %70, i64 %74
%76 = load i8, i8* %75
%77 = call signext i8 @tolower(i8 signext %76)
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 109
br i1 %79, label %80, label %81
80: ; preds = %69
br label %94
81: ; preds = %69
%82 = load i8*, i8** %4
%83 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %9, i64 0, i64 2
%84 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %83, i32 0, i32 0
%85 = load i32, i32* %84
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %82, i64 %86
%88 = load i8, i8* %87
%89 = call signext i8 @tolower(i8 signext %88)
%90 = sext i8 %89 to i32
%91 = icmp eq i32 %90, 107
%92 = zext i1 %91 to i64
%93 = select i1 %91, i32 1024, i32 1
br label %94
94: ; preds = %81, %80
%95 = phi i32 [ 1048576, %80 ], [ %93, %81 ]
br label %96
96: ; preds = %94, %68
%97 = phi i32 [ 1073741824, %68 ], [ %95, %94 ]
br label %98
98: ; preds = %96, %56
%99 = phi i32 [ 1, %56 ], [ %97, %96 ]
%100 = sext i32 %99 to i64
%101 = mul i64 %51, %100
%102 = load i64*, i64** %5
store i64 %101, i64* %102
store i32 0, i32* %3
br label %103
103: ; preds = %98, %43, %17
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@.str = [51 x i8] c"^[[:space:]]*([0-9]+)(b|kb?|mb?|gb?)?[[:space:]]*$\00"
@REG_EXTENDED = global i32 0
@REG_ICASE = global i32 0
@.str.1 = [17 x i8] c"regcomp(%s): %s\0A\00"
@REG_NOMATCH = global i32 0
@.str.2 = [42 x i8] c"regexec: Couldn't match '%s' in '%s': %s\0A\00"
define i32 @parse_size(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca [3 x %struct.TYPE_3__]
%6 = alloca [128 x i8]
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @REG_EXTENDED
%9 = load i32, i32* @REG_ICASE
%10 = or i32 %9, %8
%11 = call i32 @regcomp(i32* %3, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %2
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%15 = call i32 @regerror(i32 %11, i32* %3, i8* %14, i32 127)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 127
store i8 0, i8* %16
%17 = call i32 (i8*, i8*, i8*, ...) @err(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %67
18: ; preds = %2
%19 = bitcast [3 x %struct.TYPE_3__]* %5 to i8*
%20 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %5, i64 0, i64 0
%21 = call i32 @regexec(i32* %3, i8* %0, i32 3, %struct.TYPE_3__* %20, i32 0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%25 = call i32 @regerror(i32 %21, i32* %3, i8* %24, i32 127)
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 127
store i8 0, i8* %26
%27 = load i32, i32* @REG_NOMATCH
%28 = icmp eq i32 %21, %27
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 (i8*, i8*, i8*, ...) @err(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %24)
br label %31
31: ; preds = %29, %23
br label %65
32: ; preds = %18
%33 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %5, i64 0, i64 1, i32 0
%34 = load i32, i32* %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = call i64 @atol(i8* %36)
%38 = getelementptr inbounds [3 x %struct.TYPE_3__], [3 x %struct.TYPE_3__]* %5, i64 0, i64 2, i32 0
%39 = load i32, i32* %38
%40 = icmp eq i32 %39, -1
br i1 %40, label %62, label %41
41: ; preds = %32
%42 = sext i32 %39 to i64
%43 = getelementptr inbounds i8, i8* %0, i64 %42
%44 = load i8, i8* %43
%45 = call signext i8 @tolower(i8 signext %44)
%46 = icmp eq i8 %45, 103
br i1 %46, label %62, label %47
47: ; preds = %41
%48 = load i32, i32* %38
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %0, i64 %49
%51 = load i8, i8* %50
%52 = call signext i8 @tolower(i8 signext %51)
%53 = icmp eq i8 %52, 109
br i1 %53, label %62, label %54
54: ; preds = %47
%55 = load i32, i32* %38
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %0, i64 %56
%58 = load i8, i8* %57
%59 = call signext i8 @tolower(i8 signext %58)
%60 = icmp eq i8 %59, 107
%61 = select i1 %60, i64 1024, i64 1
br label %62
62: ; preds = %41, %47, %54, %32
%63 = phi i64 [ 1, %32 ], [ 1073741824, %41 ], [ %61, %54 ], [ 1048576, %47 ]
%64 = mul i64 %63, %37
store i64 %64, i64* %1
br label %65
65: ; preds = %62, %31
%66 = phi i32 [ -1, %31 ], [ 0, %62 ]
br label %67
67: ; preds = %65, %13
%68 = phi i32 [ -1, %13 ], [ %66, %65 ]
ret i32 %68
}