#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*/ WUNTRACED;
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, scalar_t__);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main() {
  pid_t child_pid;
  pid_t wait_result;
  int stat_loc;
  child_pid = fork();
  if (child_pid == 0) {
    printf("### Child ###\nCurrent PID: %d and Child PID: %d\n", getpid(),
           child_pid);
    sleep(1);
  } else {
    wait_result = waitpid(child_pid, &stat_loc, WUNTRACED);
    printf("### Parent ###\nCurrent PID: %d and Child PID: %d\n", getpid(),
           child_pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119d <main+0x4d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11d0 <main+0x80>
mov    -0x10(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1194 <main+0x44>
mov    %rax,%rbx
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <waitpid@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea9(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
jmp    11b5 <main+0x65>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [49 x i8] c"### Child ###\0ACurrent PID: %d and Child PID: %d\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [50 x i8] c"### Parent ###\0ACurrent PID: %d and Child PID: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = call i32 (...) @getpid()
%10 = load i64, i64* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %9, i64 %10)
%12 = call i32 @sleep(i32 1)
br label %20
13: ; preds = %0
%14 = load i64, i64* %2
%15 = load i32, i32* @WUNTRACED
%16 = call i64 @waitpid(i64 %14, i32* %4, i32 %15)
store i64 %16, i64* %3
%17 = call i32 (...) @getpid()
%18 = load i64, i64* %2
%19 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %17, i64 %18)
br label %20
20: ; preds = %13, %8
ret i32 0
}

LLVM IR (-O2):
@.str = [49 x i8] c"### Child ###\0ACurrent PID: %d and Child PID: %d\0A\00"
@WUNTRACED = global i32 0
@.str.1 = [50 x i8] c"### Parent ###\0ACurrent PID: %d and Child PID: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %6, i64 0)
%8 = tail call i32 @sleep(i32 1)
br label %14
9: ; preds = %0
%10 = load i32, i32* @WUNTRACED
%11 = call i64 @waitpid(i64 %3, i32* %1, i32 %10)
%12 = call i32 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %12, i64 %3)
br label %14
14: ; preds = %9, %5
ret i32 0
}