#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, void *, unsigned int);
int randomize(void *ptr, unsigned size) {
  int fd, ret;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    return -1;
  ret = read(fd, ptr, size);
  close(fd);
  if (ret != size) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<randomize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    116d <randomize+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <randomize+0x76>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     119f <randomize+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <randomize+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randomize>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1176 <randomize+0x46>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    %ebx,%r14d
setne  %al
neg    %eax
jmp    117b <randomize+0x4b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @randomize(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%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* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %25
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i32 @read(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %13
store i32 -1, i32* %3
br label %25
24: ; preds = %13
store i32 0, i32* %3
br label %25
25: ; preds = %24, %23, %12
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @randomize(i8* nocapture %0, i32 %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 slt i32 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %4, i8* %0, i32 %1)
%8 = tail call i32 @close(i32 %4)
%9 = icmp ne i32 %7, %1
%10 = sext i1 %9 to i32
br label %11
11: ; preds = %6, %2
%12 = phi i32 [ -1, %2 ], [ %10, %6 ]
ret i32 %12
}