#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__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ _pfatal(int /*<<< orphan*/ *, int /*<<< orphan*/, char *);
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, size_t *);
size_t fd_ravail(int fd) {
  size_t avail;
  int ret;
  avail = 0;
  do {
    ret = ioctl(fd, FIONREAD, &avail);
  } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  if (ret < 0)
    _pfatal(NULL, 0, "fd_ravailable");
  ;
  return avail;
}


x86 assembly (clang, -O0):
<fd_ravail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jne    1199 <fd_ravail+0x79>
mov    0x2e77(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e63(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x16(%rbp)
je     1193 <fd_ravail+0x73>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x16(%rbp)
mov    -0x16(%rbp),%al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1133 <fd_ravail+0x13>
cmpl   $0x0,-0x14(%rbp)
jge    11c0 <fd_ravail+0xa0>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe45(%rip),%rdx
call   1040 <_pfatal@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_ravail>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movq   $0x0,(%rsp)
mov    0x2eac(%rip),%r15
mov    %rsp,%r14
mov    0x2e92(%rip),%r12
mov    0x2e83(%rip),%r13
mov    0x2e6c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1184 <fd_ravail+0x64>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1160 <fd_ravail+0x40>
cmp    0x0(%rbp),%rax
je     1160 <fd_ravail+0x40>
jmp    1188 <fd_ravail+0x68>
test   %eax,%eax
jns    1198 <fd_ravail+0x78>
lea    0xe71(%rip),%rdx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <_pfatal@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"fd_ravailable\00"
define i64 @fd_ravail(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
store i64 0, i64* %3
br label %5
5: ; preds = %22, %1
%6 = load i32, i32* %2
%7 = load i32, i32* @FIONREAD
%8 = call i32 @ioctl(i32 %6, i32 %7, i64* %3)
store i32 %8, i32* %4
br label %9
9: ; preds = %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ true, %12 ], [ %19, %16 ]
br label %22
22: ; preds = %20, %9
%23 = phi i1 [ false, %9 ], [ %21, %20 ]
br i1 %23, label %5, label %24
24: ; preds = %22
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @_pfatal(i32* null, i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = load i64, i64* %3
ret i64 %30
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [14 x i8] c"fd_ravailable\00"
define i64 @fd_ravail(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
br label %4
4: ; preds = %8, %1
%5 = load i32, i32* @FIONREAD
%6 = call i32 @ioctl(i32 %0, i32 %5, i64* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %4, label %17
15: ; preds = %4
%16 = icmp slt i32 %6, 0
br i1 %16, label %17, label %19
17: ; preds = %8, %15
%18 = call i32 @_pfatal(i32* null, i32 0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = load i64, i64* %2
ret i64 %20
}