// File: /workspace/github_cpp_makefile_star2_O3/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 0x46f24
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x46c17
movq %r14, %rdi
callq 0x3a956
movl 0x168(%r14), %edi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rcx
xorl %edx, %edx
callq 0x27280
cmpb $0x0, 0x1f8(%r14)
jne 0x46c73
leaq 0xc(%rsp), %r15
leaq 0x10(%rsp), %r12
testl %eax, %eax
jns 0x46c87
callq 0x27050
cmpl $0x4, (%rax)
jne 0x46de1
movl 0x168(%r14), %edi
movq %r15, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x27280
cmpb $0x0, 0x1f8(%r14)
je 0x46c43
leaq 0x198(%r14), %rsi
movl $0xd, %ecx
movq %rbx, %rdi
jmp 0x46daa
cmpl 0x168(%r14), %eax
jne 0x46def
movq %r14, %rdi
xorl %esi, %esi
callq 0x3a956
movl 0x16c(%r14), %edi
leaq 0xa0(%rsp), %rsi
movl $0x1fff, %edx           # imm = 0x1FFF
callq 0x276c0
testl %eax, %eax
jg 0x46df8
movq %r14, %rdi
callq 0x3a8ca
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x46cd8
movq %rax, 0x1c0(%r14)
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0x46cec
movq %rax, 0x1c8(%r14)
movq 0x98(%rsp), %rax
testq %rax, %rax
je 0x46d00
movq %rax, 0x1d0(%r14)
movl 0xc(%rsp), %esi
leaq 0xfce7(%rip), %rdi      # 0x569f2
xorl %eax, %eax
callq 0x2ceee
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x46d48
movl %ecx, %edx
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jl 0x46e1e
movl $0x8, 0x1fc(%r14)
movl %ecx, 0x1f0(%r14)
jmp 0x46d7c
testl $0xff00, %eax           # imm = 0xFF00
je 0x46d66
movl $0x7, 0x1fc(%r14)
movzbl %ah, %eax
movl %eax, 0x1f4(%r14)
jmp 0x46d7c
movl $0x1, 0x1fc(%r14)
movl $0x0, 0x1f4(%r14)
leaq 0x198(%r14), %r15
movq %r14, %rdi
callq 0x369c6
cmpl $0x1, %eax
je 0x46d9f
movq %r14, %rdi
callq 0x369c6
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 0x47420
movq %rbx, %rax
addq $0x20a8, %rsp           # imm = 0x20A8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0xfbd6(%rip), %rdi      # 0x569be
xorl %eax, %eax
callq 0x2cd4f
leaq 0xfbd2(%rip), %rdi      # 0x569c8
jmp 0x46e48
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0xa0(%rsp,%rax)
leaq 0xfbda(%rip), %rdi      # 0x569e6
leaq 0xa0(%rsp), %rsi
movl $0x1, %edx
callq 0x2c78f
cmpb $0x7f, %al
jne 0x46e36
movl $0x9, 0x1fc(%r14)
leaq 0xfbd0(%rip), %rdi      # 0x56a04
jmp 0x46de8
movl $0xa, 0x1fc(%r14)
leaq 0xfbee(%rip), %rdi      # 0x56a36
movl %eax, %esi
xorl %eax, %eax
callq 0x2cd4f
nop