// File: /workspace/github_c_makefile_star2_O2/aap[P]pdp11/tv.c
// Function: handletv_thread
static void*
handletv_thread(void *arg)
{
	TV *tv = arg;
	int i;
	int nfds;
	int n;
	struct pollfd fds[NUMCONNECTIONS];
	int conmap[NUMCONNECTIONS];
	TVcon *con;

	for(;;){
		nfds = 0;
		for(i = 0; i < NUMCONNECTIONS; i++){
			if(tv->cons[i].fd < 0)
				continue;
			fds[nfds].fd = tv->cons[i].fd;
			fds[nfds].events = POLLIN;
			conmap[nfds] = i;
			nfds++;
		}
		if(nfds == 0){
			sleep_ms (200);
			continue;
		}

		/* We need a timeout here so poll can see
		 * when we open a new connection */
		n = poll(fds, nfds, 200);
		if(n < 0){
			perror("error: poll");
			/* Is this the correct thing to do? */
			return nil;
		}	
		/* timeout */
		if(n == 0)
			continue;

		for(i = 0; i < nfds; i++){
			if(fds[i].revents == 0)
				continue;
			con = &tv->cons[conmap[i]];
			if(fds[i].revents != POLLIN){
				/* When does this happen? */
				GUARD;
				closecon(tv, con);
				UNGUARD;
				continue;
			}
			GUARD;
			/* Check whether it's still the connection
			 * we thought we're using. */
			/* TODO: make this check more sound */
			if(fds[i].fd == con->fd){
				/* All good, talk to the display */
//				printf("handling con %d\n", conmap[i]);
				handlemsg(tv, con);
			}
			UNGUARD;
		}
	}
	return nil;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %rbx
leaq 0x808e0(%rdi), %r14
xorl %ecx, %ecx
xorl %edx, %edx
movl %edx, %r13d
movslq %edx, %rsi
cmpq $0x20, %rcx
jne 0x3bae
testl %r13d, %r13d
je 0x3e00
leaq 0x20(%rsp), %rdi
movl $0xc8, %edx
callq 0x2220
testl %eax, %eax
js 0x3e0f
xorl %ecx, %ecx
movl $0x0, %edx
testl %eax, %eax
jne 0x3bd9
jmp 0x3b75
imulq $0xc, %rcx, %rax
movl (%r14,%rax), %eax
testl %eax, %eax
js 0x3bd1
movl %eax, 0x20(%rsp,%rsi,8)
movw $0x1, 0x24(%rsp,%rsi,8)
movl %ecx, 0x120(%rsp,%rsi,4)
incl %esi
movl %esi, %r13d
incq %rcx
movl %r13d, %edx
jmp 0x3b75
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %ebp, %ebp
cmpq %r13, %rbp
je 0x3b71
movzwl 0x26(%rsp,%rbp,8), %r15d
testw %r15w, %r15w
je 0x3c65
movslq 0x120(%rsp,%rbp,4), %rax
imulq $0xc, %rax, %r12
addq %r14, %r12
leaq 0x963a2(%rip), %rdi     # 0x99fb0
callq 0x2340
cmpw $0x1, %r15w
jne 0x3c4e
movl 0x20(%rsp,%rbp,8), %edi
cmpl (%r12), %edi
jne 0x3c59
leaq 0xa(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x6c82
testl %eax, %eax
jne 0x3c4e
movzwl 0xa(%rsp), %edx
movl (%r12), %edi
leaq 0x963eb(%rip), %rsi     # 0x9a030
callq 0x6c82
testl %eax, %eax
je 0x3c6d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3e84
leaq 0x96350(%rip), %rdi     # 0x99fb0
callq 0x21e0
incq %rbp
jmp 0x3be7
movzbl 0x963bc(%rip), %edx     # 0x9a030
cmpl $0x1, %edx
je 0x3ccd
testl %edx, %edx
jne 0x3de3
testb $0x40, 0x808cc(%rbx)
je 0x3c59
movzwl 0x8(%r12), %r15d
movzwl 0x96399(%rip), %r12d    # 0x9a031
leaq 0x96339(%rip), %rdi     # 0x99fd8
callq 0x2340
orb $-0x80, 0x808cc(%rbx)
cmpl $0x0, 0x808d8(%rbx)
je 0x3db8
leaq 0x96341(%rip), %rdi     # 0x9a000
leaq 0x96312(%rip), %rsi     # 0x99fd8
callq 0x20c0
jmp 0x3cab
movzwl 0x9635d(%rip), %esi     # 0x9a031
movl %esi, 0xc(%rsp)
movzwl 0x96354(%rip), %edx     # 0x9a033
movl %edx, 0x14(%rsp)
movzwl 0x9634a(%rip), %r15d    # 0x9a035
movzwl 0x96344(%rip), %r8d     # 0x9a037
movl %r8d, 0x10(%rsp)
leaq 0xb792(%rip), %rdi      # 0xf491
movl %r15d, %ecx
xorl %eax, %eax
callq 0x20f0
movl 0xc(%rsp), %ecx
movl %ecx, %edx
shrl $0x4, %edx
addl $0xf, %r15d
movl %r15d, %r8d
shrl $0x4, %r8d
movl 0x10(%rsp), %esi
movl %esi, %eax
imull %r8d, %eax
movq %rax, 0x18(%rsp)
leal 0x9(,%rax,2), %eax
movb %al, 0x962f7(%rip)      # 0x9a030
movb %ah, 0x962f2(%rip)      # 0x9a031
movb $0x2, 0x962ec(%rip)     # 0x9a032
movb %dl, 0x962e7(%rip)      # 0x9a033
movl %ecx, %eax
shrl $0xc, %eax
movb %al, 0x962dd(%rip)      # 0x9a034
movl 0x14(%rsp), %ecx
movb %cl, 0x962d4(%rip)      # 0x9a035
movb %ch, 0x962cf(%rip)      # 0x9a036
movb %r8b, 0x962c9(%rip)     # 0x9a037
shrl $0xc, %r15d
movb %r15b, 0x962bf(%rip)    # 0x9a038
movl %esi, %eax
movb %al, 0x962b8(%rip)      # 0x9a039
movb %ah, 0x962b3(%rip)      # 0x9a03a
movl %esi, %r9d
movl 0x4(%r12), %esi
movq %rbx, %rdi
callq 0x3813
movl (%r12), %edi
movq 0x18(%rsp), %rax
leal 0xb(,%rax,2), %edx
leaq 0x96282(%rip), %rsi     # 0x9a030
callq 0x6cc7
jmp 0x3c59
movl $0x1, 0x808d8(%rbx)
movw %r12w, 0x808de(%rbx)
movw %r15w, 0x808dc(%rbx)
leaq 0x961ff(%rip), %rdi     # 0x99fd8
callq 0x21e0
jmp 0x3c59
movq 0xf20e(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0xb6b1(%rip), %rsi      # 0xf4a5
xorl %eax, %eax
callq 0x2190
jmp 0x3c59
movl $0xc8, %edi
callq 0x6ee2
jmp 0x3b71
leaq 0xb658(%rip), %rdi      # 0xf46e
callq 0x2280
xorl %eax, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq