#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*/ unit;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long, ...);
char *print_size(unsigned long size) {
  static char unit[] = "-kMG";
  static char buffer[64];
  unsigned int power = 0;
  while (size && !(size % 1024) && power < sizeof(unit)) {
    size /= 1024;
    power++;
  }
  if (!power) {
    snprintf(buffer, sizeof(buffer), "%lu", size);
  } else {
    snprintf(buffer, sizeof(buffer), "%lu%c", size, unit[power]);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<print_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0xd(%rbp)
je     115a <print_size+0x4a>
mov    -0x8(%rbp),%rcx
and    $0x3ff,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xd(%rbp)
jne    115a <print_size+0x4a>
mov    -0xc(%rbp),%eax
cmp    $0x5,%rax
setb   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    116a <print_size+0x5a>
jmp    1184 <print_size+0x74>
mov    -0x8(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1123 <print_size+0x13>
cmpl   $0x0,-0xc(%rbp)
jne    11b1 <print_size+0xa1>
mov    -0x8(%rbp),%rcx
lea    0x2ea7(%rip),%rdi
mov    $0x40,%esi
lea    0xe5b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11e0 <print_size+0xd0>
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%eax
mov    %eax,%edx
lea    0x2e67(%rip),%rax
movsbl (%rax,%rdx,1),%r8d
lea    0x2e73(%rip),%rdi
mov    $0x40,%esi
lea    0xe2b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    0x2e59(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_size>:
push   %rax
test   %rdi,%rdi
je     112b <print_size+0x1b>
mov    %rdi,%rcx
test   $0x3ff,%ecx
je     1149 <print_size+0x39>
mov    $0x1,%dl
xor    %eax,%eax
test   %dl,%dl
jne    112d <print_size+0x1d>
jmp    1161 <print_size+0x51>
xor    %ecx,%ecx
lea    0x2f0c(%rip),%rdi
lea    0xeca(%rip),%rdx
mov    $0x40,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    1187 <print_size+0x77>
mov    %rcx,%rsi
shr    $0xa,%rsi
mov    $0x1,%eax
cmp    $0x400,%rcx
jae    1190 <print_size+0x80>
mov    %rsi,%rcx
lea    0xe98(%rip),%rdx
movsbl (%rax,%rdx,1),%r8d
lea    0x2ecc(%rip),%rdi
lea    0xe8e(%rip),%rdx
mov    $0x40,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0x2eb2(%rip),%rax
pop    %rcx
ret
test   $0xffc00,%ecx
je     11a4 <print_size+0x94>
mov    $0x1,%eax
xor    %edx,%edx
mov    %rsi,%rcx
jmp    1125 <print_size+0x15>
mov    %rcx,%rsi
shr    $0x14,%rsi
mov    $0x2,%eax
cmp    $0x100000,%rcx
jb     115e <print_size+0x4e>
test   $0x3ff00000,%ecx
je     11c8 <print_size+0xb8>
mov    $0x2,%eax
jmp    119d <print_size+0x8d>
mov    %rcx,%rsi
shr    $0x1e,%rsi
mov    $0x3,%eax
cmp    $0x40000000,%rcx
jb     115e <print_size+0x4e>
movabs $0xffc0000000,%rdx
test   %rdx,%rcx
je     11f3 <print_size+0xe3>
mov    $0x3,%eax
jmp    119d <print_size+0x8d>
mov    %rcx,%rsi
shr    $0x28,%rsi
add    $0x40000000,%rdx
mov    $0x4,%eax
cmp    %rdx,%rcx
jb     115e <print_size+0x4e>
movabs $0x3ff0000000000,%rax
test   %rax,%rcx
je     1228 <print_size+0x118>
mov    $0x4,%eax
jmp    119d <print_size+0x8d>
shr    $0x32,%rcx
mov    $0x5,%eax
je     1161 <print_size+0x51>
xor    %edx,%edx
jmp    1125 <print_size+0x15>

LLVM IR (-O0):
@print_size.unit = internal global [5 x i8] c"-kMG\00"
@print_size.buffer = internal global [64 x i8] zeroinitializer
@.str = [4 x i8] c"%lu\00"
@.str.1 = [6 x i8] c"%lu%c\00"
define i8* @print_size(i64 %0) {
%2 = alloca i64
%3 = alloca i32
store i64 %0, i64* %2
store i32 0, i32* %3
br label %4
4: ; preds = %17, %1
%5 = load i64, i64* %2
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = load i64, i64* %2
%9 = urem i64 %8, 1024
%10 = icmp ne i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* %3
%13 = zext i32 %12 to i64
%14 = icmp ult i64 %13, 5
br label %15
15: ; preds = %11, %7, %4
%16 = phi i1 [ false, %7 ], [ false, %4 ], [ %14, %11 ]
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = load i64, i64* %2
%19 = udiv i64 %18, 1024
store i64 %19, i64* %2
%20 = load i32, i32* %3
%21 = add i32 %20, 1
store i32 %21, i32* %3
br label %4
22: ; preds = %15
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i64, i64* %2
%27 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %26)
br label %36
28: ; preds = %22
%29 = load i64, i64* %2
%30 = load i32, i32* %3
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [5 x i8], [5 x i8]* @print_size.unit, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %29, i32 %34)
br label %36
36: ; preds = %28, %25
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@print_size.unit = internal unnamed_addr constant [5 x i8] c"-kMG\00"
@print_size.buffer = internal global [64 x i8] zeroinitializer
@.str = [4 x i8] c"%lu\00"
@.str.1 = [6 x i8] c"%lu%c\00"
define i8* @print_size(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %37, label %3
3: ; preds = %1
%4 = and i64 %0, 1023
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %33
6: ; preds = %3
%7 = lshr i64 %0, 10
%8 = icmp ult i64 %0, 1024
br i1 %8, label %40, label %9
9: ; preds = %6
%10 = and i64 %0, 1047552
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = lshr i64 %0, 20
%14 = icmp ult i64 %0, 1048576
br i1 %14, label %40, label %15
15: ; preds = %12
%16 = and i64 %0, 1072693248
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = lshr i64 %0, 30
%20 = icmp ult i64 %0, 1073741824
br i1 %20, label %40, label %21
21: ; preds = %18
%22 = and i64 %0, 1098437885952
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = lshr i64 %0, 40
%26 = icmp ult i64 %0, 1099511627776
br i1 %26, label %40, label %27
27: ; preds = %24
%28 = and i64 %0, 1124800395214848
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = lshr i64 %0, 50
%32 = icmp ult i64 %0, 1125899906842624
br i1 %32, label %40, label %33
33: ; preds = %30, %27, %21, %15, %9, %3
%34 = phi i1 [ false, %27 ], [ false, %21 ], [ false, %15 ], [ false, %9 ], [ true, %3 ], [ false, %30 ]
%35 = phi i64 [ 4, %27 ], [ 3, %21 ], [ 2, %15 ], [ 1, %9 ], [ 0, %3 ], [ 5, %30 ]
%36 = phi i64 [ %25, %27 ], [ %19, %21 ], [ %13, %15 ], [ %7, %9 ], [ %0, %3 ], [ %31, %30 ]
br i1 %34, label %37, label %40
37: ; preds = %1, %33
%38 = phi i64 [ %36, %33 ], [ 0, %1 ]
%39 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %38)
br label %48
40: ; preds = %6, %12, %18, %24, %30, %33
%41 = phi i64 [ %35, %33 ], [ 1, %6 ], [ 2, %12 ], [ 3, %18 ], [ 4, %24 ], [ 5, %30 ]
%42 = phi i64 [ %36, %33 ], [ %7, %6 ], [ %13, %12 ], [ %19, %18 ], [ %25, %24 ], [ %31, %30 ]
%43 = and i64 %41, 4294967295
%44 = getelementptr inbounds [5 x i8], [5 x i8]* @print_size.unit, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = tail call i32 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %42, i32 %46)
br label %48
48: ; preds = %40, %37
ret i8* getelementptr inbounds ([64 x i8], [64 x i8]* @print_size.buffer, i64 0, i64 0)
}