#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*/ bzero(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned long long, char *);
char *strdup(char *);
char *printAmount(unsigned long long amount) {
  char unit[2], str[6];
  int div;
  bzero(unit, 2);
  div = 1;
  if (amount > 1024 * 10) {
    unit[0] = 'K';
    div = 1024;
  }
  if (amount > 1024 * 1024 * 10) {
    unit[0] = 'M';
    div = 1024 * 1024;
  }
  if (amount > 1024 * 1024 * 1024) {
    unit[0] = 'G';
    div = 1024 * 1024 * 1024;
  }
  sprintf(str, "%llu%s", amount / div, unit);
  return (strdup(str));
}


x86 assembly (clang, -O0):
<printAmount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xa(%rbp),%rdi
mov    $0x2,%esi
call   1030 <bzero@plt>
movl   $0x1,-0x14(%rbp)
cmpq   $0x2800,-0x8(%rbp)
jbe    116a <printAmount+0x3a>
movb   $0x4b,-0xa(%rbp)
movl   $0x400,-0x14(%rbp)
cmpq   $0xa00000,-0x8(%rbp)
jbe    1183 <printAmount+0x53>
movb   $0x4d,-0xa(%rbp)
movl   $0x100000,-0x14(%rbp)
cmpq   $0x40000000,-0x8(%rbp)
40
jbe    119c <printAmount+0x6c>
movb   $0x47,-0xa(%rbp)
movl   $0x40000000,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,%rdx
lea    -0xa(%rbp),%rcx
lea    0xe45(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x10(%rbp),%rdi
call   1050 <strdup@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printAmount>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
mov    $0x2,%esi
call   1030 <bzero@plt>
mov    $0x1,%eax
cmp    $0x2801,%rbx
jb     117e <printAmount+0x4e>
cmp    $0xa00001,%rbx
setae  %al
add    %al,%al
add    $0x4b,%al
cmp    $0x40000001,%rbx
movzbl %al,%eax
mov    $0x47,%ecx
cmovb  %eax,%ecx
mov    %cl,0x8(%rsp)
mov    $0x400,%eax
cmp    $0xa00001,%rbx
mov    $0x100000,%edx
cmovb  %rax,%rdx
cmp    $0x40000001,%rbx
mov    $0x40000000,%ecx
cmovb  %rdx,%rcx
mov    %rbx,%rax
shr    $0x20,%rax
je     11b1 <printAmount+0x81>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    11b7 <printAmount+0x87>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
lea    0xe42(%rip),%rsi
lea    0xa(%rsp),%rbx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %rax,%rdx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strdup@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%llu%s\00"
define i8* @printAmount(i64 %0) {
%2 = alloca i64
%3 = alloca [2 x i8]
%4 = alloca [6 x i8]
%5 = alloca i32
store i64 %0, i64* %2
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%7 = call i32 @bzero(i8* %6, i32 2)
store i32 1, i32* %5
%8 = load i64, i64* %2
%9 = icmp ugt i64 %8, 10240
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 75, i8* %11
store i32 1024, i32* %5
br label %12
12: ; preds = %10, %1
%13 = load i64, i64* %2
%14 = icmp ugt i64 %13, 10485760
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 77, i8* %16
store i32 1048576, i32* %5
br label %17
17: ; preds = %15, %12
%18 = load i64, i64* %2
%19 = icmp ugt i64 %18, 1073741824
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 71, i8* %21
store i32 1073741824, i32* %5
br label %22
22: ; preds = %20, %17
%23 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%24 = load i64, i64* %2
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = udiv i64 %24, %26
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%29 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %27, i8* %28)
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%31 = call i8* @strdup(i8* %30)
ret i8* %31
}

LLVM IR (-O2):
@.str = [7 x i8] c"%llu%s\00"
define noalias i8* @printAmount(i64 %0) {
%2 = alloca [2 x i8]
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%6 = call i32 @bzero(i8* %4, i32 2)
%7 = icmp ugt i64 %0, 10240
%8 = icmp ugt i64 %0, 10485760
%9 = icmp ugt i64 %0, 1073741824
br i1 %7, label %10, label %13
10: ; preds = %1
%11 = select i1 %8, i8 77, i8 75
%12 = select i1 %9, i8 71, i8 %11
store i8 %12, i8* %4
br label %13
13: ; preds = %1, %10
%14 = phi i64 [ 1, %1 ], [ 1024, %10 ]
%15 = select i1 %8, i64 1048576, i64 %14
%16 = select i1 %9, i64 1073741824, i64 %15
%17 = udiv i64 %0, %16
%18 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64 %17, i8* %4)
%19 = call i8* @strdup(i8* %5)
ret i8* %19
}