// File: overfl0[P]iojack/iojack.cpp
// Function: stdinPoll(void*)
void *stdinPoll(void *inBuf)
{
    buffer *inputBuffer = (buffer *)inBuf;
    initTerminal();
    while(!wantToExit)
    {
        int c = getTerminalChar();
        if(c != -1)
        {
            // Lock the buffer and read as much data as you can (useful with
            // special sequences of more than one character)
            inputBuffer->lock();
            do
            {
                inputBuffer->add(c);
                c = getTerminalChar();
            }
            while(c != -1);
            inputBuffer->unlock();

            // TODO: Clean up this mess! Possibly move to a separate function
            // Wake up reading processes
            foreach(processes, it)
            {
                if(it->second->inSyscall && it->second->fakingSyscall == -1)
                {
                    //TODO: if possible, check if the syscall is sys_read
                    it->second->sigstopToRestartSyscall = 1;
                    int retval = kill(it->second->pid, SIGSTOP);
                    if(retval)
                    {
                        printf("Error while sending SIGSTOP to %d\n", it->second->pid);
                        perror("Error");
                    }
                }
            }
        }
        //thread_sleep(10);
        struct timespec waitTime;
        waitTime.tv_sec = 0;
        waitTime.tv_nsec = 100000000; // 0.1 sec
        nanosleep(&waitTime, NULL);
    }
    uninitTerminal();

    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
callq 0x6260
cmpl $0x0, 0x1392c(%rip)     # 0x161bc
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x289e
jmp 0x29d6
callq 0x62a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
je 0x29b4
movq -0x10(%rbp), %rdi
callq 0x7ed0
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movsbl %al, %esi
callq 0x7f10
callq 0x62a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x28b9
movq -0x10(%rbp), %rdi
callq 0x7ef0
leaq 0x138da(%rip), %rdi     # 0x161c0
callq 0x33d0
movq %rax, -0x20(%rbp)
leaq 0x138ca(%rip), %rdi     # 0x161c0
callq 0x3430
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3400
testb $0x1, %al
jne 0x2915
jmp 0x29b2
leaq -0x20(%rbp), %rdi
callq 0x3460
movq 0x8(%rax), %rax
cmpl $0x0, 0x4(%rax)
je 0x299c
leaq -0x20(%rbp), %rdi
callq 0x3460
movq 0x8(%rax), %rax
cmpl $-0x1, 0x8(%rax)
jne 0x299c
leaq -0x20(%rbp), %rdi
callq 0x3460
movq 0x8(%rax), %rax
movl $0x1, 0x10(%rax)
leaq -0x20(%rbp), %rdi
callq 0x3460
movq 0x8(%rax), %rax
movl (%rax), %edi
movl $0x13, %esi
callq 0x2250
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x299a
leaq -0x20(%rbp), %rdi
callq 0x3460
movq 0x8(%rax), %rax
movl (%rax), %esi
leaq 0xb734(%rip), %rdi      # 0xe0bb
movb $0x0, %al
callq 0x2040
leaq 0xb749(%rip), %rdi      # 0xe0de
callq 0x2140
jmp 0x299c
jmp 0x299e
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x3480
movq %rax, -0x38(%rbp)
jmp 0x28ef
jmp 0x29b4
movq $0x0, -0x48(%rbp)
movq $0x5f5e100, -0x40(%rbp) # imm = 0x5F5E100
leaq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2090
jmp 0x2889
callq 0x6280
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)