int write_U16Fixed16Number(double d, char *p) {
  unsigned long o32;
  d = d * 65536.0 + 0.5;
  if (d >= 4294967296.0)
    return 1;
  if (d < 0.0)
    return 1;
  o32 = (unsigned long)d;
  ((unsigned char *)p)[0] = (unsigned char)((o32) >> 24);
  ((unsigned char *)p)[1] = (unsigned char)((o32) >> 16);
  ((unsigned char *)p)[2] = (unsigned char)((o32) >> 8);
  ((unsigned char *)p)[3] = (unsigned char)((o32));
  return 0;
}


x86 assembly (clang, -O0):
<write_U16Fixed16Number>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xeee(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xeea(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xec4(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1152 <write_U16Fixed16Number+0x52>
movl   $0x1,-0x4(%rbp)
jmp    11e1 <write_U16Fixed16Number+0xe1>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    116c <write_U16Fixed16Number+0x6c>
movl   $0x1,-0x4(%rbp)
jmp    11e1 <write_U16Fixed16Number+0xe1>
movsd  -0x10(%rbp),%xmm0
movsd  0xe9f(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x18,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
shr    $0x10,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x20(%rbp),%rax
shr    $0x8,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x20(%rbp),%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,0x3(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_U16Fixed16Number>:
mulsd  0xef8(%rip),%xmm0
addsd  0xef8(%rip),%xmm0
ucomisd 0xef8(%rip),%xmm0
mov    $0x1,%eax
jae    114c <write_U16Fixed16Number+0x4c>
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
ja     114c <write_U16Fixed16Number+0x4c>
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xedf(%rip),%xmm0
sar    $0x3f,%rcx
cvttsd2si %xmm0,%rdx
and    %ecx,%edx
or     %edx,%eax
bswap  %eax
mov    %eax,(%rdi)
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @write_U16Fixed16Number(double %0, i8* %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i8*
%6 = alloca i64
store double %0, double* %4
store i8* %1, i8** %5
%7 = load double, double* %4
%8 = call double @llvm.fmuladd.f64(double %7, double 6.553600e+04, double 5.000000e-01)
store double %8, double* %4
%9 = load double, double* %4
%10 = fcmp oge double %9, 0x41F0000000000000
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %38
12: ; preds = %2
%13 = load double, double* %4
%14 = fcmp olt double %13, 0.000000e+00
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 1, i32* %3
br label %38
16: ; preds = %12
%17 = load double, double* %4
%18 = fptoui double %17 to i64
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = lshr i64 %19, 24
%21 = trunc i64 %20 to i8
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 %21, i8* %23
%24 = load i64, i64* %6
%25 = lshr i64 %24, 16
%26 = trunc i64 %25 to i8
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 %26, i8* %28
%29 = load i64, i64* %6
%30 = lshr i64 %29, 8
%31 = trunc i64 %30 to i8
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 2
store i8 %31, i8* %33
%34 = load i64, i64* %6
%35 = trunc i64 %34 to i8
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i64 3
store i8 %35, i8* %37
store i32 0, i32* %3
br label %38
38: ; preds = %16, %15, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @write_U16Fixed16Number(double %0, i8* nocapture writeonly %1) {
%3 = tail call double @llvm.fmuladd.f64(double %0, double 6.553600e+04, double 5.000000e-01)
%4 = fcmp oge double %3, 0x41F0000000000000
%5 = fcmp olt double %3, 0.000000e+00
%6 = or i1 %4, %5
br i1 %6, label %19, label %7
7: ; preds = %2
%8 = fptoui double %3 to i64
%9 = lshr i64 %8, 24
%10 = trunc i64 %9 to i8
store i8 %10, i8* %1
%11 = lshr i64 %8, 16
%12 = trunc i64 %11 to i8
%13 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %12, i8* %13
%14 = lshr i64 %8, 8
%15 = trunc i64 %14 to i8
%16 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %15, i8* %16
%17 = trunc i64 %8 to i8
%18 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %17, i8* %18
br label %19
19: ; preds = %2, %7
%20 = phi i32 [ 0, %7 ], [ 1, %2 ]
ret i32 %20
}