// File: /workspace/github_cpp_makefile_star2_O2/overfl0[P]iojack/iojack.cpp
// Function: processSyscall(processInfo*, user_regs_struct*, int*)
void processSyscall(processInfo *pi, user_regs_struct *regs, int *saveRegs)
{
    // regs->ORIG_RAX - Syscall number
    //printf("__RAX: %ld (orig: %ld)\n", regs->RAX, regs->ORIG_RAX);
    // We're either in a syscall or not
    if(!pi->inSyscall)
    {
        // I read somewhere that rax should == -38 now but I should confirm that
        // Tip: everything seems to suggest that it's the value of -ENOSYS
        // ("this system call doesn't exist")
        dprintf("[%d] Entering syscall: %s (%ld), rax = %ld\n",
                pi->pid, syscallToStr(regs->ORIG_RAX), regs->ORIG_RAX, regs->RAX);
        //dprintf("RIP: %lx\n", regs->rip);
        //tmpDump(pi, regs);
        pi->orig_regs = *regs; // Backup everything
        hookPtr fun = getPreHook(regs->ORIG_RAX);
        if(fun)
        {
            // Run a hooked function
            int fakeSyscall = 0;
            fun(pi, *regs, *saveRegs, fakeSyscall);

            if(fakeSyscall)
            {
                pi->fakingSyscall = regs->ORIG_RAX;

                // This syscall can't exist :)
                regs->ORIG_RAX = (unsigned int)-1;
                regs->RAX = -1;

                *saveRegs = 1;
            }
        }

        pi->inSyscall = 1;
    }
    else // Exiting a syscall
    {
        dprintf("[%d] Exiting syscall:  %s (%ld)   with code %ld\n", pi->pid, syscallToStr(regs->ORIG_RAX), regs->ORIG_RAX, regs->RAX);
        //dprintf("RIP: %lx\n", regs->rip);

        int unused;
        if(pi->fakingSyscall != -1)
        {
            if(regs->ORIG_RAX != (unsigned int)-1)
                printf("[%d] OMG! :O, regs->ORIG_RAX == %ld\n", pi->pid, regs->ORIG_RAX);

            // Second ptrace trap. We just finished running our
            // nonexisting syscall. Now is the moment to inject the
            // "correct" return values, etc...
            hookPtr fun = getFakedHook(pi->fakingSyscall);
            if(fun)
                fun(pi, *regs, *saveRegs, unused);
            else
            {
                // Should not happen!
                printf("[%d] Can't find function handling fake %s! (%d).\n", pi->pid, syscallToStr(pi->fakingSyscall), pi->fakingSyscall);
                exit(1);
            }

            regs->ORIG_RAX = pi->fakingSyscall;
            pi->fakingSyscall = -1;
        }

        hookPtr postFun = getPostHook(pi->orig_regs.ORIG_RAX);
        if(postFun)
            postFun(pi, *regs, *saveRegs, unused);

        pi->inSyscall = 0;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
cmpl $0x0, 0x4(%rdi)
je 0x2974
movl 0x8(%rbx), %edi
cmpl $-0x1, %edi
je 0x294e
movq 0x78(%r15), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rdx
je 0x2924
movl (%rbx), %esi
leaq 0x3737(%rip), %rdi      # 0x6051
xorl %eax, %eax
callq 0x2040
movl 0x8(%rbx), %edi
callq 0x4be9
testq %rax, %rax
je 0x29e2
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *%rax
movslq 0x8(%rbx), %rax
movq %rax, 0x78(%r15)
orl $-0x1, 0x8(%rbx)
movl 0x90(%rbx), %edi
callq 0x4bf7
testq %rax, %rax
je 0x296e
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *%rax
andl $0x0, 0x4(%rbx)
jmp 0x29d5
leaq 0x18(%rbx), %rdi
movl $0xd8, %edx
movq %r15, %rsi
callq 0x2120
movl 0x78(%r15), %edi
callq 0x4bdb
testq %rax, %rax
je 0x29ce
leaq 0xc(%rsp), %r12
andl $0x0, (%r12)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq *%rax
cmpl $0x0, (%r12)
je 0x29ce
movl 0x78(%r15), %eax
movl %eax, 0x8(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x78(%r15)
orq $-0x1, 0x50(%r15)
movl $0x1, (%r14)
movl $0x1, 0x4(%rbx)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl (%rbx), %ebp
movl 0x8(%rbx), %edi
callq 0x38b0
movl 0x8(%rbx), %ecx
leaq 0x3680(%rip), %rdi      # 0x6076
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdi
callq 0x21f0