#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 *fToStringDec(float x, char *result) {
  int shift = 0;
  char *postfix = " kmgtp";
  while (x > 1000) {
    x = x / 1000;
    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):
<fToStringDec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xedd(%rip),%rax
mov    %rax,-0x20(%rbp)
movss  -0x4(%rbp),%xmm0
movss  0xec0(%rip),%xmm1
ucomiss %xmm1,%xmm0
jbe    116d <fToStringDec+0x5d>
movss  -0x4(%rbp),%xmm0
movss  0xeaa(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1133 <fToStringDec+0x23>
movss  0xe8f(%rip),%xmm0
ucomiss -0x4(%rbp),%xmm0
jbe    11a5 <fToStringDec+0x95>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe7c(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    1203 <fToStringDec+0xf3>
movss  0xe5b(%rip),%xmm0
ucomiss -0x4(%rbp),%xmm0
jbe    11dd <fToStringDec+0xcd>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe4c(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    11fe <fToStringDec+0xee>
mov    -0x10(%rbp),%rdi
movss  -0x4(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
lea    0xe2e(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
jmp    1203 <fToStringDec+0xf3>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fToStringDec>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
ucomiss 0xee3(%rip),%xmm0
jbe    113d <fToStringDec+0x2d>
movss  0xed9(%rip),%xmm1
nopw   0x0(%rax,%rax,1)
divss  %xmm1,%xmm0
add    $0x1,%rax
ucomiss %xmm1,%xmm0
ja     1130 <fToStringDec+0x20>
movss  0xebf(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    115e <fToStringDec+0x4e>
lea    0xebb(%rip),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xeb7(%rip),%rsi
jmp    1188 <fToStringDec+0x78>
movss  0xea2(%rip),%xmm1
ucomiss %xmm0,%xmm1
lea    0xe9c(%rip),%rcx
mov    (%rax,%rcx,1),%al
movsbl %al,%edx
jbe    1181 <fToStringDec+0x71>
lea    0xe9c(%rip),%rsi
jmp    1188 <fToStringDec+0x78>
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [7 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* @fToStringDec(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 = fdiv float %5, 1.000000e+03
%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 [7 x i8], [7 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 [7 x i8], [7 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
}