#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 */
scalar_t__ EIO;
scalar_t__ ENOSYS;
int /*<<< orphan*/ GRND_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int getrandom(void *, size_t, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
int getentropy(void *buf, size_t buflen) {
  int r;
  if (buflen > 256) {
    errno = EIO;
    return -1;
  }
  r = getrandom(buf, buflen, GRND_NONBLOCK);
  if (r == -1 && errno == ENOSYS) {
    int fd = open("/dev/urandom", O_RDONLY);
    if (fd == -1)
      return -1;
    r = read(fd, buf, buflen);
    close(fd);
  }
  if (r < (int)buflen)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<getentropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x100,-0x18(%rbp)
jbe    117e <getentropy+0x3e>
mov    0x2e63(%rip),%rax
mov    (%rax),%rcx
mov    0x2e51(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <getentropy+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1060 <getrandom@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1204 <getentropy+0xc4>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <getentropy+0xc4>
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11e9 <getentropy+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <getentropy+0xe6>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jge    121f <getentropy+0xdf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <getentropy+0xe6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getentropy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x101,%rsi
jb     116b <getentropy+0x2b>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    11de <getentropy+0x9e>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
call   1060 <getrandom@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11d5 <getentropy+0x95>
mov    0x2e33(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    11d5 <getentropy+0x95>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11e9 <getentropy+0xa9>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
call   1030 <close@plt>
xor    %eax,%eax
cmp    %ebx,%ebp
setl   %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    11de <getentropy+0x9e>

LLVM IR (-O0):
@EIO = global i64 0
@errno = global i64 0
@GRND_NONBLOCK = global i32 0
@ENOSYS = global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @getentropy(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp ugt i64 %8, 256
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i64, i64* @EIO
store i64 %11, i64* @errno
store i32 -1, i32* %3
br label %43
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = load i32, i32* @GRND_NONBLOCK
%16 = call i32 @getrandom(i8* %13, i64 %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %36
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOSYS
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -1, i32* %3
br label %43
29: ; preds = %23
%30 = load i32, i32* %7
%31 = load i8*, i8** %4
%32 = load i64, i64* %5
%33 = call i32 @read(i32 %30, i8* %31, i64 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %29, %19, %12
%37 = load i32, i32* %6
%38 = load i64, i64* %5
%39 = trunc i64 %38 to i32
%40 = icmp slt i32 %37, %39
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 -1, i32* %3
br label %43
42: ; preds = %36
store i32 0, i32* %3
br label %43
43: ; preds = %42, %41, %28, %10
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@EIO = global i64 0
@errno = global i64 0
@GRND_NONBLOCK = global i32 0
@ENOSYS = global i64 0
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @getentropy(i8* %0, i64 %1) {
%3 = icmp ugt i64 %1, 256
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i64, i64* @EIO
store i64 %5, i64* @errno
br label %26
6: ; preds = %2
%7 = load i32, i32* @GRND_NONBLOCK
%8 = tail call i32 @getrandom(i8* %0, i64 %1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %21
10: ; preds = %6
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOSYS
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %26, label %18
18: ; preds = %14
%19 = tail call i32 @read(i32 %16, i8* %0, i64 %1)
%20 = tail call i32 @close(i32 %16)
br label %21
21: ; preds = %18, %10, %6
%22 = phi i32 [ %19, %18 ], [ -1, %10 ], [ %8, %6 ]
%23 = trunc i64 %1 to i32
%24 = icmp slt i32 %22, %23
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %14, %21, %4
%27 = phi i32 [ -1, %4 ], [ %25, %21 ], [ -1, %14 ]
ret i32 %27
}