#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 unsigned long uint16_t;

/* Variables and functions */
extern uint16_t fast_random(unsigned long *pmseed, unsigned int max) {
  unsigned long hi, lo;
  lo = 16807 * (*pmseed & 0xffff);
  hi = 16807 * (*pmseed >> 16);
  lo += (hi & 0x7fff) << 16;
  lo += hi >> 15;
  *pmseed = (lo & 0x7fffffff) + (lo >> 31);
  return ((*pmseed & 0xffff) * max) >> 16;
}


x86 assembly (clang, -O0):
<fast_random>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
and    $0xffff,%rax
imul   $0x41a7,%rax,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
shr    $0x10,%rax
imul   $0x41a7,%rax,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0x7fff,%rax
shl    $0x10,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
shr    $0xf,%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
and    $0x7fffffff,%rcx
mov    -0x20(%rbp),%rax
shr    $0x1f,%rax
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
and    $0xffff,%rax
mov    -0xc(%rbp),%ecx
imul   %rcx,%rax
shr    $0x10,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_random>:
mov    (%rdi),%rax
movzwl %ax,%ecx
imul   $0x41a7,%rcx,%rcx
shr    $0x10,%rax
imul   $0x41a7,%rax,%rdx
imul   $0x41a70000,%eax,%eax
and    $0x7fff0000,%eax
add    %rcx,%rax
shr    $0xf,%rdx
add    %rax,%rdx
mov    %edx,%eax
and    $0x7fffffff,%eax
shr    $0x1f,%rdx
add    %rax,%rdx
mov    %rdx,(%rdi)
movzwl %dx,%ecx
mov    %esi,%eax
imul   %rcx,%rax
shr    $0x10,%rax
ret

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

LLVM IR (-O2):
define i64 @fast_random(i64* nocapture %0, i32 %1) {
%3 = load i64, i64* %0
%4 = and i64 %3, 65535
%5 = mul nuw nsw i64 %4, 16807
%6 = lshr i64 %3, 16
%7 = mul nuw nsw i64 %6, 16807
%8 = mul i64 %6, 1101463552
%9 = and i64 %8, 2147418112
%10 = add nuw nsw i64 %9, %5
%11 = lshr i64 %7, 15
%12 = add nuw nsw i64 %10, %11
%13 = and i64 %12, 2147483647
%14 = lshr i64 %12, 31
%15 = add nuw nsw i64 %13, %14
store i64 %15, i64* %0
%16 = and i64 %15, 65535
%17 = zext i32 %1 to i64
%18 = mul nuw nsw i64 %16, %17
%19 = lshr i64 %18, 16
ret i64 %19
}