#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 */
scalar_t__ getseed();
int /*<<< orphan*/ initstate(unsigned int, char *, int);
long random();
int rnd(int n) {
  static int first_time = 1;
  static char state[256];
  if (first_time) {
    first_time = 0;
    unsigned int seed = (unsigned int)getseed();
    initstate(seed, state, 256);
  }
  if (n < 0) {
    return 0;
  }
  unsigned long mask = 1;
  while (mask < n)
    mask = 2 * mask + 1;
  for (;;) {
    long r = random();
    if ((r & mask) < n)
      return r & mask;
  }
}


x86 assembly (clang, -O0):
<rnd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,0x2ef6(%rip)
je     1170 <rnd+0x40>
movl   $0x0,0x2ee6(%rip)
mov    $0x0,%al
call   1040 <getseed@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0x2eea(%rip),%rsi
mov    $0x100,%edx
call   1030 <initstate@plt>
cmpl   $0x0,-0x8(%rbp)
jge    1186 <rnd+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11ee <rnd+0xbe>
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jae    11b4 <rnd+0x84>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    118e <rnd+0x5e>
jmp    11b9 <rnd+0x89>
mov    $0x0,%al
call   1050 <random@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jae    11e9 <rnd+0xb9>
mov    -0x20(%rbp),%rax
and    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11ee <rnd+0xbe>
jmp    11b9 <rnd+0x89>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rnd>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
cmpb   $0x0,0x2f12(%rip)
jne    1161 <rnd+0x31>
movb   $0x1,0x2f09(%rip)
xor    %eax,%eax
call   1040 <getseed@plt>
lea    0x2f0b(%rip),%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   1030 <initstate@plt>
test   %r14d,%r14d
js     11a1 <rnd+0x71>
mov    %r14d,%ebx
mov    $0x1,%ebp
cmp    $0x2,%r14d
jb     1190 <rnd+0x60>
mov    $0x1,%ebp
nopl   0x0(%rax)
add    %rbp,%rbp
add    $0x1,%rbp
cmp    %rbx,%rbp
jb     1180 <rnd+0x50>
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <random@plt>
and    %rbp,%rax
cmp    %rbx,%rax
jae    1190 <rnd+0x60>
jmp    11a3 <rnd+0x73>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@rnd.first_time = internal global i32 1
@rnd.state = internal global [256 x i8] zeroinitializer
define i32 @rnd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
%7 = load i32, i32* @rnd.first_time
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
store i32 0, i32* @rnd.first_time
%10 = call i64 (...) @getseed()
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i32 @initstate(i32 %12, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @rnd.state, i64 0, i64 0), i32 256)
br label %14
14: ; preds = %9, %1
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 0, i32* %2
br label %43
18: ; preds = %14
store i64 1, i64* %5
br label %19
19: ; preds = %24, %18
%20 = load i64, i64* %5
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %20, %22
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i64, i64* %5
%26 = mul i64 2, %25
%27 = add i64 %26, 1
store i64 %27, i64* %5
br label %19
28: ; preds = %19
br label %29
29: ; preds = %42, %28
%30 = call i64 (...) @random()
store i64 %30, i64* %6
%31 = load i64, i64* %6
%32 = load i64, i64* %5
%33 = and i64 %31, %32
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = icmp ult i64 %33, %35
br i1 %36, label %37, label %42
37: ; preds = %29
%38 = load i64, i64* %6
%39 = load i64, i64* %5
%40 = and i64 %38, %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %2
br label %43
42: ; preds = %29
br label %29
43: ; preds = %37, %17
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@rnd.first_time = internal unnamed_addr global i1 false
@rnd.state = internal global [256 x i8] zeroinitializer
define i32 @rnd(i32 %0) {
%2 = load i1, i1* @rnd.first_time
br i1 %2, label %7, label %3
3: ; preds = %1
store i1 true, i1* @rnd.first_time
%4 = tail call i64 (...) @getseed()
%5 = trunc i64 %4 to i32
%6 = tail call i32 @initstate(i32 %5, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @rnd.state, i64 0, i64 0), i32 256)
br label %7
7: ; preds = %3, %1
%8 = icmp slt i32 %0, 0
br i1 %8, label %25, label %9
9: ; preds = %7
%10 = zext i32 %0 to i64
%11 = icmp ugt i32 %0, 1
br i1 %11, label %14, label %12
12: ; preds = %14, %9
%13 = phi i64 [ 1, %9 ], [ %17, %14 ]
br label %19
14: ; preds = %9, %14
%15 = phi i64 [ %17, %14 ], [ 1, %9 ]
%16 = shl i64 %15, 1
%17 = or i64 %16, 1
%18 = icmp ult i64 %17, %10
br i1 %18, label %14, label %12
19: ; preds = %12, %19
%20 = tail call i64 (...) @random()
%21 = and i64 %20, %13
%22 = icmp ult i64 %21, %10
br i1 %22, label %23, label %19
23: ; preds = %19
%24 = trunc i64 %21 to i32
br label %25
25: ; preds = %23, %7
%26 = phi i32 [ 0, %7 ], [ %24, %23 ]
ret i32 %26
}