#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 */
long random();
double my_random_double() {
  long int x;
  long int mant;
  long int exp = 1022;
  long int mask = 1;
  union {
    double d;
    long int i;
  } b;
  while (1) {
    x = random();
    x <<= 32;
    long int p = random();
    x = x | p;
    if (x == 0) {
      exp -= 31;
    } else {
      break;
    }
  }
  while (x & mask) {
    mask <<= 1;
    exp--;
  }
  mant = x >> 11;
  b.i = (exp << 52) | mant;
  return b.d;
}


x86 assembly (clang, -O0):
<my_random_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x3fe,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shl    $0x20,%rax
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
or     -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1172 <my_random_double+0x62>
mov    -0x18(%rbp),%rax
sub    $0x1f,%rax
mov    %rax,-0x18(%rbp)
jmp    1177 <my_random_double+0x67>
jmp    117c <my_random_double+0x6c>
jmp    1128 <my_random_double+0x18>
jmp    1181 <my_random_double+0x71>
mov    -0x8(%rbp),%rax
and    -0x20(%rbp),%rax
cmp    $0x0,%rax
je     11b0 <my_random_double+0xa0>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1181 <my_random_double+0x71>
mov    -0x8(%rbp),%rax
sar    $0xb,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x34,%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_random_double>:
push   %r14
push   %rbx
push   %rax
mov    $0x41d,%r14d
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <random@plt>
mov    %rax,%rbx
shl    $0x20,%rbx
xor    %eax,%eax
call   1030 <random@plt>
add    $0xffffffffffffffe1,%r14
or     %rax,%rbx
je     1120 <my_random_double+0x10>
test   $0x1,%al
je     115c <my_random_double+0x4c>
mov    $0x1,%eax
nopw   0x0(%rax,%rax,1)
add    %rax,%rax
add    $0xffffffffffffffff,%r14
test   %rbx,%rax
jne    1150 <my_random_double+0x40>
sar    $0xb,%rbx
shl    $0x34,%r14
or     %rbx,%r14
movq   %r14,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%union.anon = type { double }
define double @my_random_double() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca %union.anon
%6 = alloca i64
store i64 1022, i64* %3
store i64 1, i64* %4
br label %7
7: ; preds = %0, %21
%8 = call i64 (...) @random()
store i64 %8, i64* %1
%9 = load i64, i64* %1
%10 = shl i64 %9, 32
store i64 %10, i64* %1
%11 = call i64 (...) @random()
store i64 %11, i64* %6
%12 = load i64, i64* %1
%13 = load i64, i64* %6
%14 = or i64 %12, %13
store i64 %14, i64* %1
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = load i64, i64* %3
%19 = sub nsw i64 %18, 31
store i64 %19, i64* %3
br label %21
20: ; preds = %7
br label %22
21: ; preds = %17
br label %7
22: ; preds = %20
br label %23
23: ; preds = %28, %22
%24 = load i64, i64* %1
%25 = load i64, i64* %4
%26 = and i64 %24, %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i64, i64* %4
%30 = shl i64 %29, 1
store i64 %30, i64* %4
%31 = load i64, i64* %3
%32 = add nsw i64 %31, -1
store i64 %32, i64* %3
br label %23
33: ; preds = %23
%34 = load i64, i64* %1
%35 = ashr i64 %34, 11
store i64 %35, i64* %2
%36 = load i64, i64* %3
%37 = shl i64 %36, 52
%38 = load i64, i64* %2
%39 = or i64 %37, %38
%40 = bitcast %union.anon* %5 to i64*
store i64 %39, i64* %40
%41 = bitcast %union.anon* %5 to double*
%42 = load double, double* %41
ret double %42
}

LLVM IR (-O2):
define double @my_random_double() {
br label %1
1: ; preds = %1, %0
%2 = phi i64 [ 1022, %0 ], [ %8, %1 ]
%3 = tail call i64 (...) @random()
%4 = shl i64 %3, 32
%5 = tail call i64 (...) @random()
%6 = or i64 %4, %5
%7 = icmp eq i64 %6, 0
%8 = add nsw i64 %2, -31
br i1 %7, label %1, label %9
9: ; preds = %1
%10 = and i64 %5, 1
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9, %12
%13 = phi i64 [ %16, %12 ], [ %2, %9 ]
%14 = phi i64 [ %15, %12 ], [ 1, %9 ]
%15 = shl i64 %14, 1
%16 = add nsw i64 %13, -1
%17 = and i64 %15, %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %12
19: ; preds = %12, %9
%20 = phi i64 [ %2, %9 ], [ %16, %12 ]
%21 = ashr i64 %6, 11
%22 = shl i64 %20, 52
%23 = or i64 %22, %21
%24 = bitcast i64 %23 to double
ret double %24
}