#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 /*<<< orphan*/ snprintf(char *, int, char *, unsigned long long, ...);
void format_capacity(char *buf, int buflen, unsigned long long megabytes) {
  if (megabytes >= 1024) {
    unsigned long long left = ((megabytes & 0x3FF) * 100) >> 10;
    if (left == 0)
      snprintf(buf, buflen, "%llu GB", (megabytes >> 10));
    else
      snprintf(buf, buflen, "%llu.%02llu GB", (megabytes >> 10), left);
  } else {
    snprintf(buf, buflen, "%llu MB", megabytes);
  }
}


x86 assembly (clang, -O0):
<format_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x400,-0x18(%rbp)
jb     119a <format_capacity+0x8a>
mov    -0x18(%rbp),%rax
and    $0x3ff,%rax
imul   $0x64,%rax,%rax
shr    $0xa,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1174 <format_capacity+0x64>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rcx
shr    $0xa,%rcx
lea    0xe98(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1195 <format_capacity+0x85>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rcx
shr    $0xa,%rcx
mov    -0x20(%rbp),%r8
lea    0xe7a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11b3 <format_capacity+0xa3>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rcx
lea    0xe6b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_capacity>:
mov    %rdx,%rcx
cmp    $0x400,%rdx
jb     1142 <format_capacity+0x32>
mov    %ecx,%eax
and    $0x3ff,%eax
imul   $0x64,%rax,%r8
cmp    $0x3ff,%r8
ja     1150 <format_capacity+0x40>
shr    $0xa,%rcx
lea    0xec5(%rip),%rdx
xor    %eax,%eax
jmp    1030 <snprintf@plt>
lea    0xece(%rip),%rdx
xor    %eax,%eax
jmp    1030 <snprintf@plt>
shr    $0xa,%r8
shr    $0xa,%rcx
lea    0xea9(%rip),%rdx
xor    %eax,%eax
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"%llu GB\00"
@.str.1 = [15 x i8] c"%llu.%02llu GB\00"
@.str.2 = [8 x i8] c"%llu MB\00"
define void @format_capacity(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%8 = load i64, i64* %6
%9 = icmp uge i64 %8, 1024
br i1 %9, label %10, label %31
10: ; preds = %3
%11 = load i64, i64* %6
%12 = and i64 %11, 1023
%13 = mul i64 %12, 100
%14 = lshr i64 %13, 10
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = load i64, i64* %6
%21 = lshr i64 %20, 10
%22 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %18, i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %21)
br label %30
23: ; preds = %10
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = load i64, i64* %6
%27 = lshr i64 %26, 10
%28 = load i64, i64* %7
%29 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %24, i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %27, i64 %28)
br label %30
30: ; preds = %23, %17
br label %36
31: ; preds = %3
%32 = load i8*, i8** %4
%33 = load i32, i32* %5
%34 = load i64, i64* %6
%35 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %34)
br label %36
36: ; preds = %31, %30
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"%llu GB\00"
@.str.1 = [15 x i8] c"%llu.%02llu GB\00"
@.str.2 = [8 x i8] c"%llu MB\00"
define void @format_capacity(i8* %0, i32 %1, i64 %2) {
%4 = icmp ugt i64 %2, 1023
br i1 %4, label %5, label %16
5: ; preds = %3
%6 = and i64 %2, 1023
%7 = mul nuw nsw i64 %6, 100
%8 = icmp ult i64 %7, 1024
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = lshr i64 %2, 10
%11 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %10)
br label %18
12: ; preds = %5
%13 = lshr i64 %7, 10
%14 = lshr i64 %2, 10
%15 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i64 %14, i64 %13)
br label %18
16: ; preds = %3
%17 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %2)
br label %18
18: ; preds = %9, %12, %16
ret void
}