// File: establishment[P]sandman/src/lib.hpp
// Function: Jailer::Run()
void Run() {
        Msg("Start running\n");

        if (!Base::DirExists("box")) {
            Die("Box directory not found, did you run 'isolate --init'?");
        }

        cg.prepare();  /// creates cgroup if it's not created

        /// This code will live here. Life is hard.
        /// setup pipes
        int errorPipes[2];
        if (pipe(errorPipes) < 0)
            Die("pipe: %m");

        /// close errorPipes when clone ends
        for (int i = 0; i < 2; i++)
            if (fcntl(errorPipes[i], F_SETFD, fcntl(errorPipes[i], F_GETFD) | FD_CLOEXEC) < 0 ||
                fcntl(errorPipes[i], F_SETFL, fcntl(errorPipes[i], F_GETFL) | O_NONBLOCK) < 0)
                Die("fcntl on pipe: %m");

        /// sanity check
        if (isolatedProcessStack == nullptr) {
            Die("Must provide a pointer to stack if the jailer is used as a library.");
        }

        int processPid =
            clone(ProcessInitialiser::ASyncStart,  /// Function to execute as the body of the new process
                  isolatedProcessStack,            /// stack for the new process (argv is the start of stack)
                  SIGCHLD | CLONE_NEWIPC | (config.shareNetwork ? 0 : CLONE_NEWNET) | CLONE_NEWNS | CLONE_NEWPID,
                  new ProcessInitialiser(config, uid, gid, errorPipes));  /// pass config for initialiser

        if (processPid < 0) {
            Die("clone: %m");
        }

        if (!processPid) {
            Die("clone returned 0");
        }

        Msg("Start waiting for process\n");

        ProcessKeeper keeper(config, processPid, errorPipes);
        RunStats finalStats = keeper.startKeeper();
        PrintStats(finalStats);
    }

// Assembly:
subq $0x618, %rsp            # imm = 0x618
movq %rdi, 0x610(%rsp)
movq 0x610(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x2ec8a(%rip), %rdi     # 0xf691d
xorl %eax, %eax
callq 0x94e30
leaq 0x5ef(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x8ca50
movq 0x40(%rsp), %rdx
leaq 0x2e5b0(%rip), %rsi     # 0xf6268
leaq 0x5f0(%rsp), %rdi
callq 0xa0a90
jmp 0xc7cc7
leaq 0x5f0(%rsp), %rdi
callq 0x94f30
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x37(%rsp)
leaq 0x5f0(%rsp), %rdi
callq 0x8cbd8
leaq 0x5ef(%rsp), %rdi
callq 0x8c630
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xc7d04
jmp 0xc7d38
leaq 0x2ec21(%rip), %rdi     # 0xf692c
movb $0x0, %al
callq 0x94bf0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
leaq 0x5ef(%rsp), %rdi
callq 0x8c630
jmp 0xc8098
leaq 0x56ec9(%rip), %rdi     # 0x11ec08
callq 0xc8200
leaq 0x5d4(%rsp), %rdi
callq 0x8c820
cmpl $0x0, %eax
jge 0xc7d64
leaq 0x2ec0f(%rip), %rdi     # 0xf696c
movb $0x0, %al
callq 0x94bf0
movl $0x0, 0x5d0(%rsp)
cmpl $0x2, 0x5d0(%rsp)
jge 0xc7e36
movslq 0x5d0(%rsp), %rax
movl 0x5d4(%rsp,%rax,4), %eax
movl %eax, 0x30(%rsp)
movslq 0x5d0(%rsp), %rax
movl 0x5d4(%rsp,%rax,4), %edi
movl $0x1, %esi
movb $0x0, %al
callq 0x8c300
movl 0x30(%rsp), %edi
movl %eax, %edx
orl $0x1, %edx
movl $0x2, %esi
movb $0x0, %al
callq 0x8c300
cmpl $0x0, %eax
jl 0xc7e10
movslq 0x5d0(%rsp), %rax
movl 0x5d4(%rsp,%rax,4), %eax
movl %eax, 0x2c(%rsp)
movslq 0x5d0(%rsp), %rax
movl 0x5d4(%rsp,%rax,4), %edi
movl $0x3, %esi
movb $0x0, %al
callq 0x8c300
movl 0x2c(%rsp), %edi
movl %eax, %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x8c300
cmpl $0x0, %eax
jge 0xc7e1e
leaq 0x2eb4c(%rip), %rdi     # 0xf6963
movb $0x0, %al
callq 0x94bf0
jmp 0xc7e20
movl 0x5d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5d0(%rsp)
jmp 0xc7d6f
movq 0x38(%rsp), %rax
cmpq $0x0, 0x168(%rax)
jne 0xc7e53
leaq 0x2eb29(%rip), %rdi     # 0xf6975
movb $0x0, %al
callq 0x94bf0
movq 0x38(%rsp), %rax
movq 0x168(%rax), %rcx
movq %rcx, 0x10(%rsp)
movl 0xe0(%rax), %eax
testl %eax, %eax
sete %al
movzbl %al, %eax
shll $0x1e, %eax
orl $0x28020011, %eax       # imm = 0x28020011
movl %eax, 0x1c(%rsp)
movl $0x178, %edi            # imm = 0x178
callq 0x8c5a0
movq 0x38(%rsp), %rsi
movq %rax, 0x20(%rsp)
movb $0x1, 0x45f(%rsp)
leaq 0x460(%rsp), %rdi
callq 0xc6f60
jmp 0xc7ea9
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rax
movl 0x190(%rax), %edx
movl 0x194(%rax), %ecx
leaq 0x460(%rsp), %rsi
leaq 0x5d4(%rsp), %r8
callq 0xc8f30
jmp 0xc7ed6
movq 0x20(%rsp), %rcx
movl 0x1c(%rsp), %edx
movq 0x10(%rsp), %rsi
movb $0x0, 0x45f(%rsp)
leaq -0x31513(%rip), %rdi    # 0x969e0
movb $0x0, %al
callq 0x8cad0
movl %eax, 0xc(%rsp)
leaq 0x460(%rsp), %rdi
callq 0xa2450
movl 0xc(%rsp), %eax
movl %eax, 0x5cc(%rsp)
cmpl $0x0, 0x5cc(%rsp)
jge 0xc7f85
leaq 0x2ea92(%rip), %rdi     # 0xf69b9
movb $0x0, %al
callq 0x94bf0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
jmp 0xc7f65
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
leaq 0x460(%rsp), %rdi
callq 0xa2450
testb $0x1, 0x45f(%rsp)
jne 0xc7f71
jmp 0xc7f80
movq 0x20(%rsp), %rdi
movl $0x178, %esi            # imm = 0x178
callq 0x8c5b0
jmp 0xc8098
cmpl $0x0, 0x5cc(%rsp)
jne 0xc7f9d
leaq 0x2ea2d(%rip), %rdi     # 0xf69c3
movb $0x0, %al
callq 0x94bf0
leaq 0x2ea30(%rip), %rdi     # 0xf69d4
xorl %eax, %eax
callq 0x94e30
movq 0x38(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xc6f60
movq (%rsp), %rsi
movl 0x5cc(%rsp), %edx
leaq 0x238(%rsp), %rdi
leaq 0x5d4(%rsp), %rcx
callq 0x967a0
jmp 0xc7fe3
leaq 0xd0(%rsp), %rdi
callq 0xa2450
leaq 0x48(%rsp), %rdi
leaq 0x238(%rsp), %rsi
callq 0xc8fe0
jmp 0xc8004
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xc92c0
jmp 0xc8015
leaq 0x48(%rsp), %rdi
callq 0xa2cc0
leaq 0x238(%rsp), %rdi
callq 0xc93b0
addq $0x618, %rsp            # imm = 0x618
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xa2450
jmp 0xc8098
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
jmp 0xc808b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5e0(%rsp)
movl %eax, 0x5dc(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xa2cc0
leaq 0x238(%rsp), %rdi
callq 0xc93b0
movq 0x5e0(%rsp), %rdi
callq 0x8ca40
nopw %cs:(%rax,%rax)
nop