#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*/ assert(int);
char *mymalloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *amount_to_str(long long int amount) {
  char *out = mymalloc((3 + 2 + 1));
  assert(amount >= 0);
  if (amount >= (((1024) * 1024) * 1024))
    snprintf(out, (3 + 2 + 1), "%dGB",
             (int)((amount + (((1024) * 1024) * 1024) - 1) /
                   (((1024) * 1024) * 1024)));
  else if (amount >= ((1024) * 1024))
    snprintf(out, (3 + 2 + 1), "%dMB",
             (int)((amount + ((1024) * 1024) - 1) / ((1024) * 1024)));
  else if (amount >= (1024))
    snprintf(out, (3 + 2 + 1), "%dKB", (int)((amount + (1024) - 1) / (1024)));
  else
    snprintf(out, (3 + 2 + 1), "%d", (int)(amount));
  return out;
}


x86 assembly (clang, -O0):
<amount_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x6,%edi
call   1050 <mymalloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x40000000,-0x8(%rbp)
40
jl     119e <amount_to_str+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
add    $0x40000000,%rax
sub    $0x1,%rax
mov    $0x40000000,%ecx
cqto
idiv   %rcx
mov    %eax,%ecx
mov    $0x6,%esi
lea    0xe6c(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1247 <amount_to_str+0x117>
cmpq   $0x100000,-0x8(%rbp)
jl     11e0 <amount_to_str+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
add    $0x100000,%rax
sub    $0x1,%rax
mov    $0x100000,%ecx
cqto
idiv   %rcx
mov    %eax,%ecx
mov    $0x6,%esi
lea    0xe2f(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1242 <amount_to_str+0x112>
cmpq   $0x400,-0x8(%rbp)
jl     1222 <amount_to_str+0xf2>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
add    $0x400,%rax
sub    $0x1,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %eax,%ecx
mov    $0x6,%esi
lea    0xdf2(%rip),%rdx
call   1030 <snprintf@plt>
jmp    123d <amount_to_str+0x10d>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %eax,%ecx
mov    $0x6,%esi
lea    0xdd7(%rip),%rdx
call   1030 <snprintf@plt>
jmp    1242 <amount_to_str+0x112>
jmp    1247 <amount_to_str+0x117>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<amount_to_str>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x6,%edi
call   1050 <mymalloc@plt>
mov    %rax,%r14
mov    %rbx,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1040 <assert@plt>
cmp    $0x40000000,%rbx
jl     1170 <amount_to_str+0x40>
add    $0x3fffffff,%rbx
shr    $0x1e,%rbx
lea    0xe92(%rip),%rdx
jmp    11ad <amount_to_str+0x7d>
cmp    $0x100000,%rbx
jl     118b <amount_to_str+0x5b>
add    $0xfffff,%ebx
shr    $0x14,%ebx
lea    0xe7c(%rip),%rdx
jmp    11ad <amount_to_str+0x7d>
cmp    $0x400,%rbx
jl     11a6 <amount_to_str+0x76>
add    $0x3ff,%ebx
shr    $0xa,%ebx
lea    0xe66(%rip),%rdx
jmp    11ad <amount_to_str+0x7d>
lea    0xe62(%rip),%rdx
mov    %r14,%rdi
mov    $0x6,%esi
mov    %ebx,%ecx
call   1030 <snprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%dGB\00"
@.str.1 = [5 x i8] c"%dMB\00"
@.str.2 = [5 x i8] c"%dKB\00"
@.str.3 = [3 x i8] c"%d\00"
define i8* @amount_to_str(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = call i8* @mymalloc(i32 6)
store i8* %4, i8** %3
%5 = load i64, i64* %2
%6 = icmp sge i64 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i64, i64* %2
%10 = icmp sge i64 %9, 1073741824
br i1 %10, label %11, label %19
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i64, i64* %2
%14 = add nsw i64 %13, 1073741824
%15 = sub nsw i64 %14, 1
%16 = sdiv i64 %15, 1073741824
%17 = trunc i64 %16 to i32
%18 = call i32 @snprintf(i8* %12, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %48
19: ; preds = %1
%20 = load i64, i64* %2
%21 = icmp sge i64 %20, 1048576
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = load i64, i64* %2
%25 = add nsw i64 %24, 1048576
%26 = sub nsw i64 %25, 1
%27 = sdiv i64 %26, 1048576
%28 = trunc i64 %27 to i32
%29 = call i32 @snprintf(i8* %23, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %47
30: ; preds = %19
%31 = load i64, i64* %2
%32 = icmp sge i64 %31, 1024
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i8*, i8** %3
%35 = load i64, i64* %2
%36 = add nsw i64 %35, 1024
%37 = sub nsw i64 %36, 1
%38 = sdiv i64 %37, 1024
%39 = trunc i64 %38 to i32
%40 = call i32 @snprintf(i8* %34, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %46
41: ; preds = %30
%42 = load i8*, i8** %3
%43 = load i64, i64* %2
%44 = trunc i64 %43 to i32
%45 = call i32 @snprintf(i8* %42, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %41, %33
br label %47
47: ; preds = %46, %22
br label %48
48: ; preds = %47, %11
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [5 x i8] c"%dGB\00"
@.str.1 = [5 x i8] c"%dMB\00"
@.str.2 = [5 x i8] c"%dKB\00"
@.str.3 = [3 x i8] c"%d\00"
define i8* @amount_to_str(i64 %0) {
%2 = tail call i8* @mymalloc(i32 6)
%3 = lshr i64 %0, 63
%4 = trunc i64 %3 to i32
%5 = xor i32 %4, 1
%6 = tail call i32 @assert(i32 %5)
%7 = icmp sgt i64 %0, 1073741823
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = add nuw nsw i64 %0, 1073741823
%10 = lshr i64 %9, 30
%11 = trunc i64 %10 to i32
%12 = tail call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %29
13: ; preds = %1
%14 = icmp sgt i64 %0, 1048575
br i1 %14, label %15, label %20
15: ; preds = %13
%16 = trunc i64 %0 to i32
%17 = add nuw nsw i32 %16, 1048575
%18 = lshr i32 %17, 20
%19 = tail call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %29
20: ; preds = %13
%21 = icmp sgt i64 %0, 1023
%22 = trunc i64 %0 to i32
br i1 %21, label %23, label %27
23: ; preds = %20
%24 = add nuw nsw i32 %22, 1023
%25 = lshr i32 %24, 10
%26 = tail call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %20
%28 = tail call i32 @snprintf(i8* %2, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %29
29: ; preds = %15, %27, %23, %8
ret i8* %2
}