#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*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void server(int readfd, int writefd) {
  char buffer[2048];
  int n, fd;
  if ((n = read(readfd, buffer, 2048)) == -1) {
    perror("child process read");
    exit(0);
  }
  buffer[n] = '\0';
  printf("throught pipe read the path is %s\n", buffer);
  if ((fd = open(buffer, O_RDONLY)) == -1) {
    printf("child process open\n");
    snprintf(buffer + n, 2048 - n, "error:%s", strerror(errno));
    write(writefd, buffer, 2048);
  } else {
    while ((n = read(fd, buffer, 2048)) > 0) {
      write(writefd, buffer, n);
    }
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1060 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0xffffffff,%eax
jne    11c7 <server+0x47>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
movslq -0x814(%rbp),%rax
movb   $0x0,-0x810(%rbp,%rax,1)
lea    -0x810(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x810(%rbp),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x818(%rbp)
cmp    $0xffffffff,%eax
jne    1289 <server+0x109>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x810(%rbp),%rax
movslq -0x814(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x828(%rbp)
mov    $0x800,%eax
sub    -0x814(%rbp),%eax
mov    %eax,-0x81c(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x828(%rbp),%rdi
mov    -0x81c(%rbp),%esi
mov    %rax,%rcx
lea    0xddf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1030 <write@plt>
jmp    12d3 <server+0x153>
jmp    128e <server+0x10e>
mov    -0x818(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1060 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0x0,%eax
jle    12ce <server+0x14e>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    -0x814(%rbp),%edx
call   1030 <write@plt>
jmp    128e <server+0x10e>
jmp    12d3 <server+0x153>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %esi,%r15d
mov    %rsp,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1289 <server+0xf9>
mov    %eax,%r14d
movslq %eax,%rbx
movb   $0x0,(%rsp,%rbx,1)
lea    0xe4c(%rip),%rdi
mov    %rsp,%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1232 <server+0xa2>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x800,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    127b <server+0xeb>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1210 <server+0x80>
jmp    127b <server+0xeb>
add    %rsp,%rbx
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0x800,%ebp
sub    %r14d,%ebp
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd8(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x800,%edx
call   1040 <write@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"child process read\00"
@.str.1 = [35 x i8] c"throught pipe read the path is %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [20 x i8] c"child process open\0A\00"
@.str.3 = [9 x i8] c"error:%s\00"
@errno = global i32 0
define void @server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 2048)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %39
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %6
%32 = sub nsw i32 2048, %31
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @snprintf(i8* %30, i32 %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %4
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%38 = call i32 @write(i32 %36, i8* %37, i32 2048)
br label %51
39: ; preds = %15
br label %40
40: ; preds = %45, %39
%41 = load i32, i32* %7
%42 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 2048)
store i32 %43, i32* %6
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i32, i32* %4
%47 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%48 = load i32, i32* %6
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
br label %40
50: ; preds = %40
br label %51
51: ; preds = %50, %25
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"child process read\00"
@.str.1 = [35 x i8] c"throught pipe read the path is %s\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [9 x i8] c"error:%s\00"
@errno = global i32 0
@str = [19 x i8] c"child process open\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 2048)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = sext i32 %5 to i64
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %4, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = call i32 @read(i32 %15, i8* %4, i32 2048)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %27, label %32
20: ; preds = %10
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%22 = sub nsw i32 2048, %5
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @snprintf(i8* %12, i32 %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = call i32 @write(i32 %1, i8* %4, i32 2048)
br label %32
27: ; preds = %17, %27
%28 = phi i32 [ %30, %27 ], [ %18, %17 ]
%29 = call i32 @write(i32 %1, i8* %4, i32 %28)
%30 = call i32 @read(i32 %15, i8* %4, i32 2048)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %27, label %32
32: ; preds = %27, %17, %20
ret void
}