#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 */
scalar_t__ log10(double);
int /*<<< orphan*/ sprintf(char *, char *, ...);
void real_format(double value, int nsd, char *fmt) {
  int ndigs;
  int tot = nsd + 1;
  int xtot = tot;
  if (value == 0.0) {
    sprintf(fmt, "%%%d.%df", tot, tot - 2);
    return;
  }
  if (value != value) {
    sprintf(fmt, "%%f");
    return;
  }
  if (value < 0.0) {
    value = -value;
    xtot++;
  }
  if (value < 1.0) {
    int thr = -5;
    ndigs = (int)(log10(value));
    if (ndigs <= thr) {
      sprintf(fmt, "%%%d.%de", xtot, tot - 2);
      return;
    }
    sprintf(fmt, "%%%d.%df", xtot - ndigs, nsd - ndigs);
    return;
  } else {
    int thr = -0;
    ndigs = (int)(log10(value));
    if (ndigs >= (nsd + thr)) {
      sprintf(fmt, "%%%d.%de", xtot, tot - 2);
      return;
    }
    sprintf(fmt, "%%%d.%df", xtot, (nsd + thr) - ndigs);
    return;
  }
}


x86 assembly (clang, -O0):
<real_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    117b <real_format+0x5b>
jp     117b <real_format+0x5b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x20(%rbp),%ecx
sub    $0x2,%ecx
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    12be <real_format+0x19e>
movsd  -0x8(%rbp),%xmm0
ucomisd -0x8(%rbp),%xmm0
jne    1196 <real_format+0x76>
jp     1196 <real_format+0x76>
jmp    11ad <real_format+0x8d>
mov    -0x18(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    12be <real_format+0x19e>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11e5 <real_format+0xc5>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movsd  0xe13(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    125b <real_format+0x13b>
movl   $0xfffffffb,-0x28(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1030 <log10@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     1238 <real_format+0x118>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
sub    $0x2,%ecx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    12be <real_format+0x19e>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0xc(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    12be <real_format+0x19e>
movl   $0x0,-0x2c(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1030 <log10@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    -0x2c(%rbp),%ecx
cmp    %ecx,%eax
jl     12a0 <real_format+0x180>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
sub    $0x2,%ecx
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    12be <real_format+0x19e>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0xc(%rbp),%ecx
add    -0x2c(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<real_format>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x1(%rbx),%ebp
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1141 <real_format+0x21>
jp     1141 <real_format+0x21>
add    $0xffffffff,%ebx
jmp    11a6 <real_format+0x86>
ucomisd %xmm0,%xmm0
jp     11cf <real_format+0xaf>
lea    0x2(%rbx),%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
movapd 0xea2(%rip),%xmm2
xorpd  %xmm0,%xmm2
cmova  %eax,%ebp
movapd %xmm0,%xmm3
cmpltsd %xmm1,%xmm3
andpd  %xmm3,%xmm2
andnpd %xmm0,%xmm3
orpd   %xmm2,%xmm3
movapd %xmm3,(%rsp)
movapd %xmm3,%xmm0
call   1030 <log10@plt>
movsd  0xe80(%rip),%xmm0
ucomisd (%rsp),%xmm0
jbe    11a0 <real_format+0x80>
cmp    $0xfffffffb,%eax
jle    11c3 <real_format+0xa3>
sub    %eax,%ebp
jmp    11a4 <real_format+0x84>
cmp    %ebx,%eax
jge    11c3 <real_format+0xa3>
sub    %eax,%ebx
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <sprintf@plt>
add    $0xffffffff,%ebx
lea    0xe58(%rip),%rsi
jmp    11ad <real_format+0x8d>
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <sprintf@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"%%%d.%df\00"
@.str.1 = [4 x i8] c"%%f\00"
@.str.2 = [9 x i8] c"%%%d.%de\00"
define void @real_format(double %0, i32 %1, i8* %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
store i32 %13, i32* %8
%14 = load i32, i32* %8
store i32 %14, i32* %9
%15 = load double, double* %4
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = load i32, i32* %8
%21 = sub nsw i32 %20, 2
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %21)
br label %87
23: ; preds = %3
%24 = load double, double* %4
%25 = load double, double* %4
%26 = fcmp une double %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %87
30: ; preds = %23
%31 = load double, double* %4
%32 = fcmp olt double %31, 0.000000e+00
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load double, double* %4
%35 = fneg double %34
store double %35, double* %4
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %38
38: ; preds = %33, %30
%39 = load double, double* %4
%40 = fcmp olt double %39, 1.000000e+00
br i1 %40, label %41, label %63
41: ; preds = %38
store i32 -5, i32* %10
%42 = load double, double* %4
%43 = call i64 @log10(double %42)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = load i32, i32* %10
%47 = icmp sle i32 %45, %46
br i1 %47, label %48, label %54
48: ; preds = %41
%49 = load i8*, i8** %6
%50 = load i32, i32* %9
%51 = load i32, i32* %8
%52 = sub nsw i32 %51, 2
%53 = call i32 (i8*, i8*, ...) @sprintf(i8* %49, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %50, i32 %52)
br label %87
54: ; preds = %41
%55 = load i8*, i8** %6
%56 = load i32, i32* %9
%57 = load i32, i32* %7
%58 = sub nsw i32 %56, %57
%59 = load i32, i32* %5
%60 = load i32, i32* %7
%61 = sub nsw i32 %59, %60
%62 = call i32 (i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %58, i32 %61)
br label %87
63: ; preds = %38
store i32 0, i32* %11
%64 = load double, double* %4
%65 = call i64 @log10(double %64)
%66 = trunc i64 %65 to i32
store i32 %66, i32* %7
%67 = load i32, i32* %7
%68 = load i32, i32* %5
%69 = load i32, i32* %11
%70 = add nsw i32 %68, %69
%71 = icmp sge i32 %67, %70
br i1 %71, label %72, label %78
72: ; preds = %63
%73 = load i8*, i8** %6
%74 = load i32, i32* %9
%75 = load i32, i32* %8
%76 = sub nsw i32 %75, 2
%77 = call i32 (i8*, i8*, ...) @sprintf(i8* %73, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %74, i32 %76)
br label %87
78: ; preds = %63
%79 = load i8*, i8** %6
%80 = load i32, i32* %9
%81 = load i32, i32* %5
%82 = load i32, i32* %11
%83 = add nsw i32 %81, %82
%84 = load i32, i32* %7
%85 = sub nsw i32 %83, %84
%86 = call i32 (i8*, i8*, ...) @sprintf(i8* %79, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %80, i32 %85)
br label %87
87: ; preds = %78, %72, %54, %48, %27, %17
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"%%%d.%df\00"
@.str.1 = [4 x i8] c"%%f\00"
@.str.2 = [9 x i8] c"%%%d.%de\00"
define void @real_format(double %0, i32 %1, i8* nocapture writeonly %2) {
%4 = add nsw i32 %1, 1
%5 = fcmp oeq double %0, 0.000000e+00
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = add nsw i32 %1, -1
%8 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %7)
br label %39
9: ; preds = %3
%10 = fcmp uno double %0, 0.000000e+00
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %39
13: ; preds = %9
%14 = fcmp olt double %0, 0.000000e+00
%15 = fneg double %0
%16 = add nsw i32 %1, 2
%17 = select i1 %14, i32 %16, i32 %4
%18 = select i1 %14, double %15, double %0
%19 = fcmp olt double %18, 1.000000e+00
%20 = tail call i64 @log10(double %18)
%21 = trunc i64 %20 to i32
br i1 %19, label %22, label %31
22: ; preds = %13
%23 = icmp slt i32 %21, -4
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = add nsw i32 %1, -1
%26 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %25)
br label %39
27: ; preds = %22
%28 = sub nsw i32 %17, %21
%29 = sub nsw i32 %1, %21
%30 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29)
br label %39
31: ; preds = %13
%32 = icmp slt i32 %21, %1
br i1 %32, label %36, label %33
33: ; preds = %31
%34 = add nsw i32 %1, -1
%35 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %34)
br label %39
36: ; preds = %31
%37 = sub nsw i32 %1, %21
%38 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %37)
br label %39
39: ; preds = %33, %36, %24, %27, %11, %6
ret void
}