// File: /workspace/github_cpp_makefile_star2_O3/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 0x2aa7
movl 0x8(%rbx), %edi
cmpl $-0x1, %edi
je 0x2a7e
movq 0x78(%r15), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rdx
je 0x2a51
movl (%rbx), %esi
leaq 0x3626(%rip), %rdi      # 0x606d
xorl %eax, %eax
callq 0x2040
movl 0x8(%rbx), %edi
callq 0x4e5f
testq %rax, %rax
je 0x2b1b
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *%rax
movslq 0x8(%rbx), %rax
movq %rax, 0x78(%r15)
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
movl 0x90(%rbx), %edi
callq 0x4e87
testq %rax, %rax
je 0x2a9e
leaq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *%rax
movl $0x0, 0x4(%rbx)
jmp 0x2b0e
leaq 0x18(%rbx), %rdi
movl $0xd8, %edx
movq %r15, %rsi
callq 0x2120
movl 0x78(%r15), %edi
callq 0x4e37
testq %rax, %rax
je 0x2b07
leaq 0xc(%rsp), %r12
movl $0x0, (%r12)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq *%rax
cmpl $0x0, (%r12)
je 0x2b07
movl 0x78(%r15), %eax
movl %eax, 0x8(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x78(%r15)
movq $-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 0x3a5c
movl 0x8(%rbx), %ecx
leaq 0x3563(%rip), %rdi      # 0x6092
movl %ebp, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2040
movl $0x1, %edi
callq 0x21e0