#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ dolog(int, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int strerror(int /*<<< orphan*/);
int read_entropy_avail(int fd) {
  ssize_t data = 0;
  char buf[5];
  int entropy = 0;
  data = read(fd, buf, sizeof(buf));
  lseek(fd, 0, SEEK_SET);
  if (0 > data) {
    dolog(1, "Error reading data from entropy_avail: %s", strerror(errno));
    return 0;
  }
  if (0 == data) {
    dolog(1, "Could not read data from entropy_avail");
    return 0;
  }
  entropy = atoi(buf);
  if (0 > entropy || 4096 < entropy) {
    dolog(1, "Entropy read from entropy_avail (%d) is outsize of range",
          entropy);
    return 0;
  }
  return entropy;
}


x86 assembly (clang, -O0):
<read_entropy_avail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x5,%edx
call   1050 <read@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jle    11cd <read_entropy_avail+0x7d>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1030 <dolog@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <read_entropy_avail+0xf5>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f8 <read_entropy_avail+0xa8>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1030 <dolog@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <read_entropy_avail+0xf5>
lea    -0x15(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jg     121d <read_entropy_avail+0xcd>
mov    $0x1000,%eax
cmp    -0x1c(%rbp),%eax
jge    123f <read_entropy_avail+0xef>
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1030 <dolog@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <read_entropy_avail+0xf5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_entropy_avail>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xb(%rsp),%rsi
mov    $0x5,%edx
call   1050 <read@plt>
mov    %rax,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rbx,%rbx
js     11b7 <read_entropy_avail+0x67>
je     11dc <read_entropy_avail+0x8c>
lea    0xb(%rsp),%rdi
call   1060 <atoi@plt>
mov    %eax,%r14d
cmp    $0x1001,%eax
jb     11f2 <read_entropy_avail+0xa2>
lea    0xeae(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <dolog@plt>
xor    %r14d,%r14d
jmp    11f2 <read_entropy_avail+0xa2>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe34(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <dolog@plt>
jmp    11f2 <read_entropy_avail+0xa2>
lea    0xe47(%rip),%rsi
xor    %r14d,%r14d
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <dolog@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error reading data from entropy_avail: %s\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"Could not read data from entropy_avail\00"
@.str.2 = [57 x i8] c"Entropy read from entropy_avail (%d) is outsize of range\00"
define i32 @read_entropy_avail(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca [5 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i64 0, i64* %4
store i32 0, i32* %6
%7 = load i32, i32* %3
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%9 = call i64 @read(i32 %7, i8* %8, i32 5)
store i64 %9, i64* %4
%10 = load i32, i32* %3
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @lseek(i32 %10, i32 0, i32 %11)
%13 = load i64, i64* %4
%14 = icmp sgt i64 0, %13
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 0, i32* %2
br label %37
19: ; preds = %1
%20 = load i64, i64* %4
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %37
24: ; preds = %19
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp sgt i32 0, %27
br i1 %28, label %32, label %29
29: ; preds = %24
%30 = load i32, i32* %6
%31 = icmp slt i32 4096, %30
br i1 %31, label %32, label %35
32: ; preds = %29, %24
%33 = load i32, i32* %6
%34 = call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 0, i32* %2
br label %37
35: ; preds = %29
%36 = load i32, i32* %6
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %32, %22, %15
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [42 x i8] c"Error reading data from entropy_avail: %s\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"Could not read data from entropy_avail\00"
@.str.2 = [57 x i8] c"Entropy read from entropy_avail (%d) is outsize of range\00"
define i32 @read_entropy_avail(i32 %0) {
%2 = alloca [5 x i8]
%3 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 5)
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @lseek(i32 %0, i32 0, i32 %5)
%7 = icmp slt i64 %4, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %21
12: ; preds = %1
%13 = icmp eq i64 %4, 0
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %21
16: ; preds = %12
%17 = call i32 @atoi(i8* %3)
%18 = icmp ugt i32 %17, 4096
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i32, i8*, ...) @dolog(i32 1, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %21
21: ; preds = %16, %19, %14, %8
%22 = phi i32 [ 0, %8 ], [ 0, %14 ], [ 0, %19 ], [ %17, %16 ]
ret i32 %22
}