#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 int uint32_t;
typedef int pid_t;
typedef scalar_t__ int32_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int getpid();
int getppid();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  printf("\n[Main]: Started default program...\n");
  uint32_t pipefd[2] = {0};
  uint32_t cpid = 0, ppid = 0;
  int32_t ret = -1;
  bool exec = false;
  if (argv[1]) {
    exec = atoi(argv[1]);
  }
  ret = pipe(pipefd);
  if (ret < 0) {
    printf("\nFailed to create a pipe..exiting..\n");
    exit(0);
  }
  if (!fork()) {
    pid_t pid = getpid();
    ppid = getppid();
    uint32_t nBytes = 0;
    printf("\n[Child]: own pid:%u, Parent id:%u ..\n", pid, ppid);
    close(pipefd[0]);
    nBytes = write(pipefd[1], "Hello World from Child process\n",
                   strlen("Hello World from Child process"));
    printf("\n wrote %u bytes from child process...\n", nBytes);
    while (1)
      continue;
  } else {
    pid_t pid = getpid();
    ppid = getppid();
    printf("\n[Parent]: own pid:%u, Parent id:%u ..\n", pid, ppid);
    close(pipefd[1]);
    uint32_t nBytes = 0;
    char buff[1024] = {0};
    nBytes = read(pipefd[0], buff, 1024);
    printf("\n Got from child %u bytes: [%s] ...\n", nBytes, buff);
    while (1)
      continue;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1233 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x18(%rbp),%rdi
call   1090 <pipe@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1260 <main+0xa0>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%eax
jne    12e9 <main+0x129>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    0xdda(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    %eax,%edx
lea    0xda6(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12e4 <main+0x124>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   10c0 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x3c(%rbp)
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    -0x440(%rbp),%rdx
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1362 <main+0x1a2>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
lea    0xf0b(%rip),%rdi
call   1030 <puts@plt>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   10a0 <pipe@plt>
test   %rax,%rax
js     12c2 <main+0x102>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
test   %ebp,%ebp
je     1272 <main+0xb2>
lea    0xe79(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
lea    0x10(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   10b0 <read@plt>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
jmp    1270 <main+0xb0>
lea    0xd87(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
mov    0xc(%rsp),%ebx
lea    0xdaf(%rip),%rdi
call   1060 <strlen@plt>
lea    0xd83(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xdb2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
nopl   (%rax)
jmp    12c0 <main+0x100>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"\0A[Main]: Started default program...\0A\00"
@.str.1 = [37 x i8] c"\0AFailed to create a pipe..exiting..\0A\00"
@.str.2 = [39 x i8] c"\0A[Child]: own pid:%u, Parent id:%u ..\0A\00"
@.str.3 = [32 x i8] c"Hello World from Child process\0A\00"
@.str.4 = [31 x i8] c"Hello World from Child process\00"
@.str.5 = [40 x i8] c"\0A wrote %u bytes from child process...\0A\00"
@.str.6 = [40 x i8] c"\0A[Parent]: own pid:%u, Parent id:%u ..\0A\00"
@.str.7 = [37 x i8] c"\0A Got from child %u bytes: [%s] ...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%17 = bitcast [2 x i32]* %6 to i8*
store i32 0, i32* %7
store i32 0, i32* %8
store i64 -1, i64* %9
store i32 0, i32* %10
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %10
br label %27
27: ; preds = %22, %2
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = call i64 @pipe(i32* %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %27
%36 = call i32 (...) @fork()
%37 = icmp ne i32 %36, 0
br i1 %37, label %54, label %38
38: ; preds = %35
%39 = call i32 (...) @getpid()
store i32 %39, i32* %11
%40 = call i32 (...) @getppid()
store i32 %40, i32* %8
store i32 0, i32* %12
%41 = load i32, i32* %11
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %41, i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%48 = load i32, i32* %47
%49 = call i32 @strlen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @write(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %49)
store i32 %50, i32* %12
%51 = load i32, i32* %12
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %38, %53
br label %53
54: ; preds = %35
%55 = call i32 (...) @getpid()
store i32 %55, i32* %13
%56 = call i32 (...) @getppid()
store i32 %56, i32* %8
%57 = load i32, i32* %13
%58 = load i32, i32* %8
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %57, i32 %58)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
store i32 0, i32* %14
%63 = bitcast [1024 x i8]* %15 to i8*
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%65 = load i32, i32* %64
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 1024)
store i32 %67, i32* %14
%68 = load i32, i32* %14
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 %68, i8* %69)
br label %71
71: ; preds = %54, %71
br label %71
}

LLVM IR (-O2):
@.str.2 = [39 x i8] c"\0A[Child]: own pid:%u, Parent id:%u ..\0A\00"
@.str.3 = [32 x i8] c"Hello World from Child process\0A\00"
@.str.4 = [31 x i8] c"Hello World from Child process\00"
@.str.5 = [40 x i8] c"\0A wrote %u bytes from child process...\0A\00"
@.str.6 = [40 x i8] c"\0A[Parent]: own pid:%u, Parent id:%u ..\0A\00"
@.str.7 = [37 x i8] c"\0A Got from child %u bytes: [%s] ...\0A\00"
@str = [36 x i8] c"\0A[Main]: Started default program...\00"
@str.8 = [36 x i8] c"\0AFailed to create a pipe..exiting..\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to [2 x i32]*
%5 = alloca [1024 x i8]
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
%7 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%8 = bitcast i64* %3 to i32*
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %2
%15 = call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @getppid()
br i1 %16, label %19, label %29
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18)
%21 = load i32, i32* %8
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @strlen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @write(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %28, %19
br label %28
29: ; preds = %14
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %17, i32 %18)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = load i32, i32* %8
%36 = call i32 @read(i32 %35, i8* %34, i32 1024)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i32 %36, i8* %34)
br label %38
38: ; preds = %38, %29
br label %38
}