#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 *convert_double(double *, int, float);
int /*<<< orphan*/ sprintf(char *, char *, double, char *, ...);
scalar_t__ strstr(char *, char *);
void format_value(char *buff, double value, char *unit) {
  char *mag;
  if (strstr(unit, "b"))
    value *= 8;
  mag = convert_double(&value, (strstr(unit, "B")) ? 1024 : 1000, 1.0f);
  if (mag[0] == 0)
    sprintf(buff, "%8ld %s", (long)value, unit);
  else
    sprintf(buff, "%7.3f %s%s", value, mag, unit);
}


x86 assembly (clang, -O0):
<format_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xebc(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     1171 <format_value+0x41>
movsd  0xe99(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,%rcx
mov    $0x3e8,%esi
mov    $0x400,%eax
cmp    $0x0,%rcx
cmovne %eax,%esi
lea    -0x10(%rbp),%rdi
movss  0xe67(%rip),%xmm0
call   1030 <convert_double@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11e0 <format_value+0xb0>
mov    -0x8(%rbp),%rdi
cvttsd2si -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    -0x18(%rbp),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
jmp    11ff <format_value+0xcf>
mov    -0x8(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe20(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_value>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movsd  %xmm0,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
lea    0xebc(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     1171 <format_value+0x41>
movsd  0x10(%rsp),%xmm0
mulsd  0xe95(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
mov    $0x3e8,%eax
mov    $0x400,%esi
cmove  %eax,%esi
lea    0x8(%rsp),%rdi
movss  0xe6b(%rip),%xmm0
call   1030 <convert_double@plt>
cmpb   $0x0,(%rax)
movsd  0x8(%rsp),%xmm0
je     11c6 <format_value+0x96>
lea    0xe64(%rip),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    $0x1,%al
call   1040 <sprintf@plt>
jmp    11e7 <format_value+0xb7>
cvttsd2si %xmm0,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
lea    0xe36(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    $0x1,%al
call   1040 <sprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"b\00"
@.str.1 = [2 x i8] c"B\00"
@.str.2 = [8 x i8] c"%8ld %s\00"
@.str.3 = [11 x i8] c"%7.3f %s%s\00"
define void @format_value(i8* %0, double %1, i8* %2) {
%4 = alloca i8*
%5 = alloca double
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store double %1, double* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %6
%9 = call i64 @strstr(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load double, double* %5
%13 = fmul double %12, 8.000000e+00
store double %13, double* %5
br label %14
14: ; preds = %11, %3
%15 = load i8*, i8** %6
%16 = call i64 @strstr(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 1024, i32 1000
%20 = call i8* @convert_double(double* %5, i32 %19, float 1.000000e+00)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = load double, double* %5
%29 = fptosi double %28 to i64
%30 = sitofp i64 %29 to double
%31 = load i8*, i8** %6
%32 = call i32 (i8*, i8*, double, i8*, ...) @sprintf(i8* %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %30, i8* %31)
br label %39
33: ; preds = %14
%34 = load i8*, i8** %4
%35 = load double, double* %5
%36 = load i8*, i8** %7
%37 = load i8*, i8** %6
%38 = call i32 (i8*, i8*, double, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), double %35, i8* %36, i8* %37)
br label %39
39: ; preds = %33, %26
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"b\00"
@.str.1 = [2 x i8] c"B\00"
@.str.2 = [8 x i8] c"%8ld %s\00"
@.str.3 = [11 x i8] c"%7.3f %s%s\00"
define void @format_value(i8* nocapture writeonly %0, double %1, i8* %2) {
%4 = alloca double
store double %1, double* %4
%5 = tail call i64 @strstr(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = fmul double %1, 8.000000e+00
store double %8, double* %4
br label %9
9: ; preds = %7, %3
%10 = tail call i64 @strstr(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i32 1000, i32 1024
%13 = call i8* @convert_double(double* %4, i32 %12, float 1.000000e+00)
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
%16 = load double, double* %4
br i1 %15, label %17, label %21
17: ; preds = %9
%18 = fptosi double %16 to i64
%19 = sitofp i64 %18 to double
%20 = call i32 (i8*, i8*, double, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %19, i8* %2)
br label %23
21: ; preds = %9
%22 = call i32 (i8*, i8*, double, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), double %16, i8* %13, i8* %2)
br label %23
23: ; preds = %21, %17
ret void
}