// File: /workspace/github_cpp_makefile_star2_O2/establishment[P]sandman/src/lib.hpp
// Function: ProcessKeeper::startKeeper()
RunStats startKeeper() {
        /// start clock even thou it is already started by the constructor
        wallClock.start();

        /// switch error pipes fd to read errors from the child process in which the
        /// process runs
        close(errorPipes[1]);

        mockSignals();

        /// if checkIntervalMs is not null, status check is on
        if (config.checkIntervalMs) {
            startStatusCheck(config.checkIntervalMs);
        }

        while (1) {
            struct rusage processUsage;
            int processStatus;

            pid_t p;
            p = wait4(processPid, &processStatus, 0, &processUsage);

            /// check if the process was killed by the signal
            if (processStats.processWasKilled) {
                return processStats;
            }

            /// check if wait4 is working properly
            if (p < 0) {
                if (errno == EINTR) {
                    continue;
                }
                Die("wait4: %m");
            }

            /// sanity check
            if (p != processPid) {
                Die("wait4: unknown pid %d exited!", p);
            }

            clearTimer();

            /// Check error pipe if there is an internal error passed from inside the box
            char interr[Base::kDieBufferSize];
            int n = read(errorPipes[0], interr, sizeof(interr) - 1);
            if (n > 0) {
                interr[n] = 0;
                Die("childErrors", interr, true);
            }

            updateStats();
            processStats.update(processUsage);
            Msg("ProcessStatus:%d\n", processStatus);

            if (WIFEXITED(processStatus)) {
                /// the process exited normaly.
                if (WEXITSTATUS(processStatus)) {
                    processStats.resultCode = RunStats::NON_ZERO_EXIT_STATUS;
                    processStats.exitCode = WEXITSTATUS(processStatus);
                } else {
                    processStats.resultCode = RunStats::OK;
                    processStats.exitCode = 0;
                }
            } else if (WIFSIGNALED(processStatus)) {
                processStats.resultCode = RunStats::RUNTIME_ERROR;
                processStats.terminalSignal = WTERMSIG(processStatus);
            } else if (WIFSTOPPED(processStatus)) {
                processStats.resultCode = RunStats::ABNORMAL_TERMINATION;
                Die("Process has stopped. Won't try to start it again.");
            } else {
                processStats.resultCode = RunStats::INTERNAL_ERROR;
                Die("wait4: unknown status %x, giving up!", processStatus);
            }

            if (checkLimits() != RunStats::OK) {
                processStats.resultCode = checkLimits();
            }

            return processStats;
        }
    }

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20a8, %rsp           # imm = 0x20A8
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x178(%rsi), %rdi
callq 0x43b22
movl 0x170(%r14), %edi
callq 0x37920
movq %r14, %rdi
callq 0x4e620
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x4e3c5
movq %r14, %rdi
callq 0x44d48
leaq 0xc(%rsp), %r15
leaq 0x10(%rsp), %r12
movl 0x168(%r14), %edi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x37270
cmpb $0x1, 0x1f8(%r14)
je 0x4e409
testl %eax, %eax
jns 0x4e41b
callq 0x37050
cmpl $0x4, (%rax)
je 0x4e3cf
leaq 0xe589(%rip), %rdi      # 0x5c98b
xorl %eax, %eax
callq 0x3b2a4
addq $0x198, %r14            # imm = 0x198
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x4e507
cmpl 0x168(%r14), %eax
jne 0x4e51e
movq %r14, %rdi
callq 0x44cdc
movl 0x16c(%r14), %edi
leaq 0xa0(%rsp), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
callq 0x376a0
testl %eax, %eax
jg 0x4e527
leaq 0x198(%r14), %r15
movq %r14, %rdi
callq 0x44ce4
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x3adb6
movl 0xc(%rsp), %esi
leaq 0xe547(%rip), %rdi      # 0x5c9bf
xorl %eax, %eax
callq 0x3b443
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x4e4b5
movl %ecx, %edx
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jl 0x4e54b
movl $0x8, 0x1fc(%r14)
movl %ecx, 0x1f0(%r14)
jmp 0x4e4e5
testw $0xff00, %ax            # imm = 0xFF00
je 0x4e4d2
movl $0x7, 0x1fc(%r14)
movzbl %ah, %eax
movl %eax, 0x1f4(%r14)
jmp 0x4e4e5
movl $0x1, 0x1fc(%r14)
andl $0x0, 0x1f4(%r14)
movq %r14, %rdi
callq 0x41758
cmpl $0x1, %eax
je 0x4e501
movq %r14, %rdi
callq 0x41758
movl %eax, 0x1fc(%r14)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4e6ea
movq %rbx, %rax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xe470(%rip), %rdi      # 0x5c995
jmp 0x4e578
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0xa0(%rsp,%rax)
leaq 0xe478(%rip), %rdi      # 0x5c9b3
leaq 0xa0(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x3af9f
cmpb $0x7f, %al
jne 0x4e566
movl $0x9, 0x1fc(%r14)
leaq 0xe470(%rip), %rdi      # 0x5c9d1
jmp 0x4e402
movl $0xa, 0x1fc(%r14)
leaq 0xe48b(%rip), %rdi      # 0x5ca03
movl %eax, %esi
xorl %eax, %eax
callq 0x3b2a4
nop