// File: /workspace/github_cpp_makefile_star2_O3/hugosantos[P]dbeacon/protocol.cpp
// Function: build_report(unsigned char*, int, int, bool)
int build_report(uint8_t *buff, int maxlen, int type, bool publishsources) {
	if (maxlen < 4)
		return -1;

	// 0-2 magic
	*((uint16_t *)buff) = htons(0xbeac);

	// 3 version
	buff[2] = PROTO_VER;

	// 4 packet type
	buff[3] = 1; // Report

	buff[4] = defaultTTL; // Original Hop Limit

	int ptr = 5;

	if (!write_tlv_string(buff, maxlen, ptr, T_BEAC_NAME, beaconName.c_str()))
		return -1;
	if (!write_tlv_string(buff, maxlen, ptr, T_ADMIN_CONTACT, adminContact.c_str()))
		return -1;

	if (type == WEBSITE_REPORT) {
		for (WebSites::const_iterator j = webSites.begin(); j != webSites.end(); j++)
			if (!write_tlv_string(buff, maxlen, ptr, j->first, j->second.c_str()))
				return -1;
		if (!twoLetterCC.empty()) {
			if (!write_tlv_string(buff, maxlen, ptr, T_CC, twoLetterCC.c_str()))
				return -1;
		}
		if (!write_tlv_uint(buff, maxlen, ptr, T_SOURCE_FLAGS, flags))
			return -1;
		return ptr;
	} else if (type == LEAVE_REPORT) {
		if (!write_tlv_start(buff, maxlen, ptr, T_LEAVE, 0))
			return -1;
		return ptr;
	}

	if (publishsources) {
		uint64_t now = get_timestamp();

		for (Sources::const_iterator i = sources.begin(); i != sources.end(); i++) {
			if (type == MAP_REPORT && !i->second.identified)
				continue;

			if (!i->second.ASM.s.valid && !i->second.SSM.s.valid)
				continue;

			int len = 18;

			if (i->first.family() == AF_INET)
				len = 6;

			if (type == MAP_REPORT) {
				int namelen = i->second.name.size();
				int contactlen = i->second.adminContact.size();
				len += 2 + namelen + 2 + contactlen;
			} else {
				len += (i->second.ASM.s.valid ? 22 : 0) + (i->second.SSM.s.valid ? 22 : 0);
			}

			if (!write_tlv_start(buff, maxlen, ptr, i->first.family() == AF_INET6 ? T_SOURCE_INFO : T_SOURCE_INFO_IPv4, len))
				break;

			if (i->first.family() == AF_INET6) {
				const sockaddr_in6 *addr = i->first.v6();

				memcpy(buff + ptr, &addr->sin6_addr, sizeof(in6_addr));
				memcpy(buff + ptr + 16, &addr->sin6_port, sizeof(uint16_t));

				ptr += 18;
			} else {
				const sockaddr_in *addr = i->first.v4();

				memcpy(buff + ptr, &addr->sin_addr, sizeof(in_addr));
				memcpy(buff + ptr + 4, &addr->sin_port, sizeof(uint16_t));

				ptr += 6;
			}

			if (type == MAP_REPORT) {
				write_tlv_string(buff, maxlen, ptr, T_BEAC_NAME, i->second.name.c_str());
				write_tlv_string(buff, maxlen, ptr, T_ADMIN_CONTACT, i->second.adminContact.c_str());
			} else {
				uint32_t age = (now - i->second.creation) / 1000;

				if (i->second.ASM.s.valid)
					write_tlv_stats(buff, maxlen, ptr, T_ASM_STATS, age, i->second.sttl, i->second.ASM);
				if (i->second.SSM.s.valid)
					write_tlv_stats(buff, maxlen, ptr, T_SSM_STATS, age, i->second.sttl, i->second.SSM);
			}
		}
	}

	return ptr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x4, %esi
jl 0xa8c3
movl %ecx, %r12d
movq %rdi, %r15
movl %edx, 0x24(%rsp)
movl $0x101acbe, (%rdi)      # imm = 0x101ACBE
leaq 0x18fd(%rip), %rax      # 0xc158
movb (%rax), %al
movb %al, 0x4(%rdi)
leaq 0x7b91(%rip), %rax      # 0x123f8
movq (%rax), %r14
movq %r14, %rdi
movl %esi, 0x10(%rsp)
callq 0x3180
leal 0x7(%rax), %ebx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl 0x10(%rsp), %ebx
jg 0xa8c3
movq %rax, %r13
movb $0x6e, 0x5(%r15)
movb %r13b, 0x6(%r15)
leaq 0x7(%r15), %rdi
movslq %r13d, %rdx
movq %r14, %rsi
callq 0x3300
leaq 0x7b72(%rip), %rax      # 0x12418
movq (%rax), %r14
movq %r14, %rdi
callq 0x3180
movq %r15, %rdx
leal (%rax,%r13), %r15d
addl $0x9, %r15d
cmpl 0x10(%rsp), %r15d
jle 0xa8d4
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq %ebx, %rcx
movq %rdx, 0x18(%rsp)
leaq (%rdx,%rcx), %rdi
addq $0x2, %rdi
movb $0x61, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r14, %rsi
callq 0x3300
movl %r15d, 0x14(%rsp)
movl 0x24(%rsp), %r14d
cmpl $0x56, %r14d
je 0xa980
cmpl $0x55, %r14d
jne 0xa9a1
leaq 0x7b71(%rip), %rbx      # 0x12488
movq 0x18(%rbx), %r14
addq $0x8, %rbx
cmpq %rbx, %r14
je 0xac40
movq 0x28(%r14), %r12
movq %r12, %rdi
callq 0x3180
leal (%rax,%r15), %r13d
addl $0x2, %r13d
cmpl 0x10(%rsp), %r13d
jg 0xa8c3
movb 0x20(%r14), %cl
movslq %r15d, %rdx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rdx), %rdi
addq $0x2, %rdi
movb %cl, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r12, %rsi
callq 0x3300
movq %r14, %rdi
callq 0x3420
movq %rax, %r14
movl %r13d, %r15d
cmpq %rbx, %rax
jne 0xa928
jmp 0xac43
leal 0x2(%r15), %eax
cmpl 0x10(%rsp), %eax
jg 0xa8c3
movslq %r15d, %rcx
movq 0x18(%rsp), %rdx
movw $0x51, (%rdx,%rcx)
jmp 0xacbe
testb %r12b, %r12b
je 0xac38
callq 0xa61c
movq %rax, 0x28(%rsp)
leaq 0x7a9d(%rip), %rax      # 0x12458
movq 0x18(%rax), %r13
addq $0x8, %rax
movl %r15d, %ebp
movq %rax, 0x30(%rsp)
cmpq %rax, %r13
je 0xa8c3
cmpl $0x54, %r14d
jne 0xa9e8
cmpb $0x1, 0x3f8(%r13)
jne 0xac19
cmpb $0x0, 0x18c(%r13)
jne 0xaa00
cmpb $0x1, 0x284(%r13)
jne 0xac19
leaq 0x20(%r13), %r12
movq %r12, %rdi
callq 0x9c38
cmpl $0x2, %eax
movl $0x12, %eax
movl $0x6, %ecx
cmovel %ecx, %eax
cmpl $0x54, %r14d
jne 0xaa38
movl 0x338(%r13), %ecx
movl 0x358(%r13), %edx
leal (%rcx,%rdx), %ebx
addl $0x4, %ebx
jmp 0xaa5b
cmpb $0x0, 0x18c(%r13)
movl $0x0, %ecx
movl $0x16, %edx
cmovnel %edx, %ecx
leal 0x16(%rcx), %ebx
cmpb $0x0, 0x284(%r13)
cmovel %ecx, %ebx
movq 0x18(%rsp), %r15
addl %eax, %ebx
movq %r12, %rdi
callq 0x9c38
movl 0x14(%rsp), %ebp
leal (%rbx,%rbp), %ecx
addl $0x2, %ecx
cmpl 0x10(%rsp), %ecx
jg 0xa8c3
movslq %ebp, %rcx
leal 0x2(%rcx), %r14d
cmpl $0xa, %eax
setne %al
shlb $0x5, %al
orb $0x49, %al
movb %al, (%r15,%rcx)
movb %bl, 0x1(%r15,%rcx)
movq %r12, %rdi
callq 0x9c38
movslq %r14d, %rbx
addq %r15, %rbx
movq %r12, %rdi
cmpl $0xa, %eax
jne 0xaac7
callq 0x9c3c
movups 0x8(%rax), %xmm0
movups %xmm0, (%rbx)
movl $0x14, %edx
movl $0x10, %ecx
jmp 0xaadb
callq 0x9c40
movl 0x4(%rax), %ecx
movl %ecx, (%rbx)
movl $0x8, %edx
movl $0x4, %ecx
movl 0x24(%rsp), %r14d
addl %edx, %ebp
movzwl 0x2(%rax), %eax
movw %ax, (%rbx,%rcx)
movl %ebp, 0x14(%rsp)
cmpl $0x54, %r14d
jne 0xab1a
movq 0x330(%r13), %r14
movq %r14, %rdi
callq 0x3180
leal (%rax,%rbp), %ebx
addl $0x2, %ebx
cmpl 0x10(%rsp), %ebx
jle 0xabb2
movl %ebp, %ebx
jmp 0xabd3
movq 0x28(%rsp), %rax
subq 0x120(%r13), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %r15
shrq $0x4, %r15
cmpb $0x1, 0x18c(%r13)
jne 0xab75
movl 0x128(%r13), %r9d
leaq 0x140(%r13), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
leaq 0x14(%rsp), %rdx
movl $0x41, %ecx
movl %r15d, %r8d
callq 0xacc5
cmpb $0x1, 0x284(%r13)
jne 0xac19
movl 0x128(%r13), %r9d
leaq 0x238(%r13), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
leaq 0x14(%rsp), %rdx
movl $0x53, %ecx
movl %r15d, %r8d
callq 0xacc5
jmp 0xac19
movslq %ebp, %rcx
leaq (%r15,%rcx), %rdi
addq $0x2, %rdi
movb $0x6e, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r14, %rsi
callq 0x3300
movl %ebx, 0x14(%rsp)
movq 0x350(%r13), %r15
movq %r15, %rdi
callq 0x3180
leal (%rax,%rbx), %ebp
addl $0x2, %ebp
cmpl 0x10(%rsp), %ebp
movl 0x24(%rsp), %r14d
jg 0xac19
movslq %ebx, %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rcx), %rdi
addq $0x2, %rdi
movb $0x61, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r15, %rsi
callq 0x3300
movl %ebp, 0x14(%rsp)
movq %r13, %rdi
callq 0x3420
movq %rax, %r13
cmpq 0x30(%rsp), %rax
jne 0xa9d4
movl 0x14(%rsp), %ebp
jmp 0xa8c3
movl %r15d, %ebp
jmp 0xa8c3
movl %r15d, %r13d
leaq 0x77ee(%rip), %rax      # 0x12438
cmpq $0x0, 0x8(%rax)
je 0xac91
movq (%rax), %r14
movq %r14, %rdi
callq 0x3180
leal (%rax,%r13), %ebx
addl $0x2, %ebx
cmpl 0x10(%rsp), %ebx
jg 0xa8c3
movslq %r13d, %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rcx), %rdi
addq $0x2, %rdi
movb $0x43, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r14, %rsi
callq 0x3300
jmp 0xac94
movl %r13d, %ebx
leal 0x6(%rbx), %eax
cmpl 0x10(%rsp), %eax
jg 0xa8c3
leaq 0x7894(%rip), %rcx      # 0x1253c
movl (%rcx), %ecx
movslq %ebx, %rdx
movq 0x18(%rsp), %rsi
movw $0x446, (%rsi,%rdx)     # imm = 0x446
bswapl %ecx
movl %ecx, 0x2(%rsi,%rdx)
movl %eax, %ebp
jmp 0xa8c3