#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*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(void) {
  char write_msg[25] = "Greetings";
  char read_msg[25];
  int fd[2];
  pid_t pid;
  if (pipe(fd) == -1) {
    fprintf(stderr, "Pipe failed");
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    return 1;
  }
  if (pid > 0) {
    printf("===== This is the parent process =====\n");
    close(fd[0]);
    printf("===== Writing message via pipe =====\n");
    write(fd[1], write_msg, strlen(write_msg) + 1);
    close(fd[1]);
  } else {
    printf("----- This is the child process -----\n");
    close(fd[1]);
    printf("----- Reading message via pipe -----\n");
    read(fd[0], read_msg, 25);
    printf("read: %s\n", read_msg);
    close(fd[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe62(%rip),%al
mov    %al,-0x8(%rbp)
lea    -0x48(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ec <main+0x6c>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12df <main+0x15f>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jge    1223 <main+0xa3>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12df <main+0x15f>
cmpq   $0x0,-0x50(%rbp)
jle    1289 <main+0x109>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
jmp    12d8 <main+0x158>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x19,%edx
call   1080 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
movups 0xe6b(%rip),%xmm0
movups %xmm0,0x19(%rsp)
movaps 0xe56(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     121c <main+0x8c>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     122e <main+0x9e>
je     124a <main+0xba>
lea    0xeb3(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
xor    %ebx,%ebx
jmp    129d <main+0x10d>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
jmp    123e <main+0xae>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%ebx
jmp    129d <main+0x10d>
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    0xda9(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.write_msg = [25 x i8] c"Greetings\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.2 = [40 x i8] c"===== This is the parent process =====\0A\00"
@.str.3 = [38 x i8] c"===== Writing message via pipe =====\0A\00"
@.str.4 = [39 x i8] c"----- This is the child process -----\0A\00"
@.str.5 = [38 x i8] c"----- Reading message via pipe -----\0A\00"
@.str.6 = [10 x i8] c"read: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [25 x i8]
%3 = alloca [25 x i8]
%4 = alloca [2 x i32]
%5 = alloca i64
store i32 0, i32* %1
%6 = bitcast [25 x i8]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %55
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %55
20: ; preds = %13
%21 = load i64, i64* %5
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%32 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%33 = call i64 @strlen(i8* %32)
%34 = add nsw i64 %33, 1
%35 = call i32 @write(i32 %30, i8* %31, i64 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
br label %54
39: ; preds = %20
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 25)
%49 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %39, %23
store i32 0, i32* %1
br label %55
55: ; preds = %54, %17, %10
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@__const.main.write_msg = [25 x i8] c"Greetings\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork Failed\00"
@.str.6 = [10 x i8] c"read: %s\0A\00"
@str = [38 x i8] c"----- This is the child process -----\00"
@str.7 = [37 x i8] c"----- Reading message via pipe -----\00"
@str.8 = [39 x i8] c"===== This is the parent process =====\00"
@str.9 = [37 x i8] c"===== Writing message via pipe =====\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = alloca [25 x i8]
%3 = alloca [2 x i32]
%4 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %44
13: ; preds = %0
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %44
19: ; preds = %13
%20 = icmp eq i64 %14, 0
br i1 %20, label %33, label %21
21: ; preds = %19
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.8, i64 0, i64 0))
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i64 @strlen(i8* %4)
%29 = add nsw i64 %28, 1
%30 = call i32 @write(i32 %27, i8* %4, i64 %29)
%31 = load i32, i32* %26
%32 = call i32 @close(i32 %31)
br label %44
33: ; preds = %19
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.7, i64 0, i64 0))
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %39, i8* %5, i32 25)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %21, %33, %16, %10
%45 = phi i32 [ 1, %10 ], [ 1, %16 ], [ 0, %33 ], [ 0, %21 ]
ret i32 %45
}