#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 */
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_NOCTTY;
int O_RDWR;
int STDIN_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ from_fd(int);
int open(char const *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ to_fd(int);
int main(const int argc, const char *argv[]) {
  int fd, nfds;
  fd_set set;
  if (argc != 2)
    fail("arg");
  fd = open(argv[1], O_RDWR | O_NOCTTY);
  if (fd < 0)
    fail("open");
  nfds = fd + 1;
  for (;;) {
    FD_ZERO(&set);
    FD_SET(STDIN_FILENO, &set);
    FD_SET(fd, &set);
    if (select(nfds, &set, NULL, NULL, NULL) < 0)
      fail("select");
    if (FD_ISSET(fd, &set)) {
      from_fd(fd);
    }
    if (FD_ISSET(STDIN_FILENO, &set))
      to_fd(fd);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1050 <fail@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e4 <main+0x64>
lea    0xe25(%rip),%rdi
call   1050 <fail@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    1241 <main+0xc1>
lea    0xdcd(%rip),%rdi
call   1050 <fail@plt>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     125f <main+0xdf>
mov    -0x14(%rbp),%edi
call   1060 <from_fd@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1283 <main+0x103>
mov    -0x14(%rbp),%edi
call   1030 <to_fd@plt>
jmp    11ed <main+0x6d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11a0 <main+0x20>
lea    0xe65(%rip),%rdi
call   1050 <fail@plt>
mov    0x8(%rbx),%rdi
mov    0x2e3d(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11cd <main+0x4d>
lea    0xe3c(%rip),%rdi
call   1050 <fail@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
lea    0xc(%rsp),%rbp
mov    0x2df0(%rip),%r12
lea    0xe22(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    (%r12),%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10a0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
jns    122d <main+0xad>
mov    %r15,%rdi
call   1050 <fail@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1243 <main+0xc3>
mov    %ebx,%edi
call   1060 <from_fd@plt>
mov    (%r12),%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11f0 <main+0x70>
mov    %ebx,%edi
call   1030 <to_fd@plt>
jmp    11f0 <main+0x70>

LLVM IR (-O0):
@.str = [4 x i8] c"arg\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@.str.2 = [7 x i8] c"select\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 i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @fail(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %13
%24 = call i32 @fail(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %13
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %28
28: ; preds = %53, %25
%29 = call i32 @FD_ZERO(i32* %8)
%30 = load i32, i32* @STDIN_FILENO
%31 = call i32 @FD_SET(i32 %30, i32* %8)
%32 = load i32, i32* %6
%33 = call i32 @FD_SET(i32 %32, i32* %8)
%34 = load i32, i32* %7
%35 = call i64 @select(i32 %34, i32* %8, i32* null, i32* null, i32* null)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @fail(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %28
%40 = load i32, i32* %6
%41 = call i64 @FD_ISSET(i32 %40, i32* %8)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* %6
%45 = call i32 @from_fd(i32 %44)
br label %46
46: ; preds = %43, %39
%47 = load i32, i32* @STDIN_FILENO
%48 = call i64 @FD_ISSET(i32 %47, i32* %8)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* %6
%52 = call i32 @to_fd(i32 %51)
br label %53
53: ; preds = %50, %46
br label %28
}

LLVM IR (-O2):
@.str = [4 x i8] c"arg\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@STDIN_FILENO = global i32 0
@.str.2 = [7 x i8] c"select\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @fail(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @fail(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
%19 = add nsw i32 %14, 1
br label %20
20: ; preds = %40, %18
%21 = call i32 @FD_ZERO(i32* %3)
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @FD_SET(i32 %22, i32* %3)
%24 = call i32 @FD_SET(i32 %14, i32* %3)
%25 = call i64 @select(i32 %19, i32* %3, i32* null, i32* null, i32* null)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @fail(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = call i64 @FD_ISSET(i32 %14, i32* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @from_fd(i32 %14)
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* @STDIN_FILENO
%36 = call i64 @FD_ISSET(i32 %35, i32* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @to_fd(i32 %14)
br label %40
40: ; preds = %38, %34
br label %20
}