#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_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  printf("test parent and child process relation\n");
  pid_t pid;
  pid = fork();
  if (pid == -1) {
    printf("fork error\n");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    printf("5140209064-child is %d\n", getpid());
    execl("/data/project/pstree", "2", "100", (char *)0);
    _exit(EXIT_SUCCESS);
  } else {
    waitpid(pid, NULL, 0);
    printf("5140209064-parent is %d\n", getpid());
  }
  wait(1);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cd <main+0x4d>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    121a <main+0x9a>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe57(%rip),%rdi
lea    0xe65(%rip),%rsi
lea    0xe60(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <execl@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1244 <main+0xc4>
mov    $0x1,%edi
call   1090 <wait@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xeb4(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11e4 <main+0x54>
cmp    $0xffffffff,%eax
je     1225 <main+0x95>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe6d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <wait@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe0e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe16(%rip),%rdi
lea    0xe24(%rip),%rsi
lea    0xe1f(%rip),%rdx
xor    %ecx,%ecx
call   1090 <execl@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xe47(%rip),%rdi
call   1040 <puts@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"test parent and child process relation\0A\00"
@.str.1 = [12 x i8] c"fork error\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"5140209064-child is %d\0A\00"
@.str.3 = [21 x i8] c"/data/project/pstree\00"
@.str.4 = [2 x i8] c"2\00"
@.str.5 = [4 x i8] c"100\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [25 x i8] c"5140209064-parent is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @execl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* null)
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @_exit(i32 %18)
unreachable
20: ; preds = %11
%21 = load i32, i32* %2
%22 = call i32 @waitpid(i32 %21, i32* null, i32 0)
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %20
br label %26
26: ; preds = %25
%27 = call i32 @wait(i32 1)
%28 = load i32, i32* @EXIT_SUCCESS
ret i32 %28
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"5140209064-child is %d\0A\00"
@.str.3 = [21 x i8] c"/data/project/pstree\00"
@.str.4 = [2 x i8] c"2\00"
@.str.5 = [4 x i8] c"100\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [25 x i8] c"5140209064-parent is %d\0A\00"
@str = [39 x i8] c"test parent and child process relation\00"
@str.7 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %13 [
i32 -1, label %3
i32 0, label %7
]
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = tail call i32 @execl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* null)
%11 = load i32, i32* @EXIT_SUCCESS
%12 = tail call i32 @_exit(i32 %11)
unreachable
13: ; preds = %0
%14 = tail call i32 @waitpid(i32 %2, i32* null, i32 0)
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %15)
%17 = tail call i32 @wait(i32 1)
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}