// File: /workspace/github_cpp_makefile_star2_O1/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
movl 0x178(%rsi), %edi
addq $0x188, %rsi            # imm = 0x188
callq 0x27420
movl 0x170(%r14), %edi
callq 0x27930
movq %r14, %rdi
callq 0x4757c
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x4726f
movq %r14, %rdi
callq 0x3afe6
movl 0x168(%r14), %edi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rcx
xorl %edx, %edx
callq 0x27280
cmpb $0x0, 0x1f8(%r14)
jne 0x472cb
leaq 0xc(%rsp), %r15
leaq 0x10(%rsp), %r12
testl %eax, %eax
jns 0x472df
callq 0x27050
cmpl $0x4, (%rax)
jne 0x47439
movl 0x168(%r14), %edi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x27280
cmpb $0x0, 0x1f8(%r14)
je 0x4729b
leaq 0x198(%r14), %rsi
movl $0xd, %ecx
movq %rbx, %rdi
jmp 0x47402
cmpl 0x168(%r14), %eax
jne 0x47447
movq %r14, %rdi
xorl %esi, %esi
callq 0x3afe6
movl 0x16c(%r14), %edi
leaq 0xa0(%rsp), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
callq 0x276c0
testl %eax, %eax
jg 0x47450
movq %r14, %rdi
callq 0x3af5a
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x47330
movq %rax, 0x1c0(%r14)
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x47344
movq %rax, 0x1c8(%r14)
movq 0x98(%rsp), %rax
testq %rax, %rax
je 0x47358
movq %rax, 0x1d0(%r14)
movl 0xc(%rsp), %esi
leaq 0xf68f(%rip), %rdi      # 0x569f2
xorl %eax, %eax
callq 0x2cfe4
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x473a0
movl %ecx, %edx
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jl 0x47476
movl $0x8, 0x1fc(%r14)
movl %ecx, 0x1f0(%r14)
jmp 0x473d4
testl $0xff00, %eax           # imm = 0xFF00
je 0x473be
movl $0x7, 0x1fc(%r14)
movzbl %ah, %eax
movl %eax, 0x1f4(%r14)
jmp 0x473d4
movl $0x1, 0x1fc(%r14)
movl $0x0, 0x1f4(%r14)
leaq 0x198(%r14), %r15
movq %r14, %rdi
callq 0x36c94
cmpl $0x1, %eax
je 0x473f7
movq %r14, %rdi
callq 0x36c94
movl %eax, 0x1fc(%r14)
movl $0xd, %ecx
movq %rbx, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x68(%rbx), %rdi
leaq 0x78(%rbx), %rax
movq %rax, 0x68(%rbx)
movq 0x200(%r14), %rsi
movq 0x208(%r14), %rdx
addq %rsi, %rdx
callq 0x47a78
movq %rbx, %rax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xf57e(%rip), %rdi      # 0x569be
xorl %eax, %eax
callq 0x2ce45
leaq 0xf57a(%rip), %rdi      # 0x569c8
jmp 0x474a0
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0xa0(%rsp,%rax)
leaq 0xf582(%rip), %rdi      # 0x569e6
leaq 0xa0(%rsp), %rsi
movl $0x1, %edx
callq 0x2c889
cmpb $0x7f, %al
jne 0x4748e
movl $0x9, 0x1fc(%r14)
leaq 0xf578(%rip), %rdi      # 0x56a04
jmp 0x47440
movl $0xa, 0x1fc(%r14)
leaq 0xf596(%rip), %rdi      # 0x56a36
movl %eax, %esi
xorl %eax, %eax
callq 0x2ce45
nop