#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 /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printMenu();
int /*<<< orphan*/ printProcessesID(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ runSelected(int);
int /*<<< orphan*/ scanf(char *, int *);
int wait(int *);
void app() {
  int select = 0, status;
  pid_t currentPid, childPid, ppid, tempPid;
  currentPid = getpid();
  ppid = getppid();
  for (;;) {
    printf("Current process ID: %d, Parent process ID: %d\n", currentPid, ppid);
    printMenu();
    scanf("%d", &select);
    if (select >= 4) {
      printf("App end\n");
      exit(0);
    } else {
      tempPid = fork();
      if (tempPid == 0) {
        currentPid = getpid();
        ppid = getppid();
        printProcessesID(currentPid, ppid);
        runSelected(select);
        exit(0);
      } else if (tempPid == -1) {
        printf("Failed to Fork.");
      } else {
        childPid = wait(&status);
        printf("\n\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<app>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <printMenu@plt>
lea    0xe4a(%rip),%rdi
lea    -0x4(%rbp),%rsi
call   1080 <scanf@plt>
cmpl   $0x4,-0x4(%rbp)
jl     120d <app+0x6d>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    124f <app+0xaf>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1090 <getppid@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1060 <printProcessesID@plt>
mov    -0x4(%rbp),%edi
call   1030 <runSelected@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    126c <app+0xcc>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1286 <app+0xe6>
lea    -0x8(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x10(%rbp)
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128b <app+0xeb>
jmp    1290 <app+0xf0>
jmp    11c3 <app+0x23>

x86 assembly (clang, -O2):
<app>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %eax,%ebp
lea    0xe25(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <printMenu@plt>
lea    0xe3b(%rip),%rdi
mov    %rsp,%rsi
call   1090 <scanf@plt>
cmpl   $0x3,(%rsp)
jle    1215 <app+0x65>
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xde4(%rip),%r15
lea    0xe0c(%rip),%r12
mov    %rsp,%r13
lea    0xe00(%rip),%r14
jmp    1264 <app+0xb4>
nop
lea    0xdfb(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <printMenu@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1090 <scanf@plt>
cmpl   $0x3,(%rsp)
jg     1202 <app+0x52>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     1230 <app+0x80>
test   %eax,%eax
je     1288 <app+0xd8>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    123e <app+0x8e>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <getppid@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1070 <printProcessesID@plt>
mov    (%rsp),%edi
call   1030 <runSelected@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"Current process ID: %d, Parent process ID: %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [9 x i8] c"App end\0A\00"
@.str.3 = [16 x i8] c"Failed to Fork.\00"
@.str.4 = [3 x i8] c"\0A\0A\00"
define void @app() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %3
%8 = call i32 (...) @getppid()
store i32 %8, i32* %5
br label %9
9: ; preds = %43, %0
%10 = load i32, i32* %3
%11 = load i32, i32* %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = call i32 (...) @printMenu()
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%15 = load i32, i32* %1
%16 = icmp sge i32 %15, 4
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %9
%21 = call i32 (...) @fork()
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = call i32 (...) @getpid()
store i32 %25, i32* %3
%26 = call i32 (...) @getppid()
store i32 %26, i32* %5
%27 = load i32, i32* %3
%28 = load i32, i32* %5
%29 = call i32 @printProcessesID(i32 %27, i32 %28)
%30 = load i32, i32* %1
%31 = call i32 @runSelected(i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %20
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %41
38: ; preds = %33
%39 = call i32 @wait(i32* %2)
store i32 %39, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %38, %36
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
br label %9
}

LLVM IR (-O2):
@.str = [47 x i8] c"Current process ID: %d, Parent process ID: %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [16 x i8] c"Failed to Fork.\00"
@str = [2 x i8] c"\0A\00"
@str.5 = [8 x i8] c"App end\00"
define void @app() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (...) @getppid()
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = call i32 (...) @printMenu()
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 3
br i1 %11, label %12, label %15
12: ; preds = %29, %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.5, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %0, %29
%16 = call i32 (...) @fork()
switch i32 %16, label %26 [
i32 0, label %17
i32 -1, label %24
]
17: ; preds = %15
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getppid()
%20 = call i32 @printProcessesID(i32 %18, i32 %19)
%21 = load i32, i32* %1
%22 = call i32 @runSelected(i32 %21)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %29
26: ; preds = %15
%27 = call i32 @wait(i32* %2)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %26, %24
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%31 = call i32 (...) @printMenu()
%32 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%33 = load i32, i32* %1
%34 = icmp sgt i32 %33, 3
br i1 %34, label %12, label %15
}