// File: estwings57[P]CasHMC/sources/Link.cpp
// Function: CasHMC::Link::Link(std::basic_ofstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&, unsigned int, bool, CasHMC::TranStatistic*)
Link::Link(ofstream &debugOut_, ofstream &stateOut_, unsigned id, bool down, TranStatistic *statisP):
	SimulatorObject(debugOut_, stateOut_),
	linkID(id),
	downstream(down),
	statis(statisP)
{
	classID << linkID;
	header = "   (LK_";
	header += downstream ? "D" : "U";
	header += classID.str() + ")";
	
	linkMasterP = NULL;
	linkSlaveP = NULL;
	inFlightPacket = NULL;
	inFlightCountdown = 0;
	
	//The probability of at least one bit error
	//  (pow(10, -LINK_BER) - 1)/pow(10, -LINK_BER) : Probability of no error in one bit
	//  (1 - pow((pow(10, -LINK_BER) - 1)/pow(10, -LINK_BER), 128)) : Probability of at least one bit error in 128 bits
	//  errorProba is square root of bits size per one bit error, because errorProba is over than variable capacity.
	//  Link error will be calculated two times by errorProba respectively
	errorProba = sqrt(1 / (1 - pow((pow(10, -LINK_BER) - 1)/pow(10, -LINK_BER), 128)));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
andb $0x1, %al
movb %al, -0x1d(%rbp)
movq %r9, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x80(%rbp)
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x21ff0
movq -0x80(%rbp), %rax
leaq 0x2aa0a(%rip), %rcx     # 0x65be0
movq %rcx, (%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x1c8(%rax)
movb -0x1d(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x1cc(%rax)
movq -0x28(%rbp), %rcx
movq %rcx, 0x1e0(%rax)
movq %rax, %rdi
addq $0x50, %rdi
movl 0x1c8(%rax), %esi
callq 0x44c0
jmp 0x3b20d
movq -0x80(%rbp), %rdi
addq $0x20, %rdi
leaq 0x18424(%rip), %rsi     # 0x53640
callq 0x44e0
jmp 0x3b223
movq -0x80(%rbp), %rax
testb $0x1, 0x1cc(%rax)
je 0x3b240
leaq 0x19276(%rip), %rax     # 0x544ad
movq %rax, -0x88(%rbp)
jmp 0x3b250
leaq 0x17cd4(%rip), %rax     # 0x52f1b
movq %rax, -0x88(%rbp)
jmp 0x3b250
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
addq $0x20, %rdi
callq 0x4350
jmp 0x3b266
movq -0x80(%rbp), %rsi
addq $0x40, %rsi
leaq -0x78(%rbp), %rdi
callq 0x4370
jmp 0x3b279
leaq 0x190f9(%rip), %rdx     # 0x54379
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x18020
jmp 0x3b28f
movq -0x80(%rbp), %rdi
addq $0x20, %rdi
leaq -0x58(%rbp), %rsi
callq 0x44a0
jmp 0x3b2a2
leaq -0x58(%rbp), %rdi
callq 0x46e8
leaq -0x78(%rbp), %rdi
callq 0x46e8
movq -0x80(%rbp), %rax
movq $0x0, 0x1d8(%rax)
movq $0x0, 0x1e8(%rax)
movq $0x0, 0x1f0(%rax)
movl $0x0, 0x1f8(%rax)
leaq 0x2b296(%rip), %rax     # 0x66580
movl (%rax), %esi
negl %esi
movl $0xa, %edi
callq 0x3c230
movsd %xmm0, -0x90(%rbp)
jmp 0x3b302
movsd -0x90(%rbp), %xmm0
movsd 0x18326(%rip), %xmm1    # 0x53638
addsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
leaq 0x2b25b(%rip), %rax     # 0x66580
movl (%rax), %esi
negl %esi
movl $0xa, %edi
callq 0x3c230
movsd %xmm0, -0x98(%rbp)
jmp 0x3b33d
movsd -0x98(%rbp), %xmm1
movsd -0xa0(%rbp), %xmm0
divsd %xmm1, %xmm0
movl $0x80, %edi
callq 0x17f50
movsd %xmm0, -0xa8(%rbp)
jmp 0x3b365
movsd -0xa8(%rbp), %xmm2
movsd 0x15d3b(%rip), %xmm0    # 0x510b0
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x42c0
movq -0x80(%rbp), %rax
cvttsd2si %xmm0, %rcx
movl %ecx, 0x1d0(%rax)
addq $0xb0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x3b3d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x3b3ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x46e8
leaq -0x78(%rbp), %rdi
callq 0x46e8
movq -0x80(%rbp), %rdi
callq 0x22e30
movq -0x30(%rbp), %rdi
callq 0x4630
nopl (%rax)