void adaptive_print_time(double value, int width) {
  char str[128];
  int acutal_width;
  int index = 0;
  static char prefix[] = "num";
  char format[32];
  if (width > 127) {
    width = 127;
  }
  while ((value >= 1000.0) && (index < strlen(prefix))) {
    index++;
    value /= 1000.0;
  }
  if (index == strlen(prefix)) {
    acutal_width = width - 1;
    (void)snprintf(format, sizeof(format), "%%%d.%dfs", acutal_width, 2);
    (void)snprintf(str, width + 1, format, value);
  } else {
    acutal_width = width - 2;
    (void)snprintf(format, sizeof(format), "%%%d.%df%%cs", acutal_width, 2);
    (void)snprintf(str, width + 1, format, value, prefix[index]);
  }
  (void)printf("%*s ", width, str);
}


x86 assembly (clang, -O0):
<adaptive_print_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x98(%rbp)
cmpl   $0x7f,-0xc(%rbp)
jle    115e <adaptive_print_time+0x2e>
movl   $0x7f,-0xc(%rbp)
jmp    1163 <adaptive_print_time+0x33>
movsd  -0x8(%rbp),%xmm0
xor    %eax,%eax
movsd  0xe8e(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0xc1(%rbp)
jb     11b2 <adaptive_print_time+0x82>
movslq -0x98(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    0x2ea1(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0xd0(%rbp),%rax
cmp    %rcx,%rax
setb   %al
mov    %al,-0xc1(%rbp)
mov    -0xc1(%rbp),%al
test   $0x1,%al
jne    11c5 <adaptive_print_time+0x95>
jmp    11ef <adaptive_print_time+0xbf>
mov    -0x98(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x98(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe1f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1163 <adaptive_print_time+0x33>
movslq -0x98(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    0x2e34(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0xd8(%rbp),%rax
cmp    %rcx,%rax
jne    1276 <adaptive_print_time+0x146>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x94(%rbp)
lea    -0xc0(%rbp),%rdi
mov    -0x94(%rbp),%ecx
mov    $0x20,%esi
lea    0xdc7(%rip),%rdx
mov    $0x2,%r8d
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rsi
lea    -0xc0(%rbp),%rdx
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1050 <snprintf@plt>
jmp    12dd <adaptive_print_time+0x1ad>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x94(%rbp)
lea    -0xc0(%rbp),%rdi
mov    -0x94(%rbp),%ecx
mov    $0x20,%esi
lea    0xd77(%rip),%rdx
mov    $0x2,%r8d
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rsi
lea    -0xc0(%rbp),%rdx
movsd  -0x8(%rbp),%xmm0
movslq -0x98(%rbp),%rcx
lea    0x2d66(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
call   1050 <snprintf@plt>
mov    -0xc(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adaptive_print_time>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xb8,%rsp
cmp    $0x7f,%edi
mov    $0x7f,%ebx
cmovl  %edi,%ebx
ucomisd 0xeaf(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
jae    115e <adaptive_print_time+0x2e>
xor    %r12d,%r12d
jmp    11a1 <adaptive_print_time+0x71>
lea    0x2ed3(%rip),%rdi
call   1030 <strlen@plt>
movsd  0x8(%rsp),%xmm1
xor    %ecx,%ecx
movsd  0xe86(%rip),%xmm0
nopw   0x0(%rax,%rax,1)
cmp    %rcx,%rax
je     1195 <adaptive_print_time+0x65>
add    $0x1,%rcx
divsd  %xmm0,%xmm1
ucomisd %xmm0,%xmm1
jae    1180 <adaptive_print_time+0x50>
jmp    1198 <adaptive_print_time+0x68>
mov    %rax,%rcx
movsd  %xmm1,0x8(%rsp)
mov    %ecx,%r12d
lea    0x2e90(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
cmp    %r12,%rax
jne    11f6 <adaptive_print_time+0xc6>
lea    -0x1(%rbx),%ecx
lea    0xe49(%rip),%rdx
lea    0x10(%rsp),%r14
mov    $0x20,%esi
mov    %r14,%rdi
mov    $0x2,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0x1(%rbx),%eax
movslq %eax,%rsi
lea    0x30(%rsp),%rdi
mov    %r14,%rdx
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1050 <snprintf@plt>
jmp    123a <adaptive_print_time+0x10a>
lea    -0x2(%rbx),%ecx
lea    0xe12(%rip),%rdx
lea    0x10(%rsp),%r15
mov    $0x20,%esi
mov    %r15,%rdi
mov    $0x2,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0x1(%rbx),%eax
movslq %eax,%rsi
movsbl (%r12,%r14,1),%ecx
lea    0x30(%rsp),%rdi
mov    %r15,%rdx
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1050 <snprintf@plt>
lea    0xdde(%rip),%rdi
lea    0x30(%rsp),%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@adaptive_print_time.prefix = internal global [4 x i8] c"num\00"
@.str = [10 x i8] c"%%%d.%dfs\00"
@.str.1 = [13 x i8] c"%%%d.%df%%cs\00"
@.str.2 = [5 x i8] c"%*s \00"
define void @adaptive_print_time(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
store double %0, double* %3
store i32 %1, i32* %4
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 127
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 127, i32* %4
br label %12
12: ; preds = %11, %2
br label %13
13: ; preds = %23, %12
%14 = load double, double* %3
%15 = fcmp oge double %14, 1.000000e+03
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 0))
%20 = icmp ult i64 %18, %19
br label %21
21: ; preds = %16, %13
%22 = phi i1 [ false, %13 ], [ %20, %16 ]
br i1 %22, label %23, label %28
23: ; preds = %21
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = load double, double* %3
%27 = fdiv double %26, 1.000000e+03
store double %27, double* %3
br label %13
28: ; preds = %21
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = call i64 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 0))
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %46
33: ; preds = %28
%34 = load i32, i32* %4
%35 = sub nsw i32 %34, 1
store i32 %35, i32* %6
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%37 = load i32, i32* %6
%38 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %36, i64 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %37, i32 2)
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%44 = load double, double* %3
%45 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %39, i64 %42, i8* %43, double %44)
br label %64
46: ; preds = %28
%47 = load i32, i32* %4
%48 = sub nsw i32 %47, 2
store i32 %48, i32* %6
%49 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %6
%51 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %49, i64 32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 2)
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%57 = load double, double* %3
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %52, i64 %55, i8* %56, double %57, i32 %62)
br label %64
64: ; preds = %46, %33
%65 = load i32, i32* %4
%66 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %65, i8* %66)
ret void
}

LLVM IR (-O2):
@adaptive_print_time.prefix = internal global [4 x i8] c"num\00"
@.str = [10 x i8] c"%%%d.%dfs\00"
@.str.1 = [13 x i8] c"%%%d.%df%%cs\00"
@.str.2 = [5 x i8] c"%*s \00"
define void @adaptive_print_time(double %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %1, 127
%8 = select i1 %7, i32 %1, i32 127
%9 = fcmp ult double %0, 1.000000e+03
br i1 %9, label %24, label %10
10: ; preds = %2
%11 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 0))
br label %12
12: ; preds = %10, %16
%13 = phi i64 [ 0, %10 ], [ %17, %16 ]
%14 = phi double [ %0, %10 ], [ %18, %16 ]
%15 = icmp eq i64 %13, %11
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = add nuw i64 %13, 1
%18 = fdiv double %14, 1.000000e+03
%19 = fcmp ult double %18, 1.000000e+03
br i1 %19, label %20, label %12
20: ; preds = %16, %12
%21 = phi double [ %14, %12 ], [ %18, %16 ]
%22 = phi i64 [ %11, %12 ], [ %17, %16 ]
%23 = and i64 %22, 4294967295
br label %24
24: ; preds = %20, %2
%25 = phi double [ %0, %2 ], [ %21, %20 ]
%26 = phi i64 [ 0, %2 ], [ %23, %20 ]
%27 = tail call i64 @strlen(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 0))
%28 = icmp eq i64 %27, %26
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = add nsw i32 %8, -1
%31 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %6, i64 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %30, i32 2)
%32 = add nsw i32 %8, 1
%33 = sext i32 %32 to i64
%34 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %5, i64 %33, i8* %6, double %25)
br label %44
35: ; preds = %24
%36 = add nsw i32 %8, -2
%37 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %6, i64 32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 2)
%38 = add nsw i32 %8, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4 x i8], [4 x i8]* @adaptive_print_time.prefix, i64 0, i64 %26
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %5, i64 %39, i8* %6, double %25, i32 %42)
br label %44
44: ; preds = %35, %29
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %8, i8* %5)
ret void
}