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

/* Variables and functions */
scalar_t__ RAND_MAX;
int do_rand(unsigned long *ctx) {
  long hi, lo, x;
  if (*ctx == 0)
    *ctx = 123459876;
  hi = *ctx / 127773;
  lo = *ctx % 127773;
  x = 16807 * lo - 2836 * hi;
  if (x < 0)
    x += 0x7fffffff;
  return ((*ctx = x) % ((u_long)RAND_MAX + 1));
}


x86 assembly (clang, -O0):
<do_rand>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1121 <do_rand+0x21>
mov    -0x8(%rbp),%rax
movq   $0x75bd924,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    $0x1f31d,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    $0x1f31d,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x18(%rbp)
imul   $0x41a7,-0x18(%rbp),%rax
imul   $0xb14,-0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    117b <do_rand+0x7b>
mov    -0x20(%rbp),%rax
add    $0x7fffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%rcx
add    $0x1,%rcx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rand>:
mov    (%rdi),%rax
test   %rax,%rax
mov    $0x75bd924,%ecx
cmovne %rax,%rcx
movabs $0x41a705af1fe3fb79,%rdx
05 a7 41
mov    %rcx,%rax
mul    %rdx
shr    $0xf,%rdx
imul   $0x1f31d,%rdx,%rax
sub    %rax,%rcx
imul   $0x41a7,%rcx,%rcx
imul   $0xfffffffffffff4ec,%rdx,%rdx
lea    (%rdx,%rcx,1),%rax
add    $0x7fffffff,%rax
add    %rcx,%rdx
cmovns %rdx,%rax
mov    %rax,(%rdi)
mov    0x2e92(%rip),%rcx
mov    (%rcx),%rcx
add    $0x1,%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1172 <do_rand+0x72>
xor    %edx,%edx
div    %rcx
mov    %rdx,%rax
ret
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i32 @do_rand(i64* %0) {
%2 = alloca i64*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i64* %0, i64** %2
%6 = load i64*, i64** %2
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = load i64*, i64** %2
store i64 123459876, i64* %10
br label %11
11: ; preds = %9, %1
%12 = load i64*, i64** %2
%13 = load i64, i64* %12
%14 = udiv i64 %13, 127773
store i64 %14, i64* %3
%15 = load i64*, i64** %2
%16 = load i64, i64* %15
%17 = urem i64 %16, 127773
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = mul nsw i64 16807, %18
%20 = load i64, i64* %3
%21 = mul nsw i64 2836, %20
%22 = sub nsw i64 %19, %21
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %11
%26 = load i64, i64* %5
%27 = add nsw i64 %26, 2147483647
store i64 %27, i64* %5
br label %28
28: ; preds = %25, %11
%29 = load i64, i64* %5
%30 = load i64*, i64** %2
store i64 %29, i64* %30
%31 = load i64, i64* @RAND_MAX
%32 = add i64 %31, 1
%33 = urem i64 %29, %32
%34 = trunc i64 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i32 @do_rand(i64* nocapture %0) {
%2 = load i64, i64* %0
%3 = icmp eq i64 %2, 0
%4 = select i1 %3, i64 123459876, i64 %2
%5 = udiv i64 %4, 127773
%6 = urem i64 %4, 127773
%7 = mul nuw nsw i64 %6, 16807
%8 = mul nsw i64 %5, -2836
%9 = add nsw i64 %8, %7
%10 = icmp slt i64 %9, 0
%11 = add nsw i64 %9, 2147483647
%12 = select i1 %10, i64 %11, i64 %9
store i64 %12, i64* %0
%13 = load i64, i64* @RAND_MAX
%14 = add i64 %13, 1
%15 = urem i64 %12, %14
%16 = trunc i64 %15 to i32
ret i32 %16
}