// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/mmulti.c
// Function: netread
int netread (int *other, void *dabuf, int bufsiz) //0:no packets in buffer
{
	char msg_control[1024];
	int i;

	if (IS_INVALID_SOCKET(mysock)) {
#ifdef MMULTI_DEBUG_SENDRECV_WIRE
		debugprintf("mmulti debug recv error: invalid socket\n");
#endif
		return 0;
	}

#ifdef _WIN32
	WSABUF iovec;
	WSAMSG msg;
	WSACMSGHDR *cmsg;
	DWORD len = 0;

	iovec.buf = dabuf;
	iovec.len = bufsiz;
	msg.name = (LPSOCKADDR)&snatchhost;
	msg.namelen = sizeof(snatchhost);
	msg.lpBuffers = &iovec;
	msg.dwBufferCount = 1;
	msg.Control.buf = msg_control;
	msg.Control.len = sizeof(msg_control);
	msg.dwFlags = 0;

	if (WSARecvMsgPtr(mysock, &msg, &len, NULL, NULL) == SOCKET_ERROR) return 0;
#else
	struct iovec iovec;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	int len;

	iovec.iov_base = dabuf;
	iovec.iov_len = bufsiz;
	msg.msg_name = &snatchhost;
	msg.msg_namelen = sizeof(snatchhost);
	msg.msg_iov = &iovec;
	msg.msg_iovlen = 1;
	msg.msg_control = msg_control;
	msg.msg_controllen = sizeof(msg_control);
	msg.msg_flags = 0;

	if ((len = (int)recvmsg(mysock, &msg, 0)) < 0) return 0;
#endif
	if (len == 0) return 0;

	if (snatchhost.ss_family != domain) {
#ifdef MMULTI_DEBUG_SENDRECV_WIRE
		debugprintf("mmulti debug recv error: received from a different protocol family\n");
#endif
		return 0;
	}

#if (SIMMIS > 0)
	if ((rand()&255) < SIMMIS) return(0);
#endif

	// Decode the message headers to record what of our IP addresses the
	// packet came in on. We reply on that same address so the peer knows
	// who it came from.
	memset(&snatchreplyfrom4, 0, sizeof(snatchreplyfrom4));
	memset(&snatchreplyfrom6, 0, sizeof(snatchreplyfrom6));
	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
#if defined(__linux) || defined(_WIN32)
		if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
			snatchreplyfrom4 = ((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_addr;
#ifdef MMULTI_DEBUG_SENDRECV_WIRE
			debugprintf("mmulti debug recv: received at %s\n", inet_ntoa(snatchreplyfrom4));
#endif
		}
#else
		if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR) {
			snatchreplyfrom4 = *(struct in_addr *)CMSG_DATA(cmsg);
#ifdef MMULTI_DEBUG_SENDRECV_WIRE
			debugprintf("mmulti debug recv: received at %s\n", inet_ntoa(snatchreplyfrom4));
#endif
		}
#endif
		else if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
			snatchreplyfrom6 = ((struct in6_pktinfo *)CMSG_DATA(cmsg))->ipi6_addr;
#ifdef MMULTI_DEBUG_SENDRECV_WIRE
			char addr[INET6_ADDRSTRLEN+1];
			debugprintf("mmulti debug recv: received at %s\n", inet_ntop(AF_INET6, &snatchreplyfrom6, addr, sizeof(addr)));
#endif
		}
	}

#ifdef MMULTI_DEBUG_SENDRECV_WIRE
	{
		const unsigned char *pakbuf = dabuf;
		debugprintf("mmulti debug recv: ");
		for(i=0;i<len;i++) debugprintf("%02x ",pakbuf[i]);
		debugprintf("\n");
	}
#endif

	(*other) = myconnectindex;
	for(i=0;i<MAXPLAYERS;i++) {
		if (issameaddress((struct sockaddr *)&snatchhost, (struct sockaddr *)&otherhost[i]))
			{ (*other) = i; break; }
	}

#if (SIMLAG > 1)
	i = simlagcnt[*other]%(SIMLAG+1);
	*(short *)&simlagfif[*other][i][0] = bufsiz; memcpy(&simlagfif[*other][i][2],dabuf,bufsiz);
	simlagcnt[*other]++; if (simlagcnt[*other] < SIMLAG+1) return(0);
	i = simlagcnt[*other]%(SIMLAG+1);
	bufsiz = *(short *)&simlagfif[*other][i][0]; memcpy(dabuf,&simlagfif[*other][i][2],bufsiz);
#endif

	return(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x0, 0xa4f57(%rip)     # 0x12c104
jge 0x871bb
movl $0x0, -0x4(%rbp)
jmp 0x873da
movq -0x18(%rbp), %rax
movq %rax, -0x438(%rbp)
movslq -0x1c(%rbp), %rax
movq %rax, -0x430(%rbp)
leaq 0x71dcd8(%rip), %rax    # 0x7a4eb0
movq %rax, -0x470(%rbp)
movl $0x80, -0x468(%rbp)
leaq -0x438(%rbp), %rax
movq %rax, -0x460(%rbp)
movq $0x1, -0x458(%rbp)
leaq -0x420(%rbp), %rax
movq %rax, -0x450(%rbp)
movq $0x400, -0x448(%rbp) # imm = 0x400
movl $0x0, -0x440(%rbp)
movl 0xa4ed9(%rip), %edi     # 0x12c104
leaq -0x470(%rbp), %rsi
xorl %edx, %edx
callq 0x8240
movl %eax, -0x47c(%rbp)
cmpl $0x0, %eax
jge 0x87250
movl $0x0, -0x4(%rbp)
jmp 0x873da
cmpl $0x0, -0x47c(%rbp)
jne 0x87265
movl $0x0, -0x4(%rbp)
jmp 0x873da
movzwl 0x71dc44(%rip), %eax    # 0x7a4eb0
cmpl 0x71d2ee(%rip), %eax    # 0x7a4560
je 0x87280
movl $0x0, -0x4(%rbp)
jmp 0x873da
leaq 0x71dca9(%rip), %rdi    # 0x7a4f30
xorl %esi, %esi
movl $0x4, %edx
callq 0x8410
leaq 0x71dc9a(%rip), %rdi    # 0x7a4f34
xorl %esi, %esi
movl $0x10, %edx
callq 0x8410
cmpq $0x10, -0x448(%rbp)
jb 0x872c0
movq -0x450(%rbp), %rax
movq %rax, -0x488(%rbp)
jmp 0x872cb
xorl %eax, %eax
movq %rax, -0x488(%rbp)
jmp 0x872cb
movq -0x488(%rbp), %rax
movq %rax, -0x478(%rbp)
cmpq $0x0, -0x478(%rbp)
je 0x8736d
movq -0x478(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x87313
movq -0x478(%rbp), %rax
cmpl $0x8, 0xc(%rax)
jne 0x87313
movq -0x478(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x71dc1f(%rip)    # 0x7a4f30
jmp 0x8734c
movq -0x478(%rbp), %rax
cmpl $0x29, 0x8(%rax)
jne 0x8734a
movq -0x478(%rbp), %rax
cmpl $0x32, 0xc(%rax)
jne 0x8734a
movq -0x478(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x71dbf5(%rip)    # 0x7a4f34
movq 0x18(%rax), %rax
movq %rax, 0x71dbf2(%rip)    # 0x7a4f3c
jmp 0x8734c
jmp 0x8734e
movq -0x478(%rbp), %rsi
leaq -0x470(%rbp), %rdi
callq 0x85b0
movq %rax, -0x478(%rbp)
jmp 0x872d9
movl 0x71dbd1(%rip), %ecx    # 0x7a4f44
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x424(%rbp)
cmpl $0x10, -0x424(%rbp)
jge 0x873d3
movslq -0x424(%rbp), %rax
leaq 0x71d1d6(%rip), %rsi    # 0x7a4570
shlq $0x7, %rax
addq %rax, %rsi
leaq 0x71db08(%rip), %rdi    # 0x7a4eb0
callq 0x873f0
cmpl $0x0, %eax
je 0x873c0
movl -0x424(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0x873d3
jmp 0x873c2
movl -0x424(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x424(%rbp)
jmp 0x87383
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
nopw %cs:(%rax,%rax)