#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 */
int strlen(char *);
long strtol(char *, char **, int);
long parse_buffersize(char *string) {
  char *suffix;
  long bytes;
  bytes = strtol(string, &suffix, 10);
  if (strlen(suffix) > 1)
    return -1;
  switch (*suffix) {
  case 'k':
  case 'K':
    bytes *= 1024;
    break;
  case 'm':
  case 'M':
    bytes *= 1048576;
    break;
  case '\0':
    break;
  default:
    return -1;
  }
  if ((bytes < 4096) || (bytes > 1048576))
    return -1;
  return bytes;
}


x86 assembly (clang, -O0):
<parse_buffersize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jle    1161 <parse_buffersize+0x41>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1221 <parse_buffersize+0x101>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
test   %eax,%eax
je     11de <parse_buffersize+0xbe>
jmp    1178 <parse_buffersize+0x58>
mov    -0x24(%rbp),%eax
sub    $0x4b,%eax
je     11bc <parse_buffersize+0x9c>
jmp    1189 <parse_buffersize+0x69>
mov    -0x24(%rbp),%eax
sub    $0x4d,%eax
je     11cd <parse_buffersize+0xad>
jmp    119a <parse_buffersize+0x7a>
mov    -0x24(%rbp),%eax
sub    $0x6b,%eax
je     11bc <parse_buffersize+0x9c>
jmp    11ab <parse_buffersize+0x8b>
mov    -0x24(%rbp),%eax
sub    $0x6d,%eax
je     11cd <parse_buffersize+0xad>
jmp    11e3 <parse_buffersize+0xc3>
mov    -0x20(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x20(%rbp)
jmp    11f0 <parse_buffersize+0xd0>
mov    -0x20(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x20(%rbp)
jmp    11f0 <parse_buffersize+0xd0>
jmp    11f0 <parse_buffersize+0xd0>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1221 <parse_buffersize+0x101>
cmpq   $0x1000,-0x20(%rbp)
jl     120c <parse_buffersize+0xec>
cmpq   $0x100000,-0x20(%rbp)
jle    1219 <parse_buffersize+0xf9>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1221 <parse_buffersize+0x101>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_buffersize>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r14
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
cmp    $0x1,%ecx
jg     1191 <parse_buffersize+0x71>
movsbl (%rbx),%ecx
lea    -0x4b(%rcx),%edx
cmp    $0x22,%edx
ja     1175 <parse_buffersize+0x55>
lea    0xea0(%rip),%rcx
movslq (%rcx,%rdx,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
shl    $0xa,%r14
jmp    1179 <parse_buffersize+0x59>
shl    $0x14,%r14
jmp    1179 <parse_buffersize+0x59>
test   %ecx,%ecx
jne    1191 <parse_buffersize+0x71>
lea    -0x100001(%r14),%rax
cmp    $0xfffffffffff00fff,%rax
mov    $0xffffffffffffffff,%rax
cmovae %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @parse_buffersize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strtol(i8* %6, i8** %4, i32 10)
store i64 %7, i64* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 -1, i64* %2
br label %33
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
switch i32 %15, label %23 [
i32 107, label %16
i32 75, label %16
i32 109, label %19
i32 77, label %19
i32 0, label %22
]
16: ; preds = %12, %12
%17 = load i64, i64* %5
%18 = mul nsw i64 %17, 1024
store i64 %18, i64* %5
br label %24
19: ; preds = %12, %12
%20 = load i64, i64* %5
%21 = mul nsw i64 %20, 1048576
store i64 %21, i64* %5
br label %24
22: ; preds = %12
br label %24
23: ; preds = %12
store i64 -1, i64* %2
br label %33
24: ; preds = %22, %19, %16
%25 = load i64, i64* %5
%26 = icmp slt i64 %25, 4096
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i64, i64* %5
%29 = icmp sgt i64 %28, 1048576
br i1 %29, label %30, label %31
30: ; preds = %27, %24
store i64 -1, i64* %2
br label %33
31: ; preds = %27
%32 = load i64, i64* %5
store i64 %32, i64* %2
br label %33
33: ; preds = %31, %30, %23, %11
%34 = load i64, i64* %2
ret i64 %34
}

LLVM IR (-O2):
define i64 @parse_buffersize(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = call i64 @strtol(i8* %0, i8** %2, i32 10)
%5 = load i8*, i8** %2
%6 = tail call i32 @strlen(i8* %5)
%7 = icmp sgt i32 %6, 1
br i1 %7, label %20, label %8
8: ; preds = %1
%9 = load i8, i8* %5
%10 = sext i8 %9 to i32
switch i32 %10, label %20 [
i32 107, label %11
i32 75, label %11
i32 109, label %13
i32 77, label %13
i32 0, label %15
]
11: ; preds = %8, %8
%12 = shl nsw i64 %4, 10
br label %15
13: ; preds = %8, %8
%14 = shl nsw i64 %4, 20
br label %15
15: ; preds = %8, %13, %11
%16 = phi i64 [ %4, %8 ], [ %14, %13 ], [ %12, %11 ]
%17 = add i64 %16, -1048577
%18 = icmp ult i64 %17, -1044481
%19 = select i1 %18, i64 -1, i64 %16
br label %20
20: ; preds = %15, %8, %1
%21 = phi i64 [ -1, %1 ], [ -1, %8 ], [ %19, %15 ]
ret i64 %21
}