#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*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(void) {
  char write_msg[25] = "Greetings";
  char read_msg[25];
  pid_t pid;
  int fd[2];
  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("\n\nparent starts execution...\n\n");
    close(fd[0]);
    write(fd[1], write_msg, strlen(write_msg) + 1);
    close(fd[1]);
    wait(NULL);
  } else {
    printf("\n\nchild starts execution...\n\n");
    close(fd[1]);
    read(fd[0], read_msg, 25);
    printf("\n\nchild read: %s\n\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    0xe5a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe54(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xe52(%rip),%al
mov    %al,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11fc <main+0x6c>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <main+0x14c>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jge    1233 <main+0xa3>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dc <main+0x14c>
cmpq   $0x0,-0x48(%rbp)
jle    1294 <main+0x104>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x4c(%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    -0x4c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    12d5 <main+0x145>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x19,%edx
call   1080 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%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 0xe5b(%rip),%xmm0
movups %xmm0,0x19(%rsp)
movaps 0xe46(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1224 <main+0x84>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1236 <main+0x96>
je     1252 <main+0xb2>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1070 <close@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
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    1299 <main+0xf9>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
jmp    1246 <main+0xa6>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%ebx
jmp    1299 <main+0xf9>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    0xdad(%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 = [31 x i8] c"\0A\0Aparent starts execution...\0A\0A\00"
@.str.3 = [30 x i8] c"\0A\0Achild starts execution...\0A\0A\00"
@.str.4 = [19 x i8] c"\0A\0Achild read: %s\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [25 x i8]
%3 = alloca [25 x i8]
%4 = alloca i64
%5 = alloca [2 x i32]
store i32 0, i32* %1
%6 = bitcast [25 x i8]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, 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 %54
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %4
%15 = load i64, i64* %4
%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 %54
20: ; preds = %13
%21 = load i64, i64* %4
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %32, 1
%34 = call i32 @write(i32 %29, i8* %30, i64 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = call i32 @wait(i32* null)
br label %53
39: ; preds = %20
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 25)
%48 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %53
53: ; preds = %39, %23
store i32 0, i32* %1
br label %54
54: ; preds = %53, %17, %10
%55 = load i32, i32* %1
ret i32 %55
}

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.4 = [19 x i8] c"\0A\0Achild read: %s\0A\0A\00"
@str = [29 x i8] c"\0A\0Achild starts execution...\0A\00"
@str.5 = [30 x i8] c"\0A\0Aparent starts execution...\0A\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 %43
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 %43
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 ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i64 @strlen(i8* %4)
%28 = add nsw i64 %27, 1
%29 = call i32 @write(i32 %26, i8* %4, i64 %28)
%30 = load i32, i32* %25
%31 = call i32 @close(i32 %30)
%32 = call i32 @wait(i32* null)
br label %43
33: ; preds = %19
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 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 = load i32, i32* %7
%39 = call i32 @read(i32 %38, i8* %5, i32 25)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %21, %33, %16, %10
%44 = phi i32 [ 1, %10 ], [ 1, %16 ], [ 0, %33 ], [ 0, %21 ]
ret i32 %44
}