#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void read_write_pty(int pty, int sockfd) {
  char buf[512] = {0};
  int ret = 0;
  pid_t pid = 0;
  if ((pid = fork()) < 0) {
    perror("");
    exit(1);
  } else if (pid == 0) {
    while (1) {
      memset(buf, 0, 512);
      ret = recv(sockfd, buf, 512 - 1, 0);
      if (ret < 0) {
        exit(ret);
      }
      write(pty, buf, strlen(buf));
    }
  }
  memset(buf, 0, 512);
  while ((ret = read(pty, buf, 512 - 1))) {
    buf[ret - 1] = 0;
    if (ret <= 0) {
      exit(ret);
    }
    send(sockfd, buf, strlen(buf), 0);
    memset(buf, 0, 512);
  }
  return;
}


x86 assembly (clang, -O0):
<read_write_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
movl   $0x0,-0x214(%rbp)
movq   $0x0,-0x220(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x220(%rbp)
cmp    $0x0,%rax
jge    11f7 <read_write_pty+0x67>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x220(%rbp)
jne    128d <read_write_pty+0xfd>
jmp    120a <read_write_pty+0x7a>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    1251 <read_write_pty+0xc1>
mov    -0x214(%rbp),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    120a <read_write_pty+0x7a>
jmp    1292 <read_write_pty+0x102>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ff,%edx
call   1080 <read@plt>
mov    %eax,-0x214(%rbp)
cmp    $0x0,%eax
je     1344 <read_write_pty+0x1b4>
mov    -0x214(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
cmpl   $0x0,-0x214(%rbp)
jg     12f3 <read_write_pty+0x163>
mov    -0x214(%rbp),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x23c(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x238(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x23c(%rbp),%edi
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
jmp    12a5 <read_write_pty+0x115>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write_pty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1070 <memset@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1237 <read_write_pty+0xa7>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
test   %rbx,%rbx
je     124d <read_write_pty+0xbd>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1080 <read@plt>
test   %eax,%eax
je     122b <read_write_pty+0x9b>
movslq %eax,%rcx
movb   $0x0,-0x1(%rcx,%rbx,1)
test   %ecx,%ecx
jle    12a9 <read_write_pty+0x119>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
jmp    11e0 <read_write_pty+0x50>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     12a9 <read_write_pty+0x119>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    1270 <read_write_pty+0xe0>
mov    %eax,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define void @read_write_pty(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = bitcast [512 x i8]* %5 to i8*
store i32 0, i32* %6
store i64 0, i64* %7
%9 = call i64 (...) @fork()
store i64 %9, i64* %7
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i64, i64* %7
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %14
br label %18
18: ; preds = %17, %29
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 512)
%21 = load i32, i32* %4
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%23 = call i32 @recv(i32 %21, i8* %22, i32 511, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i32, i32* %6
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i32, i32* %3
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
br label %18
35: ; preds = %14
br label %36
36: ; preds = %35
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 512)
br label %39
39: ; preds = %54, %36
%40 = load i32, i32* %3
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 511)
store i32 %42, i32* %6
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %62
44: ; preds = %39
%45 = load i32, i32* %6
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %6
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* %6
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %44
%55 = load i32, i32* %4
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = call i32 @send(i32 %55, i8* %56, i32 %58, i32 0)
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%61 = call i32 @memset(i8* %60, i32 0, i32 512)
br label %39
62: ; preds = %39
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @read_write_pty(i32 %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i64 %5, 0
%12 = call i32 @memset(i8* %4, i32 0, i32 512)
br i1 %11, label %13, label %25
13: ; preds = %10
%14 = call i32 @recv(i32 %1, i8* %4, i32 511, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %19, %13
%17 = phi i32 [ %14, %13 ], [ %23, %19 ]
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %13, %19
%20 = call i32 @strlen(i8* %4)
%21 = call i32 @write(i32 %0, i8* %4, i32 %20)
%22 = call i32 @memset(i8* %4, i32 0, i32 512)
%23 = call i32 @recv(i32 %1, i8* %4, i32 511, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %16, label %19
25: ; preds = %10
%26 = call i32 @read(i32 %0, i8* %4, i32 511)
%27 = icmp eq i32 %26, 0
br i1 %27, label %42, label %28
28: ; preds = %25, %36
%29 = phi i32 [ %40, %36 ], [ %26, %25 ]
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %31
store i8 0, i8* %32
%33 = icmp slt i32 %29, 1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @exit(i32 %29)
unreachable
36: ; preds = %28
%37 = call i32 @strlen(i8* %4)
%38 = call i32 @send(i32 %1, i8* %4, i32 %37, i32 0)
%39 = call i32 @memset(i8* %4, i32 0, i32 512)
%40 = call i32 @read(i32 %0, i8* %4, i32 511)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %28
42: ; preds = %36, %25
ret void
}