// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x80, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq $0x0, 0x50(%rsi)
cmpq $0x0, 0x68(%rsi)
je 0x52c2
leaq 0x608d(%rip), %rdi      # 0xb278
callq 0x3d88
testl %eax, %eax
je 0x52c2
cmpq $0x0, 0x118(%r14)
je 0x52c2
movq 0x68(%r15), %rdx
movq %rsp, %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4332
movq 0x108(%r14), %rax
leaq 0xf8(%r14), %r13
xorl %r12d, %r12d
cmpq %r13, %rax
je 0x5257
movl 0x20(%rax), %ecx
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x6, %edx
movslq %edx, %rdx
movq (%rsp,%rdx,8), %rdx
btq %rcx, %rdx
cmovbl %ecx, %r12d
movq %rax, %rdi
callq 0x21c0
jmp 0x522b
movq 0x70(%r15), %rax
testq %rax, %rax
je 0x5282
movl $0x1, %edx
leal -0x1(%rdx), %ecx
movq $-0x2, %rsi
rolq %cl, %rsi
shrl $0x6, %ecx
andq %rsi, (%rsp,%rcx,8)
movl %edx, %ecx
incl %edx
cmpq %rcx, %rax
ja 0x5265
leal 0x3f(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
movl %eax, %ecx
andl $-0x40, %ecx
subl %ecx, %r12d
movl $0x1, %edx
movl %r12d, %ecx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, (%rsp,%rax,8)
movq 0x68(%r15), %rsi
movq %rsp, %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4404
incq 0x50(%r15)
movq 0x60(%r15), %rdx
testq %rdx, %rdx
je 0x531a
movq %rsp, %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4332
movq 0x70(%r15), %rax
testq %rax, %rax
je 0x5306
movl $0x1, %edx
leal -0x1(%rdx), %ecx
movq $-0x2, %rsi
rolq %cl, %rsi
shrl $0x6, %ecx
andq %rsi, (%rsp,%rcx,8)
movl %edx, %ecx
incl %edx
cmpq %rcx, %rax
ja 0x52e9
movq 0x60(%r15), %rsi
movq %rsp, %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4404
movq 0x58(%r15), %rdx
leaq 0x1(%rdx), %rax
cmpq $0x2, %rax
jb 0x5377
movq %rsp, %rsi
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4332
movq 0x70(%r15), %rax
testq %rax, %rax
je 0x5363
movl $0x1, %edx
leal -0x1(%rdx), %ecx
movq $-0x2, %rsi
rolq %cl, %rsi
shrl $0x6, %ecx
andq %rsi, (%rsp,%rcx,8)
movl %edx, %ecx
incl %edx
cmpq %rcx, %rax
ja 0x5346
movq 0x58(%r15), %rsi
movq %rsp, %rdx
movq %r14, %rdi
movl $0x80, %ecx
callq 0x4404
movl $0x1, (%rbx)
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq