#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 UINT64_C(int);
void i64_to_double(uint64_t in, double *out) {
  union {
    double d;
    uint64_t i64;
  } x;
  if (in == UINT64_C(0x8000000000000000)) {
    *out = 0.0;
    return;
  }
  int sign = 0;
  if (in < UINT64_C(0x8000000000000000)) {
    sign = 1;
    in = UINT64_C(0x8000000000000000) - in;
  } else {
    in -= UINT64_C(0x8000000000000000);
  }
  int i;
  for (i = 0; i < 64; i++) {
    if (in >> (63 - i)) {
      break;
    }
  }
  if (i > 11) {
    x.i64 = in << (i - 11);
  } else {
    x.i64 = in >> (11 - i);
  }
  x.i64 =
      ((UINT64_C(0x3FF) - i) << 52) + (x.i64 & UINT64_C(0x000FFFFFFFFFFFFF));
  if (sign) {
    x.i64 |= UINT64_C(0x8000000000000000);
  }
  *out = x.d;
}


x86 assembly (clang, -O0):
<i64_to_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jne    1149 <i64_to_double+0x39>
mov    -0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
jmp    125a <i64_to_double+0x14a>
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    1183 <i64_to_double+0x73>
movl   $0x1,-0x1c(%rbp)
xor    %edi,%edi
call   1030 <UINT64_C@plt>
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1194 <i64_to_double+0x84>
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x40,-0x20(%rbp)
jge    11d3 <i64_to_double+0xc3>
mov    -0x4(%rbp),%eax
mov    $0x3f,%ecx
sub    -0x20(%rbp),%ecx
sar    %cl,%eax
cmp    $0x0,%eax
je     11c0 <i64_to_double+0xb0>
jmp    11d3 <i64_to_double+0xc3>
jmp    11c5 <i64_to_double+0xb5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119b <i64_to_double+0x8b>
cmpl   $0xb,-0x20(%rbp)
jle    11f0 <i64_to_double+0xe0>
mov    -0x4(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0xb,%ecx
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
jmp    1200 <i64_to_double+0xf0>
mov    -0x4(%rbp),%eax
mov    $0xb,%ecx
sub    -0x20(%rbp),%ecx
sar    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    $0x3ff,%edi
call   1030 <UINT64_C@plt>
sub    -0x20(%rbp),%eax
mov    $0x34,%ecx
shl    %cl,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
mov    -0x30(%rbp),%ecx
mov    %eax,%edx
mov    -0x2c(%rbp),%eax
and    %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     124d <i64_to_double+0x13d>
xor    %edi,%edi
call   1030 <UINT64_C@plt>
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i64_to_double>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
xor    %edi,%edi
call   1030 <UINT64_C@plt>
cmp    %ebx,%eax
jne    112d <i64_to_double+0x1d>
movq   $0x0,(%r14)
jmp    1160 <i64_to_double+0x50>
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1030 <UINT64_C@plt>
mov    $0x3ff,%edi
call   1030 <UINT64_C@plt>
mov    $0xffffffff,%edi
call   1030 <UINT64_C@plt>
cmp    %ebx,%ebp
jle    1160 <i64_to_double+0x50>
xor    %edi,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <UINT64_C@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%union.anon = type { double }
define void @i64_to_double(i32 %0, double* %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca %union.anon
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store double* %1, double** %4
%8 = load i32, i32* %3
%9 = call i32 @UINT64_C(i32 0)
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load double*, double** %4
store double 0.000000e+00, double* %12
br label %77
13: ; preds = %2
store i32 0, i32* %6
%14 = load i32, i32* %3
%15 = call i32 @UINT64_C(i32 0)
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %13
store i32 1, i32* %6
%18 = call i32 @UINT64_C(i32 0)
%19 = load i32, i32* %3
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %3
br label %25
21: ; preds = %13
%22 = call i32 @UINT64_C(i32 0)
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %3
br label %25
25: ; preds = %21, %17
store i32 0, i32* %7
br label %26
26: ; preds = %37, %25
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 64
br i1 %28, label %29, label %40
29: ; preds = %26
%30 = load i32, i32* %3
%31 = load i32, i32* %7
%32 = sub nsw i32 63, %31
%33 = ashr i32 %30, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
br label %40
36: ; preds = %29
br label %37
37: ; preds = %36
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %26
40: ; preds = %35, %26
%41 = load i32, i32* %7
%42 = icmp sgt i32 %41, 11
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32, i32* %3
%45 = load i32, i32* %7
%46 = sub nsw i32 %45, 11
%47 = shl i32 %44, %46
%48 = bitcast %union.anon* %5 to i32*
store i32 %47, i32* %48
br label %55
49: ; preds = %40
%50 = load i32, i32* %3
%51 = load i32, i32* %7
%52 = sub nsw i32 11, %51
%53 = ashr i32 %50, %52
%54 = bitcast %union.anon* %5 to i32*
store i32 %53, i32* %54
br label %55
55: ; preds = %49, %43
%56 = call i32 @UINT64_C(i32 1023)
%57 = load i32, i32* %7
%58 = sub nsw i32 %56, %57
%59 = shl i32 %58, 52
%60 = bitcast %union.anon* %5 to i32*
%61 = load i32, i32* %60
%62 = call i32 @UINT64_C(i32 -1)
%63 = and i32 %61, %62
%64 = add nsw i32 %59, %63
%65 = bitcast %union.anon* %5 to i32*
store i32 %64, i32* %65
%66 = load i32, i32* %6
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %55
%69 = call i32 @UINT64_C(i32 0)
%70 = bitcast %union.anon* %5 to i32*
%71 = load i32, i32* %70
%72 = or i32 %71, %69
store i32 %72, i32* %70
br label %73
73: ; preds = %68, %55
%74 = bitcast %union.anon* %5 to double*
%75 = load double, double* %74
%76 = load double*, double** %4
store double %75, double* %76
br label %77
77: ; preds = %73, %11
ret void
}

LLVM IR (-O2):
define void @i64_to_double(i32 %0, double* nocapture writeonly %1) {
%3 = tail call i32 @UINT64_C(i32 0)
%4 = icmp eq i32 %3, %0
br i1 %4, label %5, label %6
5: ; preds = %2
store double 0.000000e+00, double* %1
br label %14
6: ; preds = %2
%7 = tail call i32 @UINT64_C(i32 0)
%8 = icmp sgt i32 %7, %0
%9 = tail call i32 @UINT64_C(i32 0)
%10 = tail call i32 @UINT64_C(i32 1023)
%11 = tail call i32 @UINT64_C(i32 -1)
br i1 %8, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @UINT64_C(i32 0)
br label %14
14: ; preds = %6, %12, %5
ret void
}