// File: 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
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x248(%rbp)
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
callq 0x6a50
testb $0x1, %al
jne 0x711d
movl $0x1, -0x4(%rbp)
jmp 0x758c
movq -0x248(%rbp), %rdi
addq $0x30, %rdi
callq 0x36b0
testb $0x1, %al
jne 0x7213
movq -0x248(%rbp), %rdi
callq 0x8320
movq %rax, -0x270(%rbp)
callq 0x35e0
movq -0x248(%rbp), %rdi
movl %eax, -0x264(%rbp)
addq $0x30, %rdi
movq %rdi, -0x260(%rbp)
callq 0x3150
movq -0x270(%rbp), %rdi
movl -0x264(%rbp), %edx
movq %rax, %rcx
leaq 0x530a(%rip), %rsi      # 0xc48b
callq 0x8570
movl $0x10, %edi
movl $0x20, %esi
callq 0x86a0
movq -0x260(%rbp), %rsi
movl %eax, %edx
leaq -0x220(%rbp), %rdi
movq %rdi, -0x258(%rbp)
callq 0x3770
callq 0x35e0
movq -0x258(%rbp), %rdi
movl %eax, %esi
callq 0x36d0
movq %rax, -0x250(%rbp)
jmp 0x71cd
movq -0x250(%rbp), %rdi
movq 0x9dc5(%rip), %rsi      # 0x10fa0
callq 0x3410
jmp 0x71e2
leaq -0x220(%rbp), %rdi
callq 0x35f0
jmp 0x7213
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x35f0
jmp 0x7598
movq -0x248(%rbp), %rdi
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x6550
callq 0x3a80
movl $0x0, -0x230(%rbp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq -0x230(%rbp), %rsi
xorl %edx, %edx
callq 0x3300
movl %eax, -0x234(%rbp)
cmpl $0x0, -0x234(%rbp)
jge 0x7274
leaq 0x5246(%rip), %rdi      # 0xc4a9
callq 0x3340
movl $0x1, -0x4(%rbp)
jmp 0x758c
movq -0x248(%rbp), %rdi
movl -0x234(%rbp), %eax
movl %eax, -0x274(%rbp)
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x86e0
movl %eax, %ecx
movl -0x274(%rbp), %eax
cmpl %ecx, %eax
je 0x72ca
movq -0x248(%rbp), %rdi
callq 0x8320
movq %rax, %rdi
movl -0x234(%rbp), %edx
leaq 0x51f1(%rip), %rsi      # 0xc4b1
callq 0x7ca0
jmp 0x722b
movl -0x230(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x743b
movq -0x248(%rbp), %rdi
movl -0x230(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, 0x88(%rdi)
callq 0x8320
movq -0x248(%rbp), %rdi
movq %rax, -0x280(%rbp)
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x86e0
movq -0x280(%rbp), %rdi
movl %eax, %edx
movq -0x248(%rbp), %rax
movl 0x88(%rax), %ecx
leaq 0x518e(%rip), %rsi      # 0xc4c6
callq 0x8700
movq -0x248(%rbp), %rdi
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x6750
testb $0x1, %al
jne 0x735b
jmp 0x73a0
movq -0x248(%rbp), %rdi
callq 0x8320
movq -0x248(%rbp), %rdi
movq %rax, -0x288(%rbp)
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x86e0
movq -0x288(%rbp), %rdi
movl %eax, %edx
leaq 0x5165(%rip), %rsi      # 0xc4fb
callq 0x7ca0
jmp 0x722b
movq -0x248(%rbp), %rdi
callq 0x75b0
testb $0x1, %al
jne 0x73fe
movq -0x248(%rbp), %rax
cmpl $0x0, 0x7c(%rax)
je 0x73fe
movq -0x248(%rbp), %rdi
callq 0x8320
movq %rax, %rdi
movq -0x248(%rbp), %rax
movl 0x88(%rax), %edx
leaq 0x5138(%rip), %rsi      # 0xc518
callq 0x7ca0
movq -0x248(%rbp), %rdi
callq 0x75e0
testb $0x1, %al
jne 0x73f7
jmp 0x73fc
jmp 0x722b
jmp 0x73fe
movq -0x248(%rbp), %rdi
callq 0x8320
movq %rax, %rdi
movq -0x248(%rbp), %rax
movl 0x88(%rax), %edx
leaq 0x5117(%rip), %rsi      # 0xc538
callq 0x7ca0
movq -0x248(%rbp), %rax
movl 0x88(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x758c
movl -0x230(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x7565
movq -0x248(%rbp), %rdi
movl -0x230(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x238(%rbp)
callq 0x8320
movq -0x248(%rbp), %rdi
movq %rax, -0x2a8(%rbp)
addq $0x28, %rdi
callq 0x86c0
movq %rax, %rdi
callq 0x86e0
movl %eax, -0x29c(%rbp)
movl -0x238(%rbp), %edi
callq 0x3700
movq -0x2a8(%rbp), %rdi
movl -0x29c(%rbp), %edx
movq %rax, %rcx
movl -0x238(%rbp), %r8d
leaq 0x50af(%rip), %rsi      # 0xc56d
callq 0x8820
leaq 0x519e(%rip), %rdi      # 0xc668
callq 0x89b0
movq %rax, -0x298(%rbp)
leaq 0x518b(%rip), %rdi      # 0xc668
callq 0x89c0
movq -0x298(%rbp), %rdi
movq %rax, %rsi
leaq -0x238(%rbp), %rdx
callq 0x8960
movq %rax, -0x290(%rbp)
leaq 0x5162(%rip), %rdi      # 0xc668
callq 0x89c0
movq %rax, %rcx
movq -0x290(%rbp), %rax
cmpq %rcx, %rax
setne %al
andb $0x1, %al
movb %al, -0x239(%rbp)
testb $0x1, -0x239(%rbp)
jne 0x7553
movq -0x248(%rbp), %rax
cmpl $0x0, 0x80(%rax)
je 0x7553
movq -0x248(%rbp), %rdi
callq 0x75e0
testb $0x1, %al
jne 0x754e
jmp 0x7553
jmp 0x722b
movq -0x248(%rbp), %rax
movl 0x88(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x758c
movq -0x248(%rbp), %rdi
callq 0x75e0
testb $0x1, %al
jne 0x7577
jmp 0x757c
jmp 0x722b
movq -0x248(%rbp), %rax
movl 0x88(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
movq -0x228(%rbp), %rdi
callq 0x36e0
nopw %cs:(%rax,%rax)