// File: ctSkennerton[P]fxtract/util/concurs.c
// Function: play
static void
play(int skt, const char *file, int timeout)
{
    char	mode[] = { PLAY };
    char	*cp;
    char	expect[MAX_CONCUR_NAME+1+MAX_CONCUR_MSG];
    char	actual[MAX_CONCUR_MSG];
    MAP	conh = map_create((map_diff*)strcmp, (map_hash*)fnv04, NULL);
    FILE	*fp = fopen(file, "r");
    fd_set	fds;

    if (!fp) {
	fprintf(stderr, "error: cannot read %s\n", file);
	exit(2);
    }

    FD_ZERO(&fds);

    while (fgets(expect, sizeof expect, fp)) {
	int	fd, ret;
	struct timeval    to = { timeout, 0 };

	for (cp = expect + strlen(expect); cp > expect && isspace(cp[-1]); *--cp = 0);

	if (!expect[0])
	    continue;

	if (expect[0] == '#') {
	    if (expect[1] != ':') 
		fputs(expect, stdout);
	    continue;
	}

	if (!(cp = strchr(expect, ':'))) {
	    fprintf(stderr, "concurs: invalid script line:\n%s\n", expect);
	    continue;
	}
	*cp++ = 0;

	while (!(fd = (int)(uintptr_t)map_get(conh, expect))) {
	    FD_SET(skt, &fds);
	    if (timeout && 0 > select(skt+1, &fds, NULL, NULL, &to)) {
		fprintf(stderr, "concurs: timed out waiting for %s:connect\n", expect);
		exit(1);
	    }
	    FD_CLR(skt, &fds);
		
	    fd = sock_accept(skt);
	    FD_SET(fd, &open_fds);
	    cp = playerv[fd] = strdup(expect);
	    map_set(conh, playerv[fd], (void*)(uintptr_t)fd);
	}

	FD_SET(fd, &fds);
	if (timeout && 0 > select(skt+1, &fds, NULL, NULL, &to)) {
	    fprintf(stderr, "concurs: error waiting for %s:%s\n", expect, cp);
	    exit(1);
	}
	FD_CLR(fd, &fds);

	if (0 >= (ret = sock_recv(fd, actual, sizeof actual))) {
	    close(fd);
	    FD_CLR(fd, &open_fds);
	    map_del(conh, playerv[fd]);
	    free(playerv[fd]);
	    playerv[fd] = NULL;
	}

	sock_send(fd, mode, sizeof(mode));
	//REVISIT: select(nowait) on all open fds and report who was blocked.
	printf("%s:%s\n", expect, actual);
	if (strcmp(cp, actual)) {
	    fprintf(stderr, "concurs: broken\n");
	    exit(1);
	}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x990, %rsp            # imm = 0x990
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movb 0x5556(%rip), %al       # 0x9131
movb %al, -0x15(%rbp)
movq 0x83fb(%rip), %rdi      # 0xbfe0
leaq 0x5c4(%rip), %rsi       # 0x41b0
xorl %eax, %eax
movl %eax, %edx
callq 0x4670
movq %rax, -0x8b8(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5490(%rip), %rsi      # 0x9097
callq 0x3310
movq %rax, -0x8c0(%rbp)
cmpq $0x0, -0x8c0(%rbp)
jne 0x3c43
movq 0x83b4(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x5467(%rip), %rsi      # 0x9099
movb $0x0, %al
callq 0x3390
movl $0x2, %edi
callq 0x3330
jmp 0x3c45
leaq -0x940(%rbp), %rax
movq %rax, -0x950(%rbp)
movl $0x0, -0x944(%rbp)
movl -0x944(%rbp), %eax
cmpq $0x10, %rax
jae 0x3c8f
movq -0x950(%rbp), %rax
movl -0x944(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x944(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x944(%rbp)
jmp 0x3c5d
jmp 0x3c91
jmp 0x3c93
leaq -0x4b0(%rbp), %rdi
movq -0x8c0(%rbp), %rdx
movl $0x481, %esi            # imm = 0x481
callq 0x33e0
cmpq $0x0, %rax
je 0x419e
movslq -0x14(%rbp), %rax
movq %rax, -0x968(%rbp)
movq $0x0, -0x960(%rbp)
leaq -0x4b0(%rbp), %rax
movq %rax, -0x970(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0x3110
movq %rax, %rcx
movq -0x970(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x4b0(%rbp), %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x971(%rbp)
jbe 0x3d36
callq 0x32c0
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movsbl -0x1(%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
movb %al, -0x971(%rbp)
movb -0x971(%rbp), %al
testb $0x1, %al
jne 0x3d42
jmp 0x3d59
jmp 0x3d44
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, -0x1(%rax)
jmp 0x3cf6
cmpb $0x0, -0x4b0(%rbp)
jne 0x3d67
jmp 0x3c93
movsbl -0x4b0(%rbp), %eax
cmpl $0x23, %eax
jne 0x3d9a
movsbl -0x4af(%rbp), %eax
cmpl $0x3a, %eax
je 0x3d95
leaq -0x4b0(%rbp), %rdi
movq 0x8233(%rip), %rax      # 0xbfc0
movq (%rax), %rsi
callq 0x32f0
jmp 0x3c93
leaq -0x4b0(%rbp), %rdi
movl $0x3a, %esi
callq 0x3100
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x3dd9
movq 0x821c(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq -0x4b0(%rbp), %rdx
leaq 0x52e3(%rip), %rsi      # 0x90b0
movb $0x0, %al
callq 0x3390
jmp 0x3c93
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movb $0x0, (%rax)
movq -0x8b8(%rbp), %rdi
leaq -0x4b0(%rbp), %rsi
callq 0x49c0
movl %eax, -0x954(%rbp)
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3e15
jmp 0x3f87
movl -0x4(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x4(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x940(%rbp,%rax,8), %rcx
movq %rcx, -0x940(%rbp,%rax,8)
cmpl $0x0, -0x14(%rbp)
je 0x3ea3
movl -0x4(%rbp), %edi
addl $0x1, %edi
leaq -0x940(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x968(%rbp), %r8
movq %rcx, %rdx
callq 0x3280
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jle 0x3ea3
movq 0x8157(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq -0x4b0(%rbp), %rdx
leaq 0x5240(%rip), %rsi      # 0x90d2
movb $0x0, %al
callq 0x3390
movl $0x1, %edi
callq 0x3330
movl -0x4(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0x4(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq -0x940(%rbp,%rax,8), %rcx
movq %rcx, -0x940(%rbp,%rax,8)
movl -0x4(%rbp), %edi
callq 0x78c0
movl %eax, -0x954(%rbp)
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x980(%rbp)
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x980(%rbp), %rdx
movslq %eax, %rcx
leaq 0x894c(%rip), %rax      # 0xc878
orq (%rax,%rcx,8), %rdx
leaq 0x8941(%rip), %rax      # 0xc878
movq %rdx, (%rax,%rcx,8)
leaq -0x4b0(%rbp), %rdi
callq 0x3460
movslq -0x954(%rbp), %rdx
leaq 0x89ab(%rip), %rcx      # 0xc900
movq %rax, (%rcx,%rdx,8)
movq %rax, -0x20(%rbp)
movq -0x8b8(%rbp), %rdi
movslq -0x954(%rbp), %rcx
leaq 0x898e(%rip), %rax      # 0xc900
movq (%rax,%rcx,8), %rsi
movslq -0x954(%rbp), %rdx
callq 0x4db0
jmp 0x3deb
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x954(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x940(%rbp,%rax,8), %rcx
movq %rcx, -0x940(%rbp,%rax,8)
cmpl $0x0, -0x14(%rbp)
je 0x401f
movl -0x4(%rbp), %edi
addl $0x1, %edi
leaq -0x940(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x968(%rbp), %r8
movq %rcx, %rdx
callq 0x3280
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jle 0x401f
movq 0x7fdf(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq -0x4b0(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x50ef(%rip), %rsi      # 0x90fd
movb $0x0, %al
callq 0x3390
movl $0x1, %edi
callq 0x3330
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
xorq $-0x1, %rcx
movl -0x954(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
andq -0x940(%rbp,%rax,8), %rcx
movq %rcx, -0x940(%rbp,%rax,8)
movl -0x954(%rbp), %edi
leaq -0x8b0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x8520
movl %eax, %ecx
movl %ecx, -0x958(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
jl 0x4132
movl -0x954(%rbp), %edi
callq 0x34b0
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x988(%rbp)
movl -0x954(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x988(%rbp), %rdx
movslq %eax, %rcx
leaq 0x87a0(%rip), %rax      # 0xc878
andq (%rax,%rcx,8), %rdx
leaq 0x8795(%rip), %rax      # 0xc878
movq %rdx, (%rax,%rcx,8)
movq -0x8b8(%rbp), %rdi
movslq -0x954(%rbp), %rcx
leaq 0x8804(%rip), %rax      # 0xc900
movq (%rax,%rcx,8), %rsi
callq 0x4720
movslq -0x954(%rbp), %rcx
leaq 0x87ed(%rip), %rax      # 0xc900
movq (%rax,%rcx,8), %rdi
callq 0x3320
movslq -0x954(%rbp), %rcx
leaq 0x87d6(%rip), %rax      # 0xc900
movq $0x0, (%rax,%rcx,8)
movl -0x954(%rbp), %edi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x8580
leaq -0x4b0(%rbp), %rsi
leaq -0x8b0(%rbp), %rdx
leaq 0x4fbd(%rip), %rdi      # 0x9118
movb $0x0, %al
callq 0x3050
movq -0x20(%rbp), %rdi
leaq -0x8b0(%rbp), %rsi
callq 0x34c8
cmpl $0x0, %eax
je 0x4199
movq 0x7e5a(%rip), %rax      # 0xbfd8
movq (%rax), %rdi
leaq 0x4f97(%rip), %rsi      # 0x911f
movb $0x0, %al
callq 0x3390
movl $0x1, %edi
callq 0x3330
jmp 0x3c93
addq $0x990, %rsp            # imm = 0x990
popq %rbp
retq
nopw (%rax,%rax)