#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ sigset_t;
struct TYPE_3__ {
  scalar_t__ si_signo;
};
typedef TYPE_1__ siginfo_t;

/* Variables and functions */
scalar_t__ SIGCHLD;
scalar_t__ SIGINT;
scalar_t__ SIGQUIT;
scalar_t__ SIGTERM;
scalar_t__ SIGUSR1;
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SetPidFile(char *);
int WEXITSTATUS(int);
int WorkProc();
int /*<<< orphan*/ WriteLog(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ kill(int, scalar_t__);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwaitinfo(int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strsignal(scalar_t__);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ wait(int *);
int MonitorProc() {
  int pid;
  int status;
  int need_start = 1;
  sigset_t sigset;
  siginfo_t siginfo;
  sigemptyset(&sigset);
  sigaddset(&sigset, SIGQUIT);
  sigaddset(&sigset, SIGINT);
  sigaddset(&sigset, SIGTERM);
  sigaddset(&sigset, SIGCHLD);
  sigaddset(&sigset, SIGCHLD);
  sigaddset(&sigset, SIGUSR1);
  sigprocmask(SIG_BLOCK, &sigset, NULL);
  SetPidFile("/var/run/my_daemon.pid");
  for (;;) {
    if (need_start) {
      pid = fork();
    }
    need_start = 1;
    if (pid == -1) {
      WriteLog("[MONITOR] Fork failed (%s)\n", strerror(errno));
    } else if (!pid) {
      status = WorkProc();
      exit(status);
    } else {
      sigwaitinfo(&sigset, &siginfo);
      if (siginfo.si_signo == SIGCHLD) {
        wait(&status);
        status = WEXITSTATUS(status);
        if (status == 2) {
          WriteLog("[MONITOR] Childer stopped\n");
          break;
        } else if (status == 1) {
          WriteLog("[MONITOR] Childer restart\n");
        }
      } else if (siginfo.si_signo == SIGUSR1) {
        kill(pid, SIGUSR1);
        need_start = 0;
      } else {
        WriteLog("[MONITOR] Signal %s\n", strsignal(siginfo.si_signo));
        kill(pid, SIGTERM);
        status = 0;
        break;
      }
    }
  }
  WriteLog("[MONITOR] Stopped\n");
  unlink("/var/run/my_daemon.pid");
  return status;
}


x86 assembly (clang, -O0):
<MonitorProc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <sigemptyset@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%rsi
lea    -0x10(%rbp),%rdi
call   1100 <sigaddset@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
lea    0xd69(%rip),%rdi
call   1060 <SetPidFile@plt>
cmpl   $0x0,-0xc(%rbp)
je     12b0 <MonitorProc+0xc0>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    12e4 <MonitorProc+0xf4>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %eax,%esi
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1050 <WriteLog@plt>
jmp    13ec <MonitorProc+0x1fc>
cmpl   $0x0,-0x4(%rbp)
jne    1300 <MonitorProc+0x110>
mov    $0x0,%al
call   10c0 <WorkProc@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   10d0 <exit@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <sigwaitinfo@plt>
mov    -0x18(%rbp),%rax
mov    0x2cb0(%rip),%rcx
cmp    (%rcx),%rax
jne    1374 <MonitorProc+0x184>
lea    -0x8(%rbp),%rdi
call   10f0 <wait@plt>
mov    -0x8(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1352 <MonitorProc+0x162>
lea    0xced(%rip),%rdi
mov    $0x0,%al
call   1050 <WriteLog@plt>
jmp    13f1 <MonitorProc+0x201>
cmpl   $0x1,-0x8(%rbp)
jne    136a <MonitorProc+0x17a>
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   1050 <WriteLog@plt>
jmp    136f <MonitorProc+0x17f>
jmp    13e2 <MonitorProc+0x1f2>
mov    -0x18(%rbp),%rax
mov    0x2c69(%rip),%rcx
cmp    (%rcx),%rax
jne    13a6 <MonitorProc+0x1b6>
mov    -0x4(%rbp),%edi
mov    0x2c56(%rip),%rax
mov    (%rax),%rsi
call   1080 <kill@plt>
movl   $0x0,-0xc(%rbp)
jmp    13dd <MonitorProc+0x1ed>
mov    -0x18(%rbp),%rdi
call   10a0 <strsignal@plt>
mov    %eax,%esi
lea    0xcb1(%rip),%rdi
mov    $0x0,%al
call   1050 <WriteLog@plt>
mov    -0x4(%rbp),%edi
mov    0x2c0f(%rip),%rax
mov    (%rax),%rsi
call   1080 <kill@plt>
movl   $0x0,-0x8(%rbp)
jmp    13f1 <MonitorProc+0x201>
jmp    13e2 <MonitorProc+0x1f2>
jmp    13e7 <MonitorProc+0x1f7>
jmp    13ec <MonitorProc+0x1fc>
jmp    129c <MonitorProc+0xac>
lea    0xc86(%rip),%rdi
mov    $0x0,%al
call   1050 <WriteLog@plt>
lea    0xbfa(%rip),%rdi
call   1040 <unlink@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MonitorProc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sigemptyset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2d80(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x0(%rbp),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1100 <sigaddset@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%r13
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
lea    0xd71(%rip),%rdi
call   1060 <SetPidFile@plt>
mov    0x2d1d(%rip),%r12
lea    0xd75(%rip),%r15
lea    0x8(%rsp),%rbx
xor    %r14d,%r14d
jmp    12c8 <MonitorProc+0xd8>
nopl   0x0(%rax)
mov    (%r12),%edi
call   10e0 <strerror@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <WriteLog@plt>
test   $0x1,%r14b
je     1320 <MonitorProc+0x130>
cmp    $0xffffffff,%ebp
je     12b0 <MonitorProc+0xc0>
test   %ebp,%ebp
je     13c9 <MonitorProc+0x1d9>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1090 <sigwaitinfo@plt>
mov    0x8(%rsp),%rsi
mov    0x2cd6(%rip),%rax
cmp    (%rax),%rsi
jne    1330 <MonitorProc+0x140>
lea    0x4(%rsp),%rdi
call   10f0 <wait@plt>
mov    0x4(%rsp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,0x4(%rsp)
xor    %r14d,%r14d
cmp    $0x1,%eax
je     1348 <MonitorProc+0x158>
cmp    $0x2,%eax
jne    12c8 <MonitorProc+0xd8>
jmp    138e <MonitorProc+0x19e>
nopl   (%rax)
xor    %eax,%eax
call   1110 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%ebp
jne    12d3 <MonitorProc+0xe3>
jmp    12b0 <MonitorProc+0xc0>
mov    0x2cb1(%rip),%rax
cmp    (%rax),%rsi
jne    135b <MonitorProc+0x16b>
mov    %ebp,%edi
call   1080 <kill@plt>
mov    $0x1,%r14b
jmp    12c8 <MonitorProc+0xd8>
lea    0xcff(%rip),%rdi
xor    %eax,%eax
call   1050 <WriteLog@plt>
jmp    12c8 <MonitorProc+0xd8>
mov    %rsi,%rdi
call   10a0 <strsignal@plt>
lea    0xcff(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <WriteLog@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%rsi
mov    %ebp,%edi
call   1080 <kill@plt>
movl   $0x0,0x4(%rsp)
jmp    139c <MonitorProc+0x1ac>
lea    0xc9e(%rip),%rdi
xor    %eax,%eax
call   1050 <WriteLog@plt>
lea    0xcdb(%rip),%rdi
xor    %eax,%eax
call   1050 <WriteLog@plt>
lea    0xc4f(%rip),%rdi
call   1040 <unlink@plt>
mov    0x4(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10c0 <WorkProc@plt>
mov    %eax,0x4(%rsp)
mov    %eax,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64 }
@SIGQUIT = global i64 0
@SIGINT = global i64 0
@SIGTERM = global i64 0
@SIGCHLD = global i64 0
@SIGUSR1 = global i64 0
@SIG_BLOCK = global i32 0
@.str = [23 x i8] c"/var/run/my_daemon.pid\00"
@.str.1 = [28 x i8] c"[MONITOR] Fork failed (%s)\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"[MONITOR] Childer stopped\0A\00"
@.str.3 = [27 x i8] c"[MONITOR] Childer restart\0A\00"
@.str.4 = [21 x i8] c"[MONITOR] Signal %s\0A\00"
@.str.5 = [19 x i8] c"[MONITOR] Stopped\0A\00"
define i32 @MonitorProc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.TYPE_3__
store i32 1, i32* %3
%6 = call i32 @sigemptyset(i32* %4)
%7 = load i64, i64* @SIGQUIT
%8 = call i32 @sigaddset(i32* %4, i64 %7)
%9 = load i64, i64* @SIGINT
%10 = call i32 @sigaddset(i32* %4, i64 %9)
%11 = load i64, i64* @SIGTERM
%12 = call i32 @sigaddset(i32* %4, i64 %11)
%13 = load i64, i64* @SIGCHLD
%14 = call i32 @sigaddset(i32* %4, i64 %13)
%15 = load i64, i64* @SIGCHLD
%16 = call i32 @sigaddset(i32* %4, i64 %15)
%17 = load i64, i64* @SIGUSR1
%18 = call i32 @sigaddset(i32* %4, i64 %17)
%19 = load i32, i32* @SIG_BLOCK
%20 = call i32 @sigprocmask(i32 %19, i32* %4, i32* null)
%21 = call i32 @SetPidFile(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %82, %0
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 (...) @fork()
store i32 %26, i32* %1
br label %27
27: ; preds = %25, %22
store i32 1, i32* %3
%28 = load i32, i32* %1
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %82
34: ; preds = %27
%35 = load i32, i32* %1
%36 = icmp ne i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = call i32 (...) @WorkProc()
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34
%42 = call i32 @sigwaitinfo(i32* %4, %struct.TYPE_3__* %5)
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%44 = load i64, i64* %43
%45 = load i64, i64* @SIGCHLD
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %62
47: ; preds = %41
%48 = call i32 @wait(i32* %2)
%49 = load i32, i32* %2
%50 = call i32 @WEXITSTATUS(i32 %49)
store i32 %50, i32* %2
%51 = load i32, i32* %2
%52 = icmp eq i32 %51, 2
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %83
55: ; preds = %47
%56 = load i32, i32* %2
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
br label %61
61: ; preds = %60
br label %80
62: ; preds = %41
%63 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%64 = load i64, i64* %63
%65 = load i64, i64* @SIGUSR1
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = load i32, i32* %1
%69 = load i64, i64* @SIGUSR1
%70 = call i32 @kill(i32 %68, i64 %69)
store i32 0, i32* %3
br label %79
71: ; preds = %62
%72 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %5, i32 0, i32 0
%73 = load i64, i64* %72
%74 = call i32 @strsignal(i64 %73)
%75 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = load i32, i32* %1
%77 = load i64, i64* @SIGTERM
%78 = call i32 @kill(i32 %76, i64 %77)
store i32 0, i32* %2
br label %83
79: ; preds = %67
br label %80
80: ; preds = %79, %61
br label %81
81: ; preds = %80
br label %82
82: ; preds = %81, %30
br label %22
83: ; preds = %71, %53
%84 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%85 = call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64 }
@SIGQUIT = global i64 0
@SIGINT = global i64 0
@SIGTERM = global i64 0
@SIGCHLD = global i64 0
@SIGUSR1 = global i64 0
@SIG_BLOCK = global i32 0
@.str = [23 x i8] c"/var/run/my_daemon.pid\00"
@.str.1 = [28 x i8] c"[MONITOR] Fork failed (%s)\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"[MONITOR] Childer stopped\0A\00"
@.str.3 = [27 x i8] c"[MONITOR] Childer restart\0A\00"
@.str.4 = [21 x i8] c"[MONITOR] Signal %s\0A\00"
@.str.5 = [19 x i8] c"[MONITOR] Stopped\0A\00"
define i32 @MonitorProc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.TYPE_3__
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.TYPE_3__* %3 to i8*
%7 = call i32 @sigemptyset(i32* %2)
%8 = load i64, i64* @SIGQUIT
%9 = call i32 @sigaddset(i32* %2, i64 %8)
%10 = load i64, i64* @SIGINT
%11 = call i32 @sigaddset(i32* %2, i64 %10)
%12 = load i64, i64* @SIGTERM
%13 = call i32 @sigaddset(i32* %2, i64 %12)
%14 = load i64, i64* @SIGCHLD
%15 = call i32 @sigaddset(i32* %2, i64 %14)
%16 = load i64, i64* @SIGCHLD
%17 = call i32 @sigaddset(i32* %2, i64 %16)
%18 = load i64, i64* @SIGUSR1
%19 = call i32 @sigaddset(i32* %2, i64 %18)
%20 = load i32, i32* @SIG_BLOCK
%21 = call i32 @sigprocmask(i32 %20, i32* %2, i32* null)
%22 = call i32 @SetPidFile(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 0
br label %24
24: ; preds = %47, %0
%25 = phi i32 [ undef, %0 ], [ %30, %47 ]
%26 = phi i1 [ false, %0 ], [ %48, %47 ]
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 (...) @fork()
br label %29
29: ; preds = %27, %24
%30 = phi i32 [ %28, %27 ], [ %25, %24 ]
switch i32 %30, label %38 [
i32 -1, label %31
i32 0, label %35
]
31: ; preds = %29
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %33)
br label %47
35: ; preds = %29
%36 = call i32 (...) @WorkProc()
store i32 %36, i32* %1
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = call i32 @sigwaitinfo(i32* %2, %struct.TYPE_3__* %3)
%40 = load i64, i64* %23
%41 = load i64, i64* @SIGCHLD
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = call i32 @wait(i32* %1)
%45 = load i32, i32* %1
%46 = call i32 @WEXITSTATUS(i32 %45)
store i32 %46, i32* %1
switch i32 %46, label %47 [
i32 2, label %49
i32 1, label %51
]
47: ; preds = %43, %51, %56, %31
%48 = phi i1 [ false, %43 ], [ false, %51 ], [ true, %56 ], [ false, %31 ]
br label %24
49: ; preds = %43
%50 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %63
51: ; preds = %43
%52 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %47
53: ; preds = %38
%54 = load i64, i64* @SIGUSR1
%55 = icmp eq i64 %40, %54
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @kill(i32 %30, i64 %40)
br label %47
58: ; preds = %53
%59 = call i32 @strsignal(i64 %40)
%60 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = load i64, i64* @SIGTERM
%62 = call i32 @kill(i32 %30, i64 %61)
store i32 0, i32* %1
br label %63
63: ; preds = %58, %49
%64 = call i32 (i8*, ...) @WriteLog(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%66 = load i32, i32* %1
ret i32 %66
}