// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rsp, %rdi
xorl %esi, %esi
callq 0x5b437
callq 0x5ba97
movl %eax, 0x7479e3(%rip)    # 0x7a2b64
movl 0x856e1(%rip), %eax     # 0xe0868
testl %eax, %eax
jne 0x5b27f
movl 0x74796f(%rip), %ecx    # 0x7a2b04
cmpl 0x747975(%rip), %ecx    # 0x7a2b10
jne 0x5b27f
movl 0x747965(%rip), %eax    # 0x7a2b0c
leal -0x1(%rax), %esi
movl %esi, (%rsp)
movl $0x1, %edi
cmpl $0x2, %eax
jl 0x5b412
movl (%rsp), %eax
leaq 0x746f6b(%rip), %r15    # 0x7a2130
leaq 0x7479a4(%rip), %rbx    # 0x7a2b70
leaq 0x7479dd(%rip), %r8     # 0x7a2bb0
leaq 0x747a16(%rip), %r14    # 0x7a2bf0
movl %edi, %ebp
movl %eax, %r13d
cmpl 0x74791f(%rip), %esi    # 0x7a2b04
je 0x5b265
movl %esi, %eax
movq %rax, %rcx
shlq $0x7, %rcx
xorl %edi, %edi
cmpw $0x0, (%rcx,%r15)
je 0x5b267
movl (%rbx,%rax,4), %ecx
testl %ecx, %ecx
je 0x5b267
movslq %esi, %r12
cmpl %ecx, (%r8,%rax,4)
jne 0x5b233
movl 0x747954(%rip), %edx    # 0x7a2b64
subl %ecx, %edx
movl %ebp, %edi
cmpl $0x7d1, %edx            # imm = 0x7D1
jl 0x5b267
xorl %ecx, %ecx
cmpb $0x0, (%rax,%r14)
leaq 0x4883d(%rip), %rdi     # 0xa3a67
movl $0x0, %ebp
jne 0x5b243
jmp 0x5b25d
cmpb $0x0, (%rax,%r14)
jne 0x5b25d
movb $0x1, %cl
leaq 0x48809(%rip), %rdi     # 0xa3a4c
movl %ecx, 0x4(%rsp)
xorl %eax, %eax
callq 0x36050
leaq 0x74795b(%rip), %r8     # 0x7a2bb0
movl 0x4(%rsp), %eax
movb %al, (%r12,%r14)
movl (%rbx,%r12,4), %eax
movl %eax, (%r8,%r12,4)
movl %ebp, %edi
leal -0x1(%r13), %eax
movl %eax, %esi
cmpl $0x1, %r13d
jg 0x5b1da
movl %eax, (%rsp)
jmp 0x5b412
xorl %ebp, %ebp
testl %eax, %eax
setne %al
jne 0x5b290
movl 0x747882(%rip), %ecx    # 0x7a2b10
jmp 0x5b298
movl 0x747876(%rip), %ecx    # 0x7a2b0c
decl %ecx
movb %al, %bpl
movl %ecx, (%rsp)
movslq %ecx, %rbx
movl %ebx, %r13d
sarl $0x1f, %r13d
andl %ebx, %r13d
decl %r13d
leaq 0x74794b(%rip), %r14    # 0x7a2c00
leaq 0x7478b4(%rip), %rsi    # 0x7a2b70
leaq 0x74792d(%rip), %rdi    # 0x7a2bf0
leaq 0x747976(%rip), %r12    # 0x7a2c40
leaq 0xb57b3f(%rip), %r15    # 0xbb2e10
cmpl %ebx, 0x74782d(%rip)    # 0x7a2b04
je 0x5b3ee
movl 0x747881(%rip), %eax    # 0x7a2b64
cmpl (%r14,%rbx,4), %eax
jge 0x5b2ed
movl %eax, (%r14,%rbx,4)
movl (%r14,%rbx,4), %ecx
movl $0xfa, %edx
addl %edx, %ecx
cmpl %ecx, %eax
jl 0x5b363
movl %eax, (%r14,%rbx,4)
movl $0xffffffff, 0x747938(%rip) # imm = 0xFFFFFFFF
movb $-0x56, 0x747935(%rip)  # 0x7a2c46
movw $0x7, 0x747926(%rip)    # 0x7a2c40
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 0x5b321
movw %cx, 0x747901(%rip)     # 0x7a2c47
movl %ebx, %edi
movq %r12, %rsi
movl $0x9, %edx
callq 0x5a549
leaq 0x747814(%rip), %rsi    # 0x7a2b70
leaq 0x74788d(%rip), %rdi    # 0x7a2bf0
movl (%rsi,%rbx,4), %ecx
testl %ecx, %ecx
je 0x5b3b1
leaq 0x74783f(%rip), %rax    # 0x7a2bb0
movl (%rax,%rbx,4), %eax
cmpl %ecx, %eax
jne 0x5b3b5
subl 0x7477e6(%rip), %eax    # 0x7a2b64
cmpl $0x7d1, %eax            # imm = 0x7D1
jl 0x5b3ee
cmpb $0x0, (%rbx,%rdi)
je 0x5b3df
xorl %ebp, %ebp
leaq 0x486d3(%rip), %rdi     # 0xa3a67
movl %ebx, %esi
xorl %eax, %eax
callq 0x36050
leaq 0x74784c(%rip), %rdi    # 0x7a2bf0
leaq 0x7477c5(%rip), %rsi    # 0x7a2b70
movb $0x0, (%rbx,%rdi)
jmp 0x5b3e1
xorl %ebp, %ebp
jmp 0x5b3ee
cmpb $0x0, (%rbx,%rdi)
jne 0x5b3e1
leaq 0x4868a(%rip), %rdi     # 0xa3a4c
movl %ebx, %esi
xorl %eax, %eax
callq 0x36050
leaq 0x74781e(%rip), %rdi    # 0x7a2bf0
leaq 0x747797(%rip), %rsi    # 0x7a2b70
movb $0x1, (%rbx,%rdi)
jmp 0x5b3e1
xorl %ebp, %ebp
movl (%rsi,%rbx,4), %eax
leaq 0x7477c5(%rip), %rcx    # 0x7a2bb0
movl %eax, (%rcx,%rbx,4)
cmpl $0x0, 0x85473(%rip)     # 0xe0868
je 0x5b409
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0x5b2d1
jmp 0x5b40c
movl %ebx, %r13d
movl %r13d, (%rsp)
movl %ebp, %edi
xorl %eax, %eax
xorl %ecx, %ecx
orl 0x747744(%rip), %edi    # 0x7a2b60
sete %al
setne %cl
movl %ecx, 0x747738(%rip)    # 0x7a2b60
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq