// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl $0x98, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2150
movsbl (%r12), %r8d
movl %r8d, 0x20(%rbx)
leaq 0x51e4(%rip), %rbp      # 0x9d6c
leaq 0x51e3(%rip), %rcx      # 0x9d72
pushq $0x65
popq %rsi
pushq $0x3
popq %r13
movq %rbp, %rdi
movl %r13d, %edx
movl %r8d, %r9d
xorl %eax, %eax
callq 0x4998
leaq 0x90(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x85d0
movq %r15, %rdi
callq 0x86aa
testl %eax, %eax
jne 0x5080
movq %r15, %rdi
callq 0x85ac
movl %eax, %r14d
callq 0x48ce
movzwl %r14w, %r12d
cmpl $0x3, %eax
jl 0x4c3a
movq %r15, %rdi
callq 0x85bd
movq %r12, 0x8(%rsp)
movq %rbx, %r12
movl %r13d, %ebx
movq %rbp, %r13
movl %eax, %ebp
movq %r15, %rdi
callq 0x85bd
movl %eax, %edi
callq 0x88b4
movl $0x80, %esi
leaq 0x10(%rsp), %rdi
leaq 0x516c(%rip), %rdx      # 0x9d89
movl %ebp, %ecx
movq %r13, %rbp
movl %ebx, %r13d
movq %r12, %rbx
movq 0x8(%rsp), %r12
movq %rax, %r8
movl %r12d, %r9d
xorl %eax, %eax
callq 0x2120
movq %r15, %rdi
callq 0x85bd
cmpl $0xc, %eax
ja 0x4f84
movl %eax, %eax
leaq 0x50e4(%rip), %rcx      # 0x9d38
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpw $0x1, %r14w
jne 0x4fd7
movq %r15, %rdi
callq 0x859d
movzbl (%rax), %r9d
movb %r9b, 0x91(%rbx)
movq %rbp, %rdi
pushq $0x79
popq %rsi
jmp 0x4ee4
cmpw $0x8, %r14w
jne 0x4f9b
movq %r15, %rdi
callq 0x859d
movl (%rax), %r14d
bswapl %r14d
movq %r14, 0x10(%rbx)
movq %r15, %rdi
callq 0x859d
movl 0x4(%rax), %eax
bswapl %eax
movq %rax, 0x18(%rbx)
movl $0x1, 0x68(%rbx)
movl %eax, (%rsp)
movq %rbp, %rdi
movl $0x111, %esi            # imm = 0x111
jmp 0x4e2b
cmpw $0x3, %r14w
jb 0x4fa5
movq %r15, %rdi
callq 0x859d
movzwl (%rax), %eax
movl %eax, %ecx
rolw $0x8, %cx
movzwl %cx, %r9d
movl %eax, %ecx
xorl $0x100, %ecx            # imm = 0x100
movl %r14d, %edx
xorl $0x6, %edx
orw %cx, %dx
je 0x4d11
xorl $0x200, %eax            # imm = 0x200
xorl $0x12, %r14d
orw %ax, %r14w
jne 0x5069
movq %r12, 0x30(%rbx)
movq %r15, %rdi
movl %r9d, %r14d
callq 0x859d
movq %rax, 0x78(%rbx)
movq %rbp, %rdi
movl $0xb0, %esi
movl %r13d, %edx
leaq 0x5098(%rip), %rcx      # 0x9dce
leaq 0x10(%rsp), %r8
movl %r14d, %r9d
jmp 0x4ef3
cmpw $0x2, %r14w
setb %al
orb %al, %r14b
testb $0x1, %r14b
jne 0x4faf
shrl %r12d
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x4bbc
movq %r15, %rdi
callq 0x859d
movzwl (%rax,%r14,2), %eax
rolw $0x8, %ax
cmpw $0xc, %ax
je 0x4d9e
movzwl %ax, %eax
cmpl $0x6, %eax
jne 0x4dc0
movl $0x1, 0x5c(%rbx)
movl $0x6, (%rsp)
movq %rbp, %rdi
movl $0xc4, %esi
jmp 0x4db4
movl $0x1, 0x60(%rbx)
movl $0xc, (%rsp)
movq %rbp, %rdi
movl $0xc9, %esi
movl %r13d, %edx
leaq 0x5036(%rip), %rcx      # 0x9df4
jmp 0x4dd5
movl %eax, (%rsp)
movq %rbp, %rdi
movl $0xcd, %esi
movl %r13d, %edx
leaq 0x502c(%rip), %rcx      # 0x9e01
leaq 0x10(%rsp), %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x4998
incq %r14
jmp 0x4d5e
cmpw $0x8, %r14w
jne 0x4fc3
movq %r15, %rdi
callq 0x859d
movl (%rax), %r14d
bswapl %r14d
movq %r14, (%rbx)
movq %r15, %rdi
callq 0x859d
movl 0x4(%rax), %eax
bswapl %eax
movq %rax, 0x8(%rbx)
movl $0x1, 0x54(%rbx)
movl %eax, (%rsp)
movq %rbp, %rdi
movl $0xa1, %esi
movl %r13d, %edx
leaq 0x4f84(%rip), %rcx      # 0x9db9
leaq 0x10(%rsp), %r8
movl %r14d, %r9d
jmp 0x4ef3
cmpw $0x4, %r14w
jne 0x4fdf
movl $0x1, 0x64(%rbx)
movq %r15, %rdi
callq 0x859d
movl (%rax), %r9d
bswapl %r9d
movl %r9d, 0x50(%rbx)
movq %rbp, %rdi
movl $0x8f, %esi
jmp 0x4ee4
testw %r14w, %r14w
je 0x501b
movq %r12, 0x28(%rbx)
movq %r15, %rdi
callq 0x859d
movq %rax, 0x70(%rbx)
movq %rbp, %rdi
movl $0x83, %esi
jmp 0x4f24
testw %r14w, %r14w
je 0x5025
movq %r15, %rdi
callq 0x859d
movq %rax, 0x80(%rbx)
movq %r12, 0x40(%rbx)
movq %rbp, %rdi
movl $0xda, %esi
jmp 0x4f24
cmpw $0x1, %r14w
jne 0x4ff3
movq %r15, %rdi
callq 0x859d
movzbl (%rax), %r9d
movb %r9b, 0x90(%rbx)
movq %rbp, %rdi
movl $0xe5, %esi
movl %r13d, %edx
leaq 0x4ea5(%rip), %rcx      # 0x9d93
leaq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x4998
jmp 0x4bbc
testw %r14w, %r14w
je 0x502f
movq %r12, 0x48(%rbx)
movq %r15, %rdi
callq 0x859d
movq %rax, 0x88(%rbx)
movq %rbp, %rdi
movl $0xff, %esi
movl %r13d, %edx
leaq 0x10(%rsp), %rcx
movq %rax, %r8
movq %r12, %r9
callq 0x48d5
jmp 0x4bbc
cmpw $0x3, %r14w
jb 0x5007
movq %r15, %rdi
callq 0x859d
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %r9d
decl %eax
cmpw $0x1, %ax
ja 0x5052
movl $0x1, 0x58(%rbx)
movq %rbp, %rdi
movl $0xf2, %esi
movl %r13d, %edx
leaq 0x4e4f(%rip), %rcx      # 0x9dce
jmp 0x4eee
movq %rbp, %rdi
movl $0x117, %esi            # imm = 0x117
movl %r13d, %edx
leaq 0x4758(%rip), %rcx      # 0x96ee
jmp 0x5041
movq %rbp, %rdi
movl $0x113, %esi            # imm = 0x113
jmp 0x4fcb
movq %rbp, %rdi
movl $0xb5, %esi
jmp 0x500f
movq %rbp, %rdi
movl $0xd2, %esi
movl %r13d, %edx
leaq 0x4e55(%rip), %rcx      # 0x9e16
jmp 0x5041
movq %rbp, %rdi
movl $0xa3, %esi
movl %r13d, %edx
leaq 0x4dee(%rip), %rcx      # 0x9dc3
jmp 0x5041
movq %rbp, %rdi
pushq $0x7b
popq %rsi
jmp 0x4ffb
movq %rbp, %rdi
movl $0x91, %esi
movl %r13d, %edx
leaq 0x4dbd(%rip), %rcx      # 0x9dae
jmp 0x5041
movq %rbp, %rdi
movl $0xe7, %esi
movl %r13d, %edx
leaq 0x4d93(%rip), %rcx      # 0x9d98
jmp 0x5041
movq %rbp, %rdi
movl $0xf7, %esi
movl %r13d, %edx
leaq 0x4dd0(%rip), %rcx      # 0x9de9
jmp 0x5041
movq %rbp, %rdi
movl $0x85, %esi
jmp 0x5037
movq %rbp, %rdi
movl $0xdc, %esi
jmp 0x5037
movq %rbp, %rdi
movl $0x101, %esi            # imm = 0x101
movl %r13d, %edx
leaq 0x4d62(%rip), %rcx      # 0x9da3
leaq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x4998
jmp 0x4bbc
movq %rbp, %rdi
movl $0xf4, %esi
movl %r13d, %edx
leaq 0x4dd3(%rip), %rcx      # 0x9e37
jmp 0x4eee
movq %rbp, %rdi
movl $0xb2, %esi
movl %r13d, %edx
leaq 0x4d5b(%rip), %rcx      # 0x9dd6
jmp 0x4eee
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq