#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 */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int wait_for_read(int fd) {
  fd_set readfds;
  do {
    FD_ZERO(&readfds);
    FD_SET(fd, &readfds);
    int rv = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
    if (rv == -1) {
      perror("select failure");
      return -1;
    }
    if (FD_ISSET(fd, &readfds)) {
      return 1;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<wait_for_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11b2 <wait_for_read+0x62>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <wait_for_read+0x98>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11d4 <wait_for_read+0x84>
movl   $0x1,-0x4(%rbp)
jmp    11e8 <wait_for_read+0x98>
jmp    11d9 <wait_for_read+0x89>
mov    $0x1,%al
test   $0x1,%al
jne    115b <wait_for_read+0xb>
jmp    11e8 <wait_for_read+0x98>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    0x2e7a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    (%r14),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11b6 <wait_for_read+0x66>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1170 <wait_for_read+0x20>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ad <wait_for_read+0x5d>

LLVM IR (-O0):
@FD_SETSIZE = global i32 0
@.str = [15 x i8] c"select failure\00"
define i32 @wait_for_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
br label %6
6: ; preds = %22, %1
%7 = call i32 @FD_ZERO(i32* %4)
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %4)
%10 = load i32, i32* @FD_SETSIZE
%11 = call i32 @select(i32 %10, i32* %4, i32* null, i32* null, i32* null)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
16: ; preds = %6
%17 = load i32, i32* %3
%18 = call i64 @FD_ISSET(i32 %17, i32* %4)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 1, i32* %2
br label %23
21: ; preds = %16
br label %22
22: ; preds = %21
br i1 true, label %6, label %23
23: ; preds = %14, %20, %22
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@FD_SETSIZE = global i32 0
@.str = [15 x i8] c"select failure\00"
define i32 @wait_for_read(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %12, %1
%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* null)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %15
12: ; preds = %4
%13 = call i64 @FD_ISSET(i32 %0, i32* %2)
%14 = icmp eq i64 %13, 0
br i1 %14, label %4, label %15
15: ; preds = %12, %10
%16 = phi i32 [ -1, %10 ], [ 1, %12 ]
ret i32 %16
}