#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 FP_INFINITE;
int FP_NAN;
int FP_ZERO;
int __fpclassifyl(long double);
int /*<<< orphan*/ fabsl(long double);
scalar_t__ floorl(int /*<<< orphan*/);
int isinfl(long double);
int /*<<< orphan*/ log10l(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int longdouble2string(long double _longdouble, char *_string, long ndigit) {
  char format[1000];
  long exponent;
  int class = __fpclassifyl(_longdouble);
  if (class == FP_ZERO) {
    snprintf(_string, 1000, "0.0");
  } else if (class == FP_INFINITE) {
    if (isinfl(_longdouble) == 1) {
      snprintf(_string, 1000, "+inf");
    } else
      snprintf(_string, 1000, "-inf");
  } else if (class == FP_NAN) {
    snprintf(_string, 1000, "nan");
  } else {
    exponent = (long)floorl(log10l(fabsl(_longdouble)));
    if (exponent >= 0 && exponent < 5) {
      snprintf(format, 1000, "%%.%0ldLf", ndigit);
    } else if (exponent > -5) {
      snprintf(format, 1000, "%%.%0ldLf", ndigit - exponent);
    } else {
      snprintf(format, 1000, "%%.%0ldLe", ndigit);
    }
    snprintf(_string, 1000, format, _longdouble);
  }
  return 0;
}


x86 assembly (clang, -O0):
<longdouble2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
fldt   0x10(%rbp)
fstpt  -0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1040 <__fpclassifyl@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%eax
jne    11bd <longdouble2string+0x5d>
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
lea    0xe4f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1336 <longdouble2string+0x1d6>
mov    -0x41c(%rbp),%eax
mov    0x2e16(%rip),%rcx
cmp    (%rcx),%eax
jne    1220 <longdouble2string+0xc0>
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1080 <isinfl@plt>
cmp    $0x1,%eax
jne    1204 <longdouble2string+0xa4>
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
lea    0xe0c(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    121b <longdouble2string+0xbb>
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
lea    0xdf5(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1331 <longdouble2string+0x1d1>
mov    -0x41c(%rbp),%eax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%eax
jne    1251 <longdouble2string+0xf1>
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
lea    0xdc9(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    132c <longdouble2string+0x1cc>
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1030 <fabsl@plt>
mov    %eax,%edi
call   1060 <log10l@plt>
mov    %eax,%edi
call   1070 <floorl@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jl     12b2 <longdouble2string+0x152>
cmpq   $0x5,-0x418(%rbp)
05
jge    12b2 <longdouble2string+0x152>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x3e8,%esi
lea    0xd6c(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    130d <longdouble2string+0x1ad>
cmpq   $0xfffffffffffffffb,-0x418(%rbp)
fb
jle    12ea <longdouble2string+0x18a>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rcx
sub    -0x418(%rbp),%rcx
mov    $0x3e8,%esi
lea    0xd34(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1308 <longdouble2string+0x1a8>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x3e8,%esi
lea    0xd1b(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    130d <longdouble2string+0x1ad>
mov    -0x18(%rbp),%rdi
lea    -0x410(%rbp),%rdx
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
xor    %eax,%eax
mov    $0x3e8,%esi
call   1050 <snprintf@plt>
jmp    1331 <longdouble2string+0x1d1>
jmp    1336 <longdouble2string+0x1d6>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<longdouble2string>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
fldt   0x420(%rsp)
fld    %st(0)
fstpt  0x14(%rsp)
fstpt  (%rsp)
call   1040 <__fpclassifyl@plt>
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
jne    119c <longdouble2string+0x3c>
lea    0xe69(%rip),%rdx
jmp    1240 <longdouble2string+0xe0>
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%eax
jne    11c1 <longdouble2string+0x61>
fldt   0x14(%rsp)
fstpt  (%rsp)
call   1080 <isinfl@plt>
cmp    $0x1,%eax
jne    11d5 <longdouble2string+0x75>
lea    0xe45(%rip),%rdx
jmp    1240 <longdouble2string+0xe0>
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
jne    11de <longdouble2string+0x7e>
lea    0xe3b(%rip),%rdx
jmp    1240 <longdouble2string+0xe0>
lea    0xe2d(%rip),%rdx
jmp    1240 <longdouble2string+0xe0>
fldt   0x14(%rsp)
fstpt  (%rsp)
call   1030 <fabsl@plt>
mov    %eax,%edi
call   1060 <log10l@plt>
mov    %eax,%edi
call   1070 <floorl@plt>
cmp    $0x4,%rax
ja     1207 <longdouble2string+0xa7>
lea    0xe0d(%rip),%rdx
jmp    1220 <longdouble2string+0xc0>
cmp    $0xfffffffffffffffc,%rax
jl     1219 <longdouble2string+0xb9>
sub    %rax,%r14
lea    0xdfb(%rip),%rdx
jmp    1220 <longdouble2string+0xc0>
lea    0xdfc(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x3e8,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
fldt   0x14(%rsp)
fstpt  (%rsp)
lea    0x20(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x3e8,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@FP_ZERO = global i32 0
@.str = [4 x i8] c"0.0\00"
@FP_INFINITE = global i32 0
@.str.1 = [5 x i8] c"+inf\00"
@.str.2 = [5 x i8] c"-inf\00"
@FP_NAN = global i32 0
@.str.3 = [4 x i8] c"nan\00"
@.str.4 = [10 x i8] c"%%.%0ldLf\00"
@.str.5 = [10 x i8] c"%%.%0ldLe\00"
define i32 @longdouble2string(x86_fp80 %0, i8* %1, i64 %2) {
%4 = alloca x86_fp80
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [1000 x i8]
%8 = alloca i64
%9 = alloca i32
store x86_fp80 %0, x86_fp80* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load x86_fp80, x86_fp80* %4
%11 = call i32 @__fpclassifyl(x86_fp80 %10)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = load i32, i32* @FP_ZERO
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %16, i32 1000, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %75
18: ; preds = %3
%19 = load i32, i32* %9
%20 = load i32, i32* @FP_INFINITE
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %33
22: ; preds = %18
%23 = load x86_fp80, x86_fp80* %4
%24 = call i32 @isinfl(x86_fp80 %23)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 1000, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %30, i32 1000, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %29, %26
br label %74
33: ; preds = %18
%34 = load i32, i32* %9
%35 = load i32, i32* @FP_NAN
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %5
%39 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %38, i32 1000, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %73
40: ; preds = %33
%41 = load x86_fp80, x86_fp80* %4
%42 = call i32 @fabsl(x86_fp80 %41)
%43 = call i32 @log10l(i32 %42)
%44 = call i64 @floorl(i32 %43)
store i64 %44, i64* %8
%45 = load i64, i64* %8
%46 = icmp sge i64 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %40
%48 = load i64, i64* %8
%49 = icmp slt i64 %48, 5
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%52 = load i64, i64* %6
%53 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %51, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %52)
br label %68
54: ; preds = %47, %40
%55 = load i64, i64* %8
%56 = icmp sgt i64 %55, -5
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%59 = load i64, i64* %6
%60 = load i64, i64* %8
%61 = sub nsw i64 %59, %60
%62 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %58, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %61)
br label %67
63: ; preds = %54
%64 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%65 = load i64, i64* %6
%66 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %64, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i64 %65)
br label %67
67: ; preds = %63, %57
br label %68
68: ; preds = %67, %50
%69 = load i8*, i8** %5
%70 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%71 = load x86_fp80, x86_fp80* %4
%72 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %69, i32 1000, i8* %70, x86_fp80 %71)
br label %73
73: ; preds = %68, %37
br label %74
74: ; preds = %73, %32
br label %75
75: ; preds = %74, %15
ret i32 0
}

LLVM IR (-O2):
@FP_ZERO = global i32 0
@.str = [4 x i8] c"0.0\00"
@FP_INFINITE = global i32 0
@.str.1 = [5 x i8] c"+inf\00"
@.str.2 = [5 x i8] c"-inf\00"
@FP_NAN = global i32 0
@.str.3 = [4 x i8] c"nan\00"
@.str.4 = [10 x i8] c"%%.%0ldLf\00"
@.str.5 = [10 x i8] c"%%.%0ldLe\00"
define i32 @longdouble2string(x86_fp80 %0, i8* %1, i64 %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @__fpclassifyl(x86_fp80 %0)
%7 = load i32, i32* @FP_ZERO
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 1000, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %42
11: ; preds = %3
%12 = load i32, i32* @FP_INFINITE
%13 = icmp eq i32 %6, %12
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = tail call i32 @isinfl(x86_fp80 %0)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 1000, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %42
19: ; preds = %14
%20 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 1000, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %42
21: ; preds = %11
%22 = load i32, i32* @FP_NAN
%23 = icmp eq i32 %6, %22
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 1000, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %42
26: ; preds = %21
%27 = tail call i32 @fabsl(x86_fp80 %0)
%28 = tail call i32 @log10l(i32 %27)
%29 = tail call i64 @floorl(i32 %28)
%30 = icmp ult i64 %29, 5
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %2)
br label %40
33: ; preds = %26
%34 = icmp sgt i64 %29, -5
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = sub nsw i64 %2, %29
%37 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i64 %36)
br label %40
38: ; preds = %33
%39 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 1000, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i64 %2)
br label %40
40: ; preds = %35, %38, %31
%41 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 1000, i8* %5, x86_fp80 %0)
br label %42
42: ; preds = %19, %17, %40, %24, %9
ret i32 0
}