long nextlongrand(long seed) {
  unsigned long lo, hi;
  lo = 16807 * (long)(seed & 0xFFFF);
  hi = 16807 * (long)((unsigned long)seed >> 16);
  lo += (hi & 0x7FFF) << 16;
  if (lo > 2147483647L) {
    lo &= 2147483647L;
    ++lo;
  }
  lo += hi >> 15;
  if (lo > 2147483647L) {
    lo &= 2147483647L;
    ++lo;
  }
  return (long)lo;
}


x86 assembly (clang, -O0):
<nextlongrand>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
and    $0xffff,%rax
imul   $0x41a7,%rax,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x10,%rax
imul   $0x41a7,%rax,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7fff,%rax
shl    $0x10,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x7fffffff,-0x10(%rbp)
7f
jbe    116e <nextlongrand+0x6e>
mov    -0x10(%rbp),%rax
and    $0x7fffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
shr    $0xf,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x7fffffff,-0x10(%rbp)
7f
jbe    11a6 <nextlongrand+0xa6>
mov    -0x10(%rbp),%rax
and    $0x7fffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextlongrand>:
movzwl %di,%eax
imul   $0x41a7,%rax,%rax
shr    $0x10,%rdi
imul   $0x41a7,%rdi,%rcx
imul   $0x41a70000,%edi,%edx
and    $0x7fff0000,%edx
add    %rax,%rdx
mov    %edx,%eax
and    $0x7fffffff,%eax
add    $0x1,%rax
cmp    $0x7fffffff,%rdx
cmovbe %rdx,%rax
shr    $0xf,%rcx
add    %rax,%rcx
mov    %ecx,%eax
and    $0x7fffffff,%eax
add    $0x1,%rax
cmp    $0x7fffffff,%rcx
cmovbe %rcx,%rax
ret

LLVM IR (-O0):
define i64 @nextlongrand(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = and i64 %5, 65535
%7 = mul nsw i64 16807, %6
store i64 %7, i64* %3
%8 = load i64, i64* %2
%9 = lshr i64 %8, 16
%10 = mul nsw i64 16807, %9
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = and i64 %11, 32767
%13 = shl i64 %12, 16
%14 = load i64, i64* %3
%15 = add i64 %14, %13
store i64 %15, i64* %3
%16 = load i64, i64* %3
%17 = icmp ugt i64 %16, 2147483647
br i1 %17, label %18, label %23
18: ; preds = %1
%19 = load i64, i64* %3
%20 = and i64 %19, 2147483647
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = add i64 %21, 1
store i64 %22, i64* %3
br label %23
23: ; preds = %18, %1
%24 = load i64, i64* %4
%25 = lshr i64 %24, 15
%26 = load i64, i64* %3
%27 = add i64 %26, %25
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = icmp ugt i64 %28, 2147483647
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = load i64, i64* %3
%32 = and i64 %31, 2147483647
store i64 %32, i64* %3
%33 = load i64, i64* %3
%34 = add i64 %33, 1
store i64 %34, i64* %3
br label %35
35: ; preds = %30, %23
%36 = load i64, i64* %3
ret i64 %36
}

LLVM IR (-O2):
define i64 @nextlongrand(i64 %0) {
%2 = and i64 %0, 65535
%3 = mul nuw nsw i64 %2, 16807
%4 = lshr i64 %0, 16
%5 = mul nuw nsw i64 %4, 16807
%6 = mul i64 %4, 1101463552
%7 = and i64 %6, 2147418112
%8 = add nuw nsw i64 %7, %3
%9 = icmp ugt i64 %8, 2147483647
%10 = and i64 %8, 2147483647
%11 = add nuw nsw i64 %10, 1
%12 = select i1 %9, i64 %11, i64 %8
%13 = lshr i64 %5, 15
%14 = add nuw nsw i64 %12, %13
%15 = icmp ugt i64 %14, 2147483647
%16 = and i64 %14, 2147483647
%17 = add nuw nsw i64 %16, 1
%18 = select i1 %15, i64 %17, i64 %14
ret i64 %18
}