#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*/ cat_n(char *, unsigned long);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *libmail_str_sizekb(unsigned long n, char *sizebuf) {
  if (n < 1024) {
    strcpy(sizebuf, "0.");
    cat_n(sizebuf, (int)(10 * n / 1024));
    strcat(sizebuf, "K");
  } else if (n < 1024 * 1024) {
    *sizebuf = 0;
    cat_n(sizebuf, (unsigned long)(n + 512) / 1024);
    strcat(sizebuf, "K");
  } else {
    unsigned long nm = (double)n / (1024.0 * 1024.0) * 10;
    *sizebuf = 0;
    cat_n(sizebuf, nm / 10);
    strcat(sizebuf, ".");
    cat_n(sizebuf, nm % 10);
    strcat(sizebuf, "M");
  }
  return (sizebuf);
}


x86 assembly (clang, -O0):
<libmail_str_sizekb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x400,-0x8(%rbp)
jae    1188 <libmail_str_sizekb+0x58>
mov    -0x10(%rbp),%rdi
lea    0xedf(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
imul   $0xa,-0x8(%rbp),%rax
shr    $0xa,%rax
movslq %eax,%rsi
call   1040 <cat_n@plt>
mov    -0x10(%rbp),%rdi
lea    0xebd(%rip),%rsi
call   1050 <strcat@plt>
jmp    1293 <libmail_str_sizekb+0x163>
cmpq   $0x100000,-0x8(%rbp)
jae    11ca <libmail_str_sizekb+0x9a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x200,%rsi
shr    $0xa,%rsi
call   1040 <cat_n@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1050 <strcat@plt>
jmp    128e <libmail_str_sizekb+0x15e>
movaps 0xe2f(%rip),%xmm1
movq   -0x8(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xe2e(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe27(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe23(%rip),%xmm1
mulsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe10(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rsi
call   1040 <cat_n@plt>
mov    -0x10(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,%rsi
call   1040 <cat_n@plt>
mov    -0x10(%rbp),%rdi
lea    0xdb4(%rip),%rsi
call   1050 <strcat@plt>
jmp    1293 <libmail_str_sizekb+0x163>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libmail_str_sizekb>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
cmp    $0x3ff,%rdi
ja     1163 <libmail_str_sizekb+0x33>
lea    0xeed(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
add    %rbx,%rbx
lea    (%rbx,%rbx,4),%rsi
shr    $0xa,%rsi
mov    %r14,%rdi
jmp    1181 <libmail_str_sizekb+0x51>
cmp    $0xfffff,%rbx
ja     1192 <libmail_str_sizekb+0x62>
movb   $0x0,(%r14)
add    $0x200,%rbx
shr    $0xa,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <cat_n@plt>
lea    0xeae(%rip),%rsi
jmp    1233 <libmail_str_sizekb+0x103>
movq   %rbx,%xmm0
punpckldq 0xe61(%rip),%xmm0
subpd  0xe69(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
mulsd  0xe65(%rip),%xmm1
mulsd  0xe65(%rip),%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rcx
subsd  0xe5d(%rip),%xmm1
cvttsd2si %xmm1,%r15
sar    $0x3f,%rcx
and    %rcx,%r15
or     %rax,%r15
movb   $0x0,(%r14)
movabs $0xcccccccccccccccd,%rcx
cc cc cc
mov    %r15,%rax
mul    %rcx
mov    %rdx,%rbx
shr    $0x3,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <cat_n@plt>
lea    0xe2a(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcat@plt>
add    %rbx,%rbx
lea    (%rbx,%rbx,4),%rax
sub    %rax,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <cat_n@plt>
lea    0xe0a(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcat@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0.\00"
@.str.1 = [2 x i8] c"K\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"M\00"
define i8* @libmail_str_sizekb(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
store i64 %0, i64* %3
store i8* %1, i8** %4
%6 = load i64, i64* %3
%7 = icmp ult i64 %6, 1024
br i1 %7, label %8, label %20
8: ; preds = %2
%9 = load i8*, i8** %4
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %4
%12 = load i64, i64* %3
%13 = mul i64 10, %12
%14 = udiv i64 %13, 1024
%15 = trunc i64 %14 to i32
%16 = sext i32 %15 to i64
%17 = call i32 @cat_n(i8* %11, i64 %16)
%18 = load i8*, i8** %4
%19 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %52
20: ; preds = %2
%21 = load i64, i64* %3
%22 = icmp ult i64 %21, 1048576
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = load i8*, i8** %4
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = load i64, i64* %3
%27 = add i64 %26, 512
%28 = udiv i64 %27, 1024
%29 = call i32 @cat_n(i8* %25, i64 %28)
%30 = load i8*, i8** %4
%31 = call i32 @strcat(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %51
32: ; preds = %20
%33 = load i64, i64* %3
%34 = uitofp i64 %33 to double
%35 = fdiv double %34, 0x4130000000000000
%36 = fmul double %35, 1.000000e+01
%37 = fptoui double %36 to i64
store i64 %37, i64* %5
%38 = load i8*, i8** %4
store i8 0, i8* %38
%39 = load i8*, i8** %4
%40 = load i64, i64* %5
%41 = udiv i64 %40, 10
%42 = call i32 @cat_n(i8* %39, i64 %41)
%43 = load i8*, i8** %4
%44 = call i32 @strcat(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = load i8*, i8** %4
%46 = load i64, i64* %5
%47 = urem i64 %46, 10
%48 = call i32 @cat_n(i8* %45, i64 %47)
%49 = load i8*, i8** %4
%50 = call i32 @strcat(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %32, %23
br label %52
52: ; preds = %51, %8
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"0.\00"
@.str.1 = [2 x i8] c"K\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"M\00"
define i8* @libmail_str_sizekb(i64 %0, i8* returned %1) {
%3 = icmp ult i64 %0, 1024
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = mul nuw nsw i64 %0, 10
%7 = lshr i64 %6, 10
%8 = tail call i32 @cat_n(i8* %1, i64 %7)
%9 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %28
10: ; preds = %2
%11 = icmp ult i64 %0, 1048576
br i1 %11, label %12, label %17
12: ; preds = %10
store i8 0, i8* %1
%13 = add nuw nsw i64 %0, 512
%14 = lshr i64 %13, 10
%15 = tail call i32 @cat_n(i8* %1, i64 %14)
%16 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %28
17: ; preds = %10
%18 = uitofp i64 %0 to double
%19 = fmul double %18, 0x3EB0000000000000
%20 = fmul double %19, 1.000000e+01
%21 = fptoui double %20 to i64
store i8 0, i8* %1
%22 = udiv i64 %21, 10
%23 = tail call i32 @cat_n(i8* %1, i64 %22)
%24 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = urem i64 %21, 10
%26 = tail call i32 @cat_n(i8* %1, i64 %25)
%27 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %12, %17, %4
ret i8* %1
}