#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  pid_t proc_id = fork();
  if (proc_id == -1) {
    fprintf(stderr, "Fork failure\n");
    exit(1);
  }
  fprintf(stdout, "Hello form process %ld\n", (long)getpid());
  if (proc_id == 0) {
    fprintf(stdout, "CHILD : PID=%ld, Parent PID=%ld\n", (long)getpid(),
            (long)getppid());
  } else {
    fprintf(stdout,
            "PARENT : PID=%ld, Parent PID=%ld, Child PID (fork return)=%ld\n",
            (long)getpid(), (long)getppid(), (long)proc_id);
  }
  fprintf(stdout, "Goodbye form process %ld\n", (long)getpid());
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119b <main+0x4b>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1207 <main+0xb7>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1241 <main+0xf1>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
movslq -0x14(%rbp),%r8
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffff,%eax
je     1202 <main+0xb2>
mov    %eax,%ebx
mov    0x2e68(%rip),%r15
mov    (%r15),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8d(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <getppid@plt>
test   %ebx,%ebx
je     11c1 <main+0x71>
movslq %ebx,%r8
lea    0xe98(%rip),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11d8 <main+0x88>
lea    0xe5e(%rip),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%r15),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failure\0A\00"
@stdout = global i32 0
@.str.1 = [24 x i8] c"Hello form process %ld\0A\00"
@.str.2 = [33 x i8] c"CHILD : PID=%ld, Parent PID=%ld\0A\00"
@.str.3 = [63 x i8] c"PARENT : PID=%ld, Parent PID=%ld, Child PID (fork return)=%ld\0A\00"
@.str.4 = [26 x i8] c"Goodbye form process %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @stdout
%16 = call i64 (...) @getpid()
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32, i32* @stdout
%22 = call i64 (...) @getpid()
%23 = call i64 (...) @getppid()
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %22, i64 %23)
br label %32
25: ; preds = %14
%26 = load i32, i32* @stdout
%27 = call i64 (...) @getpid()
%28 = call i64 (...) @getppid()
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i64 %27, i64 %28, i64 %30)
br label %32
32: ; preds = %25, %20
%33 = load i32, i32* @stdout
%34 = call i64 (...) @getpid()
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %34)
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"Fork failure\0A\00"
@stdout = global i32 0
@.str.1 = [24 x i8] c"Hello form process %ld\0A\00"
@.str.2 = [33 x i8] c"CHILD : PID=%ld, Parent PID=%ld\0A\00"
@.str.3 = [63 x i8] c"PARENT : PID=%ld, Parent PID=%ld, Child PID (fork return)=%ld\0A\00"
@.str.4 = [26 x i8] c"Goodbye form process %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @stdout
%11 = tail call i64 (...) @getpid()
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%13 = icmp eq i32 %3, 0
%14 = load i32, i32* @stdout
%15 = tail call i64 (...) @getpid()
%16 = tail call i64 (...) @getppid()
br i1 %13, label %17, label %19
17: ; preds = %9
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 %15, i64 %16)
br label %22
19: ; preds = %9
%20 = sext i32 %3 to i64
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i64 %15, i64 %16, i64 %20)
br label %22
22: ; preds = %19, %17
%23 = load i32, i32* @stdout
%24 = tail call i64 (...) @getpid()
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %24)
ret i32 0
}