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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int rand();
int /*<<< orphan*/ srand(unsigned int);
void __random(int *buf, int len) {
  unsigned int seed, i, val, times = len / sizeof(int);
  FILE *urandom = fopen("/dev/urandom", "r");
  fread(&seed, sizeof(int), 1, urandom);
  fclose(urandom);
  srand(seed);
  for (i = 0; i < times; i++)
    buf[i] = rand();
}


x86 assembly (clang, -O0):
<__random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x1c(%rbp)
lea    0xe8f(%rip),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%edi
call   1050 <srand@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    11dd <__random+0x8d>
mov    $0x0,%al
call   1070 <rand@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b0 <__random+0x60>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__random>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
lea    0xe97(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xc(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%edi
call   1050 <srand@plt>
cmp    $0x4,%r14d
jb     11d4 <__random+0x84>
sar    $0x2,%r14d
cmp    $0x2,%r14d
cmovae %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <rand@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11c0 <__random+0x70>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define void @__random(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32* %0, i32** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = udiv i64 %11, 4
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = call i32 @fread(i32* %5, i32 4, i32 1, i32* %15)
%17 = load i32*, i32** %9
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* %5
%20 = call i32 @srand(i32 %19)
store i32 0, i32* %6
br label %21
21: ; preds = %31, %2
%22 = load i32, i32* %6
%23 = load i32, i32* %8
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %21
%26 = call i32 (...) @rand()
%27 = load i32*, i32** %3
%28 = load i32, i32* %6
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 %26, i32* %30
br label %31
31: ; preds = %25
%32 = load i32, i32* %6
%33 = add i32 %32, 1
store i32 %33, i32* %6
br label %21
34: ; preds = %21
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define void @__random(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %5)
%7 = tail call i32 @fclose(i32* %5)
%8 = load i32, i32* %3
%9 = tail call i32 @srand(i32 %8)
%10 = icmp ult i32 %1, 4
br i1 %10, label %21, label %11
11: ; preds = %2
%12 = ashr i32 %1, 2
%13 = call i32 @llvm.umax.i32(i32 %12, i32 1)
%14 = zext i32 %13 to i64
br label %15
15: ; preds = %11, %15
%16 = phi i64 [ 0, %11 ], [ %19, %15 ]
%17 = tail call i32 (...) @rand()
%18 = getelementptr inbounds i32, i32* %0, i64 %16
store i32 %17, i32* %18
%19 = add nuw nsw i64 %16, 1
%20 = icmp eq i64 %19, %14
br i1 %20, label %21, label %15
21: ; preds = %15, %2
ret void
}