#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 */
double INFINITY;
double NAN;
double nth_bit(long long, int);
double pow(int, double);
double algoritmic_long2double(long long x) {
  double sign = nth_bit(x, 1) ? -1 : 1;
  double mantissa = 0;
  double exp = 0;
  int p = 1;
  for (int i = 12; i > 1; i--) {
    exp += nth_bit(x, i) * p;
    p *= 2;
  }
  double pw = 0.5;
  for (int i = 13; i <= 64; i++) {
    mantissa += nth_bit(x, i) * pw;
    pw /= 2;
  }
  if (exp == 2047) {
    if (mantissa != 0.0)
      return NAN;
    if (sign > 0)
      return INFINITY;
    return -INFINITY;
  }
  if (exp == 0) {
    if (mantissa != 0)
      return sign * pow(2, -1022) * mantissa;
    if (sign > 0)
      return 0;
    return -0.0;
  }
  return sign * pow(2, exp - 1023) * (mantissa + 1);
}


x86 assembly (clang, -O0):
<algoritmic_long2double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <nth_bit@plt>
xorps  %xmm1,%xmm1
mov    $0x1,%eax
mov    $0xffffffff,%ecx
ucomisd %xmm1,%xmm0
setp   %sil
setne  %dl
or     %sil,%dl
cmovne %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0xc,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jle    11c3 <algoritmic_long2double+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1030 <nth_bit@plt>
cvtsi2sdl -0x2c(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
jmp    117f <algoritmic_long2double+0x5f>
movsd  0xe35(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0xd,-0x3c(%rbp)
cmpl   $0x40,-0x3c(%rbp)
jg     1228 <algoritmic_long2double+0x108>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1030 <nth_bit@plt>
movsd  -0x38(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  0xe1f(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11d7 <algoritmic_long2double+0xb7>
movsd  -0x28(%rbp),%xmm0
movsd  0xdd3(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    12ca <algoritmic_long2double+0x1aa>
jp     12ca <algoritmic_long2double+0x1aa>
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1262 <algoritmic_long2double+0x142>
jp     1262 <algoritmic_long2double+0x142>
jmp    1277 <algoritmic_long2double+0x157>
mov    0x2d8f(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    129e <algoritmic_long2double+0x17e>
mov    0x2d50(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
mov    0x2d3b(%rip),%rax
movsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1367 <algoritmic_long2double+0x247>
jp     1367 <algoritmic_long2double+0x247>
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    12ff <algoritmic_long2double+0x1df>
jp     12ff <algoritmic_long2double+0x1df>
jmp    1336 <algoritmic_long2double+0x216>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x2,%edi
movsd  0xd12(%rip),%xmm0
call   1040 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1355 <algoritmic_long2double+0x235>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
movsd  0xcc3(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13ae <algoritmic_long2double+0x28e>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xc9a(%rip),%xmm1
subsd  %xmm1,%xmm0
mov    $0x2,%edi
call   1040 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xc70(%rip),%xmm1
addsd  -0x20(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<algoritmic_long2double>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    $0x1,%esi
call   1030 <nth_bit@plt>
movsd  %xmm0,0x30(%rsp)
mov    %rbx,%rdi
mov    $0xc,%esi
call   1030 <nth_bit@plt>
movapd %xmm0,%xmm2
xorpd  %xmm1,%xmm1
xorpd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
addsd  %xmm1,%xmm2
movsd  %xmm2,0x8(%rsp)
mov    %rbx,%rdi
mov    $0xb,%esi
call   1030 <nth_bit@plt>
addsd  %xmm0,%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <nth_bit@plt>
mulsd  0xe6c(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x9,%esi
call   1030 <nth_bit@plt>
mulsd  0xe53(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x8,%esi
call   1030 <nth_bit@plt>
mulsd  0xe3a(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x7,%esi
call   1030 <nth_bit@plt>
mulsd  0xe21(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x6,%esi
call   1030 <nth_bit@plt>
mulsd  0xe08(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x5,%esi
call   1030 <nth_bit@plt>
mulsd  0xdef(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <nth_bit@plt>
mulsd  0xdd6(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x3,%esi
call   1030 <nth_bit@plt>
mulsd  0xdbd(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <nth_bit@plt>
mulsd  0xda4(%rip),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x28(%rsp)
movsd  0xd98(%rip),%xmm2
mov    $0xd,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm2,0x8(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <nth_bit@plt>
movsd  0x8(%rsp),%xmm2
mulsd  %xmm2,%xmm0
movapd 0x10(%rsp),%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,0x10(%rsp)
mulsd  0xd56(%rip),%xmm2
add    $0x1,%ebp
cmp    $0x41,%ebp
jne    12c0 <algoritmic_long2double+0x1a0>
xorpd  %xmm2,%xmm2
movsd  0x30(%rsp),%xmm1
cmpneqsd %xmm2,%xmm1
movq   %xmm1,%rax
and    $0x1,%eax
lea    0xd68(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm3
movsd  0x28(%rsp),%xmm0
ucomisd 0xd25(%rip),%xmm0
jne    134d <algoritmic_long2double+0x22d>
jp     134d <algoritmic_long2double+0x22d>
movapd 0x10(%rsp),%xmm1
ucomisd %xmm2,%xmm1
jne    133d <algoritmic_long2double+0x21d>
jnp    1391 <algoritmic_long2double+0x271>
mov    0x2cb4(%rip),%rax
movsd  (%rax),%xmm1
jmp    13fb <algoritmic_long2double+0x2db>
ucomisd %xmm2,%xmm0
jne    13b0 <algoritmic_long2double+0x290>
jp     13b0 <algoritmic_long2double+0x290>
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    1367 <algoritmic_long2double+0x247>
jnp    13e6 <algoritmic_long2double+0x2c6>
movsd  0xd01(%rip),%xmm0
mov    $0x2,%edi
movsd  %xmm3,0x8(%rsp)
call   1040 <pow@plt>
movapd 0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm2
mulsd  %xmm0,%xmm2
jmp    13e0 <algoritmic_long2double+0x2c0>
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm3
mov    0x2c40(%rip),%rax
movsd  (%rax),%xmm1
ja     13fb <algoritmic_long2double+0x2db>
xorpd  0xce2(%rip),%xmm1
jmp    13fb <algoritmic_long2double+0x2db>
addsd  0xca0(%rip),%xmm0
mov    $0x2,%edi
movsd  %xmm3,0x8(%rsp)
call   1040 <pow@plt>
movsd  0x8(%rsp),%xmm2
mulsd  %xmm0,%xmm2
movapd 0x10(%rsp),%xmm1
addsd  0xc80(%rip),%xmm1
mulsd  %xmm2,%xmm1
jmp    13fb <algoritmic_long2double+0x2db>
cmpltsd %xmm3,%xmm0
movsd  0xc75(%rip),%xmm1
andnpd %xmm1,%xmm0
movapd %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @algoritmic_long2double(i64 %0) {
%2 = alloca double
%3 = alloca i64
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
store i64 %0, i64* %3
%11 = load i64, i64* %3
%12 = call double @nth_bit(i64 %11, i32 1)
%13 = fcmp une double %12, 0.000000e+00
%14 = zext i1 %13 to i64
%15 = select i1 %13, i32 -1, i32 1
%16 = sitofp i32 %15 to double
store double %16, double* %4
store double 0.000000e+00, double* %5
store double 0.000000e+00, double* %6
store i32 1, i32* %7
store i32 12, i32* %8
br label %17
17: ; preds = %30, %1
%18 = load i32, i32* %8
%19 = icmp sgt i32 %18, 1
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i64, i64* %3
%22 = load i32, i32* %8
%23 = call double @nth_bit(i64 %21, i32 %22)
%24 = load i32, i32* %7
%25 = sitofp i32 %24 to double
%26 = load double, double* %6
%27 = call double @llvm.fmuladd.f64(double %23, double %25, double %26)
store double %27, double* %6
%28 = load i32, i32* %7
%29 = mul nsw i32 %28, 2
store i32 %29, i32* %7
br label %30
30: ; preds = %20
%31 = load i32, i32* %8
%32 = add nsw i32 %31, -1
store i32 %32, i32* %8
br label %17
33: ; preds = %17
store double 5.000000e-01, double* %9
store i32 13, i32* %10
br label %34
34: ; preds = %46, %33
%35 = load i32, i32* %10
%36 = icmp sle i32 %35, 64
br i1 %36, label %37, label %49
37: ; preds = %34
%38 = load i64, i64* %3
%39 = load i32, i32* %10
%40 = call double @nth_bit(i64 %38, i32 %39)
%41 = load double, double* %9
%42 = load double, double* %5
%43 = call double @llvm.fmuladd.f64(double %40, double %41, double %42)
store double %43, double* %5
%44 = load double, double* %9
%45 = fdiv double %44, 2.000000e+00
store double %45, double* %9
br label %46
46: ; preds = %37
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
store i32 %48, i32* %10
br label %34
49: ; preds = %34
%50 = load double, double* %6
%51 = fcmp oeq double %50, 2.047000e+03
br i1 %51, label %52, label %65
52: ; preds = %49
%53 = load double, double* %5
%54 = fcmp une double %53, 0.000000e+00
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = load double, double* @NAN
store double %56, double* %2
br label %91
57: ; preds = %52
%58 = load double, double* %4
%59 = fcmp ogt double %58, 0.000000e+00
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = load double, double* @INFINITY
store double %61, double* %2
br label %91
62: ; preds = %57
%63 = load double, double* @INFINITY
%64 = fneg double %63
store double %64, double* %2
br label %91
65: ; preds = %49
%66 = load double, double* %6
%67 = fcmp oeq double %66, 0.000000e+00
br i1 %67, label %68, label %82
68: ; preds = %65
%69 = load double, double* %5
%70 = fcmp une double %69, 0.000000e+00
br i1 %70, label %71, label %77
71: ; preds = %68
%72 = load double, double* %4
%73 = call double @pow(i32 2, double -1.022000e+03)
%74 = fmul double %72, %73
%75 = load double, double* %5
%76 = fmul double %74, %75
store double %76, double* %2
br label %91
77: ; preds = %68
%78 = load double, double* %4
%79 = fcmp ogt double %78, 0.000000e+00
br i1 %79, label %80, label %81
80: ; preds = %77
store double 0.000000e+00, double* %2
br label %91
81: ; preds = %77
store double -0.000000e+00, double* %2
br label %91
82: ; preds = %65
%83 = load double, double* %4
%84 = load double, double* %6
%85 = fsub double %84, 1.023000e+03
%86 = call double @pow(i32 2, double %85)
%87 = fmul double %83, %86
%88 = load double, double* %5
%89 = fadd double %88, 1.000000e+00
%90 = fmul double %87, %89
store double %90, double* %2
br label %91
91: ; preds = %82, %81, %80, %71, %62, %60, %55
%92 = load double, double* %2
ret double %92
}

LLVM IR (-O2):
@NAN = global double 0.000000e+00
@INFINITY = global double 0.000000e+00
define double @algoritmic_long2double(i64 %0) {
%2 = tail call double @nth_bit(i64 %0, i32 1)
%3 = fcmp une double %2, 0.000000e+00
%4 = tail call double @nth_bit(i64 %0, i32 12)
%5 = fadd double %4, 0.000000e+00
%6 = tail call double @nth_bit(i64 %0, i32 11)
%7 = tail call double @llvm.fmuladd.f64(double %6, double 2.000000e+00, double %5)
%8 = tail call double @nth_bit(i64 %0, i32 10)
%9 = tail call double @llvm.fmuladd.f64(double %8, double 4.000000e+00, double %7)
%10 = tail call double @nth_bit(i64 %0, i32 9)
%11 = tail call double @llvm.fmuladd.f64(double %10, double 8.000000e+00, double %9)
%12 = tail call double @nth_bit(i64 %0, i32 8)
%13 = tail call double @llvm.fmuladd.f64(double %12, double 1.600000e+01, double %11)
%14 = tail call double @nth_bit(i64 %0, i32 7)
%15 = tail call double @llvm.fmuladd.f64(double %14, double 3.200000e+01, double %13)
%16 = tail call double @nth_bit(i64 %0, i32 6)
%17 = tail call double @llvm.fmuladd.f64(double %16, double 6.400000e+01, double %15)
%18 = tail call double @nth_bit(i64 %0, i32 5)
%19 = tail call double @llvm.fmuladd.f64(double %18, double 1.280000e+02, double %17)
%20 = tail call double @nth_bit(i64 %0, i32 4)
%21 = tail call double @llvm.fmuladd.f64(double %20, double 2.560000e+02, double %19)
%22 = tail call double @nth_bit(i64 %0, i32 3)
%23 = tail call double @llvm.fmuladd.f64(double %22, double 5.120000e+02, double %21)
%24 = tail call double @nth_bit(i64 %0, i32 2)
%25 = tail call double @llvm.fmuladd.f64(double %24, double 1.024000e+03, double %23)
br label %29
26: ; preds = %29
%27 = select i1 %3, double -1.000000e+00, double 1.000000e+00
%28 = fcmp oeq double %25, 2.047000e+03
br i1 %28, label %38, label %47
29: ; preds = %1, %29
%30 = phi i32 [ 13, %1 ], [ %36, %29 ]
%31 = phi double [ 5.000000e-01, %1 ], [ %35, %29 ]
%32 = phi double [ 0.000000e+00, %1 ], [ %34, %29 ]
%33 = tail call double @nth_bit(i64 %0, i32 %30)
%34 = tail call double @llvm.fmuladd.f64(double %33, double %31, double %32)
%35 = fmul double %31, 5.000000e-01
%36 = add nuw nsw i32 %30, 1
%37 = icmp eq i32 %36, 65
br i1 %37, label %26, label %29
38: ; preds = %26
%39 = fcmp une double %34, 0.000000e+00
br i1 %39, label %40, label %42
40: ; preds = %38
%41 = load double, double* @NAN
br label %64
42: ; preds = %38
%43 = fcmp ogt double %27, 0.000000e+00
%44 = load double, double* @INFINITY
br i1 %43, label %64, label %45
45: ; preds = %42
%46 = fneg double %44
br label %64
47: ; preds = %26
%48 = fcmp oeq double %25, 0.000000e+00
br i1 %48, label %49, label %58
49: ; preds = %47
%50 = fcmp une double %34, 0.000000e+00
br i1 %50, label %51, label %55
51: ; preds = %49
%52 = tail call double @pow(i32 2, double -1.022000e+03)
%53 = fmul double %27, %52
%54 = fmul double %34, %53
br label %64
55: ; preds = %49
%56 = fcmp ogt double %27, 0.000000e+00
%57 = select i1 %56, double 0.000000e+00, double -0.000000e+00
br label %64
58: ; preds = %47
%59 = fadd double %25, -1.023000e+03
%60 = tail call double @pow(i32 2, double %59)
%61 = fmul double %27, %60
%62 = fadd double %34, 1.000000e+00
%63 = fmul double %62, %61
br label %64
64: ; preds = %42, %55, %58, %51, %45, %40
%65 = phi double [ %41, %40 ], [ %46, %45 ], [ %54, %51 ], [ %63, %58 ], [ %57, %55 ], [ %44, %42 ]
ret double %65
}