#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_from_pipe(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_to_pipe(int);
int main(int argc, char **argv) {
  pid_t pid;
  int mypipe[2];
  printf("Create Pipe in parent process!\n");
  if (pipe(mypipe)) {
    fprintf(stderr, "Pipe failed.\n");
    return EXIT_FAILURE;
  }
  printf("Fork this process now!\n");
  pid = fork();
  if (pid == (pid_t)0) {
    printf("This is the child process!\n");
    printf("%d\n", pid);
    close(mypipe[1]);
    printf("Read from Pipe!\n");
    read_from_pipe(mypipe[0]);
    return EXIT_SUCCESS;
  } else if (pid < (pid_t)0) {
    fprintf(stderr, "Fork failed.\n");
    return EXIT_FAILURE;
  } else {
    printf("This is the parent process!\n");
    printf("%d\n", pid);
    close(mypipe[0]);
    printf("Write to Pipe!\n");
    write_to_pipe(mypipe[1]);
    return EXIT_SUCCESS;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     11cd <main+0x5d>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <main+0x14b>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1240 <main+0xd0>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <read_from_pipe@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <main+0x14b>
cmpq   $0x0,-0x18(%rbp)
jge    1271 <main+0x101>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <main+0x14b>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1030 <write_to_pipe@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
je     11c1 <main+0x41>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2e1c(%rip),%rax
jmp    1260 <main+0xe0>
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     121f <main+0x9f>
js     1268 <main+0xe8>
mov    %rax,%rbx
lea    0xe6d(%rip),%rdi
call   1040 <puts@plt>
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
mov    0xc(%rsp),%edi
call   1030 <write_to_pipe@plt>
jmp    1259 <main+0xd9>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
lea    0xddc(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
call   1060 <read_from_pipe@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
jmp    11b0 <main+0x30>

LLVM IR (-O0):
@.str = [32 x i8] c"Create Pipe in parent process!\0A\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"Pipe failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"Fork this process now!\0A\00"
@.str.3 = [28 x i8] c"This is the child process!\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [17 x i8] c"Read from Pipe!\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [14 x i8] c"Fork failed.\0A\00"
@.str.7 = [29 x i8] c"This is the parent process!\0A\00"
@.str.8 = [16 x i8] c"Write to Pipe!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %3
br label %52
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%18 = call i64 (...) @fork()
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%23 = load i64, i64* %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @read_from_pipe(i32 %30)
%32 = load i32, i32* @EXIT_SUCCESS
store i32 %32, i32* %3
br label %52
33: ; preds = %16
%34 = load i64, i64* %6
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %3
br label %52
40: ; preds = %33
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0))
%42 = load i64, i64* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @write_to_pipe(i32 %49)
%51 = load i32, i32* @EXIT_SUCCESS
store i32 %51, i32* %3
br label %52
52: ; preds = %40, %36, %21, %12
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [14 x i8] c"Pipe failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [14 x i8] c"Fork failed.\0A\00"
@str = [31 x i8] c"Create Pipe in parent process!\00"
@str.9 = [23 x i8] c"Fork this process now!\00"
@str.10 = [28 x i8] c"This is the parent process!\00"
@str.11 = [15 x i8] c"Write to Pipe!\00"
@str.12 = [27 x i8] c"This is the child process!\00"
@str.13 = [16 x i8] c"Read from Pipe!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %39
12: ; preds = %2
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.12, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 0)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.13, i64 0, i64 0))
%23 = load i32, i32* %6
%24 = call i32 @read_from_pipe(i32 %23)
br label %39
25: ; preds = %12
%26 = icmp slt i64 %14, 0
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %39
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.10, i64 0, i64 0))
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %14)
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @write_to_pipe(i32 %37)
br label %39
39: ; preds = %30, %27, %16, %9
%40 = phi i32* [ @EXIT_FAILURE, %9 ], [ @EXIT_SUCCESS, %16 ], [ @EXIT_FAILURE, %27 ], [ @EXIT_SUCCESS, %30 ]
%41 = load i32, i32* %40
ret i32 %41
}