#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 /*<<< orphan*/ EXIT_FAILURE;
long double HUGE_VAL;
char *calloc(unsigned int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
double my_fabs(long double);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, long double);
char *strdup(char *);
char *ldtostr(long double d) {
  unsigned int digits;
  char *ret;
  long double h;
  if (d == HUGE_VAL || d == -HUGE_VAL)
    return (strdup("inf"));
  if (d == 0.0)
    digits = 1;
  else
    digits = 0;
  if (d < 0)
    digits++;
  for (h = d; my_fabs(h) > 0.0; h /= 10.0)
    digits++;
  if ((ret = calloc(digits + 1 + 65 + 1, sizeof(char))) == NULL) {
    perror("calloc");
    exit(EXIT_FAILURE);
  }
  sprintf(ret, "%.65Lf", d);
  return (ret);
}


x86 assembly (clang, -O0):
<ldtostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
fldt   0x10(%rbp)
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
mov    0x2e70(%rip),%rax
fldt   (%rax)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    1191 <ldtostr+0x31>
jp     1191 <ldtostr+0x31>
jmp    11b6 <ldtostr+0x56>
fldt   -0x20(%rbp)
mov    0x2e4d(%rip),%rax
fldt   (%rax)
fchs
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    11cb <ldtostr+0x6b>
jp     11cb <ldtostr+0x6b>
jmp    11b6 <ldtostr+0x56>
lea    0xe47(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12b1 <ldtostr+0x151>
fldt   -0x20(%rbp)
fldz
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jne    11f3 <ldtostr+0x93>
jp     11f3 <ldtostr+0x93>
jmp    11e7 <ldtostr+0x87>
movl   $0x1,-0x24(%rbp)
jmp    11fa <ldtostr+0x9a>
movl   $0x0,-0x24(%rbp)
fldt   -0x20(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
jbe    1217 <ldtostr+0xb7>
jmp    120e <ldtostr+0xae>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
fldt   -0x20(%rbp)
fstpt  -0x40(%rbp)
fldt   -0x40(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1040 <my_fabs@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1253 <ldtostr+0xf3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
fldt   -0x40(%rbp)
flds   0xdb7(%rip)
fdivrp %st,%st(1)
fstpt  -0x40(%rbp)
jmp    121d <ldtostr+0xbd>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
add    $0x41,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1291 <ldtostr+0x131>
lea    0xd8a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rdi
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
lea    0xd6b(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ldtostr>:
push   %rbx
sub    $0x30,%rsp
fldt   0x40(%rsp)
mov    0x2e78(%rip),%rax
fldt   (%rax)
fucomi %st(1),%st
fchs
jne    117a <ldtostr+0x1a>
jnp    11e7 <ldtostr+0x87>
fucomip %st(1),%st
fldz
jne    1182 <ldtostr+0x22>
jnp    11e7 <ldtostr+0x87>
fstp   %st(0)
fldz
fxch   %st(1)
fucomi %st(1),%st
setnp  %al
sete   %cl
and    %al,%cl
movzbl %cl,%eax
lea    0x1(%rax),%ebx
fxch   %st(1)
fucomip %st(1),%st
cmovbe %eax,%ebx
fld    %st(0)
fstpt  (%rsp)
fstpt  0x18(%rsp)
call   1040 <my_fabs@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11fc <ldtostr+0x9c>
fldt   0x18(%rsp)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
fdivs  0xe37(%rip)
fld    %st(0)
fstpt  0x24(%rsp)
fstpt  (%rsp)
call   1040 <my_fabs@plt>
fldt   0x24(%rsp)
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
ja     11c0 <ldtostr+0x60>
jmp    1204 <ldtostr+0xa4>
fstp   %st(0)
fstp   %st(0)
lea    0xe12(%rip),%rdi
add    $0x30,%rsp
pop    %rbx
jmp    1080 <strdup@plt>
fldt   0x18(%rsp)
fstp   %st(0)
fldz
fstp   %st(0)
add    $0x43,%ebx
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
test   %rax,%rax
je     123c <ldtostr+0xdc>
mov    %rax,%rbx
fldt   0x18(%rsp)
fstpt  (%rsp)
lea    0xde4(%rip),%rsi
mov    %rax,%rdi
call   1060 <sprintf@plt>
mov    %rbx,%rax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xdc5(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@HUGE_VAL = global x86_fp80 0xK00000000000000000000
@.str = [4 x i8] c"inf\00"
@.str.1 = [7 x i8] c"calloc\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%.65Lf\00"
define i8* @ldtostr(x86_fp80 %0) {
%2 = alloca i8*
%3 = alloca x86_fp80
%4 = alloca i32
%5 = alloca i8*
%6 = alloca x86_fp80
store x86_fp80 %0, x86_fp80* %3
%7 = load x86_fp80, x86_fp80* %3
%8 = load x86_fp80, x86_fp80* @HUGE_VAL
%9 = fcmp oeq x86_fp80 %7, %8
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load x86_fp80, x86_fp80* %3
%12 = load x86_fp80, x86_fp80* @HUGE_VAL
%13 = fneg x86_fp80 %12
%14 = fcmp oeq x86_fp80 %11, %13
br i1 %14, label %15, label %17
15: ; preds = %10, %1
%16 = call i8* @strdup(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %2
br label %56
17: ; preds = %10
%18 = load x86_fp80, x86_fp80* %3
%19 = fcmp oeq x86_fp80 %18, 0xK00000000000000000000
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 1, i32* %4
br label %22
21: ; preds = %17
store i32 0, i32* %4
br label %22
22: ; preds = %21, %20
%23 = load x86_fp80, x86_fp80* %3
%24 = fcmp olt x86_fp80 %23, 0xK00000000000000000000
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %4
%27 = add i32 %26, 1
store i32 %27, i32* %4
br label %28
28: ; preds = %25, %22
%29 = load x86_fp80, x86_fp80* %3
store x86_fp80 %29, x86_fp80* %6
br label %30
30: ; preds = %37, %28
%31 = load x86_fp80, x86_fp80* %6
%32 = call double @my_fabs(x86_fp80 %31)
%33 = fcmp ogt double %32, 0.000000e+00
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* %4
%36 = add i32 %35, 1
store i32 %36, i32* %4
br label %37
37: ; preds = %34
%38 = load x86_fp80, x86_fp80* %6
%39 = fdiv x86_fp80 %38, 0xK4002A000000000000000
store x86_fp80 %39, x86_fp80* %6
br label %30
40: ; preds = %30
%41 = load i32, i32* %4
%42 = add i32 %41, 1
%43 = add i32 %42, 65
%44 = add i32 %43, 1
%45 = call i8* @calloc(i32 %44, i32 1)
store i8* %45, i8** %5
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %51
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %40
%52 = load i8*, i8** %5
%53 = load x86_fp80, x86_fp80* %3
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), x86_fp80 %53)
%55 = load i8*, i8** %5
store i8* %55, i8** %2
br label %56
56: ; preds = %51, %15
%57 = load i8*, i8** %2
ret i8* %57
}

LLVM IR (-O2):
@HUGE_VAL = global x86_fp80 0xK00000000000000000000
@.str = [4 x i8] c"inf\00"
@.str.1 = [7 x i8] c"calloc\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"%.65Lf\00"
define noalias i8* @ldtostr(x86_fp80 %0) {
%2 = load x86_fp80, x86_fp80* @HUGE_VAL
%3 = fcmp oeq x86_fp80 %2, %0
%4 = fneg x86_fp80 %2
%5 = fcmp oeq x86_fp80 %4, %0
%6 = or i1 %3, %5
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call dereferenceable_or_null(4) i8* @strdup(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %35
9: ; preds = %1
%10 = fcmp oeq x86_fp80 %0, 0xK00000000000000000000
%11 = zext i1 %10 to i32
%12 = fcmp olt x86_fp80 %0, 0xK00000000000000000000
%13 = select i1 %10, i32 2, i32 1
%14 = select i1 %12, i32 %13, i32 %11
%15 = tail call double @my_fabs(x86_fp80 %0)
%16 = fcmp ogt double %15, 0.000000e+00
br i1 %16, label %17, label %24
17: ; preds = %9, %17
%18 = phi x86_fp80 [ %21, %17 ], [ %0, %9 ]
%19 = phi i32 [ %20, %17 ], [ %14, %9 ]
%20 = add i32 %19, 1
%21 = fdiv x86_fp80 %18, 0xK4002A000000000000000
%22 = tail call double @my_fabs(x86_fp80 %21)
%23 = fcmp ogt double %22, 0.000000e+00
br i1 %23, label %17, label %24
24: ; preds = %17, %9
%25 = phi i32 [ %14, %9 ], [ %20, %17 ]
%26 = add i32 %25, 67
%27 = tail call i8* @calloc(i32 %26, i32 1)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = tail call i32 @sprintf(i8* %27, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), x86_fp80 %0)
br label %35
35: ; preds = %33, %7
%36 = phi i8* [ %8, %7 ], [ %27, %33 ]
ret i8* %36
}