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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int read(int /*<<< orphan*/, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/);
main() {
  char buffer[30];
  int ret, nread;
  fd_set inputs;
  FD_ZERO(&inputs);
  FD_SET(0, &inputs);
  ret = select(FD_SETSIZE, &inputs, (fd_set *)0, (fd_set *)0, 0);
  printf("ret:%d\n", ret);
  ioctl(0, FIONREAD, nread);
  ret = read(0, buffer, nread);
  printf("ret:%d\n", ret);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x2c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
xor    %edi,%edi
call   1040 <ioctl@plt>
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
xor    %edi,%edi
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
lea    0xe65(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <ioctl@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
call   1050 <read@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@FD_SETSIZE = global i32 0
@.str = [8 x i8] c"ret:%d\0A\00"
@FIONREAD = global i32 0
define i32 @main() {
%1 = alloca [30 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @FD_ZERO(i32* %4)
%6 = call i32 @FD_SET(i32 0, i32* %4)
%7 = load i32, i32* @FD_SETSIZE
%8 = call i32 @select(i32 %7, i32* %4, i32* null, i32* null, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @FIONREAD
%12 = load i32, i32* %3
%13 = call i32 @ioctl(i32 0, i32 %11, i32 %12)
%14 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @read(i32 0, i8* %14, i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %17)
ret i32 0
}

LLVM IR (-O2):
@FD_SETSIZE = global i32 0
@.str = [8 x i8] c"ret:%d\0A\00"
@FIONREAD = global i32 0
define i32 @main() {
%1 = alloca [30 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = call i32 @FD_ZERO(i32* %2)
%6 = call i32 @FD_SET(i32 0, i32* %2)
%7 = load i32, i32* @FD_SETSIZE
%8 = call i32 @select(i32 %7, i32* %2, i32* null, i32* null, i32 0)
%9 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @FIONREAD
%11 = call i32 @ioctl(i32 0, i32 %10, i32 undef)
%12 = call i32 @read(i32 0, i8* %3, i32 undef)
%13 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12)
ret i32 0
}