// File: /workspace/github_cpp_makefile_star2_O2/hugosantos[P]dbeacon/dbeacon.cpp
// Function: beaconMcastState::update(unsigned char, unsigned int, unsigned long, unsigned long, unsigned long)
void beaconMcastState::update(uint8_t ttl, uint32_t seqnum, uint64_t timestamp, uint64_t tsnow, uint64_t _now) {
	/*
	 * ttl - received TTL
	 * seqnum - received seqnum in probe
	 * timestamp - received timestamp in probe (timeofday in sender)
	 * _now - when this packet was received locally (timeofday of host)
	 */

	int64_t now = (uint32_t)_now;

	int64_t diff = now - timestamp;
	int64_t absdiff = abs64(diff);

	if (udiff(seqnum, lastseq) > PACKETS_VERY_OLD) {
		refresh(seqnum - 1, tsnow);
	}

	if (seqnum < lastseq && (lastseq - seqnum) >= packetcount)
		return;

	s.timestamp = timestamp;
	s.lastupdate = tsnow;

	bool dup = false;

	uint32_t expectseq = lastseq + 1;

	if (seqnum < expectseq) {
		for (uint32_t i = 0; i < pointer; i++) {
			if (cacheseqnum[i] == seqnum) {
				dup = true;
				break;
			}
		}
	}

	s.rttl = ttl;

	if (dup) {
		lastdup ++;
	} else {
		packetcountreal++;

		cacheseqnum[pointer++] = seqnum;

		lastdelay += diff;

		int newjitter = absdiff - lastjitter;
		lastjitter = absdiff;
		if (newjitter < 0)
			newjitter = -newjitter;
		s.avgjitter = 15/16. * s.avgjitter + 1/16. * newjitter;

		if (expectseq == seqnum) {
			packetcount ++;
		} else if (seqnum > expectseq) {
			packetcount += seqnum - lastseq;

			lastloss += seqnum - lastseq - 1;
		} else {
			lastloss --;
			lastooo ++;
		}

		if (expectseq <= seqnum) {
			lastseq = seqnum;
		}
	}

	if (packetcount >= PACKETS_PERIOD) {
		s.avgdelay = lastdelay / (float)packetcountreal;
		s.avgloss = lastloss / (float)packetcount;
		s.avgooo = lastooo / (float)packetcount;
		s.avgdup = lastdup / (float)packetcount;

		s.valid = true;

		lastdelay = 0;
		lastloss = 0;
		lastooo = 0;
		lastdup = 0;
		packetcount = 0;
		packetcountreal = 0;
		pointer = 0;
	}
}

// Assembly:
pushq %rbx
movl (%rdi), %eax
movl %eax, %r10d
subl %edx, %r10d
negl %r10d
movl %eax, %r11d
subl %edx, %r11d
movl %r9d, %r9d
cmovbl %r10d, %r11d
subq %rcx, %r9
movq %r9, %r10
negq %r10
cmovsq %r9, %r10
cmpl $0x97, %r11d
jb 0x66b7
leal -0x1(%rdx), %eax
movl %eax, (%rdi)
andq $0x0, 0x28(%rdi)
movq %r8, 0x30(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rdi)
movups %xmm0, 0x14(%rdi)
movups %xmm0, 0x38(%rdi)
andq $0x0, 0x45(%rdi)
movl %eax, %r11d
subl %edx, %r11d
jbe 0x66c9
cmpl 0x4(%rdi), %r11d
jae 0x67de
movq %rcx, 0x28(%rdi)
movq %r8, 0x30(%rdi)
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
jbe 0x66fe
movl 0xc(%rdi), %r8d
xorl %r11d, %r11d
cmpq %r11, %r8
je 0x6708
leaq 0x1(%r11), %rbx
cmpl %edx, 0x50(%rdi,%r11,4)
movq %rbx, %r11
jne 0x66df
movb %sil, 0x4d(%rdi)
incl 0x1c(%rdi)
jmp 0x6782
movb %sil, 0x4d(%rdi)
movl 0xc(%rdi), %r8d
jmp 0x670c
movb %sil, 0x4d(%rdi)
movl %r8d, %esi
incl 0x8(%rdi)
incl %esi
movl %esi, 0xc(%rdi)
movl %edx, 0x50(%rdi,%r8,4)
addl %r9d, 0x10(%rdi)
movl %r10d, %esi
subl 0x14(%rdi), %esi
movl %r10d, 0x14(%rdi)
movl %esi, %r8d
negl %r8d
cmovsl %esi, %r8d
cvtss2sd 0x3c(%rdi), %xmm0
cvtsi2sd %r8d, %xmm1
mulsd 0x4912(%rip), %xmm1     # 0xb058
mulsd 0x4912(%rip), %xmm0     # 0xb060
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x3c(%rdi)
cmpl %edx, %ecx
jne 0x6764
incl 0x4(%rdi)
jmp 0x677c
jae 0x6776
movl %edx, %esi
subl %eax, %esi
addl %esi, 0x4(%rdi)
notl %eax
addl %edx, %eax
addl %eax, 0x18(%rdi)
jmp 0x677c
decl 0x18(%rdi)
incl 0x20(%rdi)
cmpl %edx, %ecx
ja 0x6782
movl %edx, (%rdi)
movl 0x4(%rdi), %eax
cmpl $0x28, %eax
jb 0x67de
cvtsi2ssl 0x10(%rdi), %xmm0
movl 0x8(%rdi), %ecx
cvtsi2ss %rcx, %xmm1
divss %xmm1, %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
movss %xmm0, 0x38(%rdi)
cvtsi2ssl 0x20(%rdi), %xmm0
divss %xmm1, %xmm0
movss %xmm0, 0x48(%rdi)
movsd 0x18(%rdi), %xmm0
cvtdq2ps %xmm0, %xmm0
unpcklps %xmm1, %xmm1            # xmm1 = xmm1[0,0,1,1]
divps %xmm1, %xmm0
movlps %xmm0, 0x40(%rdi)
movb $0x1, 0x4c(%rdi)
andq $0x0, 0x18(%rdi)
andl $0x0, 0x20(%rdi)
leaq 0x4(%rdi), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
popq %rbx
retq