#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_ZERO(int /*<<< orphan*/ *);
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char buf[256];
  int tub1 = open("tuberia", O_RDWR | O_NONBLOCK);
  int tub2 = open("tuberia2", O_RDWR | O_NONBLOCK);
  fd_set s;
  FD_ZERO(&s);
  FD_SET(tub1, &s);
  FD_SET(tub2, &s);
  int max = tub1;
  int r;
  if (tub2 > tub1)
    max = tub2;
  int val = select(max + 1, &s, NULL, NULL, NULL);
  if (val) {
    if (FD_ISSET(tub1, &s)) {
      printf("tuberia1\n");
      r = read(tub1, buf, 256);
    } else {
      printf("tuberia2\n");
      r = read(tub2, buf, 256);
    }
    buf[r] = '\0';
    printf("%s", buf);
  }
  close(tub1);
  close(tub2);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x114(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
lea    0xe32(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x118(%rbp)
lea    -0x11c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x114(%rbp),%edi
lea    -0x11c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x118(%rbp),%edi
lea    -0x11c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x114(%rbp),%eax
mov    %eax,-0x120(%rbp)
mov    -0x118(%rbp),%eax
cmp    -0x114(%rbp),%eax
jle    123b <main+0xbb>
mov    -0x118(%rbp),%eax
mov    %eax,-0x120(%rbp)
mov    -0x120(%rbp),%edi
add    $0x1,%edi
lea    -0x11c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x0,-0x128(%rbp)
je     1309 <main+0x189>
mov    -0x114(%rbp),%edi
lea    -0x11c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ba <main+0x13a>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x124(%rbp)
jmp    12e5 <main+0x165>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x118(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x124(%rbp)
movslq -0x124(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x114(%rbp),%edi
call   1050 <close@plt>
mov    -0x118(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    0x2e42(%rip),%r14
mov    0x2e33(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe47(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe3f(%rip),%r14
mov    %r14,%rdi
call   10a0 <open@plt>
mov    %eax,%r12d
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   10b0 <FD_SET@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   10b0 <FD_SET@plt>
cmp    %ebp,%r12d
mov    %ebp,%edi
cmovg  %r12d,%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
je     1269 <main+0xd9>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
lea    0xde6(%rip),%rdi
cmove  %r14,%rdi
mov    %ebp,%ebx
cmove  %r12d,%ebx
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xda9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"tuberia\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [9 x i8] c"tuberia2\00"
@.str.2 = [10 x i8] c"tuberia1\0A\00"
@.str.3 = [10 x i8] c"tuberia2\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %8
%21 = call i32 @FD_ZERO(i32* %9)
%22 = load i32, i32* %7
%23 = call i32 @FD_SET(i32 %22, i32* %9)
%24 = load i32, i32* %8
%25 = call i32 @FD_SET(i32 %24, i32* %9)
%26 = load i32, i32* %7
store i32 %26, i32* %10
%27 = load i32, i32* %8
%28 = load i32, i32* %7
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = load i32, i32* %8
store i32 %31, i32* %10
br label %32
32: ; preds = %30, %2
%33 = load i32, i32* %10
%34 = add nsw i32 %33, 1
%35 = call i32 @select(i32 %34, i32* %9, i32* null, i32* null, i32* null)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %58
38: ; preds = %32
%39 = load i32, i32* %7
%40 = call i64 @FD_ISSET(i32 %39, i32* %9)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %7
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 256)
store i32 %46, i32* %11
br label %52
47: ; preds = %38
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %8
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 256)
store i32 %51, i32* %11
br label %52
52: ; preds = %47, %42
%53 = load i32, i32* %11
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %52, %32
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [8 x i8] c"tuberia\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@str = [9 x i8] c"tuberia2\00"
@str.5 = [9 x i8] c"tuberia1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0), i32 %12)
%14 = bitcast i32* %4 to i8*
%15 = call i32 @FD_ZERO(i32* %4)
%16 = call i32 @FD_SET(i32 %9, i32* %4)
%17 = call i32 @FD_SET(i32 %13, i32* %4)
%18 = icmp sgt i32 %13, %9
%19 = select i1 %18, i32 %13, i32 %9
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %4, i32* null, i32* null, i32* null)
%22 = icmp eq i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %2
%24 = call i64 @FD_ISSET(i32 %9, i32* %4)
%25 = icmp eq i64 %24, 0
%26 = select i1 %25, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @str.5, i64 0, i64 0)
%27 = select i1 %25, i32 %13, i32 %9
%28 = call i32 @puts(i8* dereferenceable(1) %26)
%29 = call i32 @read(i32 %27, i8* %5, i32 256)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %33
33: ; preds = %23, %2
%34 = call i32 @close(i32 %9)
%35 = call i32 @close(i32 %13)
ret i32 0
}