#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 errno;
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
int dht_random_bytes(void *buf, size_t size) {
  int fd, rc, save;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    return -1;
  rc = read(fd, buf, size);
  save = errno;
  close(fd);
  errno = save;
  return rc;
}


x86 assembly (clang, -O0):
<dht_random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    116e <dht_random_bytes+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <dht_random_bytes+0x77>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e39(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dht_random_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1181 <dht_random_bytes+0x51>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %eax,%r15d
mov    0x2e69(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,(%r14)
mov    %r15d,%eax
jmp    1186 <dht_random_bytes+0x56>
mov    $0xffffffff,%eax
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
@errno = global i32 0
define i32 @dht_random_bytes(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %24
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %7
%19 = load i32, i32* @errno
store i32 %19, i32* %8
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %8
store i32 %22, i32* @errno
%23 = load i32, i32* %7
store i32 %23, i32* %3
br label %24
24: ; preds = %14, %13
%25 = load i32, i32* %3
ret i32 %25
}

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