#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 */

/* Variables and functions */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void client(int readfd, int writefd) {
  char buffer[2048];
  int n;
  printf("enter the path\n");
  gets(buffer);
  n = strlen(buffer);
  if (buffer[n - 2] == '\n')
    buffer[n - 2] = '\0';
  printf("parent process read from tty:%s\n", buffer);
  write(writefd, buffer, n);
  printf("read data throught pie from child\n");
  while ((n = read(readfd, buffer, 2048)) > 0) {
    buffer[n - 1] = '\0';
    fputs(buffer, stdout);
  }
  puts("");
}


x86 assembly (clang, -O0):
<client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x810(%rbp),%rdi
call   1090 <gets@plt>
lea    -0x810(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%eax
sub    $0x2,%eax
cltq
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11dc <client+0x6c>
mov    -0x814(%rbp),%eax
sub    $0x2,%eax
cltq
movb   $0x0,-0x810(%rbp,%rax,1)
lea    -0x810(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    -0x814(%rbp),%edx
call   1040 <write@plt>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1080 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0x0,%eax
jle    1264 <client+0xf4>
mov    -0x814(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x810(%rbp,%rax,1)
lea    -0x810(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
jmp    1214 <client+0xa4>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%r15
mov    %r15,%rdi
call   10a0 <gets@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
cltq
cmpb   $0xa,-0x2(%rax,%r15,1)
jne    11c2 <client+0x42>
movb   $0x0,-0x2(%rsp,%rax,1)
lea    0xe37(%rip),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
lea    0xe46(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x800,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1235 <client+0xb5>
mov    0x2dd7(%rip),%r14
mov    %rsp,%rbp
nopl   0x0(%rax)
add    $0xffffffff,%eax
movb   $0x0,(%rsp,%rax,1)
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <fputs@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x800,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1210 <client+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"enter the path\0A\00"
@.str.1 = [33 x i8] c"parent process read from tty:%s\0A\00"
@.str.2 = [35 x i8] c"read data throught pie from child\0A\00"
@stdout = global i32 0
@.str.3 = [1 x i8] zeroinitializer
define void @client(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%9 = call i32 @gets(i8* %8)
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = sub nsw i32 %12, 2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %2
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %4
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %37, %24
%33 = load i32, i32* %3
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 2048)
store i32 %35, i32* %6
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%43 = load i32, i32* @stdout
%44 = call i32 @fputs(i8* %42, i32 %43)
br label %32
45: ; preds = %32
%46 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [33 x i8] c"parent process read from tty:%s\0A\00"
@stdout = global i32 0
@str = [15 x i8] c"enter the path\00"
@str.4 = [34 x i8] c"read data throught pie from child\00"
define void @client(i32 %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%6 = call i32 @gets(i8* %4)
%7 = call i32 @strlen(i8* %4)
%8 = add nsw i32 %7, -2
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %2
store i8 0, i8* %10
br label %14
14: ; preds = %13, %2
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%16 = call i32 @write(i32 %1, i8* %4, i32 %7)
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.4, i64 0, i64 0))
%18 = call i32 @read(i32 %0, i8* %4, i32 2048)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %14, %20
%21 = phi i32 [ %27, %20 ], [ %18, %14 ]
%22 = add nsw i32 %21, -1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* @stdout
%26 = call i32 @fputs(i8* %4, i32 %25)
%27 = call i32 @read(i32 %0, i8* %4, i32 2048)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %20, label %29
29: ; preds = %20, %14
%30 = call i32 @putchar(i32 10)
ret void
}