// File: 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
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x68(%rax)
je 0xb83c
leaq 0xac2b(%rip), %rdi      # 0x16278
callq 0x8020
cmpl $0x0, %eax
je 0xb83c
movq -0x8(%rbp), %rdi
addq $0xf0, %rdi
callq 0xc320
cmpq $0x0, %rax
je 0xb83c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdx
leaq -0xa0(%rbp), %rsi
movl $0x80, %ecx
callq 0x9850
movl $0x0, -0xa4(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf0, %rdi
callq 0xc340
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
addq $0xf0, %rdi
callq 0xa340
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0xa2d0
testb $0x1, %al
jne 0xb6e6
jmp 0xb770
leaq -0xb0(%rbp), %rdi
callq 0xc370
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xa0(%rbp,%rax,8), %rax
movq %rax, -0x1e0(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xc370
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1e0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0xb754
leaq -0xb0(%rbp), %rdi
callq 0xc370
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
jmp 0xb756
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0xab20
movq %rax, -0xc0(%rbp)
jmp 0xb6b3
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jae 0xb7d2
movl -0xc4(%rbp), %eax
andl $0x3f, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0xc4(%rbp), %eax
shrl $0x6, %eax
movl %eax, %eax
andq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xb77a
movl -0xa4(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0xa4(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
leaq -0xa0(%rbp), %rdx
movl $0x80, %ecx
callq 0x9970
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xb8e7
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdx
leaq -0x148(%rbp), %rsi
movl $0x80, %ecx
callq 0x9850
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jae 0xb8ca
movl -0x14c(%rbp), %eax
andl $0x3f, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0x14c(%rbp), %eax
shrl $0x6, %eax
movl %eax, %eax
andq -0x148(%rbp,%rax,8), %rcx
movq %rcx, -0x148(%rbp,%rax,8)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0xb872
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
leaq -0x148(%rbp), %rdx
movl $0x80, %ecx
callq 0x9970
movq -0x10(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0xb9a1
movq -0x10(%rbp), %rax
cmpq $-0x1, 0x58(%rax)
je 0xb9a1
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdx
leaq -0x1d0(%rbp), %rsi
movl $0x80, %ecx
callq 0x9850
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jae 0xb984
movl -0x1d4(%rbp), %eax
andl $0x3f, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0x1d4(%rbp), %eax
shrl $0x6, %eax
movl %eax, %eax
andq -0x1d0(%rbp,%rax,8), %rcx
movq %rcx, -0x1d0(%rbp,%rax,8)
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0xb92c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rsi
leaq -0x1d0(%rbp), %rdx
movl $0x80, %ecx
callq 0x9970
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw %cs:(%rax,%rax)