#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 */
void *random();
double my_random_double() {
  unsigned long x;
  int mant;
  int exp = 1022;
  int mask = 1;
  int count = 0;
  union {
    double f;
    unsigned long i[2];
  } b;
  while (1) {
    x = random();
    if (x == 0) {
      exp -= 31;
    } else {
      break;
    }
  }
  while (x & mask) {
    mask <<= 1;
    count++;
  }
  exp -= count;
  if (exp < 0)
    exp = 0;
  if (count > 11)
    x = random();
  mant = x >> 11;
  b.i[1] = (exp << 20) | mant;
  b.i[0] = random();
  return b.f;
}


x86 assembly (clang, -O0):
<my_random_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x3fe,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1151 <my_random_double+0x41>
mov    -0x10(%rbp),%eax
sub    $0x1f,%eax
mov    %eax,-0x10(%rbp)
jmp    1156 <my_random_double+0x46>
jmp    115b <my_random_double+0x4b>
jmp    112d <my_random_double+0x1d>
jmp    1160 <my_random_double+0x50>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     118c <my_random_double+0x7c>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <my_random_double+0x50>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a8 <my_random_double+0x98>
movl   $0x0,-0x10(%rbp)
cmpl   $0xb,-0x18(%rbp)
jle    11bd <my_random_double+0xad>
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0xb,%rax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x14,%eax
or     -0xc(%rbp),%eax
cltq
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
mov    %rax,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x30,%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   %rax,%rax
je     1120 <my_random_double+0x10>
test   $0x1,%al
je     1159 <my_random_double+0x49>
mov    $0x1,%edx
mov    $0xffffffff,%ecx
nopw   0x0(%rax,%rax,1)
add    %edx,%edx
movslq %edx,%rsi
add    $0x1,%ecx
test   %rax,%rsi
jne    1140 <my_random_double+0x30>
cmp    $0xb,%ecx
jb     1159 <my_random_double+0x49>
xor    %eax,%eax
call   1030 <random@plt>
xor    %eax,%eax
call   1030 <random@plt>
movq   %rax,%xmm0
pop    %rax
ret

LLVM IR (-O0):
%union.anon = type { [2 x i64] }
define double @my_random_double() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %union.anon
store i32 1022, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %0, %16
%8 = call i8* (...) @random()
%9 = ptrtoint i8* %8 to i64
store i64 %9, i64* %1
%10 = load i64, i64* %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 31
store i32 %14, i32* %3
br label %16
15: ; preds = %7
br label %17
16: ; preds = %12
br label %7
17: ; preds = %15
br label %18
18: ; preds = %24, %17
%19 = load i64, i64* %1
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = and i64 %19, %21
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* %4
%26 = shl i32 %25, 1
store i32 %26, i32* %4
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %18
29: ; preds = %18
%30 = load i32, i32* %5
%31 = load i32, i32* %3
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 0, i32* %3
br label %36
36: ; preds = %35, %29
%37 = load i32, i32* %5
%38 = icmp sgt i32 %37, 11
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i8* (...) @random()
%41 = ptrtoint i8* %40 to i64
store i64 %41, i64* %1
br label %42
42: ; preds = %39, %36
%43 = load i64, i64* %1
%44 = lshr i64 %43, 11
%45 = trunc i64 %44 to i32
store i32 %45, i32* %2
%46 = load i32, i32* %3
%47 = shl i32 %46, 20
%48 = load i32, i32* %2
%49 = or i32 %47, %48
%50 = sext i32 %49 to i64
%51 = bitcast %union.anon* %6 to [2 x i64]*
%52 = getelementptr inbounds [2 x i64], [2 x i64]* %51, i64 0, i64 1
store i64 %50, i64* %52
%53 = call i8* (...) @random()
%54 = ptrtoint i8* %53 to i64
%55 = bitcast %union.anon* %6 to [2 x i64]*
%56 = getelementptr inbounds [2 x i64], [2 x i64]* %55, i64 0, i64 0
store i64 %54, i64* %56
%57 = bitcast %union.anon* %6 to double*
%58 = load double, double* %57
ret double %58
}

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