#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 POLLHUP;
int POLLIN;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void readwrite(int tty1in, int tty1out, int tty2in, int tty2out) {
  int n;
  struct pollfd pfd[2];
  char c;
  pfd[0].fd = tty2in;
  pfd[0].events = POLLIN;
  pfd[1].fd = tty1in;
  pfd[1].events = POLLIN;
  while (pfd[0].fd != -1) {
    n = poll(pfd, 2, -1);
    if (n < 0) {
      perror("error poll");
      return;
    }
    if (n == 0)
      return;
    if (pfd[0].revents & POLLHUP)
      return;
    if (pfd[0].revents & POLLIN) {
      if (n = read(tty2in, &c, 1), n <= 0)
        return;
      else {
        c &= 0177;
        if (c < 0140)
          write(tty1out, &c, 1);
      }
    }
    if (pfd[1].revents & POLLIN) {
      if (n = read(tty1in, &c, 1), n <= 0)
        return;
      else {
        if (c == 035)
          return;
        if (c == 033)
          c = 0175;
        if (c >= 'a' && c <= 'z')
          c &= ~040;
        c |= 0200;
        write(tty2out, &c, 1);
      }
    }
  }
}


x86 assembly (clang, -O0):
<readwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     12ec <readwrite+0x1ac>
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1050 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b3 <readwrite+0x73>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    12ec <readwrite+0x1ac>
cmpl   $0x0,-0x14(%rbp)
jne    11c2 <readwrite+0x82>
jmp    12ec <readwrite+0x1ac>
mov    -0x28(%rbp),%eax
mov    0x2e1c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11dc <readwrite+0x9c>
jmp    12ec <readwrite+0x1ac>
mov    -0x28(%rbp),%eax
mov    0x2dfa(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1246 <readwrite+0x106>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1214 <readwrite+0xd4>
jmp    12ec <readwrite+0x1ac>
movsbl -0x31(%rbp),%eax
and    $0x7f,%eax
mov    %al,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
cmp    $0x60,%eax
jge    123c <readwrite+0xfc>
mov    -0x8(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1241 <readwrite+0x101>
jmp    1246 <readwrite+0x106>
mov    -0x1c(%rbp),%eax
mov    0x2d90(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12e7 <readwrite+0x1a7>
mov    -0x4(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     127e <readwrite+0x13e>
jmp    12ec <readwrite+0x1ac>
movsbl -0x31(%rbp),%eax
cmp    $0x1d,%eax
jne    1290 <readwrite+0x150>
jmp    12ec <readwrite+0x1ac>
movsbl -0x31(%rbp),%eax
cmp    $0x1b,%eax
jne    12a1 <readwrite+0x161>
movb   $0x7d,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
cmp    $0x61,%eax
jl     12c5 <readwrite+0x185>
movsbl -0x31(%rbp),%eax
cmp    $0x7a,%eax
jg     12c5 <readwrite+0x185>
movsbl -0x31(%rbp),%eax
and    $0xffffffdf,%eax
mov    %al,-0x31(%rbp)
movsbl -0x31(%rbp),%eax
or     $0x80,%eax
mov    %al,-0x31(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12e7 <readwrite+0x1a7>
jmp    1178 <readwrite+0x38>
add    $0x40,%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    %esi,0xc(%rsp)
mov    %edx,0x10(%rsp)
mov    0x2e83(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x14(%rsp)
mov    %edi,0x1c(%rsp)
mov    %eax,0x20(%rsp)
cmp    $0xffffffff,%edx
je     125d <readwrite+0x11d>
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %edi,%r13d
lea    0x10(%rsp),%r12
lea    0xb(%rsp),%rbx
jmp    11d0 <readwrite+0x90>
nopl   0x0(%rax)
cmp    $0x1b,%al
movzbl %al,%eax
mov    $0x7d,%ecx
cmove  %ecx,%eax
lea    -0x61(%rax),%ecx
mov    %eax,%edx
and    $0x5f,%dl
cmp    $0x1a,%cl
movzbl %dl,%ecx
cmovae %eax,%ecx
or     $0x80,%cl
mov    %cl,0xb(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmpl   $0xffffffff,0x10(%rsp)
je     125d <readwrite+0x11d>
mov    %r12,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1050 <poll@plt>
test   %eax,%eax
js     126c <readwrite+0x12c>
je     125d <readwrite+0x11d>
mov    0x18(%rsp),%eax
mov    0x2df1(%rip),%rcx
test   %eax,(%rcx)
jne    125d <readwrite+0x11d>
test   %eax,(%r14)
je     1233 <readwrite+0xf3>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    125d <readwrite+0x11d>
movzbl 0xb(%rsp),%eax
and    $0x7f,%al
mov    %al,0xb(%rsp)
cmp    $0x5f,%al
ja     1233 <readwrite+0xf3>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    (%r14),%eax
test   %eax,0x24(%rsp)
je     11c5 <readwrite+0x85>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    125d <readwrite+0x11d>
movzbl 0xb(%rsp),%eax
cmp    $0x1d,%al
jne    1190 <readwrite+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8d(%rip),%rdi
call   1060 <perror@plt>
jmp    125d <readwrite+0x11d>

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

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