#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*/ sprintf(char *, char *, int, ...);
void format_size(char *string, int size) {
  if (size > (1024 * 1024)) {
    int sz = size / (1024 * 1024);
    int dc = (size % (1024 * 1024)) / (1024 * 10);
    if (dc > 0) {
      if (dc < 10)
        sprintf(string, "%d.0%d MB", sz, dc);
      else if (dc < 100)
        sprintf(string, "%d.%d MB", sz, dc);
      else
        sprintf(string, "%d.99 MB", sz);
    } else
      sprintf(string, "%d MB", sz);
  } else if (size > 1024)
    sprintf(string, "%d KB", (size / 1024));
  else
    sprintf(string, "%d bytes", size);
}


x86 assembly (clang, -O0):
<format_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x100000,-0xc(%rbp)
jle    11e3 <format_size+0xd3>
mov    -0xc(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x2800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11c9 <format_size+0xb9>
cmpl   $0xa,-0x14(%rbp)
jge    1183 <format_size+0x73>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11c4 <format_size+0xb4>
cmpl   $0x64,-0x14(%rbp)
jge    11aa <format_size+0x9a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11bf <format_size+0xaf>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    11c4 <format_size+0xb4>
jmp    11de <format_size+0xce>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    122e <format_size+0x11e>
cmpl   $0x400,-0xc(%rbp)
jle    1214 <format_size+0x104>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    1229 <format_size+0x119>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    122e <format_size+0x11e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_size>:
mov    %esi,%eax
cmp    $0x100001,%esi
jl     114d <format_size+0x3d>
mov    %eax,%edx
shr    $0x14,%edx
and    $0xfffff,%eax
cmp    $0x2800,%eax
jb     1160 <format_size+0x50>
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x2d,%rcx
cmp    $0x18fff,%eax
ja     117e <format_size+0x6e>
lea    0xeba(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>
cmp    $0x401,%eax
jl     116e <format_size+0x5e>
shr    $0xa,%eax
lea    0xebe(%rip),%rsi
jmp    1175 <format_size+0x65>
lea    0xea6(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>
lea    0xead(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
jmp    1030 <sprintf@plt>
cmp    $0xf9fff,%eax
ja     1193 <format_size+0x83>
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>
lea    0xe79(%rip),%rsi
xor    %eax,%eax
jmp    1030 <sprintf@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%d.0%d MB\00"
@.str.1 = [9 x i8] c"%d.%d MB\00"
@.str.2 = [9 x i8] c"%d.99 MB\00"
@.str.3 = [6 x i8] c"%d MB\00"
@.str.4 = [6 x i8] c"%d KB\00"
@.str.5 = [9 x i8] c"%d bytes\00"
define void @format_size(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp sgt i32 %7, 1048576
br i1 %8, label %9, label %44
9: ; preds = %2
%10 = load i32, i32* %4
%11 = sdiv i32 %10, 1048576
store i32 %11, i32* %5
%12 = load i32, i32* %4
%13 = srem i32 %12, 1048576
%14 = sdiv i32 %13, 10240
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %9
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 10
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
br label %38
25: ; preds = %17
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 100
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %31)
br label %37
33: ; preds = %25
%34 = load i8*, i8** %3
%35 = load i32, i32* %5
%36 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %28
br label %38
38: ; preds = %37, %20
br label %43
39: ; preds = %9
%40 = load i8*, i8** %3
%41 = load i32, i32* %5
%42 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %39, %38
br label %57
44: ; preds = %2
%45 = load i32, i32* %4
%46 = icmp sgt i32 %45, 1024
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load i8*, i8** %3
%49 = load i32, i32* %4
%50 = sdiv i32 %49, 1024
%51 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %56
52: ; preds = %44
%53 = load i8*, i8** %3
%54 = load i32, i32* %4
%55 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %53, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %52, %47
br label %57
57: ; preds = %56, %43
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"%d.0%d MB\00"
@.str.1 = [9 x i8] c"%d.%d MB\00"
@.str.2 = [9 x i8] c"%d.99 MB\00"
@.str.3 = [6 x i8] c"%d MB\00"
@.str.4 = [6 x i8] c"%d KB\00"
@.str.5 = [9 x i8] c"%d bytes\00"
define void @format_size(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 1048576
br i1 %3, label %4, label %21
4: ; preds = %2
%5 = lshr i32 %1, 20
%6 = and i32 %1, 1048575
%7 = udiv i32 %6, 10240
%8 = icmp ugt i32 %6, 10239
br i1 %8, label %9, label %19
9: ; preds = %4
%10 = icmp ult i32 %6, 102400
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %7)
br label %28
13: ; preds = %9
%14 = icmp ult i32 %6, 1024000
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %7)
br label %28
17: ; preds = %13
%18 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %5)
br label %28
19: ; preds = %4
%20 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %5)
br label %28
21: ; preds = %2
%22 = icmp sgt i32 %1, 1024
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = lshr i32 %1, 10
%25 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %28
26: ; preds = %21
%27 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %1)
br label %28
28: ; preds = %19, %15, %17, %11, %23, %26
ret void
}