#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*/ buffer;

/* Variables and functions */
long long LLONG_MAX;
scalar_t__ isdigit(char const);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
int /*<<< orphan*/ strcasecmp(char const *, char *);
long strtoll(char *, int /*<<< orphan*/ *, int);
long long memtoll(const char *value, int *err) {
  char buffer[128];
  const char *ptr;
  unsigned int digits;
  long mul;
  *err = 0;
  ptr = value;
  while (*ptr && isdigit(*ptr)) {
    ptr++;
  }
  if (*ptr == '\0' || !strcasecmp(ptr, "b")) {
    mul = 1;
  } else if (!strcasecmp(ptr, "k")) {
    mul = 1000;
  } else if (!strcasecmp(ptr, "m")) {
    mul = 1000 * 1000;
  } else if (!strcasecmp(ptr, "g")) {
    mul = 1000L * 1000 * 1000;
  } else {
    *err = 1;
    mul = 1;
  }
  digits = ptr - value;
  if (digits >= sizeof(buffer)) {
    *err = 1;
    return LLONG_MAX;
  }
  memcpy(buffer, value, digits);
  buffer[digits] = '\0';
  return strtoll(buffer, NULL, 10) * mul;
}


x86 assembly (clang, -O0):
<memtoll>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xb9(%rbp)
je     119f <memtoll+0x5f>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xb9(%rbp)
mov    -0xb9(%rbp),%al
test   $0x1,%al
jne    11b2 <memtoll+0x72>
jmp    11c9 <memtoll+0x89>
mov    -0xa8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa8(%rbp)
jmp    1168 <memtoll+0x28>
mov    -0xa8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f8 <memtoll+0xb8>
mov    -0xa8(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1208 <memtoll+0xc8>
movq   $0x1,-0xb8(%rbp)
jmp    12b0 <memtoll+0x170>
mov    -0xa8(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1234 <memtoll+0xf4>
movq   $0x3e8,-0xb8(%rbp)
jmp    12ab <memtoll+0x16b>
mov    -0xa8(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    1260 <memtoll+0x120>
movq   $0xf4240,-0xb8(%rbp)
jmp    12a6 <memtoll+0x166>
mov    -0xa8(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%eax
jne    128c <memtoll+0x14c>
movq   $0x3b9aca00,-0xb8(%rbp)
jmp    12a1 <memtoll+0x161>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
movq   $0x1,-0xb8(%rbp)
jmp    12a6 <memtoll+0x166>
jmp    12ab <memtoll+0x16b>
jmp    12b0 <memtoll+0x170>
mov    -0xa8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%eax
cmp    $0x80,%rax
jb     12f3 <memtoll+0x1b3>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    0x2d01(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1338 <memtoll+0x1f8>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0xac(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0xac(%rbp),%eax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoll@plt>
imul   -0xb8(%rbp),%rax
ff
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memtoll>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,(%rsi)
mov    (%rdi),%al
mov    $0x1,%r12d
mov    %rdi,%rbx
test   %al,%al
je     1189 <memtoll+0x49>
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     11e4 <memtoll+0xa4>
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1170 <memtoll+0x30>
sub    %r15,%rbx
mov    %ebx,%r13d
cmp    $0x80,%r13
jb     11ab <memtoll+0x6b>
movl   $0x1,(%r14)
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
jmp    11d3 <memtoll+0x93>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
movb   $0x0,(%rsp,%r13,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoll@plt>
imul   %r12,%rax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
cmpb   $0x0,(%rbx)
je     1189 <memtoll+0x49>
lea    0xe10(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     1189 <memtoll+0x49>
lea    0xdff(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     1241 <memtoll+0x101>
lea    0xdee(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     124c <memtoll+0x10c>
lea    0xddd(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %eax,%eax
je     1257 <memtoll+0x117>
movl   $0x1,(%r14)
jmp    1189 <memtoll+0x49>
mov    $0x3e8,%r12d
jmp    1189 <memtoll+0x49>
mov    $0xf4240,%r12d
jmp    1189 <memtoll+0x49>
mov    $0x3b9aca00,%r12d
jmp    1189 <memtoll+0x49>

LLVM IR (-O0):
@.str = [2 x i8] c"b\00"
@.str.1 = [2 x i8] c"k\00"
@.str.2 = [2 x i8] c"m\00"
@.str.3 = [2 x i8] c"g\00"
@LLONG_MAX = global i64 0
define i64 @memtoll(i8* %0, i32* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [128 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i32*, i32** %5
store i32 0, i32* %10
%11 = load i8*, i8** %4
store i8* %11, i8** %7
br label %12
12: ; preds = %24, %2
%13 = load i8*, i8** %7
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i8*, i8** %7
%19 = load i8, i8* %18
%20 = call i64 @isdigit(i8 signext %19)
%21 = icmp ne i64 %20, 0
br label %22
22: ; preds = %17, %12
%23 = phi i1 [ false, %12 ], [ %21, %17 ]
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %7
br label %12
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %27
%33 = load i8*, i8** %7
%34 = call i32 @strcasecmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32, %27
store i64 1, i64* %9
br label %57
37: ; preds = %32
%38 = load i8*, i8** %7
%39 = call i32 @strcasecmp(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %37
store i64 1000, i64* %9
br label %56
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = call i32 @strcasecmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %42
store i64 1000000, i64* %9
br label %55
47: ; preds = %42
%48 = load i8*, i8** %7
%49 = call i32 @strcasecmp(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp ne i32 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %47
store i64 1000000000, i64* %9
br label %54
52: ; preds = %47
%53 = load i32*, i32** %5
store i32 1, i32* %53
store i64 1, i64* %9
br label %54
54: ; preds = %52, %51
br label %55
55: ; preds = %54, %46
br label %56
56: ; preds = %55, %41
br label %57
57: ; preds = %56, %36
%58 = load i8*, i8** %7
%59 = load i8*, i8** %4
%60 = ptrtoint i8* %58 to i64
%61 = ptrtoint i8* %59 to i64
%62 = sub i64 %60, %61
%63 = trunc i64 %62 to i32
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = zext i32 %64 to i64
%66 = icmp uge i64 %65, 128
br i1 %66, label %67, label %70
67: ; preds = %57
%68 = load i32*, i32** %5
store i32 1, i32* %68
%69 = load i64, i64* @LLONG_MAX
store i64 %69, i64* %3
br label %82
70: ; preds = %57
%71 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%72 = load i8*, i8** %4
%73 = load i32, i32* %8
%74 = call i32 @memcpy(i8* %71, i8* %72, i32 %73)
%75 = load i32, i32* %8
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %76
store i8 0, i8* %77
%78 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%79 = call i64 @strtoll(i8* %78, i32* null, i32 10)
%80 = load i64, i64* %9
%81 = mul nsw i64 %79, %80
store i64 %81, i64* %3
br label %82
82: ; preds = %70, %67
%83 = load i64, i64* %3
ret i64 %83
}

LLVM IR (-O2):
@.str = [2 x i8] c"b\00"
@.str.1 = [2 x i8] c"k\00"
@.str.2 = [2 x i8] c"m\00"
@.str.3 = [2 x i8] c"g\00"
@LLONG_MAX = global i64 0
define i64 @memtoll(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i32 0, i32* %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %32, label %7
7: ; preds = %2, %12
%8 = phi i8 [ %14, %12 ], [ %5, %2 ]
%9 = phi i8* [ %13, %12 ], [ %0, %2 ]
%10 = tail call i64 @isdigit(i8 signext %8)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %32, label %7
16: ; preds = %7
%17 = load i8, i8* %9
%18 = icmp eq i8 %17, 0
br i1 %18, label %32, label %19
19: ; preds = %16
%20 = tail call i32 @strcasecmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %32, label %22
22: ; preds = %19
%23 = tail call i32 @strcasecmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %22
%26 = tail call i32 @strcasecmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = tail call i32 @strcasecmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %28
store i32 1, i32* %1
br label %32
32: ; preds = %12, %2, %28, %25, %22, %16, %19, %31
%33 = phi i8* [ %9, %31 ], [ %9, %19 ], [ %9, %16 ], [ %9, %22 ], [ %9, %25 ], [ %9, %28 ], [ %0, %2 ], [ %13, %12 ]
%34 = phi i64 [ 1, %31 ], [ 1, %19 ], [ 1, %16 ], [ 1000, %22 ], [ 1000000, %25 ], [ 1000000000, %28 ], [ 1, %2 ], [ 1, %12 ]
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %0 to i64
%37 = sub i64 %35, %36
%38 = and i64 %37, 4294967295
%39 = icmp ugt i64 %38, 127
br i1 %39, label %40, label %42
40: ; preds = %32
store i32 1, i32* %1
%41 = load i64, i64* @LLONG_MAX
br label %48
42: ; preds = %32
%43 = trunc i64 %37 to i32
%44 = call i32 @memcpy(i8* %4, i8* %0, i32 %43)
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %38
store i8 0, i8* %45
%46 = call i64 @strtoll(i8* nocapture %4, i32* null, i32 10)
%47 = mul nsw i64 %46, %34
br label %48
48: ; preds = %42, %40
%49 = phi i64 [ %41, %40 ], [ %47, %42 ]
ret i64 %49
}