#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int ioctl(long, int /*<<< orphan*/, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int _CharReady(long port) {
  int n, res;
  res = ioctl(port, FIONREAD, &n);
  if (res < 0) {
    fprintf(stderr, "I/O error: %s\n", strerror(errno));
    exit(1);
  }
  return n;
}


x86 assembly (clang, -O0):
<_CharReady>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a5 <_CharReady+0x65>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_CharReady>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
js     1166 <_CharReady+0x26>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e6b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"I/O error: %s\0A\00"
@errno = global i32 0
define i32 @_CharReady(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = load i32, i32* @FIONREAD
%7 = call i32 @ioctl(i64 %5, i32 %6, i32* %3)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32, i32* %3
ret i32 %17
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"I/O error: %s\0A\00"
@errno = global i32 0
define i32 @_CharReady(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @FIONREAD
%5 = call i32 @ioctl(i64 %0, i32 %4, i32* %2)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %13
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 ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %2
ret i32 %14
}