#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_WR;
int atomicio(int /*<<< orphan*/, int, char *, int);
int /*<<< orphan*/ close(int);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write;
void readwrite(int nfd) {
  fd_set read_fds;
  char buf[BUFSIZ];
  int wfd = fileno(stdin), n, ret, max_fd;
  int lfd = fileno(stdout);
  int escape = 0;
  struct timeval timeout;
  if (nfd == -1)
    return;
  max_fd = nfd + 1;
  while (1) {
    FD_ZERO(&read_fds);
    FD_SET(wfd, &read_fds);
    FD_SET(nfd, &read_fds);
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    n = select(max_fd, &read_fds, NULL, NULL, &timeout);
    if (n < 0) {
      close(nfd);
      perror("Select Error:");
    }
    if (n == 0) {
      if (read(nfd, buf, 0) < 0)
        return;
      continue;
    }
    if (read(nfd, buf, 0) < 0)
      return;
    if (FD_ISSET(nfd, &read_fds)) {
      if ((n = read(nfd, buf, sizeof(buf))) < 0)
        return;
      else if (n == 0) {
        shutdown(nfd, SHUT_RD);
        return;
      } else {
        if ((ret = atomicio(write, lfd, buf, n)) != n)
          return;
      }
    }
    if (FD_ISSET(wfd, &read_fds)) {
      if ((n = read(wfd, buf, sizeof(buf))) < 0)
        return;
      else if (n == 0) {
        shutdown(nfd, SHUT_WR);
      } else {
        if (escape) {
          char buf2[] = "~";
          if (*buf == '.') {
            printf("quit!\n");
            return;
          }
          escape = 0;
          if (*buf != '~' && (ret = atomicio(write, nfd, buf2, 1)) != n)
            return;
        } else {
          escape = (*buf == '~');
          if (escape)
            continue;
        }
        if ((ret = atomicio(write, nfd, buf, n)) != n)
          return;
      }
    }
  }
}


x86 assembly (clang, -O0):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    122a <readwrite+0x7a>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1238 <readwrite+0x88>
lea    -0x8(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10d0 <FD_SET@plt>
movl   $0x1,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   10a0 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a0 <readwrite+0xf0>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
cmpl   $0x0,-0x20(%rbp)
jne    12d2 <readwrite+0x122>
mov    -0x50(%rbp),%rsi
mov    -0x4(%rbp),%edi
xor    %edx,%edx
call   1070 <read@plt>
cmp    $0x0,%eax
jge    12cd <readwrite+0x11d>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
jmp    1233 <readwrite+0x83>
mov    -0x50(%rbp),%rsi
mov    -0x4(%rbp),%edi
xor    %edx,%edx
call   1070 <read@plt>
cmp    $0x0,%eax
jge    12f5 <readwrite+0x145>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139b <readwrite+0x1eb>
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x4(%rbp),%edi
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1335 <readwrite+0x185>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
cmpl   $0x0,-0x20(%rbp)
jne    135c <readwrite+0x1ac>
mov    -0x4(%rbp),%edi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
mov    -0x50(%rbp),%rdx
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%esi
mov    -0x20(%rbp),%ecx
call   10c0 <atomicio@plt>
mov    %eax,-0x24(%rbp)
cmp    -0x20(%rbp),%eax
je     138c <readwrite+0x1dc>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
jmp    1391 <readwrite+0x1e1>
jmp    1396 <readwrite+0x1e6>
jmp    139b <readwrite+0x1eb>
mov    -0x1c(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14ee <readwrite+0x33e>
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    13db <readwrite+0x22b>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
cmpl   $0x0,-0x20(%rbp)
jne    13fb <readwrite+0x24b>
mov    -0x4(%rbp),%edi
mov    0x2bf9(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
jmp    14e4 <readwrite+0x334>
cmpl   $0x0,-0x30(%rbp)
je     1486 <readwrite+0x2d6>
mov    -0x50(%rbp),%rax
mov    0xbfe(%rip),%cx
mov    %cx,-0x46(%rbp)
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1438 <readwrite+0x288>
lea    0xbe9(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
mov    -0x50(%rbp),%rax
movl   $0x0,-0x30(%rbp)
movsbl (%rax),%eax
cmp    $0x7e,%eax
je     1481 <readwrite+0x2d1>
mov    0x2b7a(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
lea    -0x46(%rbp),%rdx
mov    $0x1,%ecx
call   10c0 <atomicio@plt>
mov    %eax,-0x24(%rbp)
cmp    -0x20(%rbp),%eax
je     1481 <readwrite+0x2d1>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
jmp    14af <readwrite+0x2ff>
mov    -0x50(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     14aa <readwrite+0x2fa>
jmp    1233 <readwrite+0x83>
jmp    14af <readwrite+0x2ff>
mov    -0x50(%rbp),%rdx
mov    0x2b16(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x20(%rbp),%ecx
call   10c0 <atomicio@plt>
mov    %eax,-0x24(%rbp)
cmp    -0x20(%rbp),%eax
je     14df <readwrite+0x32f>
movl   $0x1,-0x44(%rbp)
jmp    14f3 <readwrite+0x343>
jmp    14e4 <readwrite+0x334>
jmp    14e9 <readwrite+0x339>
jmp    14ee <readwrite+0x33e>
jmp    1233 <readwrite+0x83>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebx
mov    %rsp,-0x50(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,%r12d
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%ebx
je     13b9 <readwrite+0x209>
lea    0x1(%rbx),%r13d
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
lea    -0x54(%rbp),%r14
xchg   %ax,%ax
mov    %r14,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
movl   $0x1,-0x68(%rbp)
movq   $0x0,-0x60(%rbp)
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    -0x68(%rbp),%r8
call   10a0 <select@plt>
test   %eax,%eax
js     127a <readwrite+0xca>
jne    128d <readwrite+0xdd>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    1220 <readwrite+0x70>
jmp    13b9 <readwrite+0x209>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd78(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <read@plt>
test   %eax,%eax
js     13b9 <readwrite+0x209>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12ee <readwrite+0x13e>
mov    %ebx,%edi
mov    %r15,%rsi
mov    -0x48(%rbp),%rdx
call   1070 <read@plt>
test   %eax,%eax
js     13b9 <readwrite+0x209>
je     13a9 <readwrite+0x1f9>
mov    %eax,%ecx
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%esi
mov    %r15,%rdx
mov    %ecx,-0x2c(%rbp)
call   10c0 <atomicio@plt>
cmp    -0x2c(%rbp),%eax
jne    13b9 <readwrite+0x209>
mov    %r12d,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1220 <readwrite+0x70>
mov    %r12d,%edi
mov    %r15,%rsi
mov    -0x48(%rbp),%rdx
call   1070 <read@plt>
test   %eax,%eax
js     13b9 <readwrite+0x209>
jne    1330 <readwrite+0x180>
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <shutdown@plt>
jmp    1220 <readwrite+0x70>
mov    %eax,%ecx
cmpl   $0x0,-0x38(%rbp)
mov    %eax,-0x2c(%rbp)
je     136f <readwrite+0x1bf>
movw   $0x7e,-0x2e(%rbp)
movzbl (%r15),%eax
cmp    $0x7e,%al
je     1382 <readwrite+0x1d2>
cmp    $0x2e,%al
je     13cc <readwrite+0x21c>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
lea    -0x2e(%rbp),%rdx
mov    $0x1,%ecx
call   10c0 <atomicio@plt>
mov    -0x2c(%rbp),%ecx
cmp    %ecx,%eax
je     1382 <readwrite+0x1d2>
jmp    13b9 <readwrite+0x209>
mov    $0x1,%eax
mov    %rax,-0x38(%rbp)
cmpb   $0x7e,(%r15)
je     1220 <readwrite+0x70>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
mov    %r15,%rdx
call   10c0 <atomicio@plt>
mov    $0x0,%ecx
mov    %rcx,-0x38(%rbp)
cmp    -0x2c(%rbp),%eax
je     1220 <readwrite+0x70>
jmp    13b9 <readwrite+0x209>
mov    0x2c30(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <shutdown@plt>
mov    -0x50(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc3b(%rip),%rdi
call   1030 <puts@plt>
jmp    13b9 <readwrite+0x209>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@BUFSIZ = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@.str = [14 x i8] c"Select Error:\00"
@SHUT_RD = global i32 0
@write = global i32 0
@SHUT_WR = global i32 0
@__const.readwrite.buf2 = [2 x i8] c"~\00"
@.str.1 = [7 x i8] c"quit!\0A\00"
define void @readwrite(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
%14 = alloca [2 x i8]
store i32 %0, i32* %2
%15 = load i32, i32* @BUFSIZ
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %4
%18 = alloca i8, i64 %16
store i64 %16, i64* %5
%19 = load i32, i32* @stdin
%20 = call i32 @fileno(i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* @stdout
%22 = call i32 @fileno(i32 %21)
store i32 %22, i32* %10
store i32 0, i32* %11
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %1
store i32 1, i32* %13
br label %149
26: ; preds = %1
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
br label %29
29: ; preds = %148, %135, %54, %26
br label %30
30: ; preds = %29
%31 = call i32 @FD_ZERO(i32* %3)
%32 = load i32, i32* %6
%33 = call i32 @FD_SET(i32 %32, i32* %3)
%34 = load i32, i32* %2
%35 = call i32 @FD_SET(i32 %34, i32* %3)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 1, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %37
%38 = load i32, i32* %9
%39 = call i32 @select(i32 %38, i32* %3, i32* null, i32* null, %struct.timeval* %12)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %30
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
%45 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %42, %30
%47 = load i32, i32* %7
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* %2
%51 = call i32 @read(i32 %50, i8* %18, i32 0)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 1, i32* %13
br label %149
54: ; preds = %49
br label %29
55: ; preds = %46
%56 = load i32, i32* %2
%57 = call i32 @read(i32 %56, i8* %18, i32 0)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 1, i32* %13
br label %149
60: ; preds = %55
%61 = load i32, i32* %2
%62 = call i64 @FD_ISSET(i32 %61, i32* %3)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %88
64: ; preds = %60
%65 = load i32, i32* %2
%66 = trunc i64 %16 to i32
%67 = call i32 @read(i32 %65, i8* %18, i32 %66)
store i32 %67, i32* %7
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %64
store i32 1, i32* %13
br label %149
70: ; preds = %64
%71 = load i32, i32* %7
%72 = icmp eq i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = load i32, i32* %2
%75 = load i32, i32* @SHUT_RD
%76 = call i32 @shutdown(i32 %74, i32 %75)
store i32 1, i32* %13
br label %149
77: ; preds = %70
%78 = load i32, i32* @write
%79 = load i32, i32* %10
%80 = load i32, i32* %7
%81 = call i32 @atomicio(i32 %78, i32 %79, i8* %18, i32 %80)
store i32 %81, i32* %8
%82 = load i32, i32* %7
%83 = icmp ne i32 %81, %82
br i1 %83, label %84, label %85
84: ; preds = %77
store i32 1, i32* %13
br label %149
85: ; preds = %77
br label %86
86: ; preds = %85
br label %87
87: ; preds = %86
br label %88
88: ; preds = %87, %60
%89 = load i32, i32* %6
%90 = call i64 @FD_ISSET(i32 %89, i32* %3)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %148
92: ; preds = %88
%93 = load i32, i32* %6
%94 = trunc i64 %16 to i32
%95 = call i32 @read(i32 %93, i8* %18, i32 %94)
store i32 %95, i32* %7
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %92
store i32 1, i32* %13
br label %149
98: ; preds = %92
%99 = load i32, i32* %7
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %105
101: ; preds = %98
%102 = load i32, i32* %2
%103 = load i32, i32* @SHUT_WR
%104 = call i32 @shutdown(i32 %102, i32 %103)
br label %146
105: ; preds = %98
%106 = load i32, i32* %11
%107 = icmp ne i32 %106, 0
br i1 %107, label %108, label %128
108: ; preds = %105
%109 = bitcast [2 x i8]* %14 to i8*
%110 = load i8, i8* %18
%111 = sext i8 %110 to i32
%112 = icmp eq i32 %111, 46
br i1 %112, label %113, label %115
113: ; preds = %108
%114 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %13
br label %149
115: ; preds = %108
store i32 0, i32* %11
%116 = load i8, i8* %18
%117 = sext i8 %116 to i32
%118 = icmp ne i32 %117, 126
br i1 %118, label %119, label %127
119: ; preds = %115
%120 = load i32, i32* @write
%121 = load i32, i32* %2
%122 = getelementptr inbounds [2 x i8], [2 x i8]* %14, i64 0, i64 0
%123 = call i32 @atomicio(i32 %120, i32 %121, i8* %122, i32 1)
store i32 %123, i32* %8
%124 = load i32, i32* %7
%125 = icmp ne i32 %123, %124
br i1 %125, label %126, label %127
126: ; preds = %119
store i32 1, i32* %13
br label %149
127: ; preds = %119, %115
br label %137
128: ; preds = %105
%129 = load i8, i8* %18
%130 = sext i8 %129 to i32
%131 = icmp eq i32 %130, 126
%132 = zext i1 %131 to i32
store i32 %132, i32* %11
%133 = load i32, i32* %11
%134 = icmp ne i32 %133, 0
br i1 %134, label %135, label %136
135: ; preds = %128
br label %29
136: ; preds = %128
br label %137
137: ; preds = %136, %127
%138 = load i32, i32* @write
%139 = load i32, i32* %2
%140 = load i32, i32* %7
%141 = call i32 @atomicio(i32 %138, i32 %139, i8* %18, i32 %140)
store i32 %141, i32* %8
%142 = load i32, i32* %7
%143 = icmp ne i32 %141, %142
br i1 %143, label %144, label %145
144: ; preds = %137
store i32 1, i32* %13
br label %149
145: ; preds = %137
br label %146
146: ; preds = %145, %101
br label %147
147: ; preds = %146
br label %148
148: ; preds = %147, %88
br label %29
149: ; preds = %144, %126, %113, %97, %84, %73, %69, %59, %53, %25
%150 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@BUFSIZ = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@.str = [14 x i8] c"Select Error:\00"
@SHUT_RD = global i32 0
@write = global i32 0
@SHUT_WR = global i32 0
@str = [6 x i8] c"quit!\00"
define void @readwrite(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i16
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @BUFSIZ
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i32, i32* @stdin
%11 = tail call i32 @fileno(i32 %10)
%12 = load i32, i32* @stdout
%13 = tail call i32 @fileno(i32 %12)
%14 = bitcast %struct.timeval* %3 to i8*
%15 = icmp eq i32 %0, -1
br i1 %15, label %89, label %16
16: ; preds = %1
%17 = add nuw nsw i32 %0, 1
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%20 = bitcast i16* %4 to i8*
br label %21
21: ; preds = %83, %16
%22 = phi i32 [ 0, %16 ], [ %84, %83 ]
br label %23
23: ; preds = %59, %21
br label %24
24: ; preds = %23, %35
%25 = call i32 @FD_ZERO(i32* %2)
%26 = call i32 @FD_SET(i32 %11, i32* %2)
%27 = call i32 @FD_SET(i32 %0, i32* %2)
store i32 1, i32* %18
store i64 0, i64* %19
%28 = call i32 @select(i32 %17, i32* %2, i32* null, i32* null, %struct.timeval* %3)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 @close(i32 %0)
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %38
33: ; preds = %24
%34 = icmp eq i32 %28, 0
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = call i32 @read(i32 %0, i8* %9, i32 0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %89, label %24
38: ; preds = %33, %30
%39 = call i32 @read(i32 %0, i8* %9, i32 0)
%40 = icmp slt i32 %39, 0
br i1 %40, label %89, label %41
41: ; preds = %38
%42 = call i64 @FD_ISSET(i32 %0, i32* %2)
%43 = icmp eq i64 %42, 0
br i1 %43, label %56, label %44
44: ; preds = %41
%45 = call i32 @read(i32 %0, i8* %9, i32 %6)
%46 = icmp slt i32 %45, 0
br i1 %46, label %89, label %47
47: ; preds = %44
%48 = icmp eq i32 %45, 0
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = load i32, i32* @SHUT_RD
%51 = call i32 @shutdown(i32 %0, i32 %50)
br label %89
52: ; preds = %47
%53 = load i32, i32* @write
%54 = call i32 @atomicio(i32 %53, i32 %13, i8* %9, i32 %45)
%55 = icmp eq i32 %54, %45
br i1 %55, label %56, label %89
56: ; preds = %52, %41
%57 = call i64 @FD_ISSET(i32 %11, i32* %2)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %56, %65
br label %23
60: ; preds = %56
%61 = call i32 @read(i32 %11, i8* %9, i32 %6)
%62 = icmp slt i32 %61, 0
br i1 %62, label %89, label %63
63: ; preds = %60
%64 = icmp eq i32 %61, 0
br i1 %64, label %65, label %68
65: ; preds = %63
%66 = load i32, i32* @SHUT_WR
%67 = call i32 @shutdown(i32 %0, i32 %66)
br label %59
68: ; preds = %63
%69 = icmp eq i32 %22, 0
br i1 %69, label %80, label %70
70: ; preds = %68
store i16 126, i16* %4
%71 = load i8, i8* %9
switch i8 %71, label %74 [
i8 46, label %72
i8 126, label %79
]
72: ; preds = %70
%73 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %78
74: ; preds = %70
%75 = load i32, i32* @write
%76 = call i32 @atomicio(i32 %75, i32 %0, i8* %20, i32 1)
%77 = icmp eq i32 %76, %61
br i1 %77, label %79, label %78
78: ; preds = %74, %72
br label %89
79: ; preds = %74, %70
br label %85
80: ; preds = %68
%81 = load i8, i8* %9
%82 = icmp eq i8 %81, 126
br i1 %82, label %83, label %85
83: ; preds = %80, %85
%84 = phi i32 [ 0, %85 ], [ 1, %80 ]
br label %21
85: ; preds = %79, %80
%86 = load i32, i32* @write
%87 = call i32 @atomicio(i32 %86, i32 %0, i8* %9, i32 %61)
%88 = icmp eq i32 %87, %61
br i1 %88, label %83, label %89
89: ; preds = %60, %52, %44, %38, %85, %35, %78, %1, %49
ret void
}