// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
cmpl $0x4, %esi
jge 0xa14c
pushq $-0x1
popq %r15
jmp 0xa4a0
movl %ecx, %r12d
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
movl $0x101acbe, (%rdi)      # imm = 0x101ACBE
leaq 0xff4(%rip), %rax       # 0xb158
movb (%rax), %al
movb %al, 0x4(%rdi)
leaq 0xc(%rsp), %rdx
movl $0x5, (%rdx)
leaq 0x827d(%rip), %rax      # 0x123f8
movq (%rax), %r8
pushq $0x6e
popq %rcx
callq 0xa4b2
pushq $-0x1
popq %r15
testb %al, %al
je 0xa4a0
leaq 0x827f(%rip), %rax      # 0x12418
movq (%rax), %r8
leaq 0xc(%rsp), %rdx
pushq $0x61
popq %rcx
movq %rbx, %rdi
movl %ebp, %esi
callq 0xa4b2
testb %al, %al
je 0xa4a0
cmpl $0x56, %r14d
je 0xa20c
cmpl $0x55, %r14d
jne 0xa227
leaq 0x82bf(%rip), %r13      # 0x12488
movq 0x18(%r13), %r12
addq $0x8, %r13
leaq 0xc(%rsp), %r14
cmpq %r13, %r12
je 0xa455
movzbl 0x20(%r12), %ecx
movq 0x28(%r12), %r8
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0xa4b2
testb %al, %al
je 0xa4a0
movq %r12, %rdi
callq 0x3420
movq %rax, %r12
jmp 0xa1d6
movslq 0xc(%rsp), %rax
leal 0x2(%rax), %ecx
cmpl %ebp, %ecx
jg 0xa4a0
movw $0x51, (%rbx,%rax)
jmp 0xa49d
movl %r14d, 0x10(%rsp)
testb %r12b, %r12b
je 0xa44e
callq 0x9f86
movq %rax, 0x18(%rsp)
leaq 0x8212(%rip), %rax      # 0x12458
movq 0x18(%rax), %r12
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movl %ebp, 0x14(%rsp)
cmpq 0x20(%rsp), %r12
je 0xa44e
movl 0x10(%rsp), %r14d
cmpl $0x54, %r14d
jne 0xa27c
cmpb $0x1, 0x3f8(%r12)
jne 0xa43e
cmpb $0x0, 0x18c(%r12)
jne 0xa296
cmpb $0x1, 0x284(%r12)
jne 0xa43e
leaq 0x20(%r12), %r15
movq %r15, %rdi
callq 0x9686
cmpl $0x2, %eax
pushq $0x12
popq %rax
pushq $0x6
popq %rcx
cmovel %ecx, %eax
cmpl $0x54, %r14d
jne 0xa2cf
movl 0x338(%r12), %ecx
movl 0x358(%r12), %edx
leal (%rcx,%rdx), %r14d
addl $0x4, %r14d
jmp 0xa2f4
cmpb $0x0, 0x18c(%r12)
movl $0x0, %ecx
pushq $0x16
popq %rdx
cmovnel %edx, %ecx
leal 0x16(%rcx), %r14d
cmpb $0x0, 0x284(%r12)
cmovel %ecx, %r14d
addl %eax, %r14d
movq %r15, %rdi
callq 0x9686
movslq 0xc(%rsp), %r13
leal (%r14,%r13), %ecx
addl $0x2, %ecx
cmpl %ebp, %ecx
jg 0xa44e
leal 0x2(%r13), %ebp
cmpl $0xa, %eax
setne %al
shlb $0x5, %al
orb $0x49, %al
movb %al, (%rbx,%r13)
movb %r14b, 0x1(%rbx,%r13)
movq %r15, %rdi
callq 0x9686
movslq %ebp, %r14
addq %rbx, %r14
movq %r15, %rdi
cmpl $0xa, %eax
jne 0xa355
callq 0x968a
movups 0x8(%rax), %xmm0
movups %xmm0, (%r14)
pushq $0x10
popq %rcx
pushq $0x14
jmp 0xa365
callq 0x968e
movl 0x4(%rax), %ecx
movl %ecx, (%r14)
pushq $0x4
popq %rcx
pushq $0x8
popq %rdx
addl %edx, %r13d
movzwl 0x2(%rax), %eax
movw %ax, (%r14,%rcx)
movl %r13d, 0xc(%rsp)
cmpl $0x54, 0x10(%rsp)
movl 0x14(%rsp), %ebp
jne 0xa3bc
movq 0x330(%r12), %r8
movq %rbx, %rdi
movl %ebp, %esi
leaq 0xc(%rsp), %r14
movq %r14, %rdx
pushq $0x6e
popq %rcx
callq 0xa4b2
movq 0x350(%r12), %r8
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
pushq $0x61
popq %rcx
callq 0xa4b2
jmp 0xa43e
movq 0x18(%rsp), %rax
subq 0x120(%r12), %rax
xorl %edx, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
divq %rcx
movq %rax, %r15
cmpb $0x1, 0x18c(%r12)
jne 0xa40a
movl 0x128(%r12), %r9d
leaq 0x140(%r12), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0xc(%rsp), %rdx
pushq $0x41
popq %rcx
movl %r15d, %r8d
callq 0xa527
cmpb $0x1, 0x284(%r12)
jne 0xa43e
movl 0x128(%r12), %r9d
leaq 0x238(%r12), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movl %ebp, %esi
leaq 0xc(%rsp), %rdx
pushq $0x53
popq %rcx
movl %r15d, %r8d
callq 0xa527
movq %r12, %rdi
callq 0x3420
movq %rax, %r12
jmp 0xa257
movl 0xc(%rsp), %r15d
jmp 0xa4a0
leaq 0x7fdc(%rip), %rax      # 0x12438
cmpq $0x0, 0x8(%rax)
je 0xa47c
movq (%rax), %r8
leaq 0xc(%rsp), %rdx
pushq $0x43
popq %rcx
movq %rbx, %rdi
movl %ebp, %esi
callq 0xa4b2
testb %al, %al
je 0xa4a0
movslq 0xc(%rsp), %rax
leal 0x6(%rax), %ecx
cmpl %ebp, %ecx
jg 0xa4a0
leaq 0x80ad(%rip), %rdx      # 0x1253c
movl (%rdx), %edx
movw $0x446, (%rbx,%rax)     # imm = 0x446
bswapl %edx
movl %edx, 0x2(%rbx,%rax)
movl %ecx, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq