#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_sec;
  scalar_t__ tv_usec;
};
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_RDWR;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main() {
  int fd1, fd2;
  fd_set read_set, write_set, except_set;
  char buf[10];
  struct timeval timeout;
  int n;
  timeout.tv_sec = 90;
  timeout.tv_usec = 0;
  fd1 = open("./pone", O_RDWR);
  fd2 = open("./ptwo", O_RDWR);
  FD_ZERO(&read_set);
  FD_SET(fd1, &read_set);
  FD_SET(fd2, &read_set);
  n = select(FD_SETSIZE, &read_set, NULL, NULL, &timeout);
  if (n < 0) {
    perror("select ");
    exit(1);
  }
  printf("Time left %ld\n", timeout.tv_sec);
  if (FD_ISSET(fd1, &read_set)) {
    printf(" reading from fd1 (pone)\n");
    n = read(fd1, buf, 10);
    printf(" read %d from pone \n", n);
  }
  if (FD_ISSET(fd2, &read_set)) {
    printf(" reading from fd2 (ptwo)\n");
    n = read(fd2, buf, 10);
    printf(" read %d from ptwo \n", n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5a,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    123f <main+0xaf>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1299 <main+0x109>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12e2 <main+0x152>
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movl   $0x5a,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e28(%rip),%rbx
mov    (%rbx),%esi
lea    0xe37(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
lea    0xe2e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r14d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
js     12c0 <main+0x120>
mov    0x10(%rsp),%esi
lea    0xde6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1275 <main+0xd5>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
lea    0x26(%rsp),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
call   1060 <read@plt>
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12b5 <main+0x115>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
lea    0x26(%rsp),%rsi
mov    %r14d,%edi
mov    $0xa,%edx
call   1060 <read@plt>
lea    0xd8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"./pone\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"./ptwo\00"
@FD_SETSIZE = global i32 0
@.str.2 = [8 x i8] c"select \00"
@.str.3 = [15 x i8] c"Time left %ld\0A\00"
@.str.4 = [26 x i8] c" reading from fd1 (pone)\0A\00"
@.str.5 = [21 x i8] c" read %d from pone \0A\00"
@.str.6 = [26 x i8] c" reading from fd2 (ptwo)\0A\00"
@.str.7 = [21 x i8] c" read %d from ptwo \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 0, i32* %1
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 90, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = call i32 @FD_ZERO(i32* %4)
%17 = load i32, i32* %2
%18 = call i32 @FD_SET(i32 %17, i32* %4)
%19 = load i32, i32* %3
%20 = call i32 @FD_SET(i32 %19, i32* %4)
%21 = load i32, i32* @FD_SETSIZE
%22 = call i32 @select(i32 %21, i32* %4, i32* null, i32* null, %struct.timeval* %8)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %0
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %0
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %2
%33 = call i64 @FD_ISSET(i32 %32, i32* %4)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 10)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %35, %28
%43 = load i32, i32* %3
%44 = call i64 @FD_ISSET(i32 %43, i32* %4)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 10)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %46, %42
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"./pone\00"
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"./ptwo\00"
@FD_SETSIZE = global i32 0
@.str.2 = [8 x i8] c"select \00"
@.str.3 = [15 x i8] c"Time left %ld\0A\00"
@.str.5 = [21 x i8] c" read %d from pone \0A\00"
@.str.7 = [21 x i8] c" read %d from ptwo \0A\00"
@str = [25 x i8] c" reading from fd2 (ptwo)\00"
@str.8 = [25 x i8] c" reading from fd1 (pone)\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca %struct.timeval
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.timeval* %3 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 90, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %8
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = call i32 @FD_ZERO(i32* %1)
%14 = call i32 @FD_SET(i32 %10, i32* %1)
%15 = call i32 @FD_SET(i32 %12, i32* %1)
%16 = load i32, i32* @FD_SETSIZE
%17 = call i32 @select(i32 %16, i32* %1, i32* null, i32* null, %struct.timeval* %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i64 @FD_ISSET(i32 %10, i32* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
%29 = call i32 @read(i32 %10, i8* %5, i32 10)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %22
%32 = call i64 @FD_ISSET(i32 %12, i32* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%36 = call i32 @read(i32 %12, i8* %5, i32 10)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %31
ret i32 0
}