#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*/ free(char *);
char *int_to_str(int);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *fp_to_str(double x) {
  char *str = NULL;
  char *integral = NULL;
  char *fraction = NULL;
  size_t length = 0;
  integral = int_to_str((int)x);
  if (x < 0)
    x = -x;
  fraction = int_to_str((int)(100.0 * (x + 0.005 - (int)x)));
  length = strlen(integral) + strlen(fraction) + 2;
  if (strlen(fraction) < 2)
    ++length;
  str = (char *)malloc(length);
  strcpy(str, integral);
  strcat(str, ".");
  if (strlen(fraction) < 2)
    strcat(str, "0");
  strcat(str, fraction);
  free(integral);
  free(fraction);
  return str;
}


x86 assembly (clang, -O0):
<fp_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cvttsd2si -0x8(%rbp),%edi
call   1060 <int_to_str@plt>
mov    %rax,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11ca <fp_to_str+0x6a>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  0xe36(%rip),%xmm1
addsd  -0x8(%rbp),%xmm1
cvttsd2si -0x8(%rbp),%eax
cvtsi2sd %eax,%xmm0
subsd  %xmm0,%xmm1
movsd  0xe14(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1060 <int_to_str@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jge    1240 <fp_to_str+0xe0>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jge    128c <fp_to_str+0x12c>
mov    -0x10(%rbp),%rdi
lea    0xd8b(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fp_to_str>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,(%rsp)
cvttsd2si %xmm0,%edi
call   1060 <int_to_str@plt>
movapd 0xe81(%rip),%xmm0
movapd (%rsp),%xmm3
xorpd  %xmm3,%xmm0
xorpd  %xmm1,%xmm1
movapd %xmm3,%xmm2
cmpltsd %xmm1,%xmm2
andpd  %xmm2,%xmm0
andnpd %xmm3,%xmm2
orpd   %xmm0,%xmm2
cvttpd2dq %xmm2,%xmm0
addsd  0xe63(%rip),%xmm2
mov    %rax,%r14
cvtdq2pd %xmm0,%xmm0
subsd  %xmm0,%xmm2
mulsd  0xe58(%rip),%xmm2
cvttsd2si %xmm2,%edi
call   1060 <int_to_str@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
add    %ebx,%eax
add    $0x2,%eax
movslq %eax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
cmp    $0x2,%eax
setl   %dil
add    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jg     1238 <fp_to_str+0xd8>
lea    0xdf2(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"0\00"
define i8* @fp_to_str(double %0) {
%2 = alloca double
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store double %0, double* %2
store i8* null, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
store i64 0, i64* %6
%7 = load double, double* %2
%8 = fptosi double %7 to i32
%9 = call i8* @int_to_str(i32 %8)
store i8* %9, i8** %4
%10 = load double, double* %2
%11 = fcmp olt double %10, 0.000000e+00
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load double, double* %2
%14 = fneg double %13
store double %14, double* %2
br label %15
15: ; preds = %12, %1
%16 = load double, double* %2
%17 = fadd double %16, 5.000000e-03
%18 = load double, double* %2
%19 = fptosi double %18 to i32
%20 = sitofp i32 %19 to double
%21 = fsub double %17, %20
%22 = fmul double 1.000000e+02, %21
%23 = fptosi double %22 to i32
%24 = call i8* @int_to_str(i32 %23)
store i8* %24, i8** %5
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %29, 2
%31 = sext i32 %30 to i64
store i64 %31, i64* %6
%32 = load i8*, i8** %5
%33 = call i32 @strlen(i8* %32)
%34 = icmp slt i32 %33, 2
br i1 %34, label %35, label %38
35: ; preds = %15
%36 = load i64, i64* %6
%37 = add i64 %36, 1
store i64 %37, i64* %6
br label %38
38: ; preds = %35, %15
%39 = load i64, i64* %6
%40 = call i64 @malloc(i64 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %3
%42 = load i8*, i8** %3
%43 = load i8*, i8** %4
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %3
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%47 = load i8*, i8** %5
%48 = call i32 @strlen(i8* %47)
%49 = icmp slt i32 %48, 2
br i1 %49, label %50, label %53
50: ; preds = %38
%51 = load i8*, i8** %3
%52 = call i32 @strcat(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %50, %38
%54 = load i8*, i8** %3
%55 = load i8*, i8** %5
%56 = call i32 @strcat(i8* %54, i8* %55)
%57 = load i8*, i8** %4
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %5
%60 = call i32 @free(i8* %59)
%61 = load i8*, i8** %3
ret i8* %61
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"0\00"
define i8* @fp_to_str(double %0) {
%2 = fptosi double %0 to i32
%3 = tail call i8* @int_to_str(i32 %2)
%4 = fcmp olt double %0, 0.000000e+00
%5 = fneg double %0
%6 = select i1 %4, double %5, double %0
%7 = fadd double %6, 5.000000e-03
%8 = fptosi double %6 to i32
%9 = sitofp i32 %8 to double
%10 = fsub double %7, %9
%11 = fmul double %10, 1.000000e+02
%12 = fptosi double %11 to i32
%13 = tail call i8* @int_to_str(i32 %12)
%14 = tail call i32 @strlen(i8* %3)
%15 = tail call i32 @strlen(i8* %13)
%16 = add i32 %14, 2
%17 = add i32 %16, %15
%18 = sext i32 %17 to i64
%19 = tail call i32 @strlen(i8* %13)
%20 = icmp slt i32 %19, 2
%21 = zext i1 %20 to i64
%22 = add nsw i64 %21, %18
%23 = tail call i64 @malloc(i64 %22)
%24 = inttoptr i64 %23 to i8*
%25 = tail call i32 @strcpy(i8* %24, i8* %3)
%26 = tail call i32 @strcat(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = tail call i32 @strlen(i8* %13)
%28 = icmp slt i32 %27, 2
br i1 %28, label %29, label %31
29: ; preds = %1
%30 = tail call i32 @strcat(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %1
%32 = tail call i32 @strcat(i8* %24, i8* %13)
%33 = tail call i32 @free(i8* %3)
%34 = tail call i32 @free(i8* %13)
ret i8* %24
}