// File: /workspace/github_cpp_makefile_star2_O2/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 0x55c4
pushq $0x1
popq %rcx
testb %al, %al
je 0x5abc
cmpq $0x0, 0x38(%r14)
je 0x593a
leaq 0x30(%r14), %rbx
callq 0x3520
movq 0x30(%r14), %rcx
leaq 0x1a82(%rip), %rsi      # 0x737a
movq %r14, %rdi
movl %eax, %edx
callq 0x6018
leaq -0x230(%rbp), %r15
pushq $0x30
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x36a0
callq 0x3520
movq %r15, %rdi
movl %eax, %esi
callq 0x35f0
movq %rax, %rdi
callq 0x3190
leaq -0x230(%rbp), %rdi
callq 0x3530
movq 0x28(%r14), %rdi
callq 0x52b2
pushq $-0x1
popq %r15
leaq -0x230(%rbp), %r12
leaq 0x1a4b(%rip), %r13      # 0x73a0
callq 0x38ef
andl $0x0, -0x230(%rbp)
movl %r15d, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x32c0
testl %eax, %eax
js 0x5ad0
movl %eax, %ebx
movq 0x28(%r14), %rax
cmpl 0x60(%rax), %ebx
jne 0x5a11
movl -0x230(%rbp), %ecx
movl %ecx, %eax
andl $0x7f, %eax
je 0x5a23
movl %eax, %ecx
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x59fc
movl %eax, -0x2c(%rbp)
movl %eax, %edi
callq 0x3610
movl -0x2c(%rbp), %r8d
movq %r14, %rdi
leaq 0x1a9b(%rip), %rsi      # 0x745c
movl %ebx, %edx
movq %rax, %rcx
callq 0x61a2
leaq 0x167a(%rip), %rdi      # 0x704c
leaq 0x167f(%rip), %rbx      # 0x7058
movq %rbx, %rsi
leaq -0x2c(%rbp), %rdx
callq 0x6b9c
cmpq %rbx, %rax
jne 0x5ab5
cmpl $0x0, 0x80(%r14)
je 0x5ab5
movq %r14, %rdi
callq 0x5b08
testb %al, %al
jne 0x5955
jmp 0x5ab5
movq %r14, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x5cf6
jmp 0x5955
movzbl %ch, %ecx
movl %ecx, 0x88(%r14)
movq %r14, %rdi
leaq 0x197e(%rip), %rsi      # 0x73b5
movl %ebx, %edx
callq 0x60de
movq 0x28(%r14), %rdi
callq 0x53d4
testb %al, %al
je 0x5a66
movq 0x28(%r14), %rax
movl 0x60(%rax), %edx
movq %r14, %rdi
leaq 0x198e(%rip), %rsi      # 0x73ea
callq 0x5cf6
jmp 0x5955
movq %r14, %rdi
callq 0x5af8
testb %al, %al
jne 0x5a9f
cmpl $0x0, 0x7c(%r14)
je 0x5a9f
movl 0x88(%r14), %edx
movq %r14, %rdi
leaq 0x197d(%rip), %rsi      # 0x7407
callq 0x5cf6
movq %r14, %rdi
callq 0x5b08
testb %al, %al
jne 0x5955
movl 0x88(%r14), %edx
leaq 0x197a(%rip), %rsi      # 0x7427
movq %r14, %rdi
callq 0x5cf6
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 0x18c1(%rip), %rdi      # 0x7398
callq 0x3300
pushq $0x1
popq %rcx
jmp 0x5abc
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
callq 0x3530
movq %rbx, %rdi
callq 0x3600