#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*/ suff;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned long long, char);
char *size_cnv(char *dst, unsigned long long num, int in_kb) {
  char suff[] = {'\0', 'K', 'M', 'G', 'P'};
  unsigned int i = 0;
  if (in_kb)
    i++;
  while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
    i++;
    num /= 1000;
  }
  sprintf(dst, "%'8Lu%c", num, suff[i]);
  return dst;
}


x86 assembly (clang, -O0):
<size_cnv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xed7(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xed2(%rip),%al
mov    %al,-0x15(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     114f <size_cnv+0x3f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1154 <size_cnv+0x44>
xor    %eax,%eax
cmpq   $0xf4240,-0x10(%rbp)
mov    %al,-0x21(%rbp)
jbe    1174 <size_cnv+0x64>
mov    -0x20(%rbp),%eax
cmp    $0x4,%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1184 <size_cnv+0x74>
jmp    11a4 <size_cnv+0x94>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
jmp    1154 <size_cnv+0x44>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%eax
lea    0xe4f(%rip),%rsi
movsbl -0x19(%rbp,%rax,1),%ecx
call   1030 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size_cnv>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
test   %edx,%edx
setne  %al
cmp    $0xf4241,%rsi
jb     11bb <size_cnv+0xab>
mov    %edx,%ecx
cmp    $0x1,%edx
mov    $0x1,%edi
sbb    $0xffffffff,%edi
mov    %rsi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mul    %rdx
cmp    $0x3b9acde8,%rsi
jb     11c2 <size_cnv+0xb2>
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rsi,%rax
mul    %rdx
movabs $0xe8d4b45240,%rax
cmp    %rax,%rsi
jb     11c8 <size_cnv+0xb8>
lea    0x2(%rdi),%ebx
mov    %rsi,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rdx
mul    %rdx
shr    $0xb,%rdx
test   %ecx,%ecx
jne    11d1 <size_cnv+0xc1>
movabs $0x38d7ee0614a00,%rax
cmp    %rax,%rsi
jb     11d1 <size_cnv+0xc1>
add    $0x3,%edi
movabs $0x232f33025bd42233,%rcx
33 2f 23
mov    %rsi,%rax
mul    %rcx
shr    $0x25,%rdx
jmp    11cf <size_cnv+0xbf>
mov    %al,%bl
mov    %rsi,%rdx
jmp    11d1 <size_cnv+0xc1>
shr    $0x4,%rdx
jmp    11cf <size_cnv+0xbf>
add    $0x1,%edi
shr    $0x12,%rdx
mov    %edi,%ebx
mov    %ebx,%eax
lea    0xe26(%rip),%rcx
movsbl (%rax,%rcx,1),%ecx
lea    0xe20(%rip),%rsi
mov    %r14,%rdi
call   1030 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.size_cnv.suff = [5 x i8] c"\00KMGP"
@.str = [8 x i8] c"%'8Lu%c\00"
define i8* @size_cnv(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca [5 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = bitcast [5 x i8]* %7 to i8*
store i32 0, i32* %8
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* %8
%14 = add i32 %13, 1
store i32 %14, i32* %8
br label %15
15: ; preds = %12, %3
br label %16
16: ; preds = %25, %15
%17 = load i64, i64* %5
%18 = icmp ugt i64 %17, 1000000
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* %8
%21 = zext i32 %20 to i64
%22 = icmp ult i64 %21, 4
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = load i32, i32* %8
%27 = add i32 %26, 1
store i32 %27, i32* %8
%28 = load i64, i64* %5
%29 = udiv i64 %28, 1000
store i64 %29, i64* %5
br label %16
30: ; preds = %23
%31 = load i8*, i8** %4
%32 = load i64, i64* %5
%33 = load i32, i32* %8
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %32, i8 signext %36)
%38 = load i8*, i8** %4
ret i8* %38
}

LLVM IR (-O2):
@__const.size_cnv.suff = [5 x i8] c"\00KMGP"
@.str = [8 x i8] c"%'8Lu%c\00"
define i8* @size_cnv(i8* returned writeonly %0, i64 %1, i32 %2) {
%4 = icmp ne i32 %2, 0
%5 = zext i1 %4 to i32
%6 = icmp ugt i64 %1, 1000000
br i1 %6, label %7, label %23
7: ; preds = %3
%8 = select i1 %4, i32 2, i32 1
%9 = udiv i64 %1, 1000
%10 = icmp ugt i64 %1, 1000000999
br i1 %10, label %11, label %23
11: ; preds = %7
%12 = add nuw nsw i32 %8, 1
%13 = udiv i64 %1, 1000000
%14 = icmp ugt i64 %1, 1000000999999
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = add nuw nsw i32 %8, 2
%17 = udiv i64 %1, 1000000000
%18 = icmp ult i64 %1, 1000001000000000
%19 = or i1 %4, %18
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = add nuw nsw i32 %8, 3
%22 = udiv i64 %1, 1000000000000
br label %23
23: ; preds = %7, %11, %15, %20, %3
%24 = phi i64 [ %1, %3 ], [ %9, %7 ], [ %13, %11 ], [ %17, %15 ], [ %22, %20 ]
%25 = phi i32 [ %5, %3 ], [ %8, %7 ], [ %12, %11 ], [ %16, %15 ], [ %21, %20 ]
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds [5 x i8], [5 x i8]* @__const.size_cnv.suff, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %24, i8 signext %28)
ret i8* %0
}