// File: /workspace/github_cpp_makefile_star2_O1/hugosantos[P]dbeacon/protocol.cpp
// Function: handle_nmsg(address const&, unsigned long, int, unsigned char*, int, bool)
void handle_nmsg(const address &from, uint64_t recvdts, int ttl, uint8_t *buff, int len, bool ssm) {
	if (len < 4)
		return;

	if (ntohs(*((uint16_t *)buff)) != 0xbeac)
		return;

	if (buff[2] != PROTO_VER)
		return;

	uint64_t now = get_timestamp();

	if (buff[3] == 0) {
		if (len == 12) {
			uint32_t seq = read_u32(buff + 4);
			uint32_t ts = read_u32(buff + 8);
			getSource(from, 0, now, recvdts, true).update(ttl, seq, ts, now, recvdts, ssm);
		}
		return;
	} else if (buff[3] == 1) {
		if (len < 5)
			return;

		beaconSource &src = getSource(from, 0, now, recvdts, true);

		src.sttl = buff[4];

		len -= 5;

		for (uint8_t *hd = tlv_begin(buff + 5, len); hd; hd = tlv_next(hd, len)) {
			if (verbose > 4) {
				char tmp[64];
				from.to_string(tmp, sizeof(tmp));
				fprintf(stderr, "Parsing TLV (%i, %i) for %s [len is now %i]\n", (int)hd[0], (int)hd[1], tmp, len);
			}

			if (hd[0] == T_BEAC_NAME) {
				string name;
				if (check_string((char *)hd + 2, hd[1], name))
					src.setName(name);
			} else if (hd[0] == T_ADMIN_CONTACT) {
				check_string((char *)hd + 2, hd[1], src.adminContact);
			} else if (hd[0] == T_SOURCE_INFO || hd[0] == T_SOURCE_INFO_IPv4) {
				int blen = hd[0] == T_SOURCE_INFO ? 18 : 6;

				if (hd[1] < blen)
					continue;

				address addr;

				if (hd[0] == T_SOURCE_INFO) {
					sockaddr_in6 *a6 = (sockaddr_in6 *)&addr;

					a6->sin6_family = AF_INET6;

					memcpy(&a6->sin6_addr, hd + 2, sizeof(in6_addr));
					memcpy(&a6->sin6_port, hd + 18, sizeof(uint16_t));
				} else {
					sockaddr_in *a4 = (sockaddr_in *)&addr;

					a4->sin_family = AF_INET;

					memcpy(&a4->sin_addr, hd + 2, sizeof(in_addr));
					memcpy(&a4->sin_port, hd + 6, sizeof(uint16_t));
				}

				beaconExternalStats &stats = src.getExternal(addr, now, recvdts);

				int plen = hd[1] - blen;
				for (uint8_t *pd = tlv_begin(hd + 2 + blen, plen); pd; pd = tlv_next(pd, plen)) {
					if (pd[0] == T_BEAC_NAME) {
						if (check_string((char *)pd + 2, pd[1], stats.name)) {
							stats.identified = !stats.name.empty();
						}
					} else if (pd[0] == T_ADMIN_CONTACT) {
						check_string((char *)pd + 2, pd[1], stats.contact);
					} else if (pd[0] == T_ASM_STATS || pd[0] == T_SSM_STATS) {
						Stats *st = (pd[0] == T_ASM_STATS ? &stats.ASM : &stats.SSM);

						if (!read_tlv_stats(pd, stats, *st))
							break;
						st->lastupdate = now;
					}
				}

				// trigger local SSM join
				if (!addr.is_equal(beaconUnicastAddr)) {
					beaconSource &t = getSource(addr, stats.identified ? stats.name.c_str() : 0, now, recvdts, false);
					if (t.adminContact.empty())
						t.adminContact = stats.contact;
				}
			} else if (hd[0] == T_WEBSITE_GENERIC || hd[0] == T_WEBSITE_LG || hd[0] == T_WEBSITE_MATRIX) {
				string url;
				if (check_string((char *)hd + 2, hd[1], url)) {
					src.webSites[hd[0]] = url;
				}
			} else if (hd[0] == T_CC) {
				if (hd[1] == 2) {
					src.CC = string((char *)hd + 2, 2);
				}
			} else if (hd[0] == T_SOURCE_FLAGS) {
				if (hd[1] == 4)
					src.Flags = read_u32(hd + 2);
			} else if (hd[0] == T_LEAVE) {
				removeSource(from, false);
				break;
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x50(%rsp)
cmpl $0x4, %r8d
jl 0xb728
movq %rcx, %r12
cmpw $0xacbe, (%rcx)         # imm = 0xACBE
jne 0xb728
cmpb $0x1, 0x2(%r12)
jne 0xb728
movl %r9d, %r14d
movl %r8d, %r15d
movl %edx, %ebp
movq %rsi, %rbx
callq 0xa6ca
movq %rax, 0x30(%rsp)
movzbl 0x3(%r12), %eax
cmpl $0x1, %eax
je 0xaf63
testl %eax, %eax
jne 0xb728
cmpl $0xc, %r15d
jne 0xb728
movl 0x4(%r12), %r15d
movl 0x8(%r12), %r12d
bswapl %r15d
bswapl %r12d
movq 0x50(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x5c63
movzbl %r14b, %ecx
movl %ecx, (%rsp)
movzbl %bpl, %esi
movq %rax, %rdi
movl %r15d, %edx
movq %r12, %rcx
movq %r13, %r8
movq %rbx, %r9
callq 0x6d82
jmp 0xb728
cmpl $0x4, %r15d
je 0xb728
xorl %r13d, %r13d
movq 0x50(%rsp), %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x5c63
movzbl 0x4(%r12), %ecx
movq %rax, 0x40(%rsp)
movl %ecx, 0x88(%rax)
leal -0x5(%r15), %ebp
cmpl $0x7, %r15d
jl 0xafb8
movzbl 0x6(%r12), %eax
addq $0x5, %r12
xorl %r13d, %r13d
cmpl %eax, %ebp
cmovgeq %r12, %r13
testq %r13, %r13
je 0xb728
movq 0x40(%rsp), %rax
leaq 0x2d0(%rax), %rcx
movq %rcx, 0xe0(%rsp)
leaq 0x328(%rax), %rcx
movq %rcx, 0xf0(%rsp)
addq $0x2b0, %rax            # imm = 0x2B0
movq %rax, 0xe8(%rsp)
movq %rbx, 0x38(%rsp)
leaq 0x753a(%rip), %rax      # 0x12538
cmpl $0x5, (%rax)
jge 0xb2ee
movb (%r13), %al
movzbl %al, %ecx
cmpl $0x60, %ecx
movl %ebp, 0x5c(%rsp)
jg 0xb0a8
addl $-0x43, %ecx
cmpl $0xe, %ecx
ja 0xb6f0
leaq 0x2712(%rip), %rdx      # 0xd740
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
leaq 0x2(%r13), %r12
movzbl 0x1(%r13), %r14d
testq %r14, %r14
sete %bl
leaq 0x70(%rsp), %rbp
je 0xb27a
movsbl (%r12), %edi
callq 0x35e0
testl %eax, %eax
je 0xb270
leaq -0x1(%r14), %rbx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0xb278
movsbl 0x3(%r13,%r15), %edi
callq 0x35e0
incq %r15
testl %eax, %eax
jne 0xb082
cmpq %r14, %r15
setae %bl
jmp 0xb272
cmpl $0x61, %ecx
je 0xb1e2
cmpl $0x69, %ecx
je 0xb130
cmpl $0x6e, %ecx
jne 0xb6f0
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
leaq 0x2(%r13), %r12
movzbl 0x1(%r13), %r14d
testq %r14, %r14
sete %bl
leaq 0x70(%rsp), %rbp
je 0xb683
movsbl (%r12), %edi
callq 0x35e0
testl %eax, %eax
je 0xb679
leaq -0x1(%r14), %rbx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0xb681
movsbl 0x3(%r13,%r15), %edi
callq 0x35e0
incq %r15
testl %eax, %eax
jne 0xb10a
cmpq %r14, %r15
setae %bl
jmp 0xb67b
cmpb $0x49, %al
movl $0x6, %ebp
movl $0x12, %eax
cmovel %eax, %ebp
movzbl 0x1(%r13), %eax
cmpl %eax, %ebp
ja 0xb6f0
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0xa2ea
leaq 0x2(%r13), %rax
cmpb $0x49, (%r13)
jne 0xb302
movw $0xa, 0x60(%rsp)
movups (%rax), %xmm0
leaq 0x70(%rsp), %rax
movups %xmm0, -0x8(%rax)
movl $0x12, %eax
jmp 0xb314
cmpb $0x4, 0x1(%r13)
jne 0xb6f0
movl 0x2(%r13), %eax
bswapl %eax
movq 0x40(%rsp), %rcx
movl %eax, 0x2f0(%rcx)
jmp 0xb6f0
cmpb $0x2, 0x1(%r13)
jne 0xb6f0
leaq 0x70(%rsp), %rbx
movq %rbx, 0x60(%rsp)
movzwl 0x2(%r13), %eax
movw %ax, 0x70(%rsp)
movq $0x2, 0x68(%rsp)
movb $0x0, 0x72(%rsp)
movq 0xe0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
jmp 0xb253
leaq 0x2(%r13), %r14
movzbl 0x1(%r13), %r15d
testq %r15, %r15
je 0xb228
movsbl (%r14), %edi
callq 0x35e0
testl %eax, %eax
je 0xb6f0
leaq -0x1(%r15), %rbx
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0xb228
movsbl 0x3(%r13,%r12), %edi
callq 0x35e0
incq %r12
testl %eax, %eax
jne 0xb208
cmpq %r15, %r12
jb 0xb6f0
leaq 0x70(%rsp), %rbx
movq %rbx, 0x60(%rsp)
addq %r14, %r15
leaq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x9724
movq 0xe8(%rsp), %rdi
movq %r12, %rsi
callq 0x3460
movq 0x60(%rsp), %rdi
cmpq %rbx, %rdi
je 0xb6f0
movq 0x70(%rsp), %rsi
jmp 0xb6e8
xorl %ebx, %ebx
testb %bl, %bl
jne 0xb27a
jmp 0xb2b9
movb $0x1, %bl
movq %rbp, 0x60(%rsp)
addq %r12, %r14
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x9724
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x3460
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0xb2b9
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x33d0
testb %bl, %bl
je 0xb6d4
movzbl (%r13), %eax
movl %eax, 0x60(%rsp)
movq 0xf0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x810a
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x31e0
jmp 0xb6d4
movq 0x50(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x37f5
jmp 0xb007
movw $0x2, 0x60(%rsp)
movl (%rax), %eax
movl %eax, 0x64(%rsp)
movl $0x6, %eax
movq 0x38(%rsp), %rcx
movzwl (%r13,%rax), %eax
movw %ax, 0x62(%rsp)
movq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq 0x30(%rsp), %rdx
callq 0x6bbc
movq %rax, 0x48(%rsp)
movzbl 0x1(%r13), %ebx
subl %ebp, %ebx
cmpl $0x2, %ebx
jge 0xb34b
xorl %ebp, %ebp
jmp 0xb363
movl %ebp, %eax
addq %r13, %rax
addq $0x2, %rax
movzbl 0x1(%rax), %ecx
cmpl %ecx, %ebx
movl $0x0, %ebp
cmovgeq %rax, %rbp
testq %rbp, %rbp
je 0xb60c
movq 0x48(%rsp), %rax
leaq 0x88(%rax), %rcx
movq %rcx, 0xf8(%rsp)
addq $0x68, %rax
movq %rax, 0x100(%rsp)
movb (%rbp), %cl
movzbl %cl, %eax
cmpl $0x60, %eax
jg 0xb463
cmpl $0x41, %eax
je 0xb3a9
cmpl $0x53, %eax
jne 0xb5db
cmpb $0x14, 0x1(%rbp)
jne 0xb60c
movq 0x48(%rsp), %rsi
leaq 0x38(%rsi), %rax
leaq 0x10(%rsi), %rdx
cmpb $0x41, %cl
cmoveq %rdx, %rax
movl 0x2(%rbp), %ecx
bswapl %ecx
movq %rcx, (%rax)
movl 0x6(%rbp), %ecx
bswapl %ecx
movl %ecx, 0x8(%rsi)
movb 0xa(%rbp), %cl
movb %cl, 0x25(%rax)
movl 0xb(%rbp), %ecx
bswapl %ecx
movl %ecx, 0x10(%rax)
movl 0xf(%rbp), %ecx
bswapl %ecx
movl %ecx, 0x14(%rax)
movzbl 0x13(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0x2338(%rip), %xmm0     # 0xd738
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x18(%rax)
movzbl 0x14(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0x2314(%rip), %xmm0     # 0xd730
movss 0x2308(%rip), %xmm1     # 0xd72c
cmpb $-0x1, %cl
je 0xb430
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movss %xmm1, 0x1c(%rax)
movzbl 0x15(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0x22f0(%rip), %xmm0     # 0xd738
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rax)
movb $0x1, 0x24(%rax)
movq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0xb5db
cmpl $0x61, %eax
je 0xb4ca
cmpl $0x6e, %eax
jne 0xb5db
leaq 0x2(%rbp), %rsi
movzbl 0x1(%rbp), %r15d
testq %r15, %r15
sete %r12b
je 0xb57f
movq %rsi, 0x28(%rsp)
movsbl (%rsi), %edi
callq 0x35e0
testl %eax, %eax
je 0xb568
leaq -0x1(%r15), %r12
xorl %r14d, %r14d
cmpq %r14, %r12
je 0xb577
movsbl 0x3(%rbp,%r14), %edi
callq 0x35e0
incq %r14
testl %eax, %eax
jne 0xb4a3
cmpq %r15, %r14
setae %r12b
jmp 0xb56b
leaq 0x2(%rbp), %rax
movq %rax, 0x28(%rsp)
movzbl 0x1(%rbp), %r15d
testq %r15, %r15
je 0xb519
movq 0x28(%rsp), %rax
movsbl (%rax), %edi
callq 0x35e0
testl %eax, %eax
je 0xb5db
leaq -0x1(%r15), %r12
xorl %r14d, %r14d
cmpq %r14, %r12
je 0xb519
movsbl 0x3(%rbp,%r14), %edi
callq 0x35e0
incq %r14
testl %eax, %eax
jne 0xb4f9
cmpq %r15, %r14
jb 0xb5db
leaq 0x18(%rsp), %r14
movq %r14, 0x8(%rsp)
movq 0x28(%rsp), %rsi
addq %rsi, %r15
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x9724
movq 0xf8(%rsp), %rdi
movq %r12, %rsi
callq 0x3460
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0xb5db
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x33d0
jmp 0xb5db
xorl %r12d, %r12d
testb %r12b, %r12b
movq 0x28(%rsp), %rsi
jne 0xb57f
jmp 0xb5c8
movb $0x1, %r12b
movq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
addq %rsi, %r15
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x9724
movq 0x100(%rsp), %rdi
movq %r14, %rsi
callq 0x3460
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xb5c8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x33d0
testb %r12b, %r12b
je 0xb5db
movq 0x48(%rsp), %rax
cmpq $0x0, 0x70(%rax)
setne 0x60(%rax)
movzbl 0x1(%rbp), %eax
subl %eax, %ebx
addl $-0x2, %ebx
cmpl $0x2, %ebx
jge 0xb5ed
xorl %ebp, %ebp
jmp 0xb603
addq %rbp, %rax
addq $0x2, %rax
movzbl 0x1(%rax), %ecx
cmpl %ecx, %ebx
movl $0x0, %ebp
cmovgeq %rax, %rbp
testq %rbp, %rbp
jne 0xb38c
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x6e9d(%rip), %rsi      # 0x124b8
callq 0xa660
testb %al, %al
movq 0x48(%rsp), %r14
jne 0xb6f0
cmpb $0x0, 0x60(%r14)
je 0xb63a
movq 0x68(%r14), %rsi
jmp 0xb63c
xorl %esi, %esi
movq 0x38(%rsp), %rcx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
xorl %r8d, %r8d
callq 0x5c63
cmpq $0x0, 0x2b8(%rax)
jne 0xb6f0
addq $0x2b0, %rax            # imm = 0x2B0
addq $0x88, %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x31e0
jmp 0xb6f0
xorl %ebx, %ebx
testb %bl, %bl
jne 0xb683
jmp 0xb6c1
movb $0x1, %bl
movq %rbp, 0x60(%rsp)
addq %r12, %r14
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x9724
leaq 0x8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x3460
movq 0x60(%rsp), %rdi
cmpq %rbp, %rdi
je 0xb6c1
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x33d0
testb %bl, %bl
je 0xb6d4
movq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x6756
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xb6f0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x33d0
movzbl 0x1(%r13), %eax
movl 0x5c(%rsp), %ebp
subl %eax, %ebp
addl $-0x2, %ebp
cmpl $0x2, %ebp
jge 0xb708
xorl %r13d, %r13d
jmp 0xb71f
addq %r13, %rax
addq $0x2, %rax
movzbl 0x1(%rax), %ecx
cmpl %ecx, %ebp
movl $0x0, %r13d
cmovgeq %rax, %r13
testq %r13, %r13
jne 0xaff7
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x676f
jmp 0xb728
jmp 0xb74c
jmp 0xb74c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0xb76b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x33d0
movq %rbx, %rdi
callq 0x35d0
nop