#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*/ asprintf(char **, char *, int, int, double, char *);
double fabs(double);
char *format_value(double value, int digits_sig, int digits_nsig,
                   char abbreviate) {
  char *out = NULL, *mul = "";
  double a = fabs(value);
  double div = 1.0;
  if (!abbreviate) {
  } else if (a >= 1000000000.0) {
    div = 1000000000.0;
    mul = "G";
  } else if (a >= 1000000.0) {
    div = 1000000.0;
    mul = "M";
  } else if (a >= 1000.0) {
    div = 1000.0;
    mul = "k";
  }
  if (mul[0])
    digits_sig--;
  (void)asprintf(&out, "%*.*f%s", digits_sig, digits_nsig, value / div, mul);
  return out;
}


x86 assembly (clang, -O0):
<format_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xefa(%rip),%rax
mov    %rax,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
movaps 0xed9(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xea8(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpb   $0x0,-0x11(%rbp)
jne    116c <format_value+0x5c>
jmp    1212 <format_value+0x102>
movsd  -0x30(%rbp),%xmm0
movsd  0xe8f(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11a0 <format_value+0x90>
movsd  0xe7d(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    0xe99(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    120d <format_value+0xfd>
movsd  -0x30(%rbp),%xmm0
movsd  0xe63(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11d4 <format_value+0xc4>
movsd  0xe51(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    0xe67(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1208 <format_value+0xf8>
movsd  -0x30(%rbp),%xmm0
movsd  0xe37(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1203 <format_value+0xf3>
movsd  0xe25(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    0xe35(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1208 <format_value+0xf8>
jmp    120d <format_value+0xfd>
jmp    1212 <format_value+0x102>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1228 <format_value+0x118>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
movsd  -0x8(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm0
mov    -0x28(%rbp),%r8
lea    -0x20(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1030 <asprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_value>:
push   %rax
mov    %esi,%ecx
mov    %edi,%eax
movq   $0x0,(%rsp)
movsd  0xeeb(%rip),%xmm1
lea    0xf05(%rip),%r8
test   %dl,%dl
je     1187 <format_value+0x77>
movapd 0xec8(%rip),%xmm2
andpd  %xmm0,%xmm2
movsd  0xed4(%rip),%xmm3
ucomisd %xmm3,%xmm2
jae    1173 <format_value+0x63>
movsd  0xece(%rip),%xmm3
ucomisd %xmm3,%xmm2
jae    117c <format_value+0x6c>
ucomisd 0xec8(%rip),%xmm2
jb     1187 <format_value+0x77>
movsd  0xebe(%rip),%xmm1
lea    0xec3(%rip),%r8
jmp    1187 <format_value+0x77>
lea    0xeb6(%rip),%r8
jmp    1183 <format_value+0x73>
lea    0xeaf(%rip),%r8
movapd %xmm3,%xmm1
cmpb   $0x1,(%r8)
adc    $0xffffffff,%eax
divsd  %xmm1,%xmm0
lea    0xe9d(%rip),%rsi
mov    %rsp,%rdi
mov    %eax,%edx
call   1030 <asprintf@plt>
mov    (%rsp),%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"G\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"k\00"
@.str.4 = [8 x i8] c"%*.*f%s\00"
define i8* @format_value(double %0, i32 %1, i32 %2, i8 signext %3) {
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca double
%12 = alloca double
store double %0, double* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i8* null, i8** %9
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %10
%13 = load double, double* %5
%14 = call double @llvm.fabs.f64(double %13)
store double %14, double* %11
store double 1.000000e+00, double* %12
%15 = load i8, i8* %8
%16 = icmp ne i8 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %4
br label %33
18: ; preds = %4
%19 = load double, double* %11
%20 = fcmp oge double %19, 1.000000e+09
br i1 %20, label %21, label %22
21: ; preds = %18
store double 1.000000e+09, double* %12
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %32
22: ; preds = %18
%23 = load double, double* %11
%24 = fcmp oge double %23, 1.000000e+06
br i1 %24, label %25, label %26
25: ; preds = %22
store double 1.000000e+06, double* %12
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %10
br label %31
26: ; preds = %22
%27 = load double, double* %11
%28 = fcmp oge double %27, 1.000000e+03
br i1 %28, label %29, label %30
29: ; preds = %26
store double 1.000000e+03, double* %12
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %10
br label %30
30: ; preds = %29, %26
br label %31
31: ; preds = %30, %25
br label %32
32: ; preds = %31, %21
br label %33
33: ; preds = %32, %17
%34 = load i8*, i8** %10
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = icmp ne i8 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %6
%40 = add nsw i32 %39, -1
store i32 %40, i32* %6
br label %41
41: ; preds = %38, %33
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = load double, double* %5
%45 = load double, double* %12
%46 = fdiv double %44, %45
%47 = load i8*, i8** %10
%48 = call i32 @asprintf(i8** %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %42, i32 %43, double %46, i8* %47)
%49 = load i8*, i8** %9
ret i8* %49
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"G\00"
@.str.2 = [2 x i8] c"M\00"
@.str.3 = [2 x i8] c"k\00"
@.str.4 = [8 x i8] c"%*.*f%s\00"
define i8* @format_value(double %0, i32 %1, i32 %2, i8 signext %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%7 = tail call double @llvm.fabs.f64(double %0)
%8 = icmp eq i8 %3, 0
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = fcmp ult double %7, 1.000000e+09
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = fcmp ult double %7, 1.000000e+06
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = fcmp ult double %7, 1.000000e+03
br i1 %14, label %16, label %15
15: ; preds = %13
br label %16
16: ; preds = %11, %9, %13, %15, %4
%17 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %11 ]
%18 = phi double [ 1.000000e+03, %15 ], [ 1.000000e+00, %13 ], [ 1.000000e+00, %4 ], [ 1.000000e+09, %9 ], [ 1.000000e+06, %11 ]
%19 = load i8, i8* %17
%20 = icmp ne i8 %19, 0
%21 = sext i1 %20 to i32
%22 = add nsw i32 %21, %1
%23 = fdiv double %0, %18
%24 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %22, i32 %2, double %23, i8* %17)
%25 = load i8*, i8** %5
ret i8* %25
}