#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 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 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 = 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\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   10a0 <memset@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   10a0 <memset@plt>
movl   $0x0,-0x424(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1060 <MAX@plt>
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10d0 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x8(%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   10e0 <select@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12ba <read_write_pty+0xda>
lea    0xd6f(%rip),%rdi
call   10f0 <perror@plt>
jmp    14bb <read_write_pty+0x2db>
cmpl   $0x0,-0x424(%rbp)
jne    12da <read_write_pty+0xfa>
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    14bb <read_write_pty+0x2db>
mov    -0x8(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13c3 <read_write_pty+0x1e3>
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10a0 <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    134e <read_write_pty+0x16e>
lea    0xd02(%rip),%rdi
call   10f0 <perror@plt>
jmp    14bb <read_write_pty+0x2db>
cmpl   $0x0,-0x424(%rbp)
jne    136c <read_write_pty+0x18c>
lea    0xcf0(%rip),%rdi
call   10f0 <perror@plt>
jmp    14bb <read_write_pty+0x2db>
jmp    1371 <read_write_pty+0x191>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    0xcd5(%rip),%rdi
mov    $0x0,%al
call   1090 <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    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14ac <read_write_pty+0x2cc>
lea    0xca5(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10a0 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   10c0 <read@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1435 <read_write_pty+0x255>
lea    0xc72(%rip),%rdi
call   10f0 <perror@plt>
jmp    14bb <read_write_pty+0x2db>
cmpl   $0x0,-0x424(%rbp)
jne    1453 <read_write_pty+0x273>
lea    0xc5d(%rip),%rdi
call   10f0 <perror@plt>
jmp    14bb <read_write_pty+0x2db>
jmp    1458 <read_write_pty+0x278>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x424(%rbp),%esi
lea    0xc3e(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x424(%rbp),%edx
xor    %ecx,%ecx
call   1080 <send@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%esi
lea    0xc1f(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    14b1 <read_write_pty+0x2d1>
jmp    14b6 <read_write_pty+0x2d6>
jmp    124f <read_write_pty+0x6f>
lea    0xc13(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x42c(%rbp),%rdi
call   10d0 <FD_ZERO@plt>
lea    0xc08(%rip),%rax
mov    %rax,-0x438(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x438(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x438(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xbc8(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x4(%rbp),%edi
call   10b0 <close@plt>
mov    -0x8(%rbp),%edi
call   10b0 <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,%r15d
lea    0x220(%rsp),%r14
mov    $0x201,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <memset@plt>
lea    0x10(%rsp),%rbp
mov    $0x201,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10b0 <memset@plt>
mov    %r15d,%edi
mov    %r13d,%esi
call   1070 <MAX@plt>
add    $0x1,%eax
lea    0xdb9(%rip),%rdi
mov    %eax,0x8(%rsp)
mov    %eax,%esi
mov    %r15d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0xc(%rsp),%r12
nop
mov    %r12,%rdi
call   10e0 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1110 <FD_SET@plt>
mov    %r13d,%edi
mov    %r12,%rsi
call   1110 <FD_SET@plt>
mov    0x8(%rsp),%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10f0 <select@plt>
test   %eax,%eax
je     13bf <read_write_pty+0x1cf>
cmp    $0xffffffff,%eax
je     142b <read_write_pty+0x23b>
mov    %r13d,%edi
mov    %r12,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     132c <read_write_pty+0x13c>
mov    %r14,%rbx
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10b0 <memset@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     1439 <read_write_pty+0x249>
je     1447 <read_write_pty+0x257>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd41(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <write@plt>
lea    0xd37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbx,%r14
mov    %r15d,%edi
mov    %r12,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1260 <read_write_pty+0x70>
mov    %rbp,%rbx
lea    0xd56(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   10b0 <memset@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   10d0 <read@plt>
test   %eax,%eax
js     1458 <read_write_pty+0x268>
je     1469 <read_write_pty+0x279>
mov    %eax,%ebp
mov    %eax,%eax
movb   $0x0,0x220(%rsp,%rax,1)
lea    0xcee(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1090 <send@plt>
lea    0xce1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbx,%rbp
jmp    1260 <read_write_pty+0x70>
lea    0xd2e(%rip),%rdi
call   1040 <puts@plt>
lea    0xce2(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   10e0 <FD_ZERO@plt>
lea    0xcf6(%rip),%rbx
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0xcbe(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
call   10c0 <close@plt>
mov    %r13d,%edi
call   10c0 <close@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbed(%rip),%rdi
call   1100 <perror@plt>
jmp    13cb <read_write_pty+0x1db>
lea    0xbe6(%rip),%rdi
call   1100 <perror@plt>
jmp    13cb <read_write_pty+0x1db>
lea    0xbe4(%rip),%rdi
call   1100 <perror@plt>
jmp    13cb <read_write_pty+0x1db>
lea    0xc09(%rip),%rdi
call   1100 <perror@plt>
jmp    13cb <read_write_pty+0x1db>
lea    0xc01(%rip),%rdi
call   1100 <perror@plt>
jmp    13cb <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 = call i32 @MAX(i32 %13, i32 %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18, i32 %19)
br label %21
21: ; preds = %2, %106
%22 = call i32 @FD_ZERO(i32* %9)
%23 = load i32, i32* %3
%24 = call i32 @FD_SET(i32 %23, i32* %9)
%25 = load i32, i32* %4
%26 = call i32 @FD_SET(i32 %25, i32* %9)
%27 = load i32, i32* %8
%28 = call i32 @select(i32 %27, i32* %9, i32* null, i32* null, i32* null)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %107
33: ; preds = %21
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %107
38: ; preds = %33
%39 = load i32, i32* %4
%40 = call i64 @FD_ISSET(i32 %39, i32* %9)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %71
42: ; preds = %38
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%44 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%45 = call i32 @memset(i8* %44, i32 0, i32 512)
%46 = load i32, i32* %4
%47 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%48 = call i32 @recv(i32 %46, i8* %47, i32 512, i32 0)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %42
%52 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %107
53: ; preds = %42
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %107
58: ; preds = %53
br label %59
59: ; preds = %58
%60 = load i32, i32* %7
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 %61
store i8 0, i8* %62
%63 = load i32, i32* %7
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %63)
%65 = load i32, i32* %3
%66 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%67 = load i32, i32* %7
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
store i32 %68, i32* %7
%69 = load i32, i32* %7
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %59, %38
%72 = load i32, i32* %3
%73 = call i64 @FD_ISSET(i32 %72, i32* %9)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %104
75: ; preds = %71
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
%77 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%78 = call i32 @memset(i8* %77, i32 0, i32 512)
%79 = load i32, i32* %3
%80 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%81 = call i32 @read(i32 %79, i8* %80, i32 512)
store i32 %81, i32* %7
%82 = load i32, i32* %7
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %75
%85 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %107
86: ; preds = %75
%87 = load i32, i32* %7
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
br label %107
91: ; preds = %86
br label %92
92: ; preds = %91
%93 = load i32, i32* %7
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 %94
store i8 0, i8* %95
%96 = load i32, i32* %7
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i32 %96)
%98 = load i32, i32* %4
%99 = getelementptr inbounds [513 x i8], [513 x i8]* %5, i64 0, i64 0
%100 = load i32, i32* %7
%101 = call i32 @send(i32 %98, i8* %99, i32 %100, i32 0)
store i32 %101, i32* %7
%102 = load i32, i32* %7
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i32 %102)
br label %104
104: ; preds = %92, %71
br label %105
105: ; preds = %104
br label %106
106: ; preds = %105
br label %21
107: ; preds = %89, %84, %56, %51, %36, %31
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0))
%109 = call i32 @FD_ZERO(i32* %9)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0), i8** %10
%110 = load i32, i32* %3
%111 = load i8*, i8** %10
%112 = load i8*, i8** %10
%113 = call i32 @strlen(i8* %112)
%114 = call i32 @write(i32 %110, i8* %111, i32 %113)
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.15, i64 0, i64 0))
%116 = load i32, i32* %3
%117 = call i32 @close(i32 %116)
%118 = load i32, i32* %4
%119 = call i32 @close(i32 %118)
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 = tail call i32 @MAX(i32 %0, i32 %1)
%9 = add nsw i32 %8, 1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %0, i32 %1)
%11 = bitcast i32* %5 to i8*
br label %12
12: ; preds = %61, %2
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = call i32 @FD_SET(i32 %1, i32* %5)
%16 = call i32 @select(i32 %9, i32* %5, i32* null, i32* null, i32* null)
switch i32 %16, label %21 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %62
19: ; preds = %12
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.19, i64 0, i64 0))
br label %62
21: ; preds = %12
%22 = call i64 @FD_ISSET(i32 %1, i32* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %41, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.18, i64 0, i64 0))
%26 = call i32 @memset(i8* %7, i32 0, i32 512)
%27 = call i32 @recv(i32 %1, i8* %7, i32 512, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %62
31: ; preds = %24
%32 = icmp eq i32 %27, 0
br i1 %32, label %33, label %35
33: ; preds = %31
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %62
35: ; preds = %31
%36 = zext i32 %27 to i64
%37 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%39 = call i32 @write(i32 %0, i8* %7, i32 %27)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %35, %21
%42 = call i64 @FD_ISSET(i32 %0, i32* %5)
%43 = icmp eq i64 %42, 0
br i1 %43, label %61, label %44
44: ; preds = %41
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%46 = call i32 @memset(i8* %6, i32 0, i32 512)
%47 = call i32 @read(i32 %0, i8* %6, i32 512)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %62
51: ; preds = %44
%52 = icmp eq i32 %47, 0
br i1 %52, label %53, label %55
53: ; preds = %51
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
br label %62
55: ; preds = %51
%56 = zext i32 %47 to i64
%57 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 %56
store i8 0, i8* %57
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i32 %47)
%59 = call i32 @send(i32 %1, i8* %6, i32 %47, i32 0)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %55, %41
br label %12
62: ; preds = %53, %49, %33, %29, %19, %17
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.16, i64 0, i64 0))
%64 = call i32 @FD_ZERO(i32* %5)
%65 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0))
%66 = call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.14, i64 0, i64 0), i32 %65)
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.17, i64 0, i64 0))
%68 = call i32 @close(i32 %0)
%69 = call i32 @close(i32 %1)
ret void
}