// File: /workspace/github_cpp_makefile_star2_O3/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:
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 0x6e09
leal -0x1(%rdx), %eax
movl %eax, (%rdi)
xorl %r11d, %r11d
movq %r11, 0x28(%rdi)
movq %r8, 0x30(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rdi)
movups %xmm0, 0x14(%rdi)
movups %xmm0, 0x38(%rdi)
movq %r11, 0x45(%rdi)
movl %eax, %r11d
subl %edx, %r11d
jbe 0x6e1b
cmpl 0x4(%rdi), %r11d
jae 0x6f48
movq %rcx, 0x28(%rdi)
movq %r8, 0x30(%rdi)
leal 0x1(%rax), %ecx
cmpl %edx, %ecx
jbe 0x6e4f
movl 0xc(%rdi), %r8d
testq %r8, %r8
je 0x6e49
xorl %r11d, %r11d
cmpl %edx, 0x50(%rdi,%r11,4)
je 0x6ed3
incq %r11
cmpq %r11, %r8
jne 0x6e36
movb %sil, 0x4d(%rdi)
jmp 0x6e57
movb %sil, 0x4d(%rdi)
movl 0xc(%rdi), %r8d
incl 0x8(%rdi)
leal 0x1(%r8), %esi
movl %esi, 0xc(%rdi)
movl %r8d, %esi
movl %edx, 0x50(%rdi,%rsi,4)
addl %r9d, 0x10(%rdi)
movl %r10d, %esi
subl 0x14(%rdi), %esi
movl %r10d, 0x14(%rdi)
movl %esi, %r8d
negl %r8d
cmovsl %esi, %r8d
xorps %xmm0, %xmm0
cvtss2sd 0x3c(%rdi), %xmm0
cvtsi2sd %r8d, %xmm1
mulsd 0x51c3(%rip), %xmm1     # 0xc058
mulsd 0x51c3(%rip), %xmm0     # 0xc060
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x3c(%rdi)
cmpl %edx, %ecx
jne 0x6eb3
incl 0x4(%rdi)
jmp 0x6ecb
jae 0x6ec5
movl %edx, %esi
subl %eax, %esi
addl %esi, 0x4(%rdi)
notl %eax
addl %edx, %eax
addl %eax, 0x18(%rdi)
jmp 0x6ecb
decl 0x18(%rdi)
incl 0x20(%rdi)
cmpl %edx, %ecx
ja 0x6eda
movl %edx, (%rdi)
jmp 0x6eda
movb %sil, 0x4d(%rdi)
incl 0x1c(%rdi)
movl 0x4(%rdi), %eax
cmpl $0x28, %eax
jb 0x6f48
leaq 0x4(%rdi), %rcx
xorps %xmm0, %xmm0
cvtsi2ssl 0x10(%rdi), %xmm0
movl 0x8(%rdi), %edx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
divss %xmm1, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
movss %xmm0, 0x38(%rdi)
xorps %xmm0, %xmm0
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)
movq $0x0, 0x18(%rdi)
movl $0x0, 0x20(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, (%rcx)
retq
nop