#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 */
double fabs(double);
double floor(double);
double log10(double);
double pow(int, int);
int /*<<< orphan*/ sprintf(char *, char *, double, char const, char const *);
char *format_scientific(double v, const char *unit, char *f) {
  static const char *multiple = "EPTGMk mµnpfa";
  int u;
  if (fabs(v) < 1e-24) {
    u = 0;
  } else {
    u = floor(log10(fabs(v)) / 3.0);
    if (u > 6)
      u = 6;
    else if (u < -6)
      u = -6;
    v /= pow(10, 3 * u);
  }
  if (fabs(v) - floor(fabs(v)) < 0.01)
    sprintf(f, " %5.1f %c%s", v, multiple[6 - u], unit);
  else if (fabs(v + 0.05) < 10.0)
    sprintf(f, " %5.3f  %c%s", v, multiple[6 - u], unit);
  else if (fabs(v + 0.5) < 100.0)
    sprintf(f, " %5.2f  %c%s", v, multiple[6 - u], unit);
  else
    sprintf(f, " %5.1f  %c%s", v, multiple[6 - u], unit);
  return f;
}


x86 assembly (clang, -O0):
<format_scientific>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm1
movaps 0xedf(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xe93(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1183 <format_scientific+0x43>
movl   $0x0,-0x1c(%rbp)
jmp    11fb <format_scientific+0xbb>
movsd  -0x8(%rbp),%xmm0
movaps 0xeb1(%rip),%xmm1
pand   %xmm1,%xmm0
call   1040 <log10@plt>
movsd  0xe68(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1060 <floor@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x6,-0x1c(%rbp)
jle    11c6 <format_scientific+0x86>
movl   $0x6,-0x1c(%rbp)
jmp    11dc <format_scientific+0x9c>
cmpl   $0xfffffffa,-0x1c(%rbp)
jge    11d7 <format_scientific+0x97>
movl   $0xfffffffa,-0x1c(%rbp)
jmp    11dc <format_scientific+0x9c>
imul   $0x3,-0x1c(%rbp),%esi
mov    $0xa,%edi
call   1030 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x8(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movaps 0xe39(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
movaps 0xe24(%rip),%xmm1
pand   %xmm1,%xmm0
call   1060 <floor@plt>
movsd  -0x28(%rbp),%xmm1
subsd  %xmm0,%xmm1
movsd  0xdda(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1276 <format_scientific+0x136>
mov    -0x18(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    0x2df0(%rip),%rax
mov    $0x6,%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x10(%rbp),%rcx
lea    0xdf6(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
jmp    1371 <format_scientific+0x231>
movsd  0xda2(%rip),%xmm1
addsd  -0x8(%rbp),%xmm1
movaps 0xdb6(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xd82(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    12d6 <format_scientific+0x196>
mov    -0x18(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    0x2d90(%rip),%rax
mov    $0x6,%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x10(%rbp),%rcx
lea    0xda2(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
jmp    136c <format_scientific+0x22c>
movsd  0xd52(%rip),%xmm1
addsd  -0x8(%rbp),%xmm1
movaps 0xd56(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xd32(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1336 <format_scientific+0x1f6>
mov    -0x18(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    0x2d30(%rip),%rax
mov    $0x6,%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x10(%rbp),%rcx
lea    0xd4f(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
jmp    1367 <format_scientific+0x227>
mov    -0x18(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    0x2cfa(%rip),%rax
mov    $0x6,%ecx
sub    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x10(%rbp),%rcx
lea    0xd26(%rip),%rsi
movsbl %al,%edx
call   1050 <sprintf@plt>
jmp    136c <format_scientific+0x22c>
jmp    1371 <format_scientific+0x231>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_scientific>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
movapd %xmm0,%xmm1
movapd 0xea3(%rip),%xmm0
andpd  %xmm1,%xmm0
mov    $0x6,%r12d
movsd  0xea1(%rip),%xmm2
ucomisd %xmm0,%xmm2
movapd %xmm1,(%rsp)
ja     11d2 <format_scientific+0x92>
call   1040 <log10@plt>
divsd  0xe91(%rip),%xmm0
call   1060 <floor@plt>
cvttsd2si %xmm0,%eax
cmp    $0xfffffffb,%eax
mov    $0xfffffffa,%ebx
cmovge %eax,%ebx
cmp    $0x6,%ebx
mov    $0x6,%r12d
cmovge %r12d,%ebx
lea    (%rbx,%rbx,2),%esi
mov    $0xa,%edi
call   1030 <pow@plt>
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
sub    %ebx,%r12d
movapd 0xe37(%rip),%xmm0
movapd %xmm1,(%rsp)
andpd  %xmm1,%xmm0
movapd %xmm0,0x10(%rsp)
call   1060 <floor@plt>
movapd 0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  0xe31(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1212 <format_scientific+0xd2>
lea    0xe4c(%rip),%rax
movsbl (%r12,%rax,1),%edx
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
movapd (%rsp),%xmm0
jmp    1291 <format_scientific+0x151>
movsd  0xe0e(%rip),%xmm2
movapd (%rsp),%xmm0
addsd  %xmm0,%xmm2
andpd  0xdd5(%rip),%xmm2
movsd  0xdfd(%rip),%xmm1
ucomisd %xmm2,%xmm1
jbe    124e <format_scientific+0x10e>
lea    0xe08(%rip),%rax
movsbl (%r12,%rax,1),%edx
lea    0xe17(%rip),%rsi
jmp    128e <format_scientific+0x14e>
movsd  0xde2(%rip),%xmm2
addsd  %xmm0,%xmm2
andpd  0xd9e(%rip),%xmm2
movsd  0xdd6(%rip),%xmm1
ucomisd %xmm2,%xmm1
lea    0xdd3(%rip),%rax
mov    (%r12,%rax,1),%al
movsbl %al,%edx
jbe    1287 <format_scientific+0x147>
lea    0xdeb(%rip),%rsi
jmp    128e <format_scientific+0x14e>
lea    0xdef(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rcx
call   1050 <sprintf@plt>
mov    %r15,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@format_scientific.multiple = internal global i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0)
@.str = [15 x i8] c"EPTGMk m\C2\B5npfa\00"
@.str.1 = [12 x i8] c" %5.1f %c%s\00"
@.str.2 = [13 x i8] c" %5.3f %c%s\00"
@.str.3 = [13 x i8] c" %5.2f %c%s\00"
@.str.4 = [13 x i8] c" %5.1f %c%s\00"
define i8* @format_scientific(double %0, i8* %1, i8* %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load double, double* %4
%9 = call double @llvm.fabs.f64(double %8)
%10 = fcmp olt double %9, 0x3AF357C299A88EA7
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %7
br label %33
12: ; preds = %3
%13 = load double, double* %4
%14 = call double @llvm.fabs.f64(double %13)
%15 = call double @log10(double %14)
%16 = fdiv double %15, 3.000000e+00
%17 = call double @llvm.floor.f64(double %16)
%18 = fptosi double %17 to i32
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp sgt i32 %19, 6
br i1 %20, label %21, label %22
21: ; preds = %12
store i32 6, i32* %7
br label %27
22: ; preds = %12
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, -6
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 -6, i32* %7
br label %26
26: ; preds = %25, %22
br label %27
27: ; preds = %26, %21
%28 = load i32, i32* %7
%29 = mul nsw i32 3, %28
%30 = call double @pow(i32 10, i32 %29)
%31 = load double, double* %4
%32 = fdiv double %31, %30
store double %32, double* %4
br label %33
33: ; preds = %27, %11
%34 = load double, double* %4
%35 = call double @llvm.fabs.f64(double %34)
%36 = load double, double* %4
%37 = call double @llvm.fabs.f64(double %36)
%38 = call double @llvm.floor.f64(double %37)
%39 = fsub double %35, %38
%40 = fcmp olt double %39, 1.000000e-02
br i1 %40, label %41, label %52
41: ; preds = %33
%42 = load i8*, i8** %6
%43 = load double, double* %4
%44 = load i8*, i8** @format_scientific.multiple
%45 = load i32, i32* %7
%46 = sub nsw i32 6, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = load i8, i8* %48
%50 = load i8*, i8** %5
%51 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %43, i8 signext %49, i8* %50)
br label %97
52: ; preds = %33
%53 = load double, double* %4
%54 = fadd double %53, 5.000000e-02
%55 = call double @llvm.fabs.f64(double %54)
%56 = fcmp olt double %55, 1.000000e+01
br i1 %56, label %57, label %68
57: ; preds = %52
%58 = load i8*, i8** %6
%59 = load double, double* %4
%60 = load i8*, i8** @format_scientific.multiple
%61 = load i32, i32* %7
%62 = sub nsw i32 6, %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
%65 = load i8, i8* %64
%66 = load i8*, i8** %5
%67 = call i32 @sprintf(i8* %58, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), double %59, i8 signext %65, i8* %66)
br label %96
68: ; preds = %52
%69 = load double, double* %4
%70 = fadd double %69, 5.000000e-01
%71 = call double @llvm.fabs.f64(double %70)
%72 = fcmp olt double %71, 1.000000e+02
br i1 %72, label %73, label %84
73: ; preds = %68
%74 = load i8*, i8** %6
%75 = load double, double* %4
%76 = load i8*, i8** @format_scientific.multiple
%77 = load i32, i32* %7
%78 = sub nsw i32 6, %77
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %76, i64 %79
%81 = load i8, i8* %80
%82 = load i8*, i8** %5
%83 = call i32 @sprintf(i8* %74, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), double %75, i8 signext %81, i8* %82)
br label %95
84: ; preds = %68
%85 = load i8*, i8** %6
%86 = load double, double* %4
%87 = load i8*, i8** @format_scientific.multiple
%88 = load i32, i32* %7
%89 = sub nsw i32 6, %88
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %87, i64 %90
%92 = load i8, i8* %91
%93 = load i8*, i8** %5
%94 = call i32 @sprintf(i8* %85, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), double %86, i8 signext %92, i8* %93)
br label %95
95: ; preds = %84, %73
br label %96
96: ; preds = %95, %57
br label %97
97: ; preds = %96, %41
%98 = load i8*, i8** %6
ret i8* %98
}

LLVM IR (-O2):
@.str = [15 x i8] c"EPTGMk m\C2\B5npfa\00"
@.str.1 = [12 x i8] c" %5.1f %c%s\00"
@.str.2 = [13 x i8] c" %5.3f %c%s\00"
@.str.3 = [13 x i8] c" %5.2f %c%s\00"
@.str.4 = [13 x i8] c" %5.1f %c%s\00"
define i8* @format_scientific(double %0, i8* %1, i8* returned writeonly %2) {
%4 = tail call double @llvm.fabs.f64(double %0)
%5 = fcmp olt double %4, 0x3AF357C299A88EA7
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = tail call double @log10(double %4)
%8 = fdiv double %7, 3.000000e+00
%9 = tail call double @llvm.floor.f64(double %8)
%10 = fptosi double %9 to i32
%11 = icmp sgt i32 %10, -6
%12 = select i1 %11, i32 %10, i32 -6
%13 = icmp slt i32 %12, 6
%14 = select i1 %13, i32 %12, i32 6
%15 = mul nsw i32 %14, 3
%16 = tail call double @pow(i32 10, i32 %15)
%17 = fdiv double %0, %16
%18 = sub nsw i32 6, %14
%19 = zext i32 %18 to i64
%20 = tail call double @llvm.fabs.f64(double %17)
br label %21
21: ; preds = %3, %6
%22 = phi double [ %4, %3 ], [ %20, %6 ]
%23 = phi double [ %0, %3 ], [ %17, %6 ]
%24 = phi i64 [ 6, %3 ], [ %19, %6 ]
%25 = tail call double @llvm.floor.f64(double %22)
%26 = fsub double %22, %25
%27 = fcmp olt double %26, 1.000000e-02
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = getelementptr inbounds [15 x i8], [15 x i8]* @.str, i64 0, i64 %24
%30 = load i8, i8* %29
%31 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), double %23, i8 signext %30, i8* %1)
br label %50
32: ; preds = %21
%33 = fadd double %23, 5.000000e-02
%34 = tail call double @llvm.fabs.f64(double %33)
%35 = fcmp olt double %34, 1.000000e+01
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = getelementptr inbounds [15 x i8], [15 x i8]* @.str, i64 0, i64 %24
%38 = load i8, i8* %37
%39 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), double %23, i8 signext %38, i8* %1)
br label %50
40: ; preds = %32
%41 = fadd double %23, 5.000000e-01
%42 = tail call double @llvm.fabs.f64(double %41)
%43 = fcmp olt double %42, 1.000000e+02
%44 = getelementptr inbounds [15 x i8], [15 x i8]* @.str, i64 0, i64 %24
%45 = load i8, i8* %44
br i1 %43, label %46, label %48
46: ; preds = %40
%47 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), double %23, i8 signext %45, i8* %1)
br label %50
48: ; preds = %40
%49 = tail call i32 @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), double %23, i8 signext %45, i8* %1)
br label %50
50: ; preds = %36, %48, %46, %28
ret i8* %2
}