#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 */
char *malloc(int);
int snprintf(char *, int, char *, double);
char *ftosi(double d) {
  char *format;
  if (d >= 1e6) {
    return NULL;
  } else if (d >= 1e3) {
    d /= 1e3;
    format = "%g k";
  } else if (d >= 1) {
    format = "%g ";
  } else if (d >= 1e-3) {
    d /= 1e-3;
    format = "%g m";
  } else if (d >= 1e-6) {
    d /= 1e-6;
    format = "%g u";
  } else if (d >= 1e-9) {
    d /= 1e-9;
    format = "%g n";
  } else {
    return NULL;
  }
  int size = snprintf(NULL, 0, format, d) + 1;
  char *str = malloc(size);
  snprintf(str, size, format, d);
  return str;
}


x86 assembly (clang, -O0):
<ftosi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xec6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1151 <ftosi+0x31>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <ftosi+0x1b5>
movsd  -0x10(%rbp),%xmm0
movsd  0xeaa(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     118e <ftosi+0x6e>
movsd  -0x10(%rbp),%xmm0
movsd  0xe93(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    0xeab(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    128d <ftosi+0x16d>
movsd  -0x10(%rbp),%xmm0
movsd  0xe75(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11b5 <ftosi+0x95>
lea    0xe89(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1288 <ftosi+0x168>
movsd  -0x10(%rbp),%xmm0
movsd  0xe56(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11f2 <ftosi+0xd2>
movsd  -0x10(%rbp),%xmm0
movsd  0xe3f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    0xe50(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1283 <ftosi+0x163>
movsd  -0x10(%rbp),%xmm0
movsd  0xe21(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     122f <ftosi+0x10f>
movsd  -0x10(%rbp),%xmm0
movsd  0xe0a(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    0xe18(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    127e <ftosi+0x15e>
movsd  -0x10(%rbp),%xmm0
movsd  0xdec(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     126c <ftosi+0x14c>
movsd  -0x10(%rbp),%xmm0
movsd  0xdd5(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
lea    0xde0(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1279 <ftosi+0x159>
movq   $0x0,-0x8(%rbp)
jmp    12d5 <ftosi+0x1b5>
jmp    127e <ftosi+0x15e>
jmp    1283 <ftosi+0x163>
jmp    1288 <ftosi+0x168>
jmp    128d <ftosi+0x16d>
jmp    1292 <ftosi+0x172>
mov    -0x18(%rbp),%rdx
movsd  -0x10(%rbp),%xmm0
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1030 <snprintf@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
movsd  -0x10(%rbp),%xmm0
call   1030 <snprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftosi>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
ucomisd 0xed0(%rip),%xmm0
jae    1164 <ftosi+0x44>
ucomisd 0xece(%rip),%xmm0
jae    1168 <ftosi+0x48>
ucomisd 0xecc(%rip),%xmm0
jae    1179 <ftosi+0x59>
ucomisd 0xeca(%rip),%xmm0
jae    1182 <ftosi+0x62>
ucomisd 0xec8(%rip),%xmm0
jae    1193 <ftosi+0x73>
ucomisd 0xec6(%rip),%xmm0
jae    11a4 <ftosi+0x84>
xor    %ebx,%ebx
jmp    11e5 <ftosi+0xc5>
divsd  0xe98(%rip),%xmm0
lea    0xeb9(%rip),%r14
jmp    11b3 <ftosi+0x93>
lea    0xeb5(%rip),%r14
jmp    11b3 <ftosi+0x93>
divsd  0xe8e(%rip),%xmm0
lea    0xea8(%rip),%r14
jmp    11b3 <ftosi+0x93>
divsd  0xe85(%rip),%xmm0
lea    0xe9c(%rip),%r14
jmp    11b3 <ftosi+0x93>
divsd  0xe7c(%rip),%xmm0
lea    0xe90(%rip),%r14
xor    %edi,%edi
xor    %esi,%esi
mov    %r14,%rdx
movsd  %xmm0,0x8(%rsp)
call   1030 <snprintf@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
movsd  0x8(%rsp),%xmm0
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%g k\00"
@.str.1 = [4 x i8] c"%g \00"
@.str.2 = [5 x i8] c"%g m\00"
@.str.3 = [5 x i8] c"%g u\00"
@.str.4 = [5 x i8] c"%g n\00"
define i8* @ftosi(double %0) {
%2 = alloca i8*
%3 = alloca double
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store double %0, double* %3
%7 = load double, double* %3
%8 = fcmp oge double %7, 1.000000e+06
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %57
10: ; preds = %1
%11 = load double, double* %3
%12 = fcmp oge double %11, 1.000000e+03
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load double, double* %3
%15 = fdiv double %14, 1.000000e+03
store double %15, double* %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
br label %43
16: ; preds = %10
%17 = load double, double* %3
%18 = fcmp oge double %17, 1.000000e+00
br i1 %18, label %19, label %20
19: ; preds = %16
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %4
br label %42
20: ; preds = %16
%21 = load double, double* %3
%22 = fcmp oge double %21, 1.000000e-03
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load double, double* %3
%25 = fdiv double %24, 1.000000e-03
store double %25, double* %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %4
br label %41
26: ; preds = %20
%27 = load double, double* %3
%28 = fcmp oge double %27, 0x3EB0C6F7A0B5ED8D
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load double, double* %3
%31 = fdiv double %30, 0x3EB0C6F7A0B5ED8D
store double %31, double* %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %4
br label %40
32: ; preds = %26
%33 = load double, double* %3
%34 = fcmp oge double %33, 1.000000e-09
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load double, double* %3
%37 = fdiv double %36, 1.000000e-09
store double %37, double* %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %4
br label %39
38: ; preds = %32
store i8* null, i8** %2
br label %57
39: ; preds = %35
br label %40
40: ; preds = %39, %29
br label %41
41: ; preds = %40, %23
br label %42
42: ; preds = %41, %19
br label %43
43: ; preds = %42, %13
br label %44
44: ; preds = %43
%45 = load i8*, i8** %4
%46 = load double, double* %3
%47 = call i32 @snprintf(i8* null, i32 0, i8* %45, double %46)
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = load i32, i32* %5
%53 = load i8*, i8** %4
%54 = load double, double* %3
%55 = call i32 @snprintf(i8* %51, i32 %52, i8* %53, double %54)
%56 = load i8*, i8** %6
store i8* %56, i8** %2
br label %57
57: ; preds = %44, %38, %9
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [5 x i8] c"%g k\00"
@.str.1 = [4 x i8] c"%g \00"
@.str.2 = [5 x i8] c"%g m\00"
@.str.3 = [5 x i8] c"%g u\00"
@.str.4 = [5 x i8] c"%g n\00"
define i8* @ftosi(double %0) {
%2 = fcmp ult double %0, 1.000000e+06
br i1 %2, label %3, label %28
3: ; preds = %1
%4 = fcmp ult double %0, 1.000000e+03
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = fdiv double %0, 1.000000e+03
br label %21
7: ; preds = %3
%8 = fcmp ult double %0, 1.000000e+00
br i1 %8, label %9, label %21
9: ; preds = %7
%10 = fcmp ult double %0, 1.000000e-03
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = fdiv double %0, 1.000000e-03
br label %21
13: ; preds = %9
%14 = fcmp ult double %0, 0x3EB0C6F7A0B5ED8D
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = fdiv double %0, 0x3EB0C6F7A0B5ED8D
br label %21
17: ; preds = %13
%18 = fcmp ult double %0, 1.000000e-09
br i1 %18, label %28, label %19
19: ; preds = %17
%20 = fdiv double %0, 1.000000e-09
br label %21
21: ; preds = %7, %5, %11, %19, %15
%22 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %19 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %7 ]
%23 = phi double [ %6, %5 ], [ %12, %11 ], [ %16, %15 ], [ %20, %19 ], [ %0, %7 ]
%24 = tail call i32 @snprintf(i8* null, i32 0, i8* %22, double %23)
%25 = add nsw i32 %24, 1
%26 = tail call i8* @malloc(i32 %25)
%27 = tail call i32 @snprintf(i8* %26, i32 %25, i8* %22, double %23)
br label %28
28: ; preds = %17, %1, %21
%29 = phi i8* [ %26, %21 ], [ null, %1 ], [ null, %17 ]
ret i8* %29
}