// File: /workspace/github_cpp_makefile_star2_O3/wangyufei95[P]watchdog_linux/watchdog.cpp
// Function: Supervisor::run(int, char**)
int Supervisor::run(int argc, char* argv[]) 
{
  if (!parseArgs(argc, argv)) 
  {
    return EXIT_FAILURE;
  }

  if (!pidfile_.empty()) 
  {
    logger()->info("writing watchdog-PID %d to %s", getpid(),
                   pidfile_.c_str());

    std::ofstream fs(pidfile_, std::ios_base::out | std::ios_base::trunc);
    fs << getpid() << std::endl;
  }

  program_->start();
  for (;;) 
  {
	cpuuse();
    int status = 0;
    pid_t pid = waitpid(-1, &status, 0);
    if (pid < 0) 
	{
      perror("waitpid");
      return EXIT_FAILURE;
    }

    if (pid != program_->pid()) 
	{
      logger()->info("Reaping child PID %d", pid);
      continue;
    }

    if (WIFEXITED(status)) 
	{
      exitCode_ = WEXITSTATUS(status);

      logger()->info("program PID %d terminated normally with exit code %d",
                     program_->pid(), exitCode_);

      if (program_->resume()) 
	  {
        logger()->info("reattaching to child PID %d.", program_->pid());
        continue;
      }

      if (!isExitSuccess() && restartOnError_) {
        logger()->info("restarting due to error code %d", exitCode_);

        if (restart())
		{
			continue;
		}
      }

      logger()->info("shutting down watchdog with application exit code %d",
                     exitCode_);

      return exitCode_;
    }

    if (WIFSIGNALED(status)) {
      int sig = WTERMSIG(status);
      logger()->info("Child %d terminated with signal '%s' (%d)",
                     program_->pid(), strsignal(sig), sig);

      // do only attempt to restart if it's none of those signals.
      static const int sigs[] = {SIGTERM, SIGINT, SIGQUIT};

      bool softTerminate =
          std::find(std::begin(sigs), std::end(sigs), sig) != std::end(sigs);

      if (!softTerminate && restartOnCrash_ && restart()) 
		  continue;

      return exitCode_;
    }


    if (restart())
	{
		continue;
	}

    return exitCode_;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %r14
callq 0x5e7a
movl $0x1, %ecx
testb %al, %al
je 0x63fa
cmpq $0x0, 0x38(%r14)
je 0x625a
leaq 0x30(%r14), %r15
callq 0x3550
movq 0x30(%r14), %rcx
leaq 0x21a4(%rip), %rsi      # 0x836e
movq %r14, %rdi
movl %eax, %edx
callq 0x69b4
leaq -0x228(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl $0x30, %edx
callq 0x36d0
callq 0x3550
movq %r12, %rdi
movl %eax, %esi
callq 0x3620
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3380
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3220
movq 0x4d85(%rip), %rax      # 0xafb0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x220(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x228(%rbp,%rcx)
callq 0x3300
leaq -0x130(%rbp), %rdi
callq 0x30d0
movq 0x28(%r14), %rdi
callq 0x5af6
callq 0x3a3f
leaq -0x228(%rbp), %rsi
movl $0x0, (%rsi)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x32b0
testl %eax, %eax
js 0x640e
movl %eax, %r15d
leaq -0x228(%rbp), %r12
movq 0x28(%r14), %rax
cmpl 0x60(%rax), %r15d
jne 0x6317
movl -0x228(%rbp), %ecx
movl %ecx, %ebx
andl $0x7f, %ebx
je 0x6357
movl %ebx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x6306
movl %ecx, %r13d
movl %ebx, %edi
callq 0x3640
movq %r14, %rdi
leaq 0x217d(%rip), %rsi      # 0x8450
movl %r15d, %edx
movq %rax, %rcx
movl %ebx, %r8d
callq 0x6b72
cmpl $0xf, %ebx
je 0x63f3
andl $0x7e, %r13d
cmpl $0x2, %r13d
je 0x63f3
cmpl $0x0, 0x80(%r14)
je 0x63f3
movq %r14, %rdi
callq 0x6472
testb %al, %al
jne 0x6329
jmp 0x63f3
movq %r14, %rdi
leaq 0x2073(%rip), %rsi      # 0x8394
movl %r15d, %edx
callq 0x6690
callq 0x3a3f
movl $0x0, -0x228(%rbp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r12, %rsi
xorl %edx, %edx
callq 0x32b0
movl %eax, %r15d
testl %eax, %eax
jns 0x6293
jmp 0x640e
movzbl %ch, %ecx
movl %ecx, 0x88(%r14)
movq %r14, %rdi
leaq 0x203e(%rip), %rsi      # 0x83a9
movl %r15d, %edx
callq 0x6a94
movq 0x28(%r14), %rdi
callq 0x5c3a
testb %al, %al
je 0x6393
movq 0x28(%r14), %rax
movl 0x60(%rax), %edx
movq %r14, %rdi
leaq 0x204d(%rip), %rsi      # 0x83de
jmp 0x6324
movl 0x88(%r14), %edx
movq 0x90(%r14), %rax
movq 0x98(%r14), %rcx
cmpq %rcx, %rax
je 0x63b7
cmpl %edx, (%rax)
je 0x63e4
addq $0x4, %rax
jmp 0x63a8
cmpl $0x0, 0x7c(%r14)
je 0x63e4
movq %r14, %rdi
leaq 0x2033(%rip), %rsi      # 0x83fb
callq 0x6690
movq %r14, %rdi
callq 0x6472
testb %al, %al
jne 0x6329
movl 0x88(%r14), %edx
leaq 0x2030(%rip), %rsi      # 0x841b
movq %r14, %rdi
callq 0x6690
movl 0x88(%r14), %ecx
movl %ecx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f77(%rip), %rdi      # 0x838c
callq 0x32f0
movl $0x1, %ecx
jmp 0x63fa
movq %rax, %rbx
leaq -0x228(%rbp), %rdi
callq 0x3560
movq %rbx, %rdi
callq 0x3630