#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 uint64_t;

/* Variables and functions */
int /*<<< orphan*/ IsNan(double);
int dcastu(double);
int sprintf(char *, char *, unsigned int, ...);
char *SerializeDouble(double f, char *dstStr) {
  if (!IsNan(f)) {
    int numChars = sprintf(dstStr, "%.17g", f);
    return dstStr + numChars;
  } else {
    uint64_t u = dcastu(f);
    int numChars = sprintf(dstStr, "NaN(0x%08X%08X)", (unsigned int)(u >> 32),
                           (unsigned int)u);
    return dstStr + numChars;
  }
}


x86 assembly (clang, -O0):
<SerializeDouble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <IsNan@plt>
cmp    $0x0,%eax
jne    1185 <SerializeDouble+0x55>
mov    -0x18(%rbp),%rdi
cvttsd2si -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11c3 <SerializeDouble+0x93>
movsd  -0x10(%rbp),%xmm0
call   1030 <dcastu@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    $0x20,%ecx
sar    %cl,%edx
mov    -0x20(%rbp),%ecx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SerializeDouble>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movsd  %xmm0,0x8(%rsp)
call   1040 <IsNan@plt>
test   %eax,%eax
je     1167 <SerializeDouble+0x37>
movsd  0x8(%rsp),%xmm0
call   1030 <dcastu@plt>
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    117f <SerializeDouble+0x4f>
cvttsd2si 0x8(%rsp),%rdx
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.17g\00"
@.str.1 = [16 x i8] c"NaN(0x%08X%08X)\00"
define i8* @SerializeDouble(double %0, i8* %1) {
%3 = alloca i8*
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
%9 = load double, double* %4
%10 = call i32 @IsNan(double %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = load double, double* %4
%15 = fptoui double %14 to i32
%16 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8* %20, i8** %3
br label %33
21: ; preds = %2
%22 = load double, double* %4
%23 = call i32 @dcastu(double %22)
store i32 %23, i32* %7
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = ashr i32 %25, 32
%27 = load i32, i32* %7
%28 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
store i32 %28, i32* %8
%29 = load i8*, i8** %5
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %3
br label %33
33: ; preds = %21, %12
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.17g\00"
@.str.1 = [16 x i8] c"NaN(0x%08X%08X)\00"
define i8* @SerializeDouble(double %0, i8* writeonly %1) {
%3 = tail call i32 @IsNan(double %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = fptoui double %0 to i32
%7 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %11
8: ; preds = %2
%9 = tail call i32 @dcastu(double %0)
%10 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 poison, i32 %9)
br label %11
11: ; preds = %8, %5
%12 = phi i32 [ %10, %8 ], [ %7, %5 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
ret i8* %14
}