void resolve_mem_format(double *value, const char **format) {
  int neg = *value < 0;
  if (neg) {
    *value *= -1;
  }
  if (*value >= 1000 * 1000 * 1000) {
    *format = "%6.1fGB";
    *value /= 1 << 30;
  } else if (*value >= 1000 * 1000) {
    *format = "%6.1fMB";
    *value /= 1 << 20;
  } else if (*value >= 1000) {
    *format = "%6.1fKB";
    *value /= 1 << 10;
  } else {
    *format = "%7.fB";
  }
  if (neg) {
    *value *= -1;
  }
}


x86 assembly (clang, -O0):
<resolve_mem_format>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
xorps  %xmm0,%xmm0
ucomisd (%rax),%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1140 <resolve_mem_format+0x40>
mov    -0x8(%rbp),%rax
movsd  0xec8(%rip),%xmm0
mulsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xeb8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1185 <resolve_mem_format+0x85>
mov    -0x10(%rbp),%rax
lea    0xed3(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xeb8(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
jmp    1227 <resolve_mem_format+0x127>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xe7b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11ca <resolve_mem_format+0xca>
mov    -0x10(%rbp),%rax
lea    0xe96(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xe6b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
jmp    1222 <resolve_mem_format+0x122>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xe3e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     120f <resolve_mem_format+0x10f>
mov    -0x10(%rbp),%rax
lea    0xe59(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  0xe1e(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rax)
jmp    121d <resolve_mem_format+0x11d>
mov    -0x10(%rbp),%rax
lea    0xe36(%rip),%rcx
mov    %rcx,(%rax)
jmp    1222 <resolve_mem_format+0x122>
jmp    1227 <resolve_mem_format+0x127>
cmpl   $0x0,-0x14(%rbp)
je     1245 <resolve_mem_format+0x145>
mov    -0x8(%rbp),%rax
movsd  0xdc3(%rip),%xmm0
mulsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_mem_format>:
movsd  (%rdi),%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
movapd %xmm1,%xmm0
jbe    1122 <resolve_mem_format+0x22>
movapd 0xee6(%rip),%xmm0
xorpd  %xmm1,%xmm0
movlpd %xmm0,(%rdi)
ucomisd 0xee6(%rip),%xmm0
jae    114c <resolve_mem_format+0x4c>
ucomisd 0xeec(%rip),%xmm0
jae    1160 <resolve_mem_format+0x60>
ucomisd 0xef2(%rip),%xmm0
jae    1174 <resolve_mem_format+0x74>
lea    0xf11(%rip),%rax
mov    %rax,(%rsi)
jmp    118a <resolve_mem_format+0x8a>
lea    0xeed(%rip),%rax
mov    %rax,(%rsi)
mulsd  0xeba(%rip),%xmm0
jmp    1186 <resolve_mem_format+0x86>
lea    0xee1(%rip),%rax
mov    %rax,(%rsi)
mulsd  0xeb6(%rip),%xmm0
jmp    1186 <resolve_mem_format+0x86>
lea    0xed5(%rip),%rax
mov    %rax,(%rsi)
mulsd  0xeb2(%rip),%xmm0
movsd  %xmm0,(%rdi)
xorpd  %xmm2,%xmm2
ucomisd %xmm1,%xmm2
jbe    11a0 <resolve_mem_format+0xa0>
xorpd  0xe64(%rip),%xmm0
movlpd %xmm0,(%rdi)
ret

LLVM IR (-O0):
@.str = [8 x i8] c"%6.1fGB\00"
@.str.1 = [8 x i8] c"%6.1fMB\00"
@.str.2 = [8 x i8] c"%6.1fKB\00"
@.str.3 = [6 x i8] c"%7.fB\00"
define void @resolve_mem_format(double* %0, i8** %1) {
%3 = alloca double*
%4 = alloca i8**
%5 = alloca i32
store double* %0, double** %3
store i8** %1, i8*** %4
%6 = load double*, double** %3
%7 = load double, double* %6
%8 = fcmp olt double %7, 0.000000e+00
%9 = zext i1 %8 to i32
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load double*, double** %3
%14 = load double, double* %13
%15 = fmul double %14, -1.000000e+00
store double %15, double* %13
br label %16
16: ; preds = %12, %2
%17 = load double*, double** %3
%18 = load double, double* %17
%19 = fcmp oge double %18, 1.000000e+09
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i8**, i8*** %4
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %21
%22 = load double*, double** %3
%23 = load double, double* %22
%24 = fdiv double %23, 0x41D0000000000000
store double %24, double* %22
br label %47
25: ; preds = %16
%26 = load double*, double** %3
%27 = load double, double* %26
%28 = fcmp oge double %27, 1.000000e+06
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i8**, i8*** %4
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %30
%31 = load double*, double** %3
%32 = load double, double* %31
%33 = fdiv double %32, 0x4130000000000000
store double %33, double* %31
br label %46
34: ; preds = %25
%35 = load double*, double** %3
%36 = load double, double* %35
%37 = fcmp oge double %36, 1.000000e+03
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = load i8**, i8*** %4
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %39
%40 = load double*, double** %3
%41 = load double, double* %40
%42 = fdiv double %41, 1.024000e+03
store double %42, double* %40
br label %45
43: ; preds = %34
%44 = load i8**, i8*** %4
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %44
br label %45
45: ; preds = %43, %38
br label %46
46: ; preds = %45, %29
br label %47
47: ; preds = %46, %20
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load double*, double** %3
%52 = load double, double* %51
%53 = fmul double %52, -1.000000e+00
store double %53, double* %51
br label %54
54: ; preds = %50, %47
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"%6.1fGB\00"
@.str.1 = [8 x i8] c"%6.1fMB\00"
@.str.2 = [8 x i8] c"%6.1fKB\00"
@.str.3 = [6 x i8] c"%7.fB\00"
define void @resolve_mem_format(double* nocapture %0, i8** nocapture writeonly %1) {
%3 = load double, double* %0
%4 = fcmp olt double %3, 0.000000e+00
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = fneg double %3
store double %6, double* %0
br label %7
7: ; preds = %5, %2
%8 = phi double [ %6, %5 ], [ %3, %2 ]
%9 = fcmp ult double %8, 1.000000e+09
br i1 %9, label %12, label %10
10: ; preds = %7
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %1
%11 = fmul double %8, 0x3E10000000000000
store double %11, double* %0
br label %21
12: ; preds = %7
%13 = fcmp ult double %8, 1.000000e+06
br i1 %13, label %16, label %14
14: ; preds = %12
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %1
%15 = fmul double %8, 0x3EB0000000000000
store double %15, double* %0
br label %21
16: ; preds = %12
%17 = fcmp ult double %8, 1.000000e+03
br i1 %17, label %20, label %18
18: ; preds = %16
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %1
%19 = fmul double %8, 0x3F50000000000000
store double %19, double* %0
br label %21
20: ; preds = %16
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %21
21: ; preds = %14, %20, %18, %10
%22 = phi double [ %15, %14 ], [ %8, %20 ], [ %19, %18 ], [ %11, %10 ]
br i1 %4, label %23, label %25
23: ; preds = %21
%24 = fneg double %22
store double %24, double* %0
br label %25
25: ; preds = %23, %21
ret void
}