// File: /workspace/github_cpp_makefile_star2_O2/overfl0[P]iojack/syscalls.cpp
// Function: fakeSelectHook(processInfo*, user_regs_struct&, int&, int&)
void fakeSelectHook(processInfo *pi, user_regs_struct &regs, int &saveRegs, int &unused)
{
    regs.RAX = 0;

    // readfds
    if(regs.ARG2 && inputBuffer.lockedSize() && pi->stdin.size())
    {
        fd_set readSet;
        pi->readMemcpy(&readSet, regs.ARG2, sizeof(fd_set));
        // Get the file descriptor
        int fd = 0;
        foreach(pi->stdin, it)
            if(FD_ISSET(*it, &readSet))
                fd = *it;
        //FD_ZERO(&readSet);
        for(unsigned int i = 0; i < regs.ARG1; i++)
            FD_CLR(i, &readSet);
        FD_SET(fd, &readSet);
        pi->writeMemcpy(regs.ARG2, &readSet, sizeof(fd_set));

        regs.RAX += 1;
    }

    // writefds
    if(regs.ARG3)
    {
        fd_set writeSet;
        pi->readMemcpy(&writeSet, regs.ARG3, sizeof(fd_set));
        //FD_ZERO(&writeSet);
        for(unsigned int i = 0; i < regs.ARG1; i++)
            FD_CLR(i, &writeSet);
        pi->writeMemcpy(regs.ARG3, &writeSet, sizeof(fd_set));
    }

    // exceptfds
    if(regs.ARG4
        && regs.ARG4 != (unsigned long)-1 // Nano temporary workaround
        )
    {
        fd_set exceptSet;
        pi->readMemcpy(&exceptSet, regs.ARG4, sizeof(fd_set));
        //FD_ZERO(&exceptSet);
        for(unsigned int i = 0; i < regs.ARG1; i++)
            FD_CLR(i, &exceptSet);
        pi->writeMemcpy(regs.ARG4, &exceptSet, sizeof(fd_set));
    }
/*    if(regs.ARG4 == (unsigned long)-1)
    {
        printf("Nano, srsly, WTF?!?\n");
        printf("EIP: %lx\n", regs.rip);

    }
*/
    saveRegs = 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %rbx
movq %rsi, %r15
andq $0x0, 0x50(%rsi)
movq %rdi, %r14
cmpq $0x0, 0x68(%rsi)
pushq $-0x2
popq %r12
je 0x4ebf
leaq 0x648a(%rip), %rdi      # 0xb280
callq 0x3b5c
testl %eax, %eax
je 0x4ebf
cmpq $0x0, 0x118(%r14)
je 0x4ebf
movq %rbx, (%rsp)
movq 0x68(%r15), %rdx
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4192
movq 0x108(%r14), %rdi
leaq 0xf8(%r14), %rbx
xorl %ebp, %ebp
pushq $0x40
popq %r13
cmpq %rbx, %rdi
je 0x4e65
movl 0x20(%rdi), %ecx
movl %ecx, %eax
cltd
idivl %r13d
cltq
movq 0x8(%rsp,%rax,8), %rax
btq %rdx, %rax
cmovbl %ecx, %ebp
callq 0x21d0
movq %rax, %rdi
jmp 0x4e3f
movq 0x70(%r15), %rax
xorl %ecx, %ecx
movq (%rsp), %rbx
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x4e8a
movq %r12, %rdx
rolq %cl, %rdx
movl %ecx, %esi
shrl $0x6, %esi
andq %rdx, 0x8(%rsp,%rsi,8)
incl %ecx
jmp 0x4e6f
movl %ebp, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
subl %eax, %ebp
pushq $0x1
popq %rax
movl %ebp, %ecx
shlq %cl, %rax
orq %rax, 0x8(%rsp,%rdx,8)
movq 0x68(%r15), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x425a
incq 0x50(%r15)
movq 0x60(%r15), %rdx
testq %rdx, %rdx
je 0x4f11
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4192
movq 0x70(%r15), %rax
xorl %ecx, %ecx
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x4efb
movq %r12, %rdx
rolq %cl, %rdx
movl %ecx, %esi
shrl $0x6, %esi
andq %rdx, 0x8(%rsp,%rsi,8)
incl %ecx
jmp 0x4ee0
movq 0x60(%r15), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x425a
movq 0x58(%r15), %rdx
leaq 0x1(%rdx), %rax
cmpq $0x2, %rax
jb 0x4f68
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4192
movq 0x70(%r15), %rax
xorl %ecx, %ecx
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x4f52
movq %r12, %rdx
rolq %cl, %rdx
movl %ecx, %esi
shrl $0x6, %esi
andq %rdx, 0x8(%rsp,%rsi,8)
incl %ecx
jmp 0x4f37
movq 0x58(%r15), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x425a
movl $0x1, (%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq