// File: /workspace/github_c_makefile_star2_O1/jfriesse[P]omping/msg.c
// Function: msg_decode
void
msg_decode(const char *msg, size_t msg_len, struct msg_decoded *decoded)
{
	char debug_str[128];
	struct tlv_iterator tlv_iter;
	size_t pos;
	uint32_t u32, u32_2;
	uint16_t tlv_len;
	uint16_t u16;
	uint8_t u8;

	memset(decoded, 0, sizeof(struct msg_decoded));

	decoded->msg_type = (enum msg_type)msg[0];

	DEBUG2_PRINTF("Message type %c (0x%X)", decoded->msg_type, decoded->msg_type);

	tlv_iter_init(msg, msg_len, &tlv_iter);

	while (tlv_iter_next(&tlv_iter) == 0) {
		tlv_len = tlv_iter_get_len(&tlv_iter);

		if (logging_get_verbose() >= LOGGING_LEVEL_DEBUG2) {
			snprintf(debug_str, sizeof(debug_str), "%u:%s:%u:",
			    tlv_iter_get_type(&tlv_iter),
			    tlv_opt_type_to_str(tlv_iter_get_type(&tlv_iter)), tlv_len);
		}

		switch (tlv_iter_get_type(&tlv_iter)) {
		case TLV_OPT_TYPE_VERSION:
			if (tlv_len == 1) {
				memcpy(&u8, tlv_iter_get_data(&tlv_iter), sizeof(u8));

				decoded->version = u8;

				DEBUG2_PRINTF("%s%u", debug_str, u8);
			} else {
				DEBUG2_PRINTF("%slen != 1", debug_str);
			}
			break;
		case TLV_OPT_TYPE_CLIENT_ID:
			if (tlv_len > 0) {
				decoded->client_id_len = tlv_len;
				decoded->client_id = tlv_iter_get_data(&tlv_iter);

				DEBUG2_HEXDUMP(debug_str, decoded->client_id, tlv_len);
			} else {
				DEBUG2_PRINTF("%slen <= 0", debug_str);
			}
			break;
		case TLV_OPT_TYPE_SEQ_NUM:
			if (tlv_len == 4) {
				decoded->seq_num_isset = 1;
				memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
				u32 = ntohl(u32);
				decoded->seq_num = u32;

				DEBUG2_PRINTF("%s%u", debug_str, u32);
			} else {
				DEBUG2_PRINTF("%slen != 4", debug_str);
			}
			break;
		case TLV_OPT_TYPE_CLIENT_TSTAMP:
			if (tlv_len == 8) {
				memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
				u32 = ntohl(u32);
				decoded->client_tstamp.tv_sec = u32;

				memcpy(&u32_2, tlv_iter_get_data(&tlv_iter) + sizeof(u32),
				    sizeof(u32_2));
				u32_2 = ntohl(u32_2);
				decoded->client_tstamp.tv_usec = u32_2;

				decoded->client_tstamp_isset = 1;

				DEBUG2_PRINTF("%s(%u,%u)", debug_str, u32, u32_2);
			} else {
				DEBUG2_PRINTF("%slen != 8", debug_str);
			}
			break;
		case TLV_OPT_TYPE_MCAST_GRP:
			if (tlv_len > 2) {
				memcpy(&u16, tlv_iter_get_data(&tlv_iter), sizeof(u16));
				u16 = ntohs(u16);

				if ((u16 == AF_IANA_IP && tlv_len == 6) ||
				    (u16 == AF_IANA_IP6 && tlv_len == 18)) {
					decoded->mcast_grp_len = tlv_len;
					decoded->mcast_grp = tlv_iter_get_data(&tlv_iter);

					DEBUG2_PRINTF("%sAF %u", debug_str, u16);
				} else {
					DEBUG2_PRINTF("%sbad AF %u or len", debug_str, u16);
				}
			} else {
				DEBUG2_PRINTF("%slen <= 2", debug_str);
			}
			break;
		case TLV_OPT_TYPE_OPT_REQUEST:
			if (tlv_len > 1 && (tlv_len % 2  == 0)) {
				for (pos = 0; pos < (uint16_t)(tlv_len / 2); pos++) {
					memcpy(&u16, tlv_iter_get_data(&tlv_iter) + pos * 2,
					    sizeof(u16));

					u16 = ntohs(u16);

					switch (u16) {
					case TLV_OPT_TYPE_SERVER_INFO:
						decoded->request_opt_server_info = 1;

						DEBUG2_PRINTF("%s%zu opt %u", debug_str, pos, u16);
						break;
					case TLV_OPT_TYPE_SERVER_TSTAMP:
						decoded->request_opt_server_tstamp = 1;

						DEBUG2_PRINTF("%s%zu opt %u", debug_str, pos, u16);
						break;
					default:
						DEBUG2_PRINTF("%s%zu unknown opt %u", debug_str,
						    pos, u16);
						break;
					}
				}
			} else {
				DEBUG2_PRINTF("%slen <= 1 || (tlv_len %%2 != 0)", debug_str);
			}
			break;
		case TLV_OPT_TYPE_SERVER_INFO:
			if (tlv_len > 0) {
				decoded->server_info = tlv_iter_get_data(&tlv_iter);
				decoded->server_info_len = tlv_len;

				DEBUG2_HEXDUMP(debug_str, decoded->server_info, tlv_len);
			} else {
				DEBUG2_PRINTF("%slen <= 0", debug_str);
			}
			break;
		case TLV_OPT_TYPE_TTL:
			if (tlv_len == 1) {
				memcpy(&u8, tlv_iter_get_data(&tlv_iter), sizeof(u8));

				decoded->ttl = u8;

				DEBUG2_PRINTF("%s%u", debug_str, u8);
			} else {
				DEBUG2_PRINTF("%slen != 1", debug_str);
			}
			break;
		case TLV_OPT_TYPE_MCAST_PREFIX:
			if (tlv_len > 2) {
				memcpy(&u16, tlv_iter_get_data(&tlv_iter), sizeof(u16));
				u16 = ntohs(u16);

				if (u16 == AF_IANA_IP  || u16 == AF_IANA_IP6) {
					decoded->mcast_prefix_isset = 1;

					DEBUG2_PRINTF("%sAF %u", debug_str, u16);
				} else {
					DEBUG2_PRINTF("%sbad AF %u", debug_str, u16);
				}
			} else {
				DEBUG2_PRINTF("%slen <= 2", debug_str);
			}
			break;
		case TLV_OPT_TYPE_SES_ID:
			if (tlv_len > 0) {
				decoded->ses_id_len = tlv_len;
				decoded->ses_id = tlv_iter_get_data(&tlv_iter);

				DEBUG2_HEXDUMP(debug_str, decoded->ses_id, tlv_len);
			} else {
				DEBUG2_PRINTF("%slen <= 0", debug_str);
			}
			break;
		case TLV_OPT_TYPE_SERVER_TSTAMP:
			if (tlv_len == 8) {
				memcpy(&u32, tlv_iter_get_data(&tlv_iter), sizeof(u32));
				u32 = ntohl(u32);
				decoded->server_tstamp.tv_sec = u32;

				memcpy(&u32_2, tlv_iter_get_data(&tlv_iter) + sizeof(u32),
				    sizeof(u32_2));
				u32_2 = ntohl(u32_2);
				decoded->server_tstamp.tv_usec = u32_2;

				decoded->server_tstamp_isset = 1;

				DEBUG2_PRINTF("%s(%u,%u)", debug_str, u32, u32_2);
			} else {
				DEBUG2_PRINTF("%slen != 8", debug_str);
			}
			break;
		default:
			DEBUG2_PRINTF("%s", debug_str);
			break;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2150
movsbl (%r15), %r8d
movl %r8d, 0x20(%rbx)
leaq 0x6090(%rip), %rdi      # 0xad6c
leaq 0x608f(%rip), %rcx      # 0xad72
movl $0x65, %esi
movl $0x3, %edx
movl %r8d, %r9d
xorl %eax, %eax
callq 0x4ada
leaq 0x8(%rsp), %r12
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x8b67
movq %r12, %rdi
callq 0x8c4a
testl %eax, %eax
jne 0x5200
leaq 0x8(%rsp), %r14
leaq 0x20(%rsp), %r12
leaq 0x6041(%rip), %r13      # 0xad6c
movq %r14, %rdi
callq 0x8b43
movl %eax, %r15d
callq 0x4a07
cmpl $0x3, %eax
jl 0x4d78
movq %r14, %rdi
callq 0x8b54
movl %eax, %ebp
movq %r14, %rdi
callq 0x8b54
movl %eax, %edi
callq 0x8e76
movzwl %r15w, %r9d
movl $0x80, %esi
movq %r12, %rdi
leaq 0x601d(%rip), %rdx      # 0xad89
movl %ebp, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x2120
movq %r14, %rdi
callq 0x8b54
cmpl $0xc, %eax
ja 0x50da
movl %eax, %eax
leaq 0x5fa6(%rip), %rcx      # 0xad38
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpw $0x1, %r15w
jne 0x5136
movq %r14, %rdi
callq 0x8b34
movzbl (%rax), %r9d
movb %r9b, 0x91(%rbx)
movq %r13, %rdi
movl $0x79, %esi
jmp 0x503b
cmpw $0x8, %r15w
jne 0x50f3
movq %r14, %rdi
callq 0x8b34
movl (%rax), %r15d
bswapl %r15d
movq %r15, 0x10(%rbx)
movq %r14, %rdi
callq 0x8b34
movl 0x4(%rax), %eax
bswapl %eax
movq %rax, 0x18(%rbx)
movl $0x1, 0x68(%rbx)
movl %eax, (%rsp)
movq %r13, %rdi
movl $0x111, %esi            # imm = 0x111
jmp 0x4f6c
cmpw $0x3, %r15w
jb 0x50fd
movq %r14, %rdi
callq 0x8b34
movzwl (%rax), %eax
movl %eax, %ecx
rolw $0x8, %cx
movzwl %cx, %ebp
movl %eax, %ecx
xorl $0x100, %ecx            # imm = 0x100
movl %r15d, %edx
xorl $0x6, %edx
orw %cx, %dx
je 0x4e51
xorl $0x200, %eax            # imm = 0x200
movl %r15d, %ecx
xorl $0x12, %ecx
orw %ax, %cx
jne 0x51cf
movzwl %r15w, %eax
movq %rax, 0x30(%rbx)
movq %r14, %rdi
callq 0x8b34
movq %rax, 0x78(%rbx)
movq %r13, %rdi
movl $0xb0, %esi
movl $0x3, %edx
leaq 0x5f55(%rip), %rcx      # 0xadce
jmp 0x51e3
cmpw $0x2, %r15w
setb %al
movl %r15d, %ecx
orb %al, %cl
testb $0x1, %cl
jne 0x5107
movzwl %r15w, %ebp
shrl %ebp
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x8b34
movzwl (%rax,%r15,2), %eax
rolw $0x8, %ax
cmpw $0xc, %ax
je 0x4ed4
movzwl %ax, %eax
cmpl $0x6, %eax
jne 0x4ef8
movl $0x1, 0x5c(%rbx)
movl $0x6, (%rsp)
movq %r13, %rdi
movl $0xc4, %esi
jmp 0x4eea
movl $0x1, 0x60(%rbx)
movl $0xc, (%rsp)
movq %r13, %rdi
movl $0xc9, %esi
movl $0x3, %edx
leaq 0x5efe(%rip), %rcx      # 0xadf4
jmp 0x4f0f
movl %eax, (%rsp)
movq %r13, %rdi
movl $0xcd, %esi
movl $0x3, %edx
leaq 0x5ef2(%rip), %rcx      # 0xae01
movq %r12, %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x4ada
incq %r15
cmpq %r15, %rbp
jne 0x4e9d
jmp 0x51f0
cmpw $0x8, %r15w
jne 0x5120
movq %r14, %rdi
callq 0x8b34
movl (%rax), %r15d
bswapl %r15d
movq %r15, (%rbx)
movq %r14, %rdi
callq 0x8b34
movl 0x4(%rax), %eax
bswapl %eax
movq %rax, 0x8(%rbx)
movl $0x1, 0x54(%rbx)
movl %eax, (%rsp)
movq %r13, %rdi
movl $0xa1, %esi
movl $0x3, %edx
leaq 0x5e41(%rip), %rcx      # 0xadb9
movq %r12, %r8
movl %r15d, %r9d
jmp 0x51e9
cmpw $0x4, %r15w
jne 0x5140
movl $0x1, 0x64(%rbx)
movq %r14, %rdi
callq 0x8b34
movl (%rax), %r9d
bswapl %r9d
movl %r9d, 0x50(%rbx)
movq %r13, %rdi
movl $0x8f, %esi
jmp 0x503b
testw %r15w, %r15w
je 0x5182
movzwl %r15w, %r15d
movq %r15, 0x28(%rbx)
movq %r14, %rdi
callq 0x8b34
movq %rax, 0x70(%rbx)
movq %r13, %rdi
movl $0x83, %esi
jmp 0x5078
testw %r15w, %r15w
je 0x518c
movq %r14, %rdi
callq 0x8b34
movq %rax, 0x80(%rbx)
movzwl %r15w, %r9d
movq %r9, 0x40(%rbx)
movq %r13, %rdi
movl $0xda, %esi
movl $0x3, %edx
movq %r12, %rcx
movq %rax, %r8
jmp 0x5086
cmpw $0x1, %r15w
jne 0x5156
movq %r14, %rdi
callq 0x8b34
movzbl (%rax), %r9d
movb %r9b, 0x90(%rbx)
movq %r13, %rdi
movl $0xe5, %esi
movl $0x3, %edx
leaq 0x5d4c(%rip), %rcx      # 0xad93
movq %r12, %r8
jmp 0x51e9
testw %r15w, %r15w
je 0x5196
movzwl %r15w, %r15d
movq %r15, 0x48(%rbx)
movq %r14, %rdi
callq 0x8b34
movq %rax, 0x88(%rbx)
movq %r13, %rdi
movl $0xff, %esi
movl $0x3, %edx
movq %r12, %rcx
movq %rax, %r8
movq %r15, %r9
callq 0x4a0e
jmp 0x51f0
cmpw $0x3, %r15w
jb 0x516c
movq %r14, %rdi
callq 0x8b34
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %r9d
decl %eax
cmpw $0x1, %ax
ja 0x51b6
movl $0x1, 0x58(%rbx)
movq %r13, %rdi
movl $0xf2, %esi
movl $0x3, %edx
leaq 0x5cf9(%rip), %rcx      # 0xadce
jmp 0x5047
movq %r13, %rdi
movl $0x117, %esi            # imm = 0x117
movl $0x3, %edx
leaq 0x5600(%rip), %rcx      # 0xa6ee
jmp 0x51aa
movq %r13, %rdi
movl $0x113, %esi            # imm = 0x113
jmp 0x5128
movq %r13, %rdi
movl $0xb5, %esi
jmp 0x5174
movq %r13, %rdi
movl $0xd2, %esi
movl $0x3, %edx
leaq 0x5cfb(%rip), %rcx      # 0xae16
jmp 0x51aa
movq %r13, %rdi
movl $0xa3, %esi
movl $0x3, %edx
leaq 0x5c8f(%rip), %rcx      # 0xadc3
jmp 0x51aa
movq %r13, %rdi
movl $0x7b, %esi
jmp 0x515e
movq %r13, %rdi
movl $0x91, %esi
movl $0x3, %edx
leaq 0x5c5a(%rip), %rcx      # 0xadae
jmp 0x51aa
movq %r13, %rdi
movl $0xe7, %esi
movl $0x3, %edx
leaq 0x5c2e(%rip), %rcx      # 0xad98
jmp 0x51aa
movq %r13, %rdi
movl $0xf7, %esi
movl $0x3, %edx
leaq 0x5c69(%rip), %rcx      # 0xade9
jmp 0x51aa
movq %r13, %rdi
movl $0x85, %esi
jmp 0x519e
movq %r13, %rdi
movl $0xdc, %esi
jmp 0x519e
movq %r13, %rdi
movl $0x101, %esi            # imm = 0x101
movl $0x3, %edx
leaq 0x5bf9(%rip), %rcx      # 0xada3
movq %r12, %r8
xorl %eax, %eax
callq 0x4ada
jmp 0x51f0
movq %r13, %rdi
movl $0xf4, %esi
movl $0x3, %edx
leaq 0x5c6d(%rip), %rcx      # 0xae37
jmp 0x5047
movq %r13, %rdi
movl $0xb2, %esi
movl $0x3, %edx
leaq 0x5bf3(%rip), %rcx      # 0xadd6
movq %r12, %r8
movl %ebp, %r9d
xorl %eax, %eax
callq 0x4ada
movq %r14, %rdi
callq 0x8c4a
testl %eax, %eax
je 0x4d2b
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq