// File: /workspace/github_c_makefile_star2_O1/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 $0x1c8, %rsp            # imm = 0x1C8
leaq 0x808e0(%rdi), %r12
leaq 0x40(%rsp), %r15
xorl %eax, %eax
xorl %ecx, %ecx
movq %rdi, 0x38(%rsp)
imulq $0xc, %rax, %rdx
movl (%r12,%rdx), %edx
testl %edx, %edx
js 0x242a
movslq %ecx, %r14
movl %edx, 0x40(%rsp,%r14,8)
movw $0x1, 0x44(%rsp,%r14,8)
movl %eax, 0x140(%rsp,%r14,4)
incl %r14d
movl %r14d, %ecx
jmp 0x242d
movl %ecx, %r14d
incq %rax
cmpq $0x20, %rax
jne 0x23fe
testl %r14d, %r14d
je 0x274a
movslq %r14d, %rsi
movq %r15, %rdi
movl $0xc8, %edx
callq 0x2220
testl %eax, %eax
js 0x276b
sete %cl
testl %r14d, %r14d
setle %dl
xorl %eax, %eax
orb %cl, %dl
movl $0x0, %ecx
jne 0x23fe
movl %r14d, %ebp
xorl %r14d, %r14d
leaq 0xe002(%rip), %r15      # 0x1047a
movzwl 0x46(%rsp,%r14,8), %ebx
testw %bx, %bx
je 0x253c
movslq 0x140(%rsp,%r14,4), %rax
imulq $0xc, %rax, %r13
addq %r12, %r13
leaq 0x97b13(%rip), %rdi     # 0x99fb0
callq 0x2340
cmpw $0x1, %bx
jne 0x24fe
movl (%r13), %edi
cmpl %edi, 0x40(%rsp,%r14,8)
jne 0x2530
leaq 0x16(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x7154
testl %eax, %eax
movq 0x38(%rsp), %rbx
jne 0x24e2
movzwl 0x16(%rsp), %edx
movl (%r13), %edi
leaq 0x97b57(%rip), %rsi     # 0x9a030
callq 0x7154
testl %eax, %eax
je 0x254d
movl 0x4(%r13), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20f0
movslq 0x4(%r13), %rax
orl $-0x1, 0x80a60(%rbx,%rax,4)
jmp 0x251d
movl 0x4(%r13), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20f0
movslq 0x4(%r13), %rax
movq 0x38(%rsp), %rcx
orl $-0x1, 0x80a60(%rcx,%rax,4)
movl (%r13), %edi
callq 0x2140
orq $-0x1, (%r13)
orl $-0x1, 0x8(%r13)
leaq 0x97a79(%rip), %rdi     # 0x99fb0
callq 0x21e0
incq %r14
cmpq %rbp, %r14
jne 0x2478
jmp 0x275d
movzbl 0x97adc(%rip), %edx     # 0x9a030
cmpl $0x1, %edx
je 0x25f4
testl %edx, %edx
jne 0x272d
testb $0x40, 0x808cc(%rbx)
je 0x2530
movzwl 0x8(%r13), %eax
movw %ax, 0x10(%rsp)
movzwl 0x97ab2(%rip), %eax     # 0x9a031
movw %ax, 0xc(%rsp)
leaq 0x97a4d(%rip), %r13     # 0x99fd8
movq %r13, %rdi
callq 0x2340
orb $-0x80, 0x808cc(%rbx)
cmpl $0x0, 0x808d8(%rbx)
leaq 0x97a58(%rip), %r15     # 0x9a000
je 0x25be
movq %r15, %rdi
movq %r13, %rsi
callq 0x20c0
cmpl $0x0, 0x808d8(%rbx)
jne 0x25aa
movl $0x1, 0x808d8(%rbx)
movzwl 0xc(%rsp), %eax
movw %ax, 0x808de(%rbx)
movzwl 0x10(%rsp), %eax
movw %ax, 0x808dc(%rbx)
movq %r13, %rdi
callq 0x21e0
leaq 0xde8b(%rip), %r15      # 0x1047a
jmp 0x2530
movzbl 0x97a36(%rip), %eax     # 0x9a031
movzbl 0x97a30(%rip), %ecx     # 0x9a032
movl %ecx, 0x10(%rsp)
shll $0x8, %ecx
orl %eax, %ecx
movl %ecx, %esi
movl %ecx, 0x18(%rsp)
movzbl 0x97a1b(%rip), %ecx     # 0x9a033
movl %ecx, 0x2c(%rsp)
movzbl 0x97a11(%rip), %eax     # 0x9a034
movl %eax, 0xc(%rsp)
shll $0x8, %eax
orl %ecx, %eax
movl %eax, %edx
movl %eax, 0x30(%rsp)
movzwl 0x979fc(%rip), %ecx     # 0x9a035
movl %ecx, 0x20(%rsp)
movzbl 0x979f3(%rip), %eax     # 0x9a037
movl %eax, 0x28(%rsp)
movzbl 0x979e8(%rip), %r8d     # 0x9a038
movl %r8d, 0x34(%rsp)
shll $0x8, %r8d
orl %eax, %r8d
movl %r8d, 0x24(%rsp)
leaq 0xde29(%rip), %rdi      # 0x10491
xorl %eax, %eax
callq 0x20f0
movl 0x18(%rsp), %edx
shrl $0x4, %edx
movl 0x20(%rsp), %eax
addl $0xf, %eax
movl %eax, %r8d
movl %eax, %ecx
shrl $0x4, %r8d
movl %r8d, %eax
movl 0x24(%rsp), %r9d
imull %r9d, %eax
movq %rax, 0x18(%rsp)
leal 0x9(,%rax,2), %eax
movb %al, 0x9798c(%rip)      # 0x9a030
movb %ah, 0x97987(%rip)      # 0x9a031
movb $0x2, 0x97981(%rip)     # 0x9a032
movb %dl, 0x9797c(%rip)      # 0x9a033
movl 0x10(%rsp), %eax
shrb $0x4, %al
movb %al, 0x97970(%rip)      # 0x9a034
movl 0x2c(%rsp), %eax
movb %al, 0x97967(%rip)      # 0x9a035
movl 0xc(%rsp), %eax
movb %al, 0x9795e(%rip)      # 0x9a036
movb %r8b, 0x97958(%rip)     # 0x9a037
shrl $0xc, %ecx
movb %cl, 0x97950(%rip)      # 0x9a038
movl 0x28(%rsp), %eax
movb %al, 0x97947(%rip)      # 0x9a039
movl 0x34(%rsp), %eax
movb %al, 0x9793e(%rip)      # 0x9a03a
movl 0x4(%r13), %esi
movq %rbx, %rdi
movl 0x30(%rsp), %ecx
callq 0x3ed2
movl (%r13), %edi
movq 0x18(%rsp), %rax
leal 0xb(,%rax,2), %edx
leaq 0x9790d(%rip), %rsi     # 0x9a030
callq 0x719d
jmp 0x2530
movq 0x108c4(%rip), %rax     # 0x12ff8
movq (%rax), %rdi
leaq 0xdd67(%rip), %rsi      # 0x104a5
xorl %eax, %eax
callq 0x2190
jmp 0x2530
movl $0xc8, %edi
callq 0x72dd
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x23fe
xorl %eax, %eax
xorl %ecx, %ecx
leaq 0x40(%rsp), %r15
jmp 0x23fe
leaq 0xdcfc(%rip), %rdi      # 0x1046e
callq 0x2280
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq