#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__ EINTR;
scalar_t__ EIO;
int PTHREAD_CANCEL_DISABLE;
scalar_t__ errno;
int getrandom(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_setcancelstate(int, int *);
int getentropy(void *buffer, size_t len) {
  int cs, ret;
  char *pos = buffer;
  if (len > 256) {
    errno = EIO;
    return -1;
  }
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
  while (len) {
    ret = getrandom(pos, len, 0);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      else
        break;
    }
    pos += ret;
    len -= ret;
    ret = 0;
  }
  pthread_setcancelstate(cs, 0);
  return ret;
}


x86 assembly (clang, -O0):
<getentropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x100,-0x18(%rbp)
jbe    1166 <getentropy+0x46>
mov    0x2e93(%rip),%rax
mov    (%rax),%rcx
mov    0x2e81(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <getentropy+0xe1>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <pthread_setcancelstate@plt>
cmpq   $0x0,-0x18(%rbp)
je     11ef <getentropy+0xcf>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <getrandom@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c3 <getentropy+0xa3>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <getentropy+0x9e>
jmp    1178 <getentropy+0x58>
jmp    11ef <getentropy+0xcf>
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
jmp    1178 <getentropy+0x58>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <pthread_setcancelstate@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getentropy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x101,%rsi
jb     1150 <getentropy+0x30>
mov    0x2ea4(%rip),%rax
mov    (%rax),%rax
mov    0x2e92(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%ebp
jmp    11b5 <getentropy+0x95>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
call   1030 <pthread_setcancelstate@plt>
test   %rbx,%rbx
je     11a8 <getentropy+0x88>
mov    0x2e63(%rip),%r15
mov    0x2e54(%rip),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <getrandom@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119e <getentropy+0x7e>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1180 <getentropy+0x60>
jmp    11aa <getentropy+0x8a>
mov    %ebp,%eax
add    %rax,%r14
sub    %rax,%rbx
jne    1180 <getentropy+0x60>
xor    %ebp,%ebp
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1030 <pthread_setcancelstate@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EIO = global i64 0
@errno = global i64 0
@PTHREAD_CANCEL_DISABLE = global i32 0
@EINTR = global i64 0
define i32 @getentropy(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
store i8* %9, i8** %8
%10 = load i64, i64* %5
%11 = icmp ugt i64 %10, 256
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i64, i64* @EIO
store i64 %13, i64* @errno
store i32 -1, i32* %3
br label %45
14: ; preds = %2
%15 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%16 = call i32 @pthread_setcancelstate(i32 %15, i32* %6)
br label %17
17: ; preds = %32, %30, %14
%18 = load i64, i64* %5
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %41
20: ; preds = %17
%21 = load i8*, i8** %8
%22 = load i64, i64* %5
%23 = call i32 @getrandom(i8* %21, i64 %22, i32 0)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %17
31: ; preds = %26
br label %41
32: ; preds = %20
%33 = load i32, i32* %7
%34 = load i8*, i8** %8
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %8
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = load i64, i64* %5
%40 = sub i64 %39, %38
store i64 %40, i64* %5
store i32 0, i32* %7
br label %17
41: ; preds = %31, %17
%42 = load i32, i32* %6
%43 = call i32 @pthread_setcancelstate(i32 %42, i32* null)
%44 = load i32, i32* %7
store i32 %44, i32* %3
br label %45
45: ; preds = %41, %12
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@EIO = global i64 0
@errno = global i64 0
@PTHREAD_CANCEL_DISABLE = global i32 0
@EINTR = global i64 0
define i32 @getentropy(i8* %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp ugt i64 %1, 256
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i64, i64* @EIO
store i64 %7, i64* @errno
br label %31
8: ; preds = %2
%9 = load i32, i32* @PTHREAD_CANCEL_DISABLE
%10 = call i32 @pthread_setcancelstate(i32 %9, i32* %3)
%11 = icmp eq i64 %1, 0
br i1 %11, label %27, label %12
12: ; preds = %8, %22
%13 = phi i8* [ %24, %22 ], [ %0, %8 ]
%14 = phi i64 [ %25, %22 ], [ %1, %8 ]
br label %15
15: ; preds = %12, %18
%16 = call i32 @getrandom(i8* %13, i64 %14, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %15, label %27
22: ; preds = %15
%23 = zext i32 %16 to i64
%24 = getelementptr inbounds i8, i8* %13, i64 %23
%25 = sub i64 %14, %23
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %12
27: ; preds = %22, %18, %8
%28 = phi i32 [ 0, %8 ], [ %16, %18 ], [ 0, %22 ]
%29 = load i32, i32* %3
%30 = call i32 @pthread_setcancelstate(i32 %29, i32* null)
br label %31
31: ; preds = %27, %6
%32 = phi i32 [ -1, %6 ], [ %28, %27 ]
ret i32 %32
}