#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*/ s_itoa(unsigned int, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void s_size(unsigned int size_B, char *s, int n) {
  char tmp[32];
  int d = 0;
  while (size_B >= 1024) {
    size_B /= 1024;
    d++;
  }
  s_itoa(size_B, tmp);
  switch (d) {
  case 0:
    strcat(tmp, " B");
    break;
  case 1:
    strcat(tmp, " KB");
    break;
  case 2:
    strcat(tmp, " MB");
    break;
  case 3:
    strcat(tmp, " GB");
    break;
  }
  strncpy(s, tmp, n);
}


x86 assembly (clang, -O0):
<s_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x400,-0x4(%rbp)
jb     116d <s_size+0x3d>
mov    -0x4(%rbp),%eax
shr    $0xa,%eax
mov    %eax,-0x4(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1149 <s_size+0x19>
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <s_itoa@plt>
mov    -0x44(%rbp),%eax
mov    %rax,-0x50(%rbp)
sub    $0x3,%rax
ja     11ed <s_size+0xbd>
mov    -0x50(%rbp),%rax
lea    0xe6b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0x40(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1040 <strcat@plt>
jmp    11ed <s_size+0xbd>
lea    -0x40(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1040 <strcat@plt>
jmp    11ed <s_size+0xbd>
lea    -0x40(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1040 <strcat@plt>
jmp    11ed <s_size+0xbd>
lea    -0x40(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
cmp    $0x400,%edi
jb     117d <s_size+0x4d>
mov    $0xffffffffffffffff,%rbp
nopl   (%rax)
mov    %edi,%eax
shr    $0xa,%edi
add    $0x1,%rbp
cmp    $0xfffff,%eax
ja     1150 <s_size+0x20>
mov    %rsp,%rsi
call   1050 <s_itoa@plt>
cmp    $0x2,%ebp
ja     1194 <s_size+0x64>
lea    0xe9c(%rip),%rax
movslq (%rax,%rbp,4),%rsi
add    %rax,%rsi
jmp    118c <s_size+0x5c>
mov    %rsp,%rsi
call   1050 <s_itoa@plt>
lea    0xe74(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c" B\00"
@.str.1 = [4 x i8] c" KB\00"
@.str.2 = [4 x i8] c" MB\00"
@.str.3 = [4 x i8] c" GB\00"
define void @s_size(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %9
9: ; preds = %12, %3
%10 = load i32, i32* %4
%11 = icmp uge i32 %10, 1024
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* %4
%14 = udiv i32 %13, 1024
store i32 %14, i32* %4
%15 = load i32, i32* %8
%16 = add nsw i32 %15, 1
store i32 %16, i32* %8
br label %9
17: ; preds = %9
%18 = load i32, i32* %4
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%20 = call i32 @s_itoa(i32 %18, i8* %19)
%21 = load i32, i32* %8
switch i32 %21, label %34 [
i32 0, label %22
i32 1, label %25
i32 2, label %28
i32 3, label %31
]
22: ; preds = %17
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%24 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %34
25: ; preds = %17
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%27 = call i32 @strcat(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %34
28: ; preds = %17
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%30 = call i32 @strcat(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %34
31: ; preds = %17
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %17, %31, %28, %25, %22
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %6
%38 = call i32 @strncpy(i8* %35, i8* %36, i32 %37)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c" B\00"
@.str.1 = [4 x i8] c" KB\00"
@.str.2 = [4 x i8] c" MB\00"
@.str.3 = [4 x i8] c" GB\00"
@reltable.s_size = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.s_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.s_size to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([4 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.s_size to i64)) to i32)]
define void @s_size(i32 %0, i8* %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = icmp ugt i32 %0, 1023
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = call i32 @s_itoa(i32 %0, i8* %5)
br label %22
9: ; preds = %3, %9
%10 = phi i32 [ %13, %9 ], [ 0, %3 ]
%11 = phi i32 [ %12, %9 ], [ %0, %3 ]
%12 = lshr i32 %11, 10
%13 = add nuw nsw i32 %10, 1
%14 = icmp ugt i32 %11, 1048575
br i1 %14, label %9, label %15
15: ; preds = %9
%16 = call i32 @s_itoa(i32 %12, i8* %5)
%17 = icmp ult i32 %10, 3
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = zext i32 %10 to i64
%20 = shl i64 %19, 2
%21 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.s_size to i8*), i64 %20)
br label %22
22: ; preds = %18, %7
%23 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %7 ], [ %21, %18 ]
%24 = call i32 @strcat(i8* %5, i8* %23)
br label %25
25: ; preds = %15, %22
%26 = call i32 @strncpy(i8* %1, i8* %5, i32 %2)
ret void
}