#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 */
typedef int /*<<< orphan*/ nbuf;

/* Variables and functions */
int /*<<< orphan*/ DBL_DIG;
double const fabs(double);
int /*<<< orphan*/ snprintf(char *, int, char *, double, ...);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int ucl_fd_append_double(double val, void *ud) {
  int fd = *(int *)ud;
  const double delta = 0.0000001;
  char nbuf[64];
  if (val == (double)(int)val) {
    snprintf(nbuf, sizeof(nbuf), "%.1lf", val);
  } else if (fabs(val - (double)(int)val) < delta) {
    snprintf(nbuf, sizeof(nbuf), "%.*lg", DBL_DIG, val);
  } else {
    snprintf(nbuf, sizeof(nbuf), "%lf", val);
  }
  return write(fd, nbuf, strlen(nbuf));
}


x86 assembly (clang, -O0):
<ucl_fd_append_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
cvttsd2si -0x8(%rbp),%eax
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jne    11a6 <ucl_fd_append_double+0x66>
jp     11a6 <ucl_fd_append_double+0x66>
lea    -0x60(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    $0x40,%esi
lea    0xe6e(%rip),%rdx
mov    $0x1,%al
call   1050 <snprintf@plt>
jmp    121f <ucl_fd_append_double+0xdf>
movsd  -0x8(%rbp),%xmm0
cvttsd2si -0x8(%rbp),%eax
cvtsi2sd %eax,%xmm1
subsd  %xmm1,%xmm0
call   1060 <fabs@plt>
movaps %xmm0,%xmm1
movsd  0xe38(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11fe <ucl_fd_append_double+0xbe>
lea    -0x60(%rbp),%rdi
mov    0x2e0b(%rip),%rax
cvtsi2sdl (%rax),%xmm0
movsd  -0x8(%rbp),%xmm1
mov    $0x40,%esi
lea    0xe1c(%rip),%rdx
mov    $0x2,%al
call   1050 <snprintf@plt>
jmp    121a <ucl_fd_append_double+0xda>
lea    -0x60(%rbp),%rdi
movsd  -0x8(%rbp),%xmm0
mov    $0x40,%esi
lea    0xe01(%rip),%rdx
mov    $0x1,%al
call   1050 <snprintf@plt>
jmp    121f <ucl_fd_append_double+0xdf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ucl_fd_append_double>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
movapd %xmm0,%xmm1
mov    (%rdi),%ebp
cvttpd2dq %xmm0,%xmm0
cvtdq2pd %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jne    1155 <ucl_fd_append_double+0x25>
jp     1155 <ucl_fd_append_double+0x25>
lea    0xec5(%rip),%rdx
jmp    11a0 <ucl_fd_append_double+0x70>
movapd %xmm1,%xmm2
subsd  %xmm0,%xmm2
andpd  0xe9b(%rip),%xmm2
movsd  0xea3(%rip),%xmm0
ucomisd %xmm2,%xmm0
jbe    1199 <ucl_fd_append_double+0x69>
mov    0x2e6e(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sdl (%rax),%xmm0
lea    0xe96(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
mov    $0x2,%al
call   1050 <snprintf@plt>
jmp    11b3 <ucl_fd_append_double+0x83>
lea    0xe84(%rip),%rdx
mov    %rsp,%rdi
mov    $0x40,%esi
movapd %xmm1,%xmm0
mov    $0x1,%al
call   1050 <snprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.1lf\00"
@.str.1 = [6 x i8] c"%.*lg\00"
@DBL_DIG = global i32 0
@.str.2 = [4 x i8] c"%lf\00"
define i32 @ucl_fd_append_double(double %0, i8* %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i32
%6 = alloca double
%7 = alloca [64 x i8]
store double %0, double* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %5
store double 0x3E7AD7F29ABCAF48, double* %6
%11 = load double, double* %3
%12 = load double, double* %3
%13 = fptosi double %12 to i32
%14 = sitofp i32 %13 to double
%15 = fcmp oeq double %11, %14
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%18 = load double, double* %3
%19 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %17, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %18)
br label %39
20: ; preds = %2
%21 = load double, double* %3
%22 = load double, double* %3
%23 = fptosi double %22 to i32
%24 = sitofp i32 %23 to double
%25 = fsub double %21, %24
%26 = call double @fabs(double %25)
%27 = fcmp olt double %26, 0x3E7AD7F29ABCAF48
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* @DBL_DIG
%31 = sitofp i32 %30 to double
%32 = load double, double* %3
%33 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %29, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %31, double %32)
br label %38
34: ; preds = %20
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%36 = load double, double* %3
%37 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %35, i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %36)
br label %38
38: ; preds = %34, %28
br label %39
39: ; preds = %38, %16
%40 = load i32, i32* %5
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @write(i32 %40, i8* %41, i32 %43)
ret i32 %44
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.1lf\00"
@.str.1 = [6 x i8] c"%.*lg\00"
@DBL_DIG = global i32 0
@.str.2 = [4 x i8] c"%lf\00"
define i32 @ucl_fd_append_double(double %0, i8* nocapture %1) {
%3 = alloca [64 x i8]
%4 = bitcast i8* %1 to i32*
%5 = load i32, i32* %4
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = fptosi double %0 to i32
%8 = sitofp i32 %7 to double
%9 = fcmp oeq double %8, %0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %0)
br label %22
12: ; preds = %2
%13 = fsub double %0, %8
%14 = tail call double @llvm.fabs.f64(double %13)
%15 = fcmp olt double %14, 0x3E7AD7F29ABCAF48
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @DBL_DIG
%18 = sitofp i32 %17 to double
%19 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), double %18, double %0)
br label %22
20: ; preds = %12
%21 = call i32 (i8*, i32, i8*, double, ...) @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %0)
br label %22
22: ; preds = %16, %20, %10
%23 = call i32 @strlen(i8* %6)
%24 = call i32 @write(i32 %5, i8* %6, i32 %23)
ret i32 %24
}