double rand_val(int seed) {
  const long a = 16807;
  const long m = 2147483647;
  const long q = 127773;
  const long r = 2836;
  static long x;
  long x_div_q;
  long x_mod_q;
  long x_new;
  if (seed > 0) {
    x = seed;
    return (0.0);
  }
  x_div_q = x / q;
  x_mod_q = x % q;
  x_new = (a * x_mod_q) - (r * x_div_q);
  if (x_new > 0)
    x = x_new;
  else
    x = x_new + m;
  return ((double)x / m);
}


x86 assembly (clang, -O0):
<rand_val>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
movq   $0x41a7,-0x18(%rbp)
movq   $0x7fffffff,-0x20(%rbp)
7f
movq   $0x1f31d,-0x28(%rbp)
movq   $0xb14,-0x30(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1149 <rand_val+0x49>
movslq -0xc(%rbp),%rax
mov    %rax,0x2eec(%rip)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d0 <rand_val+0xd0>
mov    0x2ed8(%rip),%rax
mov    $0x1f31d,%ecx
cqto
idiv   %rcx
mov    %rax,-0x38(%rbp)
mov    0x2ec3(%rip),%rax
mov    $0x1f31d,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x40(%rbp)
imul   $0x41a7,-0x40(%rbp),%rax
imul   $0xb14,-0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jle    11a5 <rand_val+0xa5>
mov    -0x48(%rbp),%rax
mov    %rax,0x2e88(%rip)
jmp    11b6 <rand_val+0xb6>
mov    -0x48(%rbp),%rax
add    $0x7fffffff,%rax
mov    %rax,0x2e72(%rip)
cvtsi2sdq 0x2e69(%rip),%xmm0
movsd  0xe39(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_val>:
test   %edi,%edi
jle    1111 <rand_val+0x11>
mov    %edi,%eax
mov    %rax,0x2f1b(%rip)
xorps  %xmm0,%xmm0
ret
mov    0x2f10(%rip),%rcx
movabs $0x41a705af1fe3fb79,%rdx
05 a7 41
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xf,%rdx
add    %rax,%rdx
imul   $0x1f31d,%rdx,%rax
sub    %rax,%rcx
imul   $0x41a7,%rcx,%rax
imul   $0xfffffffffffff4ec,%rdx,%rcx
lea    (%rcx,%rax,1),%rdx
test   %rdx,%rdx
lea    0x7fffffff(%rcx,%rax,1),%rax
7f
cmovg  %rdx,%rax
mov    %rax,0x2ec0(%rip)
cvtsi2sd %rax,%xmm0
divsd  0xe8b(%rip),%xmm0
ret

LLVM IR (-O0):
@rand_val.x = internal global i64 0
define double @rand_val(i32 %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %3
store i64 16807, i64* %4
store i64 2147483647, i64* %5
store i64 127773, i64* %6
store i64 2836, i64* %7
%11 = load i32, i32* %3
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* %3
%15 = sext i32 %14 to i64
store i64 %15, i64* @rand_val.x
store double 0.000000e+00, double* %2
br label %37
16: ; preds = %1
%17 = load i64, i64* @rand_val.x
%18 = sdiv i64 %17, 127773
store i64 %18, i64* %8
%19 = load i64, i64* @rand_val.x
%20 = srem i64 %19, 127773
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = mul nsw i64 16807, %21
%23 = load i64, i64* %8
%24 = mul nsw i64 2836, %23
%25 = sub nsw i64 %22, %24
store i64 %25, i64* %10
%26 = load i64, i64* %10
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = load i64, i64* %10
store i64 %29, i64* @rand_val.x
br label %33
30: ; preds = %16
%31 = load i64, i64* %10
%32 = add nsw i64 %31, 2147483647
store i64 %32, i64* @rand_val.x
br label %33
33: ; preds = %30, %28
%34 = load i64, i64* @rand_val.x
%35 = sitofp i64 %34 to double
%36 = fdiv double %35, 0x41DFFFFFFFC00000
store double %36, double* %2
br label %37
37: ; preds = %33, %13
%38 = load double, double* %2
ret double %38
}

LLVM IR (-O2):
@rand_val.x = internal unnamed_addr global i64 0
define double @rand_val(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = zext i32 %0 to i64
store i64 %4, i64* @rand_val.x
br label %17
5: ; preds = %1
%6 = load i64, i64* @rand_val.x
%7 = sdiv i64 %6, 127773
%8 = srem i64 %6, 127773
%9 = mul nsw i64 %8, 16807
%10 = mul nsw i64 %7, -2836
%11 = add nsw i64 %10, %9
%12 = icmp sgt i64 %11, 0
%13 = add nsw i64 %11, 2147483647
%14 = select i1 %12, i64 %11, i64 %13
store i64 %14, i64* @rand_val.x
%15 = sitofp i64 %14 to double
%16 = fdiv double %15, 0x41DFFFFFFFC00000
br label %17
17: ; preds = %5, %3
%18 = phi double [ 0.000000e+00, %3 ], [ %16, %5 ]
ret double %18
}