#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 */
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t bytes_available(int fd) {
  int avail;
  if (ioctl(fd, FIONREAD, &avail)) {
    fprintf(stderr,
            "remotethread"
            " WARNING: "
            "ioctl() failed (%s)\n",
            strerror(errno));
    return 0;
  }
  return avail;
}


x86 assembly (clang, -O0):
<bytes_available>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1193 <bytes_available+0x63>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe7f(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    119b <bytes_available+0x6b>
movslq -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bytes_available>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     1179 <bytes_available+0x49>
mov    0x2e84(%rip),%rax
mov    (%rax),%ebx
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe95(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
jmp    117e <bytes_available+0x4e>
movslq 0xc(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"remotethread WARNING: ioctl() failed (%s)\0A\00"
@errno = global i32 0
define i64 @bytes_available(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @FIONREAD
%7 = call i64 @ioctl(i32 %5, i32 %6, i32* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %12)
store i64 0, i64* %2
br label %17
14: ; preds = %1
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
store i64 %16, i64* %2
br label %17
17: ; preds = %14, %9
%18 = load i64, i64* %2
ret i64 %18
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [43 x i8] c"remotethread WARNING: ioctl() failed (%s)\0A\00"
@errno = global i32 0
define i64 @bytes_available(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @FIONREAD
%5 = call i64 @ioctl(i32 %0, i32 %4, i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %15
12: ; preds = %1
%13 = load i32, i32* %2
%14 = sext i32 %13 to i64
br label %15
15: ; preds = %12, %7
%16 = phi i64 [ 0, %7 ], [ %14, %12 ]
ret i64 %16
}