// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
callq 0x5e78
movl $0x1, %r15d
testb %al, %al
je 0x6481
cmpq $0x0, 0x38(%rbx)
je 0x62ad
leaq 0x30(%rbx), %r14
callq 0x3550
movq 0x30(%rbx), %rcx
leaq 0x2151(%rip), %rsi      # 0x836e
movq %rbx, %rdi
movl %eax, %edx
callq 0x6a28
leaq -0x228(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x30, %edx
callq 0x36d0
callq 0x3550
movq %r15, %rdi
movl %eax, %esi
callq 0x3620
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3380
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3220
movq 0x4d32(%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(%rbx), %rdi
callq 0x5ad6
leaq -0x228(%rbp), %r14
callq 0x3a3f
movl $0x0, -0x228(%rbp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
xorl %edx, %edx
callq 0x32b0
testl %eax, %eax
js 0x6465
movq 0x28(%rbx), %rcx
movl 0x60(%rcx), %r12d
cmpl %r12d, %eax
jne 0x6374
movl -0x228(%rbp), %eax
movl %eax, %r13d
andl $0x7f, %r13d
je 0x638c
movl %r13d, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x63c7
movl %r13d, %edi
callq 0x3640
movq %rbx, %rdi
leaq 0x2121(%rip), %rsi      # 0x8450
movl %r12d, %edx
movq %rax, %rcx
movl %r13d, %r8d
callq 0x6be6
cmpl $0x10, %r13d
setb %al
movl $0x800c, %edx           # imm = 0x800C
movl %r13d, %ecx
shrl %cl, %edx
testb %dl, %al
jne 0x645c
cmpl $0x0, 0x80(%rbx)
je 0x645c
movq %rbx, %rdi
callq 0x64ec
testb %al, %al
jne 0x6385
jmp 0x645c
movq %rbx, %rdi
leaq 0x2016(%rip), %rsi      # 0x8394
movl %eax, %edx
callq 0x6704
xorl %eax, %eax
jmp 0x6479
movzbl %ah, %ecx
movl %ecx, 0x88(%rbx)
movq %rbx, %rdi
leaq 0x200a(%rip), %rsi      # 0x83a9
movl %r12d, %edx
callq 0x6b08
movq 0x28(%rbx), %rdi
callq 0x5c1a
testb %al, %al
je 0x63e1
movq 0x28(%rbx), %rax
movl 0x60(%rax), %edx
movq %rbx, %rdi
leaq 0x2019(%rip), %rsi      # 0x83de
jmp 0x6380
movq %rbx, %rdi
callq 0x64ec
testb %al, %al
jne 0x63da
movl 0x88(%rbx), %r15d
xorb $0x1, %al
jmp 0x6479
movl 0x88(%rbx), %edx
movq 0x90(%rbx), %rcx
movq 0x98(%rbx), %rax
cmpq %rax, %rcx
setne %sil
je 0x641d
cmpl %edx, (%rcx)
je 0x641a
addq $0x4, %rcx
cmpq %rax, %rcx
setne %sil
je 0x641d
leaq 0x4(%rcx), %rsi
cmpl %edx, (%rcx)
movq %rsi, %rcx
jne 0x6406
movb $0x1, %sil
testb %sil, %sil
jne 0x6447
cmpl $0x0, 0x7c(%rbx)
je 0x6447
movq %rbx, %rdi
leaq 0x1fc9(%rip), %rsi      # 0x83fb
callq 0x6704
movq %rbx, %rdi
callq 0x64ec
testb %al, %al
jne 0x6385
movl 0x88(%rbx), %edx
movq %rbx, %rdi
leaq 0x1fc4(%rip), %rsi      # 0x841b
callq 0x6704
movl 0x88(%rbx), %r15d
jmp 0x6477
leaq 0x1f20(%rip), %rdi      # 0x838c
callq 0x32f0
movl $0x1, %r15d
movb $0x1, %al
testb %al, %al
je 0x62bd
movl %r15d, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x228(%rbp), %rdi
callq 0x3560
movq %rbx, %rdi
callq 0x3630
nop