#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
double rand_schrage(double x) {
  if (x < 0 || x > 1) {
    fprintf(stderr, "rand_schrage: input must be within [0,1]\n");
    abort();
  }
  int32_t z = x * ((1U << 31) - 1);
  int32_t q = ((1U << 31) - 1) / (16807);
  int32_t r = ((1U << 31) - 1) % (16807);
  int32_t retval = (16807) * (z % q) - r * (z / q);
  if (retval < 0)
    retval += ((1U << 31) - 1);
  return (double)retval / (double)((1U << 31) - 1);
}


x86 assembly (clang, -O0):
<rand_schrage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
ja     1152 <rand_schrage+0x32>
movsd  -0x8(%rbp),%xmm0
movsd  0xeb8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    116e <rand_schrage+0x4e>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
lea    0xeae(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movsd  0xe92(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xc(%rbp)
movl   $0x1f31d,-0x10(%rbp)
movl   $0xb14,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x10(%rbp)
imul   $0x41a7,%edx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%eax
cltd
idivl  -0x10(%rbp)
mov    %eax,%edx
mov    -0x1c(%rbp),%eax
imul   %edx,%ecx
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cc <rand_schrage+0xac>
mov    -0x18(%rbp),%eax
add    $0x7fffffff,%eax
mov    %eax,-0x18(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm0
movsd  0xe2f(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_schrage>:
push   %rax
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
ja     118d <rand_schrage+0x6d>
ucomisd 0xecd(%rip),%xmm0
ja     118d <rand_schrage+0x6d>
movsd  0xecb(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
cltq
imul   $0xffffffff834e0b5f,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x10,%ecx
add    %edx,%ecx
imul   $0x1f31d,%ecx,%edx
sub    %edx,%eax
imul   $0x41a7,%eax,%eax
imul   $0xfffff4ec,%ecx,%ecx
lea    (%rcx,%rax,1),%edx
add    $0x7fffffff,%edx
add    %eax,%ecx
cmovns %ecx,%edx
xorps  %xmm0,%xmm0
cvtsi2sd %edx,%xmm0
divsd  %xmm1,%xmm0
pop    %rax
ret
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"rand_schrage: input must be within [0,1]\0A\00"
define double @rand_schrage(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store double %0, double* %2
%7 = load double, double* %2
%8 = fcmp olt double %7, 0.000000e+00
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load double, double* %2
%11 = fcmp ogt double %10, 1.000000e+00
br i1 %11, label %12, label %16
12: ; preds = %9, %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %9
%17 = load double, double* %2
%18 = fmul double %17, 0x41DFFFFFFFC00000
%19 = fptosi double %18 to i32
store i32 %19, i32* %3
store i32 127773, i32* %4
store i32 2836, i32* %5
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = srem i32 %20, %21
%23 = mul nsw i32 16807, %22
%24 = load i32, i32* %5
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = sdiv i32 %25, %26
%28 = mul nsw i32 %24, %27
%29 = sub nsw i32 %23, %28
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %16
%33 = load i32, i32* %6
%34 = add i32 %33, 2147483647
store i32 %34, i32* %6
br label %35
35: ; preds = %32, %16
%36 = load i32, i32* %6
%37 = sitofp i32 %36 to double
%38 = fdiv double %37, 0x41DFFFFFFFC00000
ret double %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"rand_schrage: input must be within [0,1]\0A\00"
define double @rand_schrage(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
%3 = fcmp ogt double %0, 1.000000e+00
%4 = or i1 %2, %3
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (...) @abort()
unreachable
9: ; preds = %1
%10 = fmul double %0, 0x41DFFFFFFFC00000
%11 = fptosi double %10 to i32
%12 = srem i32 %11, 127773
%13 = mul nsw i32 %12, 16807
%14 = sdiv i32 %11, 127773
%15 = mul nsw i32 %14, -2836
%16 = add i32 %15, %13
%17 = icmp slt i32 %16, 0
%18 = add i32 %16, 2147483647
%19 = select i1 %17, i32 %18, i32 %16
%20 = sitofp i32 %19 to double
%21 = fdiv double %20, 0x41DFFFFFFFC00000
ret double %21
}