#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 int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;
typedef scalar_t__ read;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *);
int randopen(char *);
int getentropy(void *buf, size_t n) {
  static int fd = -1;
  uint8_t *b = (uint8_t *)buf;
  if (fd < 0)
    fd = randopen("/dev/urandom");
  while (n > 0) {
    ssize_t m = (read)(fd, b, n);
    if (m < 0) {
      if (errno == EINTR)
        continue;
      error(1, errno, "Fatal read error while reading rand dev");
    }
    b += m;
    n -= m;
  }
  return 0;
}


x86 assembly (clang, -O0):
<getentropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,0x2ef1(%rip)
jge    1157 <getentropy+0x37>
lea    0xeb4(%rip),%rdi
call   1040 <randopen@plt>
mov    %eax,0x2ed9(%rip)
jmp    115c <getentropy+0x3c>
cmpq   $0x0,-0x10(%rbp)
jbe    11d8 <getentropy+0xb8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11b4 <getentropy+0x94>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <getentropy+0x79>
jmp    115c <getentropy+0x3c>
mov    0x2e40(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xe5e(%rip),%rdx
call   1030 <error@plt>
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    115c <getentropy+0x3c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getentropy>:
push   %rbx
mov    %rsi,%rbx
cmpl   $0x0,0x2f05(%rip)
js     1134 <getentropy+0x14>
test   %rbx,%rbx
jne    114b <getentropy+0x2b>
jmp    117b <getentropy+0x5b>
lea    0xec5(%rip),%rdi
call   1040 <randopen@plt>
mov    %eax,0x2eea(%rip)
test   %rbx,%rbx
je     117b <getentropy+0x5b>
mov    0x2e8e(%rip),%rax
mov    (%rax),%rsi
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
nop
test   %rbx,%rbx
jns    117b <getentropy+0x5b>
cmp    %rax,%rsi
je     1160 <getentropy+0x40>
lea    0xe9c(%rip),%rdx
mov    $0x1,%edi
call   1030 <error@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@getentropy.fd = internal global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [40 x i8] c"Fatal read error while reading rand dev\00"
define i32 @getentropy(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 i8*, i8** %3
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %5
%9 = load i32, i32* @getentropy.fd
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @randopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* @getentropy.fd
br label %13
13: ; preds = %11, %2
br label %14
14: ; preds = %31, %27, %13
%15 = load i64, i64* %4
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load i32, i32* @getentropy.fd
%19 = load i32*, i32** %5
%20 = load i64, i64* %4
store i64 %20, i64* %6
%21 = load i64, i64* %6
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %14
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = call i32 @error(i32 1, i64 %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %28, %17
%32 = load i64, i64* %6
%33 = load i32*, i32** %5
%34 = getelementptr inbounds i32, i32* %33, i64 %32
store i32* %34, i32** %5
%35 = load i64, i64* %6
%36 = load i64, i64* %4
%37 = sub i64 %36, %35
store i64 %37, i64* %4
br label %14
38: ; preds = %14
ret i32 0
}

LLVM IR (-O2):
@getentropy.fd = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [40 x i8] c"Fatal read error while reading rand dev\00"
define i32 @getentropy(i8* nocapture readnone %0, i64 %1) {
%3 = load i32, i32* @getentropy.fd
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @randopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %6, i32* @getentropy.fd
br label %7
7: ; preds = %5, %2
%8 = icmp eq i64 %1, 0
br i1 %8, label %18, label %9
9: ; preds = %7
%10 = icmp slt i64 %1, 0
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br label %14
14: ; preds = %15, %9
br i1 %10, label %15, label %18
15: ; preds = %14
br i1 %13, label %14, label %16
16: ; preds = %15
%17 = tail call i32 @error(i32 1, i64 %11, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %14, %16, %7
ret i32 0
}