#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 *, float, char);
char *fToStringBin(float x, char *result) {
  int shift = 0;
  char *postfix = "KMGTP";
  while (x > 1000.0) {
    x = x / 1024;
    shift++;
  }
  if (x < 10)
    sprintf(result, "%4.3f%c", x, postfix[shift]);
  else if (x < 100)
    sprintf(result, "%4.2f%c", x, postfix[shift]);
  else
    sprintf(result, "%4.1f%c", x, postfix[shift]);
  return (result);
}


x86 assembly (clang, -O0):
<fToStringBin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xee5(%rip),%rax
mov    %rax,-0x20(%rbp)
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xebc(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1172 <fToStringBin+0x62>
movss  -0x4(%rbp),%xmm0
movss  0xeb5(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1133 <fToStringBin+0x23>
movss  0xe8e(%rip),%xmm0
ucomiss -0x4(%rbp),%xmm0
jbe    11aa <fToStringBin+0x9a>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe7e(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    1208 <fToStringBin+0xf8>
movss  0xe5a(%rip),%xmm0
ucomiss -0x4(%rbp),%xmm0
jbe    11e2 <fToStringBin+0xd2>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe4e(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    1203 <fToStringBin+0xf3>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe30(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    1208 <fToStringBin+0xf8>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fToStringBin>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
ucomiss 0xee3(%rip),%xmm0
jbe    113d <fToStringBin+0x2d>
movss  0xedd(%rip),%xmm1
movss  0xed1(%rip),%xmm2
nop
mulss  %xmm1,%xmm0
add    $0x1,%rax
ucomiss %xmm2,%xmm0
ja     1130 <fToStringBin+0x20>
movss  0xec3(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    115e <fToStringBin+0x4e>
lea    0xebf(%rip),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xeba(%rip),%rsi
jmp    1188 <fToStringBin+0x78>
movss  0xea6(%rip),%xmm1
ucomiss %xmm0,%xmm1
lea    0xea0(%rip),%rcx
mov    (%rax,%rcx,1),%al
movsbl %al,%edx
jbe    1181 <fToStringBin+0x71>
lea    0xe9f(%rip),%rsi
jmp    1188 <fToStringBin+0x78>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"KMGTP\00"
@.str.1 = [8 x i8] c"%4.3f%c\00"
@.str.2 = [8 x i8] c"%4.2f%c\00"
@.str.3 = [8 x i8] c"%4.1f%c\00"
define i8* @fToStringBin(float %0, i8* %1) {
%3 = alloca float
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store float %0, float* %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %6
br label %7
7: ; preds = %11, %2
%8 = load float, float* %3
%9 = fpext float %8 to double
%10 = fcmp ogt double %9, 1.000000e+03
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load float, float* %3
%13 = fdiv float %12, 1.024000e+03
store float %13, float* %3
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
br label %7
16: ; preds = %7
%17 = load float, float* %3
%18 = fcmp olt float %17, 1.000000e+01
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = load float, float* %3
%22 = load i8*, i8** %6
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), float %21, i8 signext %26)
br label %50
28: ; preds = %16
%29 = load float, float* %3
%30 = fcmp olt float %29, 1.000000e+02
br i1 %30, label %31, label %40
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = load float, float* %3
%34 = load i8*, i8** %6
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), float %33, i8 signext %38)
br label %49
40: ; preds = %28
%41 = load i8*, i8** %4
%42 = load float, float* %3
%43 = load i8*, i8** %6
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), float %42, i8 signext %47)
br label %49
49: ; preds = %40, %31
br label %50
50: ; preds = %49, %19
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
@.str = [6 x i8] c"KMGTP\00"
@.str.1 = [8 x i8] c"%4.3f%c\00"
@.str.2 = [8 x i8] c"%4.2f%c\00"
@.str.3 = [8 x i8] c"%4.1f%c\00"
define i8* @fToStringBin(float %0, i8* returned writeonly %1) {
%3 = fcmp ogt float %0, 1.000000e+03
br i1 %3, label %4, label %12
4: ; preds = %2, %4
%5 = phi float [ %7, %4 ], [ %0, %2 ]
%6 = phi i32 [ %8, %4 ], [ 0, %2 ]
%7 = fmul float %5, 0x3F50000000000000
%8 = add nuw nsw i32 %6, 1
%9 = fcmp ogt float %7, 1.000000e+03
br i1 %9, label %4, label %10
10: ; preds = %4
%11 = zext i32 %8 to i64
br label %12
12: ; preds = %10, %2
%13 = phi i64 [ 0, %2 ], [ %11, %10 ]
%14 = phi float [ %0, %2 ], [ %7, %10 ]
%15 = fcmp olt float %14, 1.000000e+01
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = getelementptr inbounds [6 x i8], [6 x i8]* @.str, i64 0, i64 %13
%18 = load i8, i8* %17
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), float %14, i8 signext %18)
br label %28
20: ; preds = %12
%21 = fcmp olt float %14, 1.000000e+02
%22 = getelementptr inbounds [6 x i8], [6 x i8]* @.str, i64 0, i64 %13
%23 = load i8, i8* %22
br i1 %21, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), float %14, i8 signext %23)
br label %28
26: ; preds = %20
%27 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), float %14, i8 signext %23)
br label %28
28: ; preds = %24, %26, %16
ret i8* %1
}