#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 */
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
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*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pipe(int *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fdn, fdz, pfd[2], rv;
  fd_set readset;
  struct timeval timeout;
  int err = 0;
  fdz = open("/dev/zero", O_RDONLY);
  pipe(pfd);
  fdn = pfd[0];
  FD_ZERO(&readset);
  FD_SET(fdn, &readset);
  timeout.tv_sec = 0;
  timeout.tv_usec = 100000;
  rv = select(FD_SETSIZE, &readset, NULL, NULL, &timeout);
  if (rv != 0) {
    fprintf(stderr, "select with timeout failed (rv = %d, errno = %s)\n", rv,
            strerror(errno));
    err++;
  }
  FD_ZERO(&readset);
  FD_SET(fdn, &readset);
  FD_SET(fdz, &readset);
  rv = select(FD_SETSIZE, &readset, NULL, NULL, &timeout);
  if (rv != 1 || !FD_ISSET(fdz, &readset) || FD_ISSET(fdn, &readset)) {
    fprintf(stderr, "select with /dev/zero failed (rv = %d, errno = %s)\n", rv,
            strerror(errno));
    err++;
  }
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <pipe@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <FD_SET@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x186a0,-0x38(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1250 <main+0xd0>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%edx
mov    %rax,%rcx
lea    0xdc8(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
lea    -0x28(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    12c7 <main+0x147>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12c7 <main+0x147>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1305 <main+0x185>
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%edx
mov    %rax,%rcx
lea    0xd45(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r14d
lea    0x20(%rsp),%rdi
call   1040 <pipe@plt>
mov    0x20(%rsp),%r15d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
movl   $0x186a0,0x10(%rsp)
mov    0x2deb(%rip),%r13
mov    0x0(%r13),%edi
xor    %r12d,%r12d
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
je     1233 <main+0xb3>
mov    %eax,%ebp
mov    0x2dce(%rip),%rax
mov    (%rax),%ebx
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%r12d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    0x0(%r13),%edi
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    1296 <main+0x116>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1296 <main+0x116>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12c4 <main+0x144>
mov    0x2d3b(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd88(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1050 <fprintf@plt>
add    $0x1,%r12d
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@FD_SETSIZE = global i32 0
@stderr = global i32 0
@.str.1 = [50 x i8] c"select with timeout failed (rv = %d, errno = %s)\0A\00"
@errno = global i32 0
@.str.2 = [52 x i8] c"select with /dev/zero failed (rv = %d, errno = %s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %12
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %7
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%18 = load i32, i32* %17
store i32 %18, i32* %6
%19 = call i32 @FD_ZERO(i32* %10)
%20 = load i32, i32* %6
%21 = call i32 @FD_SET(i32 %20, i32* %10)
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 100000, i32* %23
%24 = load i32, i32* @FD_SETSIZE
%25 = call i32 @select(i32 %24, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %2
%29 = load i32, i32* @stderr
%30 = load i32, i32* %9
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %32)
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
br label %36
36: ; preds = %28, %2
%37 = call i32 @FD_ZERO(i32* %10)
%38 = load i32, i32* %6
%39 = call i32 @FD_SET(i32 %38, i32* %10)
%40 = load i32, i32* %7
%41 = call i32 @FD_SET(i32 %40, i32* %10)
%42 = load i32, i32* @FD_SETSIZE
%43 = call i32 @select(i32 %42, i32* %10, i32* null, i32* null, %struct.timeval* %11)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp ne i32 %44, 1
br i1 %45, label %54, label %46
46: ; preds = %36
%47 = load i32, i32* %7
%48 = call i64 @FD_ISSET(i32 %47, i32* %10)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i32, i32* %6
%52 = call i64 @FD_ISSET(i32 %51, i32* %10)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %62
54: ; preds = %50, %46, %36
%55 = load i32, i32* @stderr
%56 = load i32, i32* %9
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %56, i8* %58)
%60 = load i32, i32* %12
%61 = add nsw i32 %60, 1
store i32 %61, i32* %12
br label %62
62: ; preds = %54, %50
%63 = load i32, i32* %12
ret i32 %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@FD_SETSIZE = global i32 0
@stderr = global i32 0
@.str.1 = [50 x i8] c"select with timeout failed (rv = %d, errno = %s)\0A\00"
@errno = global i32 0
@.str.2 = [52 x i8] c"select with /dev/zero failed (rv = %d, errno = %s)\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = load i32, i32* %11
%14 = call i32 @FD_ZERO(i32* %4)
%15 = call i32 @FD_SET(i32 %13, i32* %4)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 100000, i32* %17
%18 = load i32, i32* @FD_SETSIZE
%19 = call i32 @select(i32 %18, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %24)
br label %26
26: ; preds = %21, %2
%27 = phi i32 [ 1, %21 ], [ 0, %2 ]
%28 = call i32 @FD_ZERO(i32* %4)
%29 = call i32 @FD_SET(i32 %13, i32* %4)
%30 = call i32 @FD_SET(i32 %10, i32* %4)
%31 = load i32, i32* @FD_SETSIZE
%32 = call i32 @select(i32 %31, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %40
34: ; preds = %26
%35 = call i64 @FD_ISSET(i32 %10, i32* %4)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = call i64 @FD_ISSET(i32 %13, i32* %4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %37, %34, %26
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %32, i8* %43)
%45 = add nuw nsw i32 %27, 1
br label %46
46: ; preds = %40, %37
%47 = phi i32 [ %45, %40 ], [ %27, %37 ]
ret i32 %47
}