#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*/ pid_t;
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 MAX(int, int);
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 /*<<< orphan*/ strcat(char *, char *);
int write(int, char *, int);
void read_write_pty(pid_t cid, int ptyfd, int sockfd) {
  char pbuf[512 + 1] = {0};
  char sbuf[512 + 1] = {0};
  int ret = 0, max = MAX(ptyfd, sockfd) + 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: %s\n", ret, sbuf);
        strcat(sbuf, "\n");
        ret = write(ptyfd, sbuf, ret + 1);
        printf("write %d to pty\n", ret + 1);
      }
      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: %s\n", ret, pbuf);
      }
    }
  }
  printf("pty loop exit\n");
  FD_ZERO(&rfds);
  close(ptyfd);
  close(sockfd);
}


x86 assembly (clang, -O0):
<read_write_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%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    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <MAX@plt>
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1100 <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    12bd <read_write_pty+0xdd>
lea    0xd6c(%rip),%rdi
call   10e0 <perror@plt>
jmp    14e5 <read_write_pty+0x305>
cmpl   $0x0,-0x424(%rbp)
jne    12dd <read_write_pty+0xfd>
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    14e5 <read_write_pty+0x305>
mov    -0xc(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13e6 <read_write_pty+0x206>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1090 <memset@plt>
mov    -0xc(%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    1351 <read_write_pty+0x171>
lea    0xcff(%rip),%rdi
call   10e0 <perror@plt>
jmp    14e5 <read_write_pty+0x305>
cmpl   $0x0,-0x424(%rbp)
jne    136f <read_write_pty+0x18f>
lea    0xced(%rip),%rdi
call   10e0 <perror@plt>
jmp    14e5 <read_write_pty+0x305>
jmp    1374 <read_write_pty+0x194>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x420(%rbp),%rdi
lea    0xc86(%rip),%rsi
call   10f0 <strcat@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
add    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%esi
add    $0x1,%esi
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14d6 <read_write_pty+0x2f6>
lea    0xc86(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1090 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   10b0 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1458 <read_write_pty+0x278>
lea    0xc53(%rip),%rdi
call   10e0 <perror@plt>
jmp    14e5 <read_write_pty+0x305>
cmpl   $0x0,-0x424(%rbp)
jne    1476 <read_write_pty+0x296>
lea    0xc3e(%rip),%rdi
call   10e0 <perror@plt>
jmp    14e5 <read_write_pty+0x305>
jmp    147b <read_write_pty+0x29b>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    0xc1f(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xc(%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    -0x210(%rbp),%rdx
lea    0xbf9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    14db <read_write_pty+0x2fb>
jmp    14e0 <read_write_pty+0x300>
jmp    1252 <read_write_pty+0x72>
lea    0xbf1(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
call   10a0 <close@plt>
mov    -0xc(%rbp),%edi
call   10a0 <close@plt>
add    $0x430,%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    %edx,%r13d
mov    %esi,%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>
mov    %r12d,%edi
mov    %r13d,%esi
call   1070 <MAX@plt>
add    $0x1,%eax
lea    0xdb9(%rip),%rdi
mov    %eax,0x8(%rsp)
mov    %eax,%esi
mov    %r12d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xc(%rsp),%r15
nop
mov    %r15,%rdi
call   10d0 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   1110 <FD_SET@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1110 <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     13dd <read_write_pty+0x1ed>
cmp    $0xffffffff,%eax
je     1421 <read_write_pty+0x231>
mov    %r13d,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1347 <read_write_pty+0x157>
mov    %r14,%rbx
lea    0xe0a(%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     142f <read_write_pty+0x23f>
je     143d <read_write_pty+0x24d>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd3d(%rip),%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbp,%rdi
lea    0xd24(%rip),%rsi
call   1100 <strcat@plt>
add    $0x1,%r14d
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <write@plt>
lea    0x1(%rax),%esi
lea    0xd1e(%rip),%rdi
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     1260 <read_write_pty+0x70>
mov    %rbp,%rbx
lea    0xd43(%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     144b <read_write_pty+0x25b>
je     1459 <read_write_pty+0x269>
mov    %eax,%ebp
mov    %eax,%eax
movb   $0x0,0x220(%rsp,%rax,1)
lea    0xcd7(%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    0xcca(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rbx,%rbp
jmp    1260 <read_write_pty+0x70>
lea    0xcf7(%rip),%rdi
call   1040 <puts@plt>
lea    0xccc(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   10d0 <FD_ZERO@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    0xbf7(%rip),%rdi
call   10f0 <perror@plt>
jmp    13e9 <read_write_pty+0x1f9>
lea    0xbf0(%rip),%rdi
call   10f0 <perror@plt>
jmp    13e9 <read_write_pty+0x1f9>
lea    0xbee(%rip),%rdi
call   10f0 <perror@plt>
jmp    13e9 <read_write_pty+0x1f9>
lea    0xc1a(%rip),%rdi
call   10f0 <perror@plt>
jmp    13e9 <read_write_pty+0x1f9>
lea    0xc15(%rip),%rdi
call   10f0 <perror@plt>
jmp    13e9 <read_write_pty+0x1f9>

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