// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x20, -0x14(%rbp)
jge 0x4f29
movq -0x10(%rbp), %rax
addq $0x808e0, %rax          # imm = 0x808E0
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jge 0x4ed7
jmp 0x4f1e
movq -0x10(%rbp), %rax
addq $0x808e0, %rax          # imm = 0x808E0
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movslq -0x18(%rbp), %rax
movl %ecx, -0x120(%rbp,%rax,8)
movslq -0x18(%rbp), %rax
movw $0x1, -0x11c(%rbp,%rax,8)
movl -0x14(%rbp), %ecx
movslq -0x18(%rbp), %rax
movl %ecx, -0x1a0(%rbp,%rax,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x4eb5
cmpl $0x0, -0x18(%rbp)
jne 0x4f3e
movl $0xc8, %edi
callq 0xa9a0
jmp 0x4ea7
leaq -0x120(%rbp), %rdi
movslq -0x18(%rbp), %rsi
movl $0xc8, %edx
callq 0x2220
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x4f73
leaq 0x6392(%rip), %rdi      # 0xb2f5
callq 0x2280
xorl %eax, %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
cmpl $0x0, -0x1c(%rbp)
jne 0x4f7e
jmp 0x4ea7
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x5052
movslq -0x14(%rbp), %rax
movswl -0x11a(%rbp,%rax,8), %eax
cmpl $0x0, %eax
jne 0x4fa7
jmp 0x5044
movq -0x10(%rbp), %rax
addq $0x808e0, %rax          # imm = 0x808E0
movslq -0x14(%rbp), %rcx
movslq -0x1a0(%rbp,%rcx,4), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1a8(%rbp)
movslq -0x14(%rbp), %rax
movswl -0x11a(%rbp,%rax,8), %eax
cmpl $0x1, %eax
je 0x5006
leaq 0x9016d(%rip), %rdi     # 0x95150
callq 0x2330
movq -0x10(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
callq 0x51b0
leaq 0x90151(%rip), %rdi     # 0x95150
callq 0x21e0
jmp 0x5044
leaq 0x90143(%rip), %rdi     # 0x95150
callq 0x2330
movslq -0x14(%rbp), %rax
movl -0x120(%rbp,%rax,8), %eax
movq -0x1a8(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x5038
movq -0x10(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
callq 0x5220
leaq 0x90111(%rip), %rdi     # 0x95150
callq 0x21e0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x4f85
jmp 0x4ea7
nopw (%rax,%rax)