#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int send(int, char *, int, int /*<<< orphan*/);
int strlen(char const *);
int write(int, char const *, int);
void read_write_pty(int ptyfd, int sockfd) {
  char pbuf[512 + 1] = {0};
  char sbuf[512 + 1] = {0};
  int ret = 0, max = (((ptyfd) < (sockfd)) ? (sockfd) : (ptyfd)) + 1;
  printf("max fd: %d, pty: %d, sock: %d\n", max, ptyfd, sockfd);
  fd_set rfds;
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(ptyfd, &rfds);
    FD_SET(sockfd, &rfds);
    ret = select(max, &rfds, NULL, NULL, NULL);
    if (ret == -1) {
      perror("select");
      break;
    } else if (ret == 0) {
      printf("fake wakeup!\n");
      break;
    } else {
      if (FD_ISSET(sockfd, &rfds)) {
        printf("socket has event\n");
        memset(sbuf, 0, 512);
        ret = recv(sockfd, sbuf, 512, 0);
        if (ret < 0) {
          perror("recv sockfd");
          break;
        } else if (ret == 0) {
          perror("connect break?\n");
          break;
        }
        sbuf[ret] = 0;
        printf("read %d from socket\n", ret);
        ret = write(ptyfd, sbuf, ret);
        printf("write %d to pty\n", ret);
      }
      if (FD_ISSET(ptyfd, &rfds)) {
        printf("pty files has event\n");
        memset(pbuf, 0, 512);
        ret = read(ptyfd, pbuf, 512);
        if (ret < 0) {
          perror("recv pty");
          break;
        } else if (ret == 0) {
          perror("pty break?\n");
          break;
        }
        pbuf[ret] = 0;
        printf("read %d from pty\n", ret);
        ret = send(sockfd, pbuf, ret, 0);
        printf("send %d to sock\n", ret);
      }
    }
  }
  printf("pty loop exit\n");
  FD_ZERO(&rfds);
  char const *cmd = "exit";
  write(ptyfd, cmd, strlen(cmd));
  printf("send exit to notify bash to exit\n");
  close(ptyfd);
  close(sockfd);
}


x86 assembly (clang, -O0):
<read_write_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1090 <memset@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1090 <memset@plt>
movl   $0x0,-0x424(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    122b <read_write_pty+0x5b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x43c(%rbp)
jmp    1234 <read_write_pty+0x64>
mov    -0x4(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x43c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x428(%rbp),%edi
lea    -0x42c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10d0 <select@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12c8 <read_write_pty+0xf8>
lea    0xd61(%rip),%rdi
call   10e0 <perror@plt>
jmp    14c9 <read_write_pty+0x2f9>
cmpl   $0x0,-0x424(%rbp)
jne    12e8 <read_write_pty+0x118>
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    14c9 <read_write_pty+0x2f9>
mov    -0x8(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13d1 <read_write_pty+0x201>
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1090 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    135c <read_write_pty+0x18c>
lea    0xcf4(%rip),%rdi
call   10e0 <perror@plt>
jmp    14c9 <read_write_pty+0x2f9>
cmpl   $0x0,-0x424(%rbp)
jne    137a <read_write_pty+0x1aa>
lea    0xce2(%rip),%rdi
call   10e0 <perror@plt>
jmp    14c9 <read_write_pty+0x2f9>
jmp    137f <read_write_pty+0x1af>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%esi
lea    0xcad(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14ba <read_write_pty+0x2ea>
lea    0xc97(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1090 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   10b0 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1443 <read_write_pty+0x273>
lea    0xc64(%rip),%rdi
call   10e0 <perror@plt>
jmp    14c9 <read_write_pty+0x2f9>
cmpl   $0x0,-0x424(%rbp)
jne    1461 <read_write_pty+0x291>
lea    0xc4f(%rip),%rdi
call   10e0 <perror@plt>
jmp    14c9 <read_write_pty+0x2f9>
jmp    1466 <read_write_pty+0x296>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    0xc30(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x424(%rbp),%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%esi
lea    0xc11(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    14bf <read_write_pty+0x2ef>
jmp    14c4 <read_write_pty+0x2f4>
jmp    125d <read_write_pty+0x8d>
lea    0xc05(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
lea    0xbfa(%rip),%rax
mov    %rax,-0x438(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x44c(%rbp)
mov    -0x438(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x438(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xbba(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x4(%rbp),%edi
call   10a0 <close@plt>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write_pty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %esi,%r13d
mov    %edi,%r12d
lea    0x220(%rsp),%r14
mov    $0x201,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <memset@plt>
lea    0x10(%rsp),%rbp
mov    $0x201,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10a0 <memset@plt>
cmp    %r13d,%r12d
mov    %r12d,%esi
cmovl  %r13d,%esi
add    $0x1,%esi
lea    0xdca(%rip),%rdi
mov    %esi,0x8(%rsp)
mov    %r12d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
mov    %r15,%rdi
call   10d0 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
mov    0x8(%rsp),%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10e0 <select@plt>
test   %eax,%eax
je     13af <read_write_pty+0x1cf>
cmp    $0xffffffff,%eax
je     141b <read_write_pty+0x23b>
mov    %r13d,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     131c <read_write_pty+0x13c>
mov    %r14,%rbx
lea    0xe37(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10a0 <memset@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1429 <read_write_pty+0x249>
je     1437 <read_write_pty+0x257>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd51(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <write@plt>
lea    0xd47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%r14
mov    %r12d,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1250 <read_write_pty+0x70>
mov    %rbp,%rbx
lea    0xd66(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10a0 <memset@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   10c0 <read@plt>
test   %eax,%eax
js     1448 <read_write_pty+0x268>
je     1459 <read_write_pty+0x279>
mov    %eax,%ebp
mov    %eax,%eax
movb   $0x0,0x220(%rsp,%rax,1)
lea    0xcfe(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
lea    0xcf1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rbp
jmp    1250 <read_write_pty+0x70>
lea    0xd3e(%rip),%rdi
call   1040 <puts@plt>
lea    0xcf2(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   10d0 <FD_ZERO@plt>
lea    0xd06(%rip),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0xcce(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    %r13d,%edi
call   10b0 <close@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbfd(%rip),%rdi
call   10f0 <perror@plt>
jmp    13bb <read_write_pty+0x1db>
lea    0xbf6(%rip),%rdi
call   10f0 <perror@plt>
jmp    13bb <read_write_pty+0x1db>
lea    0xbf4(%rip),%rdi
call   10f0 <perror@plt>
jmp    13bb <read_write_pty+0x1db>
lea    0xc19(%rip),%rdi
call   10f0 <perror@plt>
jmp    13bb <read_write_pty+0x1db>
lea    0xc11(%rip),%rdi
call   10f0 <perror@plt>
jmp    13bb <read_write_pty+0x1db>

LLVM IR (-O0):
@.str = [31 x i8] c"max fd: %d, pty: %d, sock: %d\0A\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [14 x i8] c"fake wakeup!\0A\00"
@.str.3 = [18 x i8] c"socket has event\0A\00"
@.str.4 = [12 x i8] c"recv sockfd\00"
@.str.5 = [16 x i8] c"connect break?\0A\00"
@.str.6 = [21 x i8] c"read %d from socket\0A\00"
@.str.7 = [17 x i8] c"write %d to pty\0A\00"
@.str.8 = [21 x i8] c"pty files has event\0A\00"
@.str.9 = [9 x i8] c"recv pty\00"
@.str.10 = [12 x i8] c"pty break?\0A\00"
@.str.11 = [18 x i8] c"read %d from pty\0A\00"
@.str.12 = [17 x i8] c"send %d to sock\0A\00"
@.str.13 = [15 x i8] c"pty loop exit\0A\00"
@.str.14 = [5 x i8] c"exit\00"
@.str.15 = [34 x i8] c"send exit to notify bash to exit\0A\00"
define void @read_write_pty(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [513 x i8]
%6 = alloca [513 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = bitcast [513 x i8]* %5 to i8*
%12 = bitcast [513 x i8]* %6 to i8*
store i32 0, i32* %7
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %4
br label %20
18: ; preds = %2
%19 = load i32, i32* %3
br label %20
20: ; preds = %18, %16
%21 = phi i32 [ %17, %16 ], [ %19, %18 ]
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = load i32, i32* %3
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %24, i32 %25)
br label %27
27: ; preds = %20, %112
%28 = call i32 @FD_ZERO(i32* %9)
%29 = load i32, i32* %3
%30 = call i32 @FD_SET(i32 %29, i32* %9)
%31 = load i32, i32* %4
%32 = call i32 @FD_SET(i32 %31, i32* %9)
%33 = load i32, i32* %8
%34 = call i32 @select(i32 %33, i32* %9, i32* null, i32* null, i32* null)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %113
39: ; preds = %27
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %113
44: ; preds = %39
%45 = load i32, i32* %4
%46 = call i64 @FD_ISSET(i32 %45, i32* %9)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %77
48: ; preds = %44
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%50 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%51 = call i32 @memset(i8* %50, i32 0, i32 512)
%52 = load i32, i32* %4
%53 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%54 = call i32 @recv(i32 %52, i8* %53, i32 512, i32 0)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %48
%58 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %113
59: ; preds = %48
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %113
64: ; preds = %59
br label %65
65: ; preds = %64
%66 = load i32, i32* %7
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 %67
store i8 0, i8* %68
%69 = load i32, i32* %7
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %69)
%71 = load i32, i32* %3
%72 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%73 = load i32, i32* %7
%74 = call i32 @write(i32 %71, i8* %72, i32 %73)
store i32 %74, i32* %7
%75 = load i32, i32* %7
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %75)
br label %77
77: ; preds = %65, %44
%78 = load i32, i32* %3
%79 = call i64 @FD_ISSET(i32 %78, i32* %9)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %110
81: ; preds = %77
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
%83 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%84 = call i32 @memset(i8* %83, i32 0, i32 512)
%85 = load i32, i32* %3
%86 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%87 = call i32 @read(i32 %85, i8* %86, i32 512)
store i32 %87, i32* %7
%88 = load i32, i32* %7
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %81
%91 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %113
92: ; preds = %81
%93 = load i32, i32* %7
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %92
%96 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
br label %113
97: ; preds = %92
br label %98
98: ; preds = %97
%99 = load i32, i32* %7
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 %100
store i8 0, i8* %101
%102 = load i32, i32* %7
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i32 %102)
%104 = load i32, i32* %4
%105 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%106 = load i32, i32* %7
%107 = call i32 @send(i32 %104, i8* %105, i32 %106, i32 0)
store i32 %107, i32* %7
%108 = load i32, i32* %7
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i32 %108)
br label %110
110: ; preds = %98, %77
br label %111
111: ; preds = %110
br label %112
112: ; preds = %111
br label %27
113: ; preds = %95, %90, %62, %57, %42, %37
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0))
%115 = call i32 @FD_ZERO(i32* %9)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0), i8** %10
%116 = load i32, i32* %3
%117 = load i8*, i8** %10
%118 = load i8*, i8** %10
%119 = call i32 @strlen(i8* %118)
%120 = call i32 @write(i32 %116, i8* %117, i32 %119)
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.15, i64 0, i64 0))
%122 = load i32, i32* %3
%123 = call i32 @close(i32 %122)
%124 = load i32, i32* %4
%125 = call i32 @close(i32 %124)
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"max fd: %d, pty: %d, sock: %d\0A\00"
@.str.1 = [7 x i8] c"select\00"
@.str.4 = [12 x i8] c"recv sockfd\00"
@.str.5 = [16 x i8] c"connect break?\0A\00"
@.str.6 = [21 x i8] c"read %d from socket\0A\00"
@.str.7 = [17 x i8] c"write %d to pty\0A\00"
@.str.9 = [9 x i8] c"recv pty\00"
@.str.10 = [12 x i8] c"pty break?\0A\00"
@.str.11 = [18 x i8] c"read %d from pty\0A\00"
@.str.12 = [17 x i8] c"send %d to sock\0A\00"
@.str.14 = [5 x i8] c"exit\00"
@str = [20 x i8] c"pty files has event\00"
@str.16 = [14 x i8] c"pty loop exit\00"
@str.17 = [33 x i8] c"send exit to notify bash to exit\00"
@str.18 = [17 x i8] c"socket has event\00"
@str.19 = [13 x i8] c"fake wakeup!\00"
define void @read_write_pty(i32 %0, i32 %1) {
%3 = alloca [513 x i8]
%4 = alloca [513 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%8 = icmp slt i32 %0, %1
%9 = select i1 %8, i32 %1, i32 %0
%10 = add nsw i32 %9, 1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %0, i32 %1)
%12 = bitcast i32* %5 to i8*
br label %13
13: ; preds = %62, %2
%14 = call i32 @FD_ZERO(i32* %5)
%15 = call i32 @FD_SET(i32 %0, i32* %5)
%16 = call i32 @FD_SET(i32 %1, i32* %5)
%17 = call i32 @select(i32 %10, i32* %5, i32* null, i32* null, i32* null)
switch i32 %17, label %22 [
i32 -1, label %18
i32 0, label %20
]
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %63
20: ; preds = %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.19, i64 0, i64 0))
br label %63
22: ; preds = %13
%23 = call i64 @FD_ISSET(i32 %1, i32* %5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %42, label %25
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.18, i64 0, i64 0))
%27 = call i32 @memset(i8* %7, i32 0, i32 512)
%28 = call i32 @recv(i32 %1, i8* %7, i32 512, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %63
32: ; preds = %25
%33 = icmp eq i32 %28, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %63
36: ; preds = %32
%37 = zext i32 %28 to i64
%38 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %28)
%40 = call i32 @write(i32 %0, i8* %7, i32 %28)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %36, %22
%43 = call i64 @FD_ISSET(i32 %0, i32* %5)
%44 = icmp eq i64 %43, 0
br i1 %44, label %62, label %45
45: ; preds = %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%47 = call i32 @memset(i8* %6, i32 0, i32 512)
%48 = call i32 @read(i32 %0, i8* %6, i32 512)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %63
52: ; preds = %45
%53 = icmp eq i32 %48, 0
br i1 %53, label %54, label %56
54: ; preds = %52
%55 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
br label %63
56: ; preds = %52
%57 = zext i32 %48 to i64
%58 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 %57
store i8 0, i8* %58
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i32 %48)
%60 = call i32 @send(i32 %1, i8* %6, i32 %48, i32 0)
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %56, %42
br label %13
63: ; preds = %54, %50, %34, %30, %20, %18
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.16, i64 0, i64 0))
%65 = call i32 @FD_ZERO(i32* %5)
%66 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%67 = call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0), i32 %66)
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.17, i64 0, i64 0))
%69 = call i32 @close(i32 %0)
%70 = call i32 @close(i32 %1)
ret void
}