#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__ strchr(char *, char);
int /*<<< orphan*/ strlen(char const *);
char *strstr(char const *, char const *);
unsigned long long strtoull(char const *, char **, int /*<<< orphan*/);
unsigned long long get_scaled_iostat(const char *buffer, const char *key) {
  const char *ptr;
  char *next;
  unsigned long long value = 0;
  ptr = strstr(buffer, key);
  if (ptr) {
    ptr += strlen(key);
    value = strtoull(ptr, &next, 0);
    if (strchr(next, 'k'))
      value *= 1024;
    else if (strchr(next, 'M'))
      value *= 1024 * 1024;
  }
  return value;
}


x86 assembly (clang, -O0):
<get_scaled_iostat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f7 <get_scaled_iostat+0xb7>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x6b,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11c9 <get_scaled_iostat+0x89>
mov    -0x28(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x28(%rbp)
jmp    11f2 <get_scaled_iostat+0xb2>
mov    -0x20(%rbp),%rdi
mov    $0x4d,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11ed <get_scaled_iostat+0xad>
mov    -0x28(%rbp),%rax
shl    $0x14,%rax
mov    %rax,-0x28(%rbp)
jmp    11f2 <get_scaled_iostat+0xb2>
jmp    11f7 <get_scaled_iostat+0xb7>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_scaled_iostat>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
call   1060 <strstr@plt>
test   %rax,%rax
je     118d <get_scaled_iostat+0x4d>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %rax,%rbx
mov    (%rsp),%r14
mov    %r14,%rdi
mov    $0x6b,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1191 <get_scaled_iostat+0x51>
shl    $0xa,%rbx
jmp    11ac <get_scaled_iostat+0x6c>
xor    %ebx,%ebx
jmp    11ac <get_scaled_iostat+0x6c>
mov    %r14,%rdi
mov    $0x4d,%esi
call   1040 <strchr@plt>
mov    %rbx,%rcx
shl    $0x14,%rcx
test   %rax,%rax
cmovne %rcx,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @get_scaled_iostat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 0, i64* %7
%8 = load i8*, i8** %3
%9 = load i8*, i8** %4
%10 = call i8* @strstr(i8* %8, i8* %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %36
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = load i8*, i8** %5
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = call i64 @strtoull(i8* %19, i8** %6, i32 0)
store i64 %20, i64* %7
%21 = load i8*, i8** %6
%22 = call i64 @strchr(i8* %21, i8 signext 107)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = load i64, i64* %7
%26 = mul i64 %25, 1024
store i64 %26, i64* %7
br label %35
27: ; preds = %13
%28 = load i8*, i8** %6
%29 = call i64 @strchr(i8* %28, i8 signext 77)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i64, i64* %7
%33 = mul i64 %32, 1048576
store i64 %33, i64* %7
br label %34
34: ; preds = %31, %27
br label %35
35: ; preds = %34, %24
br label %36
36: ; preds = %35, %2
%37 = load i64, i64* %7
ret i64 %37
}

LLVM IR (-O2):
define i64 @get_scaled_iostat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = tail call i32 @strlen(i8* %1)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
%11 = call i64 @strtoull(i8* %10, i8** %3, i32 0)
%12 = load i8*, i8** %3
%13 = tail call i64 @strchr(i8* %12, i8 signext 107)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = shl i64 %11, 10
br label %22
17: ; preds = %7
%18 = tail call i64 @strchr(i8* %12, i8 signext 77)
%19 = icmp eq i64 %18, 0
%20 = shl i64 %11, 20
%21 = select i1 %19, i64 %11, i64 %20
br label %22
22: ; preds = %17, %15, %2
%23 = phi i64 [ %16, %15 ], [ 0, %2 ], [ %21, %17 ]
ret i64 %23
}