#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 */
int random();
double my_random_double() {
  int x;
  int mant;
  long exp = 1022;
  int mask = 1;
  union {
    double d;
    int i;
  } b;
  while (1) {
    x = random();
    if (x == 0) {
      exp -= 63;
    } 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    $0x20,%rsp
movq   $0x3fe,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    114c <my_random_double+0x3c>
mov    -0x10(%rbp),%rax
sub    $0x3f,%rax
mov    %rax,-0x10(%rbp)
jmp    1151 <my_random_double+0x41>
jmp    1156 <my_random_double+0x46>
jmp    1127 <my_random_double+0x17>
jmp    115b <my_random_double+0x4b>
mov    -0x4(%rbp),%eax
and    -0x14(%rbp),%eax
cmp    $0x0,%eax
je     1184 <my_random_double+0x74>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    115b <my_random_double+0x4b>
mov    -0x4(%rbp),%eax
sar    $0xb,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x34,%rax
movslq -0x8(%rbp),%rcx
or     %rcx,%rax
mov    %eax,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_random_double>:
push   %rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <random@plt>
test   %eax,%eax
je     1120 <my_random_double+0x10>
sar    $0xb,%eax
movq   %rax,%xmm0
pop    %rax
ret

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

LLVM IR (-O2):
define double @my_random_double() {
br label %1
1: ; preds = %1, %0
%2 = tail call i32 (...) @random()
%3 = icmp eq i32 %2, 0
br i1 %3, label %1, label %4
4: ; preds = %1
%5 = ashr i32 %2, 11
%6 = zext i32 %5 to i64
%7 = bitcast i64 %6 to double
ret double %7
}