#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 */

/* Variables and functions */
long RAND_MAX;
int rand_r(unsigned int *seed) {
  long k;
  long s = (long)(*seed);
  if (s == 0)
    s = 0x12345987;
  k = s / 127773;
  s = 16807 * (s - k * 127773) - 2836 * k;
  if (s < 0)
    s += 2147483647;
  (*seed) = (unsigned int)s;
  return (int)(s & RAND_MAX);
}


x86 assembly (clang, -O0):
<rand_r>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1125 <rand_r+0x25>
movq   $0x12345987,-0x18(%rbp)
12
mov    -0x18(%rbp),%rax
mov    $0x1f31d,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
imul   $0x1f31d,-0x10(%rbp),%rcx
sub    %rcx,%rax
imul   $0x41a7,%rax,%rax
imul   $0xb14,-0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1175 <rand_r+0x75>
mov    -0x18(%rbp),%rax
add    $0x7fffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    0x2e5c(%rip),%rcx
and    (%rcx),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_r>:
mov    (%rdi),%eax
test   %eax,%eax
mov    $0x12345987,%ecx
cmovne %eax,%ecx
imul   $0x69c16bd,%rcx,%rax
shr    $0x20,%rax
mov    %ecx,%edx
sub    %eax,%edx
shr    %edx
add    %eax,%edx
shr    $0x10,%edx
imul   $0xfffffffffffe0ce3,%rdx,%rax
add    %rcx,%rax
imul   $0x41a7,%rax,%rax
imul   $0xfffffffffffff4ec,%rdx,%rcx
add    %rax,%rcx
lea    0x7fffffff(%rcx),%eax
test   %rcx,%rcx
cmovns %ecx,%eax
mov    %eax,(%rdi)
mov    0x2e96(%rip),%rcx
and    (%rcx),%eax
ret

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

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