#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 scalar_t__ ssize_t;

/* Variables and functions */
int ENOENT;
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, size_t);
int sg_rand_getentropy(unsigned *array, unsigned count) {
  unsigned char *p = (unsigned char *)array;
  size_t rem = count * sizeof(unsigned);
  int fdes, e;
  ssize_t amt;
  fdes = open("/dev/urandom", O_RDONLY, O_CLOEXEC);
  if (fdes < 0) {
    e = errno;
    if (e != ENOENT)
      return -1;
    fdes = open("/dev/random", O_RDONLY, O_CLOEXEC);
    if (fdes < 0)
      return -1;
  }
  while (rem) {
    amt = read(fdes, p, rem);
    if (amt > 0) {
      rem -= amt;
      p += amt;
    } else if (amt == 0) {
      break;
    } else if (amt < 0) {
      break;
    }
  }
  close(fdes);
  return rem == 0 ? 0 : -1;
}


x86 assembly (clang, -O0):
<sg_rand_getentropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
lea    0xe95(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11e3 <sg_rand_getentropy+0xb3>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%eax
je     11a7 <sg_rand_getentropy+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <sg_rand_getentropy+0x14e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11de <sg_rand_getentropy+0xae>
movl   $0xffffffff,-0x4(%rbp)
jmp    127e <sg_rand_getentropy+0x14e>
jmp    11e3 <sg_rand_getentropy+0xb3>
jmp    11e8 <sg_rand_getentropy+0xb8>
cmpq   $0x0,-0x28(%rbp)
je     1261 <sg_rand_getentropy+0x131>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jle    1232 <sg_rand_getentropy+0x102>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    125c <sg_rand_getentropy+0x12c>
cmpq   $0x0,-0x38(%rbp)
jne    1242 <sg_rand_getentropy+0x112>
jmp    1261 <sg_rand_getentropy+0x131>
cmpq   $0x0,-0x38(%rbp)
jge    1252 <sg_rand_getentropy+0x122>
jmp    1261 <sg_rand_getentropy+0x131>
jmp    1257 <sg_rand_getentropy+0x127>
jmp    125c <sg_rand_getentropy+0x12c>
jmp    11e8 <sg_rand_getentropy+0xb8>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sg_rand_getentropy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2ea0(%rip),%r12
mov    (%r12),%esi
mov    0x2e6d(%rip),%r13
mov    0x0(%r13),%edx
lea    0xea2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11ac <sg_rand_getentropy+0x7c>
mov    %r14d,%ebp
shl    $0x2,%rbp
test   %r14d,%r14d
je     119d <sg_rand_getentropy+0x6d>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
test   %rax,%rax
jle    119d <sg_rand_getentropy+0x6d>
add    %rax,%rbx
sub    %rax,%rbp
jne    1180 <sg_rand_getentropy+0x50>
xor    %ebp,%ebp
mov    %r15d,%edi
call   1030 <close@plt>
neg    %rbp
sbb    %ebp,%ebp
jmp    11e0 <sg_rand_getentropy+0xb0>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%eax
jne    11e0 <sg_rand_getentropy+0xb0>
mov    (%r12),%esi
mov    0x0(%r13),%edx
lea    0xe39(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    116a <sg_rand_getentropy+0x3a>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define i32 @sg_rand_getentropy(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i32* %0, i32** %4
store i32 %1, i32* %5
%11 = load i32*, i32** %4
%12 = bitcast i32* %11 to i8*
store i8* %12, i8** %6
%13 = load i32, i32* %5
%14 = zext i32 %13 to i64
%15 = mul i64 %14, 4
store i64 %15, i64* %7
%16 = load i32, i32* @O_RDONLY
%17 = load i32, i32* @O_CLOEXEC
%18 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %2
%22 = load i32, i32* @errno
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32, i32* @ENOENT
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -1, i32* %3
br label %71
27: ; preds = %21
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_CLOEXEC
%30 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 -1, i32* %3
br label %71
34: ; preds = %27
br label %35
35: ; preds = %34, %2
br label %36
36: ; preds = %63, %35
%37 = load i64, i64* %7
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %64
39: ; preds = %36
%40 = load i32, i32* %8
%41 = load i8*, i8** %6
%42 = load i64, i64* %7
%43 = call i64 @read(i32 %40, i8* %41, i64 %42)
store i64 %43, i64* %10
%44 = load i64, i64* %10
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %39
%47 = load i64, i64* %10
%48 = load i64, i64* %7
%49 = sub i64 %48, %47
store i64 %49, i64* %7
%50 = load i64, i64* %10
%51 = load i8*, i8** %6
%52 = getelementptr inbounds i8, i8* %51, i64 %50
store i8* %52, i8** %6
br label %63
53: ; preds = %39
%54 = load i64, i64* %10
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %64
57: ; preds = %53
%58 = load i64, i64* %10
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
br label %64
61: ; preds = %57
br label %62
62: ; preds = %61
br label %63
63: ; preds = %62, %46
br label %36
64: ; preds = %60, %56, %36
%65 = load i32, i32* %8
%66 = call i32 @close(i32 %65)
%67 = load i64, i64* %7
%68 = icmp eq i64 %67, 0
%69 = zext i1 %68 to i64
%70 = select i1 %68, i32 0, i32 -1
store i32 %70, i32* %3
br label %71
71: ; preds = %64, %33, %26
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define i32 @sg_rand_getentropy(i32* nocapture %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
%4 = zext i32 %1 to i64
%5 = shl nuw nsw i64 %4, 2
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_CLOEXEC
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = load i32, i32* @ENOENT
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_CLOEXEC
%17 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %14, %2
%20 = phi i32 [ %17, %14 ], [ %8, %2 ]
%21 = icmp eq i32 %1, 0
br i1 %21, label %31, label %22
22: ; preds = %19, %27
%23 = phi i8* [ %29, %27 ], [ %3, %19 ]
%24 = phi i64 [ %28, %27 ], [ %5, %19 ]
%25 = tail call i64 @read(i32 %20, i8* %23, i64 %24)
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = sub i64 %24, %25
%29 = getelementptr inbounds i8, i8* %23, i64 %25
%30 = icmp eq i64 %28, 0
br i1 %30, label %31, label %22
31: ; preds = %27, %22, %19
%32 = phi i64 [ %5, %19 ], [ %24, %22 ], [ 0, %27 ]
%33 = tail call i32 @close(i32 %20)
%34 = icmp ne i64 %32, 0
%35 = sext i1 %34 to i32
br label %36
36: ; preds = %14, %10, %31
%37 = phi i32 [ %35, %31 ], [ -1, %10 ], [ -1, %14 ]
ret i32 %37
}