// File: /workspace/github_cpp_makefile_star2_O1/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
movl %edx, 0x18(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x4, %esi
jl 0xad89
movl %ecx, %r12d
movq %rdi, %r14
movl $0x101acbe, (%rdi)      # imm = 0x101ACBE
leaq 0x184c(%rip), %rax      # 0xc158
movb (%rax), %al
movb %al, 0x4(%rdi)
movl $0x5, 0x10(%rsp)
leaq 0x7ad8(%rip), %rax      # 0x123f8
movq (%rax), %rbp
movq %rbp, %rdi
movl %esi, 0x14(%rsp)
callq 0x3180
leal 0x7(%rax), %ebx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl 0x14(%rsp), %ebx
jg 0xad89
movq %rax, %r13
movb $0x6e, 0x5(%r14)
movb %r13b, 0x6(%r14)
leaq 0x7(%r14), %rdi
movslq %r13d, %rdx
movq %rbp, %rsi
callq 0x3300
movl %ebx, 0x10(%rsp)
leaq 0x7ab0(%rip), %rax      # 0x12418
movq (%rax), %rbp
movq %rbp, %rdi
callq 0x3180
addl %eax, %r13d
addl $0x9, %r13d
cmpl 0x14(%rsp), %r13d
jg 0xad89
movslq %ebx, %rcx
leaq (%r14,%rcx), %rdi
addq $0x2, %rdi
movb $0x61, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %rbp, %rsi
callq 0x3300
movl %r13d, 0x10(%rsp)
movl 0x18(%rsp), %eax
cmpl $0x56, %eax
je 0xaa35
cmpl $0x55, %eax
jne 0xaa63
leaq 0x7ac4(%rip), %rax      # 0x12488
movq 0x18(%rax), %r12
movl 0x10(%rsp), %ebp
addq $0x8, %rax
movq %rax, 0x18(%rsp)
cmpq %rax, %r12
je 0xacf6
movq 0x28(%r12), %r13
movq %r13, %rdi
callq 0x3180
leal (%rax,%rbp), %ebx
addl $0x2, %ebx
cmpl 0x14(%rsp), %ebx
jg 0xad9b
movb 0x20(%r12), %cl
movslq %ebp, %rdx
leaq (%r14,%rdx), %rdi
addq $0x2, %rdi
movb %cl, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r13, %rsi
callq 0x3300
movq %r12, %rdi
callq 0x3420
movq %rax, %r12
movl %ebx, %ebp
cmpq 0x18(%rsp), %rax
jne 0xa9de
jmp 0xacf8
movslq 0x10(%rsp), %rcx
leal 0x2(%rcx), %eax
movl 0x14(%rsp), %edx
cmpl %edx, %eax
jg 0xaa50
movw $0x51, (%r14,%rcx)
movl %eax, 0x10(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl %edx, %eax
jle 0xad84
jmp 0xad89
testb %r12b, %r12b
movl 0x18(%rsp), %r15d
je 0xad84
callq 0xa6ca
movq %rax, 0x20(%rsp)
leaq 0x79d6(%rip), %rbp      # 0x12458
movq 0x18(%rbp), %r12
addq $0x8, %rbp
cmpq %rbp, %r12
je 0xad84
cmpl $0x54, %r15d
jne 0xaaa8
cmpb $0x1, 0x3f8(%r12)
jne 0xacdd
cmpb $0x0, 0x18c(%r12)
jne 0xaac2
cmpb $0x1, 0x284(%r12)
jne 0xacdd
leaq 0x20(%r12), %r13
movq %r13, %rdi
callq 0x9cce
cmpl $0x2, %eax
movl $0x12, %eax
movl $0x6, %ecx
cmovel %ecx, %eax
cmpl $0x54, %r15d
jne 0xaafd
movl 0x338(%r12), %ecx
movl 0x358(%r12), %edx
leal (%rcx,%rdx), %ebx
addl $0x4, %ebx
jmp 0xab22
cmpb $0x0, 0x18c(%r12)
movl $0x0, %ecx
movl $0x16, %edx
cmovnel %edx, %ecx
leal 0x16(%rcx), %ebx
cmpb $0x0, 0x284(%r12)
cmovel %ecx, %ebx
addl %eax, %ebx
movq %r13, %rdi
callq 0x9cce
movslq 0x10(%rsp), %rcx
leal (%rcx,%rbx), %edx
addl $0x2, %edx
cmpl 0x14(%rsp), %edx
jg 0xad84
cmpl $0xa, %eax
setne %al
shlb $0x5, %al
orb $0x49, %al
movb %al, (%r14,%rcx)
movb %bl, 0x1(%r14,%rcx)
leal 0x2(%rcx), %eax
movl %eax, 0x10(%rsp)
movq %r13, %rdi
callq 0x9cce
movq %r13, %rdi
cmpl $0xa, %eax
jne 0xab8e
callq 0x9cd2
movslq 0x10(%rsp), %rcx
movups 0x8(%rax), %xmm0
movups %xmm0, (%r14,%rcx)
movzwl 0x2(%rax), %eax
movw %ax, 0x10(%r14,%rcx)
leal 0x12(%rcx), %ebx
jmp 0xabac
callq 0x9cd6
movslq 0x10(%rsp), %rcx
movl 0x4(%rax), %edx
movl %edx, (%r14,%rcx)
movzwl 0x2(%rax), %eax
movw %ax, 0x4(%r14,%rcx)
leal 0x6(%rcx), %ebx
movl %ebx, 0x10(%rsp)
cmpl $0x54, %r15d
jne 0xac48
movq 0x330(%r12), %r13
movq %r13, %rdi
callq 0x3180
leal (%rbx,%rax), %r15d
addl $0x2, %r15d
cmpl 0x14(%rsp), %r15d
jg 0xabfb
movslq %ebx, %rcx
leaq (%r14,%rcx), %rdi
addq $0x2, %rdi
movb $0x6e, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r13, %rsi
callq 0x3300
movl %r15d, 0x10(%rsp)
movq 0x350(%r12), %r13
movq %r13, %rdi
callq 0x3180
movslq 0x10(%rsp), %rcx
leal (%rax,%rcx), %ebx
addl $0x2, %ebx
cmpl 0x14(%rsp), %ebx
movl 0x18(%rsp), %r15d
jg 0xacdd
leaq (%r14,%rcx), %rdi
addq $0x2, %rdi
movb $0x61, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r13, %rsi
callq 0x3300
movl %ebx, 0x10(%rsp)
jmp 0xacdd
movq 0x20(%rsp), %rax
subq 0x120(%r12), %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
mulq %rcx
movq %rdx, %r13
shrq $0x4, %r13
cmpb $0x1, 0x18c(%r12)
jne 0xaca5
movl 0x128(%r12), %r9d
leaq 0x140(%r12), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl 0x14(%rsp), %esi
leaq 0x10(%rsp), %rdx
movl $0x41, %ecx
movl %r13d, %r8d
callq 0xada1
cmpb $0x1, 0x284(%r12)
jne 0xacdd
movl 0x128(%r12), %r9d
leaq 0x238(%r12), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl 0x14(%rsp), %esi
leaq 0x10(%rsp), %rdx
movl $0x53, %ecx
movl %r13d, %r8d
callq 0xada1
movq %r12, %rdi
callq 0x3420
movq %rax, %r12
cmpq %rbp, %rax
jne 0xaa93
jmp 0xad84
movl %ebp, %ebx
movl %ebx, 0x10(%rsp)
leaq 0x7735(%rip), %rax      # 0x12438
cmpq $0x0, 0x8(%rax)
je 0xad44
movq (%rax), %r12
movq %r12, %rdi
callq 0x3180
movslq 0x10(%rsp), %rcx
leal (%rax,%rcx), %ebx
addl $0x2, %ebx
cmpl 0x14(%rsp), %ebx
jg 0xad89
leaq (%r14,%rcx), %rdi
addq $0x2, %rdi
movb $0x43, -0x2(%rdi)
movb %al, -0x1(%rdi)
movslq %eax, %rdx
movq %r12, %rsi
callq 0x3300
movl %ebx, 0x10(%rsp)
movslq 0x10(%rsp), %rcx
leal 0x6(%rcx), %eax
movl 0x14(%rsp), %edi
cmpl %edi, %eax
jg 0xad7a
leaq 0x77e1(%rip), %rdx      # 0x1253c
movl (%rdx), %edx
movw $0x446, (%r14,%rcx)     # imm = 0x446
leaq 0x2(%rcx), %rsi
movl %esi, 0x10(%rsp)
bswapl %edx
movl %edx, 0x2(%r14,%rcx)
addl $0x6, %ecx
movl %ecx, 0x10(%rsp)
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl %edi, %eax
jg 0xad89
movl 0x10(%rsp), %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, 0x10(%rsp)
jmp 0xad89