#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 pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void readwrite(int fd) {
  int n;
  struct pollfd pfd[2];
  char c;
  pfd[0].fd = fd;
  pfd[0].events = POLLIN;
  pfd[1].fd = 0;
  pfd[1].events = POLLIN;
  while (pfd[0].fd != -1) {
    n = poll(pfd, 2, -1);
    if (n < 0) {
      perror("error poll");
      goto out;
    }
    if (n == 0)
      goto out;
    if (pfd[0].revents & POLLIN) {
      if (n = read(fd, &c, 1), n < 0)
        goto out;
      if (n == 0) {
        shutdown(fd, SHUT_RD);
        pfd[0].fd = -1;
        pfd[0].events = 0;
      } else
        write(1, &c, 1);
    }
    if (pfd[1].revents & POLLIN) {
      if (n = read(0, &c, 1), n < 0)
        goto out;
      if (n == 0) {
        shutdown(fd, SHUT_WR);
        pfd[1].fd = -1;
        pfd[1].events = 0;
      } else {
        if (c == 035)
          goto out;
        write(fd, &c, 1);
      }
    }
  }
out:
  close(fd);
}


x86 assembly (clang, -O0):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     12e7 <readwrite+0x187>
lea    -0x20(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cb <readwrite+0x6b>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
jmp    12ec <readwrite+0x18c>
cmpl   $0x0,-0x8(%rbp)
jne    11da <readwrite+0x7a>
jmp    12ec <readwrite+0x18c>
mov    -0x18(%rbp),%eax
mov    0x2df4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1255 <readwrite+0xf5>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1212 <readwrite+0xb2>
jmp    12ec <readwrite+0x18c>
cmpl   $0x0,-0x8(%rbp)
jne    1240 <readwrite+0xe0>
mov    -0x4(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0xffffffff,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    1250 <readwrite+0xf0>
mov    $0x1,%edx
lea    -0x21(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
jmp    1255 <readwrite+0xf5>
mov    -0xc(%rbp),%eax
mov    0x2d79(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12e2 <readwrite+0x182>
xor    %edi,%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    128c <readwrite+0x12c>
jmp    12ec <readwrite+0x18c>
cmpl   $0x0,-0x8(%rbp)
jne    12ba <readwrite+0x15a>
mov    -0x4(%rbp),%edi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    12dd <readwrite+0x17d>
movsbl -0x21(%rbp),%eax
cmp    $0x1d,%eax
jne    12cc <readwrite+0x16c>
jmp    12ec <readwrite+0x18c>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12e2 <readwrite+0x182>
jmp    1190 <readwrite+0x30>
jmp    12ec <readwrite+0x18c>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    %edi,0x10(%rsp)
mov    0x2e5d(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
movl   $0x0,0x1c(%rsp)
mov    %eax,0x20(%rsp)
cmp    $0xffffffff,%edi
je     128e <readwrite+0x12e>
lea    0x10(%rsp),%r14
lea    0xf(%rsp),%r15
mov    $0xffffffff,%ebp
mov    0x2e32(%rip),%r13
jmp    11d0 <readwrite+0x70>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
mov    %rbp,0x1c(%rsp)
cmpl   $0xffffffff,0x10(%rsp)
je     128e <readwrite+0x12e>
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
test   %eax,%eax
js     1282 <readwrite+0x122>
je     128e <readwrite+0x12e>
mov    (%r12),%eax
test   %eax,0x18(%rsp)
je     1240 <readwrite+0xe0>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
js     128e <readwrite+0x12e>
je     1230 <readwrite+0xd0>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1240 <readwrite+0xe0>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
mov    %rbp,0x10(%rsp)
mov    (%r12),%eax
test   %eax,0x24(%rsp)
je     11c5 <readwrite+0x65>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
js     128e <readwrite+0x12e>
je     11b0 <readwrite+0x50>
cmpb   $0x1d,0xf(%rsp)
je     128e <readwrite+0x12e>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11c5 <readwrite+0x65>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [11 x i8] c"error poll\00"
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define void @readwrite(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x %struct.pollfd]
%5 = alloca i8
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %6, i32* %8
%9 = load i32, i32* @POLLIN
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %10, i32 0, i32 1
store i32 %9, i32* %11
%12 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 0, i32* %13
%14 = load i32, i32* @POLLIN
%15 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 1
store i32 %14, i32* %16
br label %17
17: ; preds = %92, %1
%18 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %93
22: ; preds = %17
%23 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%24 = call i32 @poll(%struct.pollfd* %23, i32 2, i32 -1)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %94
29: ; preds = %22
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %94
33: ; preds = %29
%34 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = load i32, i32* @POLLIN
%38 = and i32 %36, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %60
40: ; preds = %33
%41 = load i32, i32* %2
%42 = call i32 @read(i32 %41, i8* %5, i32 1)
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
br label %94
46: ; preds = %40
%47 = load i32, i32* %3
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = load i32, i32* %2
%51 = load i32, i32* @SHUT_RD
%52 = call i32 @shutdown(i32 %50, i32 %51)
%53 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%54 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %53, i32 0, i32 0
store i32 -1, i32* %54
%55 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%56 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %55, i32 0, i32 1
store i32 0, i32* %56
br label %59
57: ; preds = %46
%58 = call i32 @write(i32 1, i8* %5, i32 1)
br label %59
59: ; preds = %57, %49
br label %60
60: ; preds = %59, %33
%61 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%62 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %61, i32 0, i32 2
%63 = load i32, i32* %62
%64 = load i32, i32* @POLLIN
%65 = and i32 %63, %64
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %92
67: ; preds = %60
%68 = call i32 @read(i32 0, i8* %5, i32 1)
store i32 %68, i32* %3
%69 = load i32, i32* %3
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %67
br label %94
72: ; preds = %67
%73 = load i32, i32* %3
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %83
75: ; preds = %72
%76 = load i32, i32* %2
%77 = load i32, i32* @SHUT_WR
%78 = call i32 @shutdown(i32 %76, i32 %77)
%79 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%80 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %79, i32 0, i32 0
store i32 -1, i32* %80
%81 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1
%82 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %81, i32 0, i32 1
store i32 0, i32* %82
br label %91
83: ; preds = %72
%84 = load i8, i8* %5
%85 = sext i8 %84 to i32
%86 = icmp eq i32 %85, 29
br i1 %86, label %87, label %88
87: ; preds = %83
br label %94
88: ; preds = %83
%89 = load i32, i32* %2
%90 = call i32 @write(i32 %89, i8* %5, i32 1)
br label %91
91: ; preds = %88, %75
br label %92
92: ; preds = %91, %60
br label %17
93: ; preds = %17
br label %94
94: ; preds = %93, %87, %71, %45, %32, %27
%95 = load i32, i32* %2
%96 = call i32 @close(i32 %95)
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [11 x i8] c"error poll\00"
@SHUT_RD = global i32 0
@SHUT_WR = global i32 0
define void @readwrite(i32 %0) {
%2 = alloca [2 x %struct.pollfd]
%3 = alloca i8
%4 = bitcast [2 x %struct.pollfd]* %2 to i8*
%5 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%6 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 1
store i32 %7, i32* %10
%11 = icmp eq i32 %0, -1
br i1 %11, label %58, label %12
12: ; preds = %1
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0, i32 2
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1, i32 2
br label %15
15: ; preds = %12, %55
%16 = call i32 @poll(%struct.pollfd* %5, i32 2, i32 -1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %58
20: ; preds = %15
%21 = icmp eq i32 %16, 0
br i1 %21, label %58, label %22
22: ; preds = %20
%23 = load i32, i32* %13
%24 = load i32, i32* @POLLIN
%25 = and i32 %24, %23
%26 = icmp eq i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %22
%28 = call i32 @read(i32 %0, i8* %3, i32 1)
%29 = icmp slt i32 %28, 0
br i1 %29, label %58, label %30
30: ; preds = %27
%31 = icmp eq i32 %28, 0
br i1 %31, label %32, label %35
32: ; preds = %30
%33 = load i32, i32* @SHUT_RD
%34 = call i32 @shutdown(i32 %0, i32 %33)
store i32 -1, i32* %6
store i32 0, i32* %8
br label %37
35: ; preds = %30
%36 = call i32 @write(i32 1, i8* %3, i32 1)
br label %37
37: ; preds = %32, %35, %22
%38 = load i32, i32* %14
%39 = load i32, i32* @POLLIN
%40 = and i32 %39, %38
%41 = icmp eq i32 %40, 0
br i1 %41, label %55, label %42
42: ; preds = %37
%43 = call i32 @read(i32 0, i8* %3, i32 1)
%44 = icmp slt i32 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %42
%46 = icmp eq i32 %43, 0
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = load i32, i32* @SHUT_WR
%49 = call i32 @shutdown(i32 %0, i32 %48)
store i32 -1, i32* %9
store i32 0, i32* %10
br label %55
50: ; preds = %45
%51 = load i8, i8* %3
%52 = icmp eq i8 %51, 29
br i1 %52, label %58, label %53
53: ; preds = %50
%54 = call i32 @write(i32 %0, i8* %3, i32 1)
br label %55
55: ; preds = %47, %53, %37
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %15
58: ; preds = %20, %27, %42, %50, %55, %1, %18
%59 = call i32 @close(i32 %0)
ret void
}