#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 */
int /*<<< orphan*/ EXIT_FAILURE;
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 errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ max(int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(int);
void main(int args, char *argv[]) {
  int en;
  char buf[256];
  char *fifo1 = "tuberia", *fifo2 = "beyoce";
  int fd1 = open(fifo1, O_RDWR | O_NONBLOCK),
      fd2 = open(fifo2, O_RDWR | O_NONBLOCK);
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(fd1, &rfds);
  FD_SET(fd2, &rfds);
  struct timeval tv;
  tv.tv_sec = 15;
  tv.tv_usec = 0;
  int retval = select(max(fd1, fd2) + 1, &rfds, NULL, NULL, &tv);
  if (retval == -1) {
    en = errno;
    printf("select:\t%s\n", strerror(en));
    exit(EXIT_FAILURE);
  } else if (retval == 0)
    printf("Time wait expired\n");
  else {
    int bs;
    if (FD_ISSET(fd1, &rfds)) {
      bs = read(fd1, buf, 256);
      printf("recieved:%s\n", buf);
    } else if (FD_ISSET(fd2, &rfds)) {
      bs = read(fd2, buf, 256);
      printf("%s wrote:\t", fifo2);
    } else {
      printf("select:Who is ready?\n");
      exit(EXIT_FAILURE);
    }
    buf[bs] = '\0';
    printf("recieved:%s\n", buf);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe47(%rip),%rax
mov    %rax,-0x128(%rbp)
lea    0xe41(%rip),%rax
mov    %rax,-0x130(%rbp)
mov    -0x128(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x134(%rbp)
mov    -0x130(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x138(%rbp)
lea    -0x13c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x134(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x138(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   10c0 <FD_SET@plt>
movl   $0xf,-0x150(%rbp)
movq   $0x0,-0x148(%rbp)
mov    -0x134(%rbp),%edi
mov    -0x138(%rbp),%esi
call   1050 <max@plt>
mov    %rax,%rdi
add    $0x1,%rdi
lea    -0x13c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x150(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0xffffffff,-0x154(%rbp)
jne    12d3 <main+0x133>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x154(%rbp)
jne    12f3 <main+0x153>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13de <main+0x23e>
mov    -0x134(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1346 <main+0x1a6>
mov    -0x134(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x158(%rbp)
lea    -0x120(%rbp),%rsi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13ba <main+0x21a>
mov    -0x138(%rbp),%edi
lea    -0x13c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1399 <main+0x1f9>
mov    -0x138(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x158(%rbp)
mov    -0x130(%rbp),%rsi
lea    0xcae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13b5 <main+0x215>
lea    0xca6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    13ba <main+0x21a>
movslq -0x158(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    0xc57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13e3 <main+0x243>
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    0x2e26(%rip),%r14
mov    0x2e17(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe2b(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe20(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
movl   $0xf,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebp,%edi
mov    %r14d,%esi
call   1060 <max@plt>
lea    0x1(%rax),%rdi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
je     1282 <main+0xd2>
cmp    $0xffffffff,%eax
je     12f2 <main+0x142>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1290 <main+0xe0>
lea    0x20(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,%ebx
lea    0xd9e(%rip),%rdi
mov    %r14,%rsi
jmp    12c4 <main+0x114>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
jmp    12e6 <main+0x136>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1313 <main+0x163>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,%ebx
lea    0xd6b(%rip),%rdi
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebx,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xd41(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd08(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    131f <main+0x16f>
lea    0xd19(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"tuberia\00"
@.str.1 = [7 x i8] c"beyoce\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.2 = [12 x i8] c"select:\09%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [19 x i8] c"Time wait expired\0A\00"
@.str.4 = [13 x i8] c"recieved:%s\0A\00"
@.str.5 = [11 x i8] c"%s wrote:\09\00"
@.str.6 = [22 x i8] c"select:Who is ready?\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %8
%15 = load i8*, i8** %7
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NONBLOCK
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18)
store i32 %19, i32* %9
%20 = load i8*, i8** %8
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23)
store i32 %24, i32* %10
%25 = call i32 @FD_ZERO(i32* %11)
%26 = load i32, i32* %9
%27 = call i32 @FD_SET(i32 %26, i32* %11)
%28 = load i32, i32* %10
%29 = call i32 @FD_SET(i32 %28, i32* %11)
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 15, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* %9
%33 = load i32, i32* %10
%34 = call i64 @max(i32 %32, i32 %33)
%35 = add nsw i64 %34, 1
%36 = call i32 @select(i64 %35, i32* %11, i32* null, i32* null, %struct.timeval* %12)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %2
%40 = load i32, i32* @errno
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %2
%47 = load i32, i32* %13
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %82
51: ; preds = %46
%52 = load i32, i32* %9
%53 = call i64 @FD_ISSET(i32 %52, i32* %11)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %51
%56 = load i32, i32* %9
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 256)
store i32 %58, i32* %14
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %59)
br label %76
61: ; preds = %51
%62 = load i32, i32* %10
%63 = call i64 @FD_ISSET(i32 %62, i32* %11)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %61
%66 = load i32, i32* %10
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%68 = call i32 @read(i32 %66, i8* %67, i32 256)
store i32 %68, i32* %14
%69 = load i8*, i8** %8
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %69)
br label %75
71: ; preds = %61
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %65
br label %76
76: ; preds = %75, %55
%77 = load i32, i32* %14
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %78
store i8 0, i8* %79
%80 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %76, %49
br label %83
83: ; preds = %82
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"tuberia\00"
@.str.1 = [7 x i8] c"beyoce\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.2 = [12 x i8] c"select:\09%s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [13 x i8] c"recieved:%s\0A\00"
@.str.5 = [11 x i8] c"%s wrote:\09\00"
@str = [21 x i8] c"select:Who is ready?\00"
@str.7 = [18 x i8] c"Time wait expired\00"
define void @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = bitcast i32* %4 to i8*
%16 = call i32 @FD_ZERO(i32* %4)
%17 = call i32 @FD_SET(i32 %10, i32* %4)
%18 = call i32 @FD_SET(i32 %14, i32* %4)
%19 = bitcast %struct.timeval* %5 to i8*
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 15, i32* %20
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %21
%22 = call i64 @max(i32 %10, i32 %14)
%23 = add nsw i64 %22, 1
%24 = call i32 @select(i64 %23, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %24, label %33 [
i32 -1, label %25
i32 0, label %31
]
25: ; preds = %2
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %2
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
br label %54
33: ; preds = %2
%34 = call i64 @FD_ISSET(i32 %10, i32* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @read(i32 %10, i8* %6, i32 256)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %49
39: ; preds = %33
%40 = call i64 @FD_ISSET(i32 %14, i32* %4)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = call i32 @read(i32 %14, i8* %6, i32 256)
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %49
45: ; preds = %39
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %42, %36
%50 = phi i32 [ %37, %36 ], [ %43, %42 ]
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %51
store i8 0, i8* %52
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %54
54: ; preds = %31, %49
ret void
}