#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 int int32_t;

/* Variables and functions */
int atoi(char *);
int execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  printf("\n[Parent]: Started default program...\n");
  uint32_t cpid = 0, ppid = 0;
  int32_t exec = -1;
  if (argv[1])
    exec = atoi(argv[1]);
  if (!fork()) {
    pid_t pid = getpid();
    ppid = getppid();
    int32_t ret = 0;
    printf("\n[Child]: own pid:%u, Parent id:%u ..\n", pid, ppid);
    if (exec == 1) {
      ret = execl("./child", "./child", NULL);
      if (ret < 0) {
        printf("\nexecl failed...\n");
        exit(0);
      }
    } else {
      while (1)
        continue;
    }
  } else {
    pid_t pid = getpid();
    ppid = getppid();
    printf("\n[Parent]: own pid:%u, Parent id:%u ..\n", pid, ppid);
    while (1)
      continue;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11c8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    125a <main+0xea>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    124b <main+0xdb>
lea    0xe36(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1246 <main+0xd6>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1255 <main+0xe5>
jmp    1250 <main+0xe0>
jmp    1250 <main+0xe0>
jmp    1287 <main+0x117>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1282 <main+0x112>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
test   %rdi,%rdi
je     11a6 <main+0x26>
call   1060 <atoi@plt>
mov    %eax,%r14d
jmp    11ac <main+0x2c>
mov    $0xffffffff,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
test   %ebx,%ebx
je     11e2 <main+0x62>
lea    0xe5f(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
nopl   0x0(%rax,%rax,1)
jmp    11e0 <main+0x60>
lea    0xe17(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%r14d
jne    1220 <main+0xa0>
lea    0xe26(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1090 <execl@plt>
test   %eax,%eax
js     1222 <main+0xa2>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
jmp    1220 <main+0xa0>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"\0A[Parent]: Started default program...\0A\00"
@.str.1 = [39 x i8] c"\0A[Child]: own pid:%u, Parent id:%u ..\0A\00"
@.str.2 = [8 x i8] c"./child\00"
@.str.3 = [18 x i8] c"\0Aexecl failed...\0A\00"
@.str.4 = [40 x i8] c"\0A[Parent]: own pid:%u, Parent id:%u ..\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
store i32 0, i32* %7
store i32 -1, i32* %8
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %8
br label %22
22: ; preds = %17, %2
%23 = call i32 (...) @fork()
%24 = icmp ne i32 %23, 0
br i1 %24, label %44, label %25
25: ; preds = %22
%26 = call i32 (...) @getpid()
store i32 %26, i32* %9
%27 = call i32 (...) @getppid()
store i32 %27, i32* %7
store i32 0, i32* %10
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %41
33: ; preds = %25
%34 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* null)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %33
br label %43
41: ; preds = %25
br label %42
42: ; preds = %41, %42
br label %42
43: ; preds = %40
br label %51
44: ; preds = %22
%45 = call i32 (...) @getpid()
store i32 %45, i32* %11
%46 = call i32 (...) @getppid()
store i32 %46, i32* %7
%47 = load i32, i32* %11
%48 = load i32, i32* %7
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %47, i32 %48)
br label %50
50: ; preds = %44, %50
br label %50
51: ; preds = %43
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [39 x i8] c"\0A[Child]: own pid:%u, Parent id:%u ..\0A\00"
@.str.2 = [8 x i8] c"./child\00"
@.str.4 = [40 x i8] c"\0A[Parent]: own pid:%u, Parent id:%u ..\0A\00"
@str = [38 x i8] c"\0A[Parent]: Started default program...\00"
@str.5 = [17 x i8] c"\0Aexecl failed...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @atoi(i8* %5)
br label %9
9: ; preds = %7, %2
%10 = phi i32 [ %8, %7 ], [ -1, %2 ]
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (...) @getppid()
br i1 %12, label %15, label %26
15: ; preds = %9
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%17 = icmp eq i32 %10, 1
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* null)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %15, %24
br label %24
25: ; preds = %18
ret i32 0
26: ; preds = %9
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %13, i32 %14)
br label %28
28: ; preds = %28, %26
br label %28
}