#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 /*<<< orphan*/ seed;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
unsigned int drand48();
int open(char *, int /*<<< orphan*/);
int read(int, unsigned short *, int);
int /*<<< orphan*/ seed48(unsigned short *);
int /*<<< orphan*/ srand48(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
unsigned unif_random(unsigned n) {
  static int seeded = 0;
  if (!seeded) {
    int fd = open("/dev/urandom", O_RDONLY);
    unsigned short seed[3];
    if (fd < 0 || read(fd, seed, sizeof(seed)) < (int)sizeof(seed)) {
      srand48(time(NULL));
    } else {
      seed48(seed);
    }
    if (fd >= 0)
      close(fd);
    seeded = 1;
  }
  return (int)(drand48() * n);
}


x86 assembly (clang, -O0):
<unif_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,0x2eda(%rip)
jne    11fe <unif_random+0x8e>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11c4 <unif_random+0x54>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x6,%edx
call   1040 <read@plt>
cmp    $0x6,%eax
jge    11d9 <unif_random+0x69>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1070 <srand48@plt>
jmp    11e2 <unif_random+0x72>
lea    -0xe(%rbp),%rdi
call   1060 <seed48@plt>
cmpl   $0x0,-0x8(%rbp)
jl     11f4 <unif_random+0x84>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,0x2e5e(%rip)
mov    $0x0,%al
call   1090 <drand48@plt>
imul   -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unif_random>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
cmpb   $0x0,0x2ee0(%rip)
jne    11db <unif_random+0x6b>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11af <unif_random+0x3f>
lea    0x2(%rsp),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1040 <read@plt>
cmp    $0x6,%eax
jge    11c3 <unif_random+0x53>
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1070 <srand48@plt>
test   %ebp,%ebp
jns    11cd <unif_random+0x5d>
jmp    11d4 <unif_random+0x64>
lea    0x2(%rsp),%rdi
call   1060 <seed48@plt>
mov    %ebp,%edi
call   1030 <close@plt>
movb   $0x1,0x2e81(%rip)
xor    %eax,%eax
call   1090 <drand48@plt>
imul   %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@unif_random.seeded = internal global i32 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @unif_random(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i16]
store i32 %0, i32* %2
%5 = load i32, i32* @unif_random.seeded
%6 = icmp ne i32 %5, 0
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = load i32, i32* %3
%14 = getelementptr inbounds [3 x i16], [3 x i16]* %4, i64 0, i64 0
%15 = call i32 @read(i32 %13, i16* %14, i32 6)
%16 = icmp slt i32 %15, 6
br i1 %16, label %17, label %20
17: ; preds = %12, %7
%18 = call i32 @time(i32* null)
%19 = call i32 @srand48(i32 %18)
br label %23
20: ; preds = %12
%21 = getelementptr inbounds [3 x i16], [3 x i16]* %4, i64 0, i64 0
%22 = call i32 @seed48(i16* %21)
br label %23
23: ; preds = %20, %17
%24 = load i32, i32* %3
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26, %23
store i32 1, i32* @unif_random.seeded
br label %30
30: ; preds = %29, %1
%31 = call i32 (...) @drand48()
%32 = load i32, i32* %2
%33 = mul i32 %31, %32
ret i32 %33
}

LLVM IR (-O2):
@unif_random.seeded = internal unnamed_addr global i1 false
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @unif_random(i32 %0) {
%2 = alloca [3 x i16]
%3 = load i1, i1* @unif_random.seeded
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = bitcast [3 x i16]* %2 to i8*
%8 = icmp slt i32 %6, 0
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = getelementptr inbounds [3 x i16], [3 x i16]* %2, i64 0, i64 0
%11 = call i32 @read(i32 %6, i16* %10, i32 6)
%12 = icmp slt i32 %11, 6
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @seed48(i16* %10)
br label %19
15: ; preds = %4, %9
%16 = tail call i32 @time(i32* null)
%17 = tail call i32 @srand48(i32 %16)
%18 = icmp sgt i32 %6, -1
br i1 %18, label %19, label %21
19: ; preds = %13, %15
%20 = call i32 @close(i32 %6)
br label %21
21: ; preds = %19, %15
store i1 true, i1* @unif_random.seeded
br label %22
22: ; preds = %21, %1
%23 = call i32 (...) @drand48()
%24 = mul i32 %23, %0
ret i32 %24
}