#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 int uint32_t;

/* Variables and functions */
int RAND_MAX;
scalar_t__ getpid();
int rand();
int /*<<< orphan*/ srand(int);
scalar_t__ time(int /*<<< orphan*/ *);
uint32_t BRRand(uint32_t upperBound) {
  static int first = 1;
  uint32_t r;
  if (first)
    srand((((0x811C9dc5 ^ (unsigned)time(NULL)) * 0x01000193) ^
           (unsigned)getpid()) *
          0x01000193);
  first = 0;
  if (upperBound == 0 ||
      upperBound > ((RAND_MAX > 0x7fffffff) ? 0x7fffffff : RAND_MAX))
    upperBound = ((RAND_MAX > 0x7fffffff) ? 0x7fffffff : RAND_MAX);
  do {
    r = rand();
  } while (r < ((0xffffffff - upperBound * 2) + 1) % upperBound);
  return r % upperBound;
}


x86 assembly (clang, -O0):
<BRRand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2eee(%rip)
je     1189 <BRRand+0x49>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
xor    $0x811c9dc5,%eax
imul   $0x1000193,%eax,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rcx
mov    -0xc(%rbp),%eax
xor    %ecx,%eax
imul   $0x1000193,%eax,%edi
call   1040 <srand@plt>
movl   $0x0,0x2ead(%rip)
cmpl   $0x0,-0x4(%rbp)
je     11dd <BRRand+0x9d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e3e(%rip),%rax
cmpl   $0x7fffffff,(%rax)
jle    11c3 <BRRand+0x83>
mov    $0x7fffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11cf <BRRand+0x8f>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jle    120f <BRRand+0xcf>
mov    0x2e04(%rip),%rax
cmpl   $0x7fffffff,(%rax)
jle    11fd <BRRand+0xbd>
mov    $0x7fffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1209 <BRRand+0xc9>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <BRRand+0xd4>
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%ecx
shl    $0x1,%ecx
mov    $0xffffffff,%eax
sub    %ecx,%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    -0x1c(%rbp),%eax
cmp    %edx,%eax
jb     1214 <BRRand+0xd4>
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    %edx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BRRand>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
cmpb   $0x0,0x2ef7(%rip)
jne    1175 <BRRand+0x35>
xor    %edi,%edi
call   1050 <time@plt>
xor    $0x811c9dc5,%eax
imul   $0x1000193,%eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %ebp,%eax
imul   $0x1000193,%eax,%edi
call   1040 <srand@plt>
movb   $0x1,0x2ec8(%rip)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
cmp    %ebx,%eax
mov    %ebx,%r14d
cmovl  %eax,%r14d
test   %ebx,%ebx
cmove  %eax,%r14d
lea    (%r14,%r14,1),%ebx
neg    %ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <rand@plt>
mov    %eax,%ecx
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
cmp    %edx,%ecx
jb     11a0 <BRRand+0x60>
mov    %ecx,%eax
cltd
idiv   %r14d
mov    %edx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@BRRand.first = internal global i32 1
@RAND_MAX = global i32 0
define i32 @BRRand(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* @BRRand.first
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = call i64 @time(i32* null)
%8 = trunc i64 %7 to i32
%9 = xor i32 -2128831035, %8
%10 = mul i32 %9, 16777619
%11 = call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
%13 = xor i32 %10, %12
%14 = mul i32 %13, 16777619
%15 = call i32 @srand(i32 %14)
br label %16
16: ; preds = %6, %1
store i32 0, i32* @BRRand.first
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = load i32, i32* %2
%21 = load i32, i32* @RAND_MAX
%22 = icmp sgt i32 %21, 2147483647
br i1 %22, label %23, label %24
23: ; preds = %19
br label %26
24: ; preds = %19
%25 = load i32, i32* @RAND_MAX
br label %26
26: ; preds = %24, %23
%27 = phi i32 [ 2147483647, %23 ], [ %25, %24 ]
%28 = icmp sgt i32 %20, %27
br i1 %28, label %29, label %37
29: ; preds = %26, %16
%30 = load i32, i32* @RAND_MAX
%31 = icmp sgt i32 %30, 2147483647
br i1 %31, label %32, label %33
32: ; preds = %29
br label %35
33: ; preds = %29
%34 = load i32, i32* @RAND_MAX
br label %35
35: ; preds = %33, %32
%36 = phi i32 [ 2147483647, %32 ], [ %34, %33 ]
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %26
br label %38
38: ; preds = %40, %37
%39 = call i32 (...) @rand()
store i32 %39, i32* %3
br label %40
40: ; preds = %38
%41 = load i32, i32* %3
%42 = load i32, i32* %2
%43 = mul nsw i32 %42, 2
%44 = sub i32 -1, %43
%45 = add i32 %44, 1
%46 = load i32, i32* %2
%47 = urem i32 %45, %46
%48 = icmp ult i32 %41, %47
br i1 %48, label %38, label %49
49: ; preds = %40
%50 = load i32, i32* %3
%51 = load i32, i32* %2
%52 = srem i32 %50, %51
ret i32 %52
}

LLVM IR (-O2):
@BRRand.first = internal unnamed_addr global i1 false
@RAND_MAX = global i32 0
define i32 @BRRand(i32 %0) {
%2 = load i1, i1* @BRRand.first
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i64 @time(i32* null)
%5 = trunc i64 %4 to i32
%6 = xor i32 %5, -2128831035
%7 = mul i32 %6, 16777619
%8 = tail call i64 (...) @getpid()
%9 = trunc i64 %8 to i32
%10 = xor i32 %7, %9
%11 = mul i32 %10, 16777619
%12 = tail call i32 @srand(i32 %11)
br label %13
13: ; preds = %3, %1
store i1 true, i1* @BRRand.first
%14 = icmp eq i32 %0, 0
%15 = load i32, i32* @RAND_MAX
%16 = icmp slt i32 %15, %0
%17 = select i1 %14, i1 true, i1 %16
%18 = select i1 %17, i32 %15, i32 %0
%19 = mul i32 %18, -2
br label %20
20: ; preds = %20, %13
%21 = tail call i32 (...) @rand()
%22 = urem i32 %19, %18
%23 = icmp ult i32 %21, %22
br i1 %23, label %20, label %24
24: ; preds = %20
%25 = srem i32 %21, %18
ret i32 %25
}