#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 /*<<< orphan*/ i8ProcName;

/* Variables and functions */
int /*<<< orphan*/ cfgmng_get_taskname(int, char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getppid();
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int MonitorParentProcessDead() {
  int pid = -1;
  int ppid = -1;
  char i8ProcName[256];
  memset(i8ProcName, 0x0, sizeof(i8ProcName));
  pid = fork();
  if (pid < 0) {
    perror("fork error");
  } else if (0 == pid) {
    printf("I am child\n");
    while (1) {
      ppid = getppid();
      printf("parent id:%d\n", ppid);
      cfgmng_get_taskname(ppid, i8ProcName, 256);
      printf("Parent Process died!:%s\n", i8ProcName);
      if (1 == ppid) {
        printf("Parent Process died!\n");
        sleep(10);
        exit(0);
      }
      sleep(1);
    }
  }
  printf("child id:%d\n", pid);
  return 0;
}


x86 assembly (clang, -O0):
<MonitorParentProcessDead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0xffffffff,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d1 <MonitorParentProcessDead+0x51>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
jmp    126f <MonitorParentProcessDead+0xef>
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jne    126a <MonitorParentProcessDead+0xea>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <cfgmng_get_taskname@plt>
lea    -0x110(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%eax
cmp    -0x8(%rbp),%eax
jne    125b <MonitorParentProcessDead+0xdb>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11ea <MonitorParentProcessDead+0x6a>
jmp    126f <MonitorParentProcessDead+0xef>
mov    -0x4(%rbp),%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MonitorParentProcessDead>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11db <MonitorParentProcessDead+0x4b>
je     11e9 <MonitorParentProcessDead+0x59>
lea    0xe70(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
jmp    11bb <MonitorParentProcessDead+0x2b>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebx
lea    0xe06(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1060 <cfgmng_get_taskname@plt>
lea    0xdf2(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%ebx
je     1290 <MonitorParentProcessDead+0x100>
lea    0xdce(%rip),%r14
mov    %rsp,%rbx
lea    0xdd2(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1060 <cfgmng_get_taskname@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%ebp
jne    1250 <MonitorParentProcessDead+0xc0>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [12 x i8] c"I am child\0A\00"
@.str.2 = [14 x i8] c"parent id:%d\0A\00"
@.str.3 = [25 x i8] c"Parent Process died!:%s\0A\00"
@.str.4 = [22 x i8] c"Parent Process died!\0A\00"
@.str.5 = [13 x i8] c"child id:%d\0A\00"
define i32 @MonitorParentProcessDead() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
store i32 -1, i32* %1
store i32 -1, i32* %2
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 256)
%6 = call i32 (...) @fork()
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %34
11: ; preds = %0
%12 = load i32, i32* %1
%13 = icmp eq i32 0, %12
br i1 %13, label %14, label %33
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %31
%17 = call i32 (...) @getppid()
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%22 = call i32 @cfgmng_get_taskname(i32 %20, i8* %21, i32 256)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %2
%26 = icmp eq i32 1, %25
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @sleep(i32 10)
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %16
%32 = call i32 @sleep(i32 1)
br label %16
33: ; preds = %11
br label %34
34: ; preds = %33, %9
%35 = load i32, i32* %1
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.2 = [14 x i8] c"parent id:%d\0A\00"
@.str.3 = [25 x i8] c"Parent Process died!:%s\0A\00"
@.str.5 = [13 x i8] c"child id:%d\0A\00"
@str = [11 x i8] c"I am child\00"
@str.6 = [21 x i8] c"Parent Process died!\00"
define i32 @MonitorParentProcessDead() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 256)
%4 = call i32 (...) @fork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %28
8: ; preds = %0
%9 = icmp eq i32 %4, 0
br i1 %9, label %10, label %28
10: ; preds = %8
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%12 = call i32 (...) @getppid()
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 @cfgmng_get_taskname(i32 %12, i8* %2, i32 256)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%16 = icmp eq i32 %12, 1
br i1 %16, label %17, label %21
17: ; preds = %21, %10
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%19 = call i32 @sleep(i32 10)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %10, %21
%22 = call i32 @sleep(i32 1)
%23 = call i32 (...) @getppid()
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @cfgmng_get_taskname(i32 %23, i8* %2, i32 256)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%27 = icmp eq i32 %23, 1
br i1 %27, label %17, label %21
28: ; preds = %8, %6
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %4)
ret i32 0
}