#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 size_t ssize_t;
typedef int /*<<< orphan*/ readbuf;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main(int args, char *argv[]) {
  fd_set rfds;
  struct timeval tv;
  int selected;
  FD_ZERO(&rfds);
  int fd_tb1 = open("/home/lucas/tuberia", O_NONBLOCK | O_RDWR);
  if (fd_tb1 == -1) {
    perror("open /home/lucas/tuberia");
    exit(EXIT_FAILURE);
  }
  int fd_tb2 = open("tuberia2", O_RDWR | O_NONBLOCK);
  if (fd_tb2 == -1) {
    perror("open tuberia2");
    exit(EXIT_FAILURE);
  }
  printf("both pipes opened\n");
  FD_SET(fd_tb1, &rfds);
  FD_SET(fd_tb2, &rfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  char readbuf[256];
  selected = select(fd_tb1 + 1, &rfds, NULL, NULL, &tv);
  printf("%i\n", fd_tb1);
  printf("%i\n", fd_tb2);
  if (selected == -1)
    perror("select()");
  else if (selected) {
    printf("Data is available now.\n");
    int fdselected;
    if (FD_ISSET(fd_tb1, &rfds))
      fdselected = fd_tb1;
    else if (FD_ISSET(fd_tb2, &rfds))
      fdselected = fd_tb2;
    else {
      perror("who selected");
      exit(EXIT_FAILURE);
    }
    ssize_t readed = read(fdselected, readbuf, sizeof(readbuf));
    printf("size of read buf %ld\n", readed);
    readbuf[readed] = '\0';
    printf("%s", readbuf);
  } else
    printf("No data within five seconds.\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11f7 <main+0x67>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
lea    0xe0e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    123c <main+0xac>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x34(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12cb <main+0x13b>
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
jmp    13b9 <main+0x229>
cmpl   $0x0,-0x2c(%rbp)
je     13a6 <main+0x216>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1307 <main+0x177>
mov    -0x30(%rbp),%eax
mov    %eax,-0x144(%rbp)
jmp    134a <main+0x1ba>
mov    -0x34(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     132b <main+0x19b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x144(%rbp)
jmp    1345 <main+0x1b5>
lea    0xd2d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    134a <main+0x1ba>
mov    -0x144(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %rax,-0x150(%rbp)
mov    -0x150(%rbp),%rsi
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x150(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rsi
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b4 <main+0x224>
lea    0xcd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b9 <main+0x229>
mov    0x2c08(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x128,%rsp
lea    0xc(%rsp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2e22(%rip),%rbp
mov    0x2e23(%rip),%rbx
mov    (%rbx),%esi
or     0x0(%rbp),%esi
lea    0xe34(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a2 <main+0x102>
mov    %eax,%r15d
mov    0x0(%rbp),%esi
or     (%rbx),%esi
lea    0xe30(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12ab <main+0x10b>
mov    %eax,%r14d
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r15d,%edi
add    $0x1,%edi
lea    0x10(%rsp),%r8
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
mov    %eax,%ebp
lea    0xdd0(%rip),%rbx
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xffffffff,%ebp
jne    1290 <main+0xf0>
lea    0xdae(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
test   %ebp,%ebp
jne    12b4 <main+0x114>
lea    0xdd1(%rip),%rdi
call   1030 <puts@plt>
jmp    1282 <main+0xe2>
lea    0xd57(%rip),%rdi
jmp    12ee <main+0x14e>
lea    0xd67(%rip),%rdi
jmp    12ee <main+0x14e>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1301 <main+0x161>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1040 <FD_ISSET@plt>
mov    %r14d,%r15d
test   %rax,%rax
jne    1301 <main+0x161>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0x20(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %rax,%rbp
lea    0xd21(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x0,0x20(%rsp,%rbp,1)
lea    0xd21(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1282 <main+0xe2>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [20 x i8] c"/home/lucas/tuberia\00"
@O_NONBLOCK = global i32 0
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"open /home/lucas/tuberia\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [9 x i8] c"tuberia2\00"
@.str.3 = [14 x i8] c"open tuberia2\00"
@.str.4 = [19 x i8] c"both pipes opened\0A\00"
@.str.5 = [4 x i8] c"%i\0A\00"
@.str.6 = [9 x i8] c"select()\00"
@.str.7 = [24 x i8] c"Data is available now.\0A\00"
@.str.8 = [13 x i8] c"who selected\00"
@.str.9 = [22 x i8] c"size of read buf %ld\0A\00"
@.str.10 = [3 x i8] c"%s\00"
@.str.11 = [30 x i8] c"No data within five seconds.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i32
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 @FD_ZERO(i32* %6)
%15 = load i32, i32* @O_NONBLOCK
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %25
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* %9
%39 = call i32 @FD_SET(i32 %38, i32* %6)
%40 = load i32, i32* %10
%41 = call i32 @FD_SET(i32 %40, i32* %6)
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %42
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %43
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
%46 = call i32 @select(i32 %45, i32* %6, i32* null, i32* null, %struct.timeval* %7)
store i32 %46, i32* %8
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %10
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %36
%54 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %89
55: ; preds = %36
%56 = load i32, i32* %8
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %86
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* %9
%61 = call i64 @FD_ISSET(i32 %60, i32* %6)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = load i32, i32* %9
store i32 %64, i32* %12
br label %76
65: ; preds = %58
%66 = load i32, i32* %10
%67 = call i64 @FD_ISSET(i32 %66, i32* %6)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = load i32, i32* %10
store i32 %70, i32* %12
br label %75
71: ; preds = %65
%72 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %69
br label %76
76: ; preds = %75, %63
%77 = load i32, i32* %12
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%79 = call i64 @read(i32 %77, i8* %78, i32 256)
store i64 %79, i64* %13
%80 = load i64, i64* %13
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i64 %80)
%82 = load i64, i64* %13
%83 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %82
store i8 0, i8* %83
%84 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i8* %84)
br label %88
86: ; preds = %55
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
br label %88
88: ; preds = %86, %76
br label %89
89: ; preds = %88, %53
%90 = load i32, i32* @EXIT_SUCCESS
%91 = call i32 @exit(i32 %90)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [20 x i8] c"/home/lucas/tuberia\00"
@O_NONBLOCK = global i32 0
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"open /home/lucas/tuberia\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [9 x i8] c"tuberia2\00"
@.str.3 = [14 x i8] c"open tuberia2\00"
@.str.5 = [4 x i8] c"%i\0A\00"
@.str.6 = [9 x i8] c"select()\00"
@.str.8 = [13 x i8] c"who selected\00"
@.str.9 = [22 x i8] c"size of read buf %ld\0A\00"
@.str.10 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
@str = [18 x i8] c"both pipes opened\00"
@str.12 = [29 x i8] c"No data within five seconds.\00"
@str.13 = [23 x i8] c"Data is available now.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca [256 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = call i32 @FD_ZERO(i32* %3)
%9 = load i32, i32* @O_NONBLOCK
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%30 = call i32 @FD_SET(i32 %12, i32* %3)
%31 = call i32 @FD_SET(i32 %22, i32* %3)
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 5, i32* %32
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %33
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%35 = add nuw nsw i32 %12, 1
%36 = call i32 @select(i32 %35, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %12)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %22)
switch i32 %36, label %41 [
i32 -1, label %39
i32 0, label %58
]
39: ; preds = %28
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %60
41: ; preds = %28
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.13, i64 0, i64 0))
%43 = call i64 @FD_ISSET(i32 %12, i32* %3)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = call i64 @FD_ISSET(i32 %22, i32* %3)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45, %41
%53 = phi i32 [ %12, %41 ], [ %22, %45 ]
%54 = call i64 @read(i32 %53, i8* %34, i32 256)
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i64 %54)
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %54
store i8 0, i8* %56
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i8* %34)
br label %60
58: ; preds = %28
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.12, i64 0, i64 0))
br label %60
60: ; preds = %52, %58, %39
%61 = load i32, i32* @EXIT_SUCCESS
%62 = call i32 @exit(i32 %61)
unreachable
}