#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*/);
size_t read(int, void *, size_t);
int get_random_bytes(void *buf, size_t size) {
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    goto err;
  if (read(fd, buf, size) != size)
    goto err2;
  close(fd);
  return 0;
err2:
  close(fd);
err:
  return -1;
}


x86 assembly (clang, -O0):
<get_random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1167 <get_random_bytes+0x37>
jmp    11a2 <get_random_bytes+0x72>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
cmp    -0x18(%rbp),%rax
je     1186 <get_random_bytes+0x56>
jmp    119a <get_random_bytes+0x6a>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a9 <get_random_bytes+0x79>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random_bytes>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1176 <get_random_bytes+0x46>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
setne  %bpl
mov    %ebx,%edi
call   1030 <close@plt>
neg    %ebp
jmp    117b <get_random_bytes+0x4b>
mov    $0xffffffff,%ebp
mov    %ebp,%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 @get_random_bytes(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%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* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
br label %26
12: ; preds = %2
%13 = load i32, i32* %6
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = call i64 @read(i32 %13, i8* %14, i64 %15)
%17 = load i64, i64* %5
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %12
br label %23
20: ; preds = %12
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 0, i32* %3
br label %27
23: ; preds = %19
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23, %11
store i32 -1, i32* %3
br label %27
27: ; preds = %26, %20
%28 = load i32, i32* %3
ret i32 %28
}

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