// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/mmulti.c
// Function: initmultiplayerscycle
int initmultiplayerscycle(void)
{
	int i, k, dnetready = 1;

	getpacket(&i,0);

	tims = GetTickCount();

	if (networkmode == MMULTI_MODE_MS && myconnectindex == connecthead)
	{
		// The master waits for all players to check in.
		for(i=numplayers-1;i>0;i--) {
			if (i == myconnectindex) continue;
			if (otherhost[i].ss_family == AF_UNSPEC) {
				// There's a slot to be filled.
				dnetready = 0;
			} else if (lastrecvtims[i] == 0) {
				// There's a player who hasn't checked in.
				dnetready = 0;
			} else if (prevlastrecvtims[i] != lastrecvtims[i]) {
				if (!playerslive[i]) {
					printf("mmulti: Player %d is here\n", i);
					playerslive[i] = 1;
				}
				prevlastrecvtims[i] = lastrecvtims[i];
			} else if (tims - lastrecvtims[i] > PRESENCETIMEOUT) {
				if (playerslive[i]) {
					printf("mmulti: Player %d has gone\n", i);
					playerslive[i] = 0;
				}
				prevlastrecvtims[i] = lastrecvtims[i];
				dnetready = 0;
			}
		}
	}
	else
	{
		if (networkmode == MMULTI_MODE_MS) {
			// As a slave, we send the master pings. The netready flag gets
			// set by getpacket() and is sent by the master when it is OK
			// to launch.
			dnetready = 0;
			i = connecthead;

		} else {
			// In peer-to-peer mode we send pings to our peer group members
			// and wait for them all to check in. The netready flag is set
			// when everyone responds together within the timeout.
			i = numplayers - 1;
		}
		while (1) {
			if (i != myconnectindex) {
				if (tims < lastsendtims[i]) lastsendtims[i] = tims;
				if (tims >= lastsendtims[i]+250) //1000/PAKRATE)
				{
#ifdef MMULTI_DEBUG_SENDRECV
					debugprintf("mmulti debug: sending player %d a ping\n", i);
#endif

					lastsendtims[i] = tims;

						//   short crc16ofs;       //offset of crc16
						//   int icnt0;           //-1 (special packet for MMULTI.C's player collection)
						//   ...
						//   unsigned short crc16; //CRC16 of everything except crc16
					k = 2;
					*(int *)&pakbuf[k] = -1; k += 4;
					pakbuf[k++] = 0xaa;
					*(unsigned short *)&pakbuf[0] = (unsigned short)k;
					*(unsigned short *)&pakbuf[k] = getcrc16(pakbuf,k); k += 2;
					netsend(i,pakbuf,k);
				}

				if (lastrecvtims[i] == 0) {
					// There's a player who hasn't checked in.
					dnetready = 0;
				} else if (prevlastrecvtims[i] != lastrecvtims[i]) {
					if (!playerslive[i]) {
						printf("mmulti: Player %d is here\n", i);
						playerslive[i] = 1;
					}
					prevlastrecvtims[i] = lastrecvtims[i];
				} else if (prevlastrecvtims[i] - tims > PRESENCETIMEOUT) {
					if (playerslive[i]) {
						printf("mmulti: Player %d has gone\n", i);
						playerslive[i] = 0;
					}
					prevlastrecvtims[i] = lastrecvtims[i];
					dnetready = 0;
				}
			}

			if (networkmode == MMULTI_MODE_MS) {
				break;
			} else {
				if (--i < 0) break;
			}
		}
	}

	netready = netready || dnetready;

	return !netready;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0x14(%rsp), %rdi
xorl %esi, %esi
callq 0x5e6cb
callq 0x5ee15
movl %eax, 0x747796(%rip)    # 0x7a5b84
movl 0x85474(%rip), %edx     # 0xe3868
movl 0x747732(%rip), %ebx    # 0x7a5b2c
leal -0x1(%rbx), %eax
movl 0x74772d(%rip), %ecx    # 0x7a5b30
testl %edx, %edx
jne 0x5e54a
cmpl %ecx, 0x747713(%rip)    # 0x7a5b24
jne 0x5e54a
movl $0x1, %r14d
cmpl $0x2, %ebx
jl 0x5e6a5
movl %eax, %edx
movq %rdx, %rax
shlq $0x7, %rax
leaq 0x746d1a(%rip), %r13    # 0x7a5150
addq %rax, %r13
leaq (,%rdx,4), %rsi
movl $0x1, %ebp
leaq 0x747743(%rip), %rdi    # 0x7a5b90
leaq 0x74777c(%rip), %r8     # 0x7a5bd0
leaq 0x7477b5(%rip), %r9     # 0x7a5c10
xorl %r15d, %r15d
movq %rdx, 0x8(%rsp)
movq %rsi, (%rsp)
decl %ebx
cmpl 0x7476b5(%rip), %ebx    # 0x7a5b24
je 0x5e52f
xorl %r14d, %r14d
cmpw $0x0, (%r13)
je 0x5e532
leaq (%rsi,%r15,4), %rcx
movl (%rdi,%rcx), %eax
testl %eax, %eax
je 0x5e532
leaq (,%r15,4), %r12
cmpl %eax, (%r8,%rcx)
jne 0x5e4df
movl 0x7476dd(%rip), %ecx    # 0x7a5b84
subl %eax, %ecx
movl %ebp, %r14d
cmpl $0x7d1, %ecx            # imm = 0x7D1
jl 0x5e532
leaq (%rdx,%r15), %r14
cmpb $0x0, (%r9,%r14)
je 0x5e526
xorl %ebp, %ebp
leaq 0x4893f(%rip), %rdi     # 0xa6e07
movl %ebx, %esi
xorl %eax, %eax
callq 0x372d1
leaq 0x747738(%rip), %r9     # 0x7a5c10
movb $0x0, (%r9,%r14)
jmp 0x5e506
leaq (%rdx,%r15), %r14
cmpb $0x0, (%r9,%r14)
jne 0x5e528
leaq 0x488fb(%rip), %rdi     # 0xa6dec
movl %ebx, %esi
xorl %eax, %eax
callq 0x372d1
leaq 0x74770f(%rip), %r9     # 0x7a5c10
movb $0x1, (%r9,%r14)
movq (%rsp), %rsi
leaq (%rsi,%r12), %rax
leaq 0x74767b(%rip), %rdi    # 0x7a5b90
movl (%rdi,%rax), %eax
movq 0x8(%rsp), %rdx
leaq 0x7476ac(%rip), %r8     # 0x7a5bd0
jmp 0x5e528
xorl %ebp, %ebp
addq %rsi, %r12
movl %eax, (%r8,%r12)
movl %ebp, %r14d
addq $-0x80, %r13
decq %r15
movl %r14d, %ebp
cmpl $0x1, %ebx
jg 0x5e467
jmp 0x5e6a5
xorl %r14d, %r14d
testl %edx, %edx
setne %r14b
cmovnel %eax, %ecx
movslq %ecx, %rbx
leaq 0x7476c0(%rip), %r13    # 0x7a5c20
leaq 0x747629(%rip), %rbp    # 0x7a5b90
leaq 0x7476a2(%rip), %rdx    # 0x7a5c10
leaq 0x7476eb(%rip), %r12    # 0x7a5c60
leaq 0xb578b4(%rip), %r15    # 0xbb5e30
cmpl %ebx, 0x7475a2(%rip)    # 0x7a5b24
je 0x5e68c
movl 0x7475f6(%rip), %eax    # 0x7a5b84
movl (%r13,%rbx,4), %ecx
cmpl %ecx, %eax
jge 0x5e59e
movl %eax, (%r13,%rbx,4)
movl %eax, %ecx
addl $0xfa, %ecx
cmpl %ecx, %eax
jl 0x5e609
movl %eax, (%r13,%rbx,4)
movl $0xffffffff, 0x7476ab(%rip) # imm = 0xFFFFFFFF
movb $-0x56, 0x7476a8(%rip)  # 0x7a5c66
movw $0x7, 0x747699(%rip)    # 0x7a5c60
xorl %ecx, %ecx
movl $0x8, %eax
movzbl %ch, %edx
shll $0x8, %ecx
movzwl %cx, %ecx
movzbl -0x2(%rax,%r12), %esi
xorl %edx, %esi
xorl (%r15,%rsi,4), %ecx
decq %rax
cmpq $0x1, %rax
ja 0x5e5ce
movw %cx, 0x747674(%rip)     # 0x7a5c67
movl %ebx, %edi
movq %r12, %rsi
movl $0x9, %edx
callq 0x5d727
leaq 0x747607(%rip), %rdx    # 0x7a5c10
movl (%rbp,%rbx,4), %eax
testl %eax, %eax
je 0x5e653
leaq 0x7475b8(%rip), %rcx    # 0x7a5bd0
cmpl %eax, (%rcx,%rbx,4)
jne 0x5e658
movl %eax, %ecx
subl 0x74755f(%rip), %ecx    # 0x7a5b84
cmpl $0x7d1, %ecx            # imm = 0x7D1
jl 0x5e68c
cmpb $0x0, (%rbx,%rdx)
je 0x5e67f
xorl %r14d, %r14d
leaq 0x487ca(%rip), %rdi     # 0xa6e07
movl %ebx, %esi
xorl %eax, %eax
callq 0x372d1
leaq 0x7475c3(%rip), %rdx    # 0x7a5c10
movb $0x0, (%rbx,%rdx)
jmp 0x5e679
xorl %r14d, %r14d
jmp 0x5e68c
cmpb $0x0, (%rbx,%rdx)
jne 0x5e682
leaq 0x48787(%rip), %rdi     # 0xa6dec
movl %ebx, %esi
xorl %eax, %eax
callq 0x372d1
leaq 0x74759b(%rip), %rdx    # 0x7a5c10
movb $0x1, (%rbx,%rdx)
movl (%rbp,%rbx,4), %eax
jmp 0x5e682
xorl %r14d, %r14d
leaq 0x747547(%rip), %rcx    # 0x7a5bd0
movl %eax, (%rcx,%rbx,4)
cmpl $0x0, 0x851d5(%rip)     # 0xe3868
je 0x5e6a5
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x5e57c
xorl %eax, %eax
xorl %ecx, %ecx
orl 0x7474d0(%rip), %r14d   # 0x7a5b80
sete %al
setne %cl
movl %ecx, 0x7474c4(%rip)    # 0x7a5b80
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq