// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x808e0(%rdi), %rbp
leaq 0x20(%rsp), %r15
xorl %eax, %eax
xorl %ecx, %ecx
movq %rdi, 0x18(%rsp)
imulq $0xc, %rax, %rdx
movl (%rbp,%rdx), %edx
testl %edx, %edx
js 0x242a
movslq %ecx, %r14
movl %edx, 0x20(%rsp,%r14,8)
movw $0x1, 0x24(%rsp,%r14,8)
movl %eax, 0x120(%rsp,%r14,4)
incl %r14d
movl %r14d, %ecx
jmp 0x242d
movl %ecx, %r14d
incq %rax
cmpq $0x20, %rax
jne 0x23fe
testl %r14d, %r14d
je 0x26f6
movslq %r14d, %rsi
movq %r15, %rdi
movl $0xc8, %edx
callq 0x2220
testl %eax, %eax
js 0x2717
sete %cl
testl %r14d, %r14d
setle %dl
xorl %eax, %eax
orb %cl, %dl
movl $0x0, %ecx
jne 0x23fe
movl %r14d, %r13d
xorl %r12d, %r12d
leaq 0xe002(%rip), %r15      # 0x1047a
movzwl 0x26(%rsp,%r12,8), %ebx
testw %bx, %bx
je 0x2538
movslq 0x120(%rsp,%r12,4), %rax
imulq $0xc, %rax, %r14
addq %rbp, %r14
leaq 0x97b13(%rip), %rdi     # 0x99fb0
callq 0x2340
cmpw $0x1, %bx
jne 0x24fc
movl 0x20(%rsp,%r12,8), %edi
cmpl (%r14), %edi
jne 0x252c
leaq 0x6(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x7050
testl %eax, %eax
movq 0x18(%rsp), %rbx
jne 0x24e0
movzwl 0x6(%rsp), %edx
movl (%r14), %edi
leaq 0x97b59(%rip), %rsi     # 0x9a030
callq 0x7050
testl %eax, %eax
je 0x2549
movl 0x4(%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20f0
movslq 0x4(%r14), %rax
orl $-0x1, 0x80a60(%rbx,%rax,4)
jmp 0x251b
movl 0x4(%r14), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20f0
movslq 0x4(%r14), %rax
movq 0x18(%rsp), %rcx
orl $-0x1, 0x80a60(%rcx,%rax,4)
movl (%r14), %edi
callq 0x2140
orq $-0x1, (%r14)
orl $-0x1, 0x8(%r14)
leaq 0x97a7d(%rip), %rdi     # 0x99fb0
callq 0x21e0
incq %r12
cmpq %r13, %r12
jne 0x2478
jmp 0x2709
movzbl 0x97ae0(%rip), %edx     # 0x9a030
cmpl $0x1, %edx
je 0x25ee
testl %edx, %edx
jne 0x26d9
testb $0x40, 0x808cc(%rbx)
je 0x252c
movzwl 0x8(%r14), %eax
movw %ax, 0x8(%rsp)
movzwl 0x97ab6(%rip), %eax     # 0x9a031
movw %ax, (%rsp)
leaq 0x97a52(%rip), %r14     # 0x99fd8
movq %r14, %rdi
callq 0x2340
orb $-0x80, 0x808cc(%rbx)
cmpl $0x0, 0x808d8(%rbx)
leaq 0x97a5d(%rip), %r15     # 0x9a000
je 0x25b9
movq %r15, %rdi
movq %r14, %rsi
callq 0x20c0
cmpl $0x0, 0x808d8(%rbx)
jne 0x25a5
movl $0x1, 0x808d8(%rbx)
movzwl (%rsp), %eax
movw %ax, 0x808de(%rbx)
movzwl 0x8(%rsp), %eax
movw %ax, 0x808dc(%rbx)
movq %r14, %rdi
callq 0x21e0
leaq 0xde91(%rip), %r15      # 0x1047a
jmp 0x252c
movzwl 0x97a3c(%rip), %esi     # 0x9a031
movl %esi, 0x10(%rsp)
movzwl 0x97a33(%rip), %edx     # 0x9a033
movl %edx, (%rsp)
movzwl 0x97a2b(%rip), %ecx     # 0x9a035
movl %ecx, 0x8(%rsp)
movzwl 0x97a21(%rip), %r8d     # 0x9a037
movl %r8d, 0x14(%rsp)
leaq 0xde6f(%rip), %rdi      # 0x10491
xorl %eax, %eax
callq 0x20f0
movl 0x10(%rsp), %ecx
movl %ecx, %edx
shrl $0x4, %edx
movl 0x8(%rsp), %eax
addl $0xf, %eax
movl %eax, %r8d
movl %eax, %esi
shrl $0x4, %r8d
movl 0x14(%rsp), %edi
movl %edi, %eax
imull %r8d, %eax
movq %rax, 0x8(%rsp)
leal 0x9(,%rax,2), %eax
movb %al, 0x979d2(%rip)      # 0x9a030
movb %ah, 0x979cd(%rip)      # 0x9a031
movb $0x2, 0x979c7(%rip)     # 0x9a032
movb %dl, 0x979c2(%rip)      # 0x9a033
movl %ecx, %eax
shrl $0xc, %eax
movb %al, 0x979b8(%rip)      # 0x9a034
movl (%rsp), %ecx
movb %cl, 0x979b0(%rip)      # 0x9a035
movb %ch, 0x979ab(%rip)      # 0x9a036
movb %r8b, 0x979a5(%rip)     # 0x9a037
shrl $0xc, %esi
movb %sil, 0x9799c(%rip)     # 0x9a038
movl %edi, %eax
movb %al, 0x97995(%rip)      # 0x9a039
movb %ah, 0x97990(%rip)      # 0x9a03a
movl %edi, %r9d
movl 0x4(%r14), %esi
movq %rbx, %rdi
callq 0x3cc8
movl (%r14), %edi
movq 0x8(%rsp), %rax
leal 0xb(,%rax,2), %edx
leaq 0x97961(%rip), %rsi     # 0x9a030
callq 0x7099
jmp 0x252c
movq 0x10918(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
leaq 0xddbb(%rip), %rsi      # 0x104a5
xorl %eax, %eax
callq 0x2190
jmp 0x252c
movl $0xc8, %edi
callq 0x72ec
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x23fe
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x20(%rsp), %r15
jmp 0x23fe
leaq 0xdd50(%rip), %rdi      # 0x1046e
callq 0x2280
xorl %eax, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)