#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ getpid();
scalar_t__ monotonic_us();
int open(char *, int /*<<< orphan*/);
int rand();
int /*<<< orphan*/ read(int, unsigned char *, size_t);
int /*<<< orphan*/ srand(scalar_t__);
void read_random_bytes(unsigned char *buf, size_t size) {
  int i;
  pid_t pid;
  i = open("/dev/urandom", O_RDONLY);
  if (i >= 0) {
    read(i, buf, size);
    close(i);
  }
  srand(monotonic_us());
  pid = getpid();
  while (1) {
    for (i = 0; i < size; i++)
      buf[i] ^= rand() >> 5;
    if (pid == 0)
      break;
    srand(pid);
    pid = 0;
  }
}


x86 assembly (clang, -O0):
<read_random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11ba <read_random_bytes+0x4a>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <read@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0x0,%al
call   1080 <monotonic_us@plt>
mov    %rax,%rdi
call   1060 <srand@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1214 <read_random_bytes+0xa4>
mov    $0x0,%al
call   1090 <rand@plt>
mov    %eax,%esi
sar    $0x5,%esi
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
xor    %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11db <read_random_bytes+0x6b>
cmpq   $0x0,-0x20(%rbp)
jne    1224 <read_random_bytes+0xb4>
jmp    123a <read_random_bytes+0xca>
mov    -0x20(%rbp),%rdi
call   1060 <srand@plt>
movq   $0x0,-0x20(%rbp)
jmp    11d4 <read_random_bytes+0x64>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_random_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ac <read_random_bytes+0x3c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1080 <monotonic_us@plt>
mov    %rax,%rdi
call   1060 <srand@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r15
test   %rbx,%rbx
je     11f7 <read_random_bytes+0x87>
nopw   0x0(%rax,%rax,1)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <rand@plt>
shr    $0x5,%eax
xor    %al,(%r14,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <read_random_bytes+0x70>
test   %r15,%r15
je     120e <read_random_bytes+0x9e>
mov    %r15,%rdi
call   1060 <srand@plt>
xor    %r15d,%r15d
test   %rbx,%rbx
jne    11d0 <read_random_bytes+0x60>
jmp    11f7 <read_random_bytes+0x87>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @read_random_bytes(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = call i32 @read(i32 %12, i8* %13, i64 %14)
%16 = load i32, i32* %5
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %11, %2
%19 = call i64 (...) @monotonic_us()
%20 = call i32 @srand(i64 %19)
%21 = call i64 (...) @getpid()
store i64 %21, i64* %6
br label %22
22: ; preds = %18, %46
store i32 0, i32* %5
br label %23
23: ; preds = %39, %22
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = load i64, i64* %4
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %23
%29 = call i32 (...) @rand()
%30 = ashr i32 %29, 5
%31 = load i8*, i8** %3
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = xor i32 %36, %30
%38 = trunc i32 %37 to i8
store i8 %38, i8* %34
br label %39
39: ; preds = %28
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %23
42: ; preds = %23
%43 = load i64, i64* %6
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
br label %49
46: ; preds = %42
%47 = load i64, i64* %6
%48 = call i32 @srand(i64 %47)
store i64 0, i64* %6
br label %22
49: ; preds = %45
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define void @read_random_bytes(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%8 = tail call i32 @close(i32 %4)
br label %9
9: ; preds = %6, %2
%10 = tail call i64 (...) @monotonic_us()
%11 = tail call i32 @srand(i64 %10)
%12 = tail call i64 (...) @getpid()
%13 = icmp eq i64 %1, 0
br label %14
14: ; preds = %28, %9
%15 = phi i64 [ %12, %9 ], [ 0, %28 ]
br i1 %13, label %26, label %16
16: ; preds = %14, %16
%17 = phi i64 [ %24, %16 ], [ 0, %14 ]
%18 = tail call i32 (...) @rand()
%19 = lshr i32 %18, 5
%20 = getelementptr inbounds i8, i8* %0, i64 %17
%21 = load i8, i8* %20
%22 = trunc i32 %19 to i8
%23 = xor i8 %21, %22
store i8 %23, i8* %20
%24 = add nuw i64 %17, 1
%25 = icmp eq i64 %24, %1
br i1 %25, label %26, label %16
26: ; preds = %16, %14
%27 = icmp eq i64 %15, 0
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = tail call i32 @srand(i64 %15)
br label %14
30: ; preds = %26
ret void
}