#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;
typedef int /*<<< orphan*/ buf2;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
scalar_t__ write(int, char *, scalar_t__);
int main() {
  pid_t pid;
  int fd[2], n;
  char buf1[512], buf2[512];
  pipe(fd);
  pid = fork();
  if (pid < 0) {
    return 1;
  } else if (pid == 0) {
    close(fd[1]);
    n = read(fd[0], buf2, sizeof(buf2));
    buf2[n] = 0;
    printf("read %d bytes from pipe:\n\t%s", n, buf2);
    close(fd[0]);
  } else {
    close(fd[0]);
    strcpy(buf1, "This is a message from parent to child\n");
    if (write(fd[1], buf1, strlen(buf1)) != strlen(buf1)) {
      printf("Error writing to pipe\n");
      return 1;
    }
    close(fd[1]);
    wait(NULL);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cd <main+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    12d1 <main+0x141>
cmpq   $0x0,-0x10(%rbp)
jne    1228 <main+0x98>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1090 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    12cc <main+0x13c>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
lea    -0x220(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,-0x428(%rbp)
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x428(%rbp),%rax
cmp    %rcx,%rax
je     12bb <main+0x12b>
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d1 <main+0x141>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    12d1 <main+0x141>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    $0x1,%ebx
test   %rax,%rax
js     1277 <main+0xd7>
je     1226 <main+0x86>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
lea    0xe41(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    0xc(%rsp),%ebp
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <write@plt>
mov    %rax,%rbp
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    %rax,%rbp
jne    126b <main+0xcb>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    1277 <main+0xd7>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
movslq %eax,%rsi
movb   $0x0,0x10(%rsp,%rsi,1)
lea    0xdac(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
jmp    1277 <main+0xd7>
lea    0xdd3(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"read %d bytes from pipe:\0A\09%s\00"
@.str.1 = [40 x i8] c"This is a message from parent to child\0A\00"
@.str.2 = [23 x i8] c"Error writing to pipe\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 1, i32* %1
br label %56
13: ; preds = %0
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 512)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %4
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %27, i8* %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
br label %55
33: ; preds = %13
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%38 = call i32 @strcpy(i8* %37, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%43 = call i64 @strlen(i8* %42)
%44 = call i64 @write(i32 %40, i8* %41, i64 %43)
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%46 = call i64 @strlen(i8* %45)
%47 = icmp ne i64 %44, %46
br i1 %47, label %48, label %50
48: ; preds = %33
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %56
50: ; preds = %33
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = call i32 @wait(i32* null)
br label %55
55: ; preds = %50, %16
br label %56
56: ; preds = %12, %48, %55
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [29 x i8] c"read %d bytes from pipe:\0A\09%s\00"
@.str.1 = [40 x i8] c"This is a message from parent to child\0A\00"
@str = [22 x i8] c"Error writing to pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %40, label %11
11: ; preds = %0
%12 = icmp eq i64 %9, 0
br i1 %12, label %13, label %24
13: ; preds = %11
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* %7
%18 = call i32 @read(i32 %17, i8* %6, i32 512)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %18, i8* %6)
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
br label %40
24: ; preds = %11
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
%27 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i64 @strlen(i8* %5)
%31 = call i64 @write(i32 %29, i8* %5, i64 %30)
%32 = call i64 @strlen(i8* %5)
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %24
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %40
36: ; preds = %24
%37 = load i32, i32* %28
%38 = call i32 @close(i32 %37)
%39 = call i32 @wait(i32* null)
br label %40
40: ; preds = %36, %13, %0, %34
%41 = phi i32 [ 1, %34 ], [ 1, %0 ], [ 0, %13 ], [ 0, %36 ]
ret i32 %41
}