// File: estwings57[P]CasHMC/sources/LinkMaster.cpp
// Function: CasHMC::LinkMaster::ReturnTocken(CasHMC::Packet*)
void LinkMaster::ReturnTocken(Packet *packet)
{
	int i, extRTC = packet->LNG;
	bool findPacket = false;
	for(i=0; i<Buffers.size(); i++) {
		if(Buffers[i] != NULL && Buffers[i]->CMD != PRET && Buffers[i]->CMD != IRTRY) {
			if(Buffers[i]->LNG <= tokenCount) {
				findPacket = true;
			}
			break;
		}
	}
	if(findPacket) {
		if(Buffers[i]->RTC == 0) {
			DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[i]<<(downstream ? "Down) " : "Up)   ")<<"RTC ("<<extRTC<<") is embedded in this packet (from "<<*packet<<")");
		}
		else {
			DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[i]<<(downstream ? "Down) " : "Up)   ")<<"RTC ("<<Buffers[i]->RTC<<" + "<<extRTC<<") is embedded in this packet (from "<<*packet<<")");
		}
		Buffers[i]->RTC += extRTC;
	}
	else {
		//packet, cmd, addr, cub, lng, *lat
		Packet *packetTRET = new Packet(FLOW, TRET, 0, 0, 1, NULL);
		packetTRET->TAG = packet->TAG;
		packetTRET->RTC = extRTC;
		if(downstream)	packetTRET->bufPopDelay = 0;
		
		if(currentState == LINK_RETRY) {
			backupBuffers.push_back(packetTRET);
		}
		else {
			if(startCRC == true) {
				Buffers.insert(Buffers.begin()+Buffers[0]->LNG, packetTRET);
			}
			else {
				Buffers.insert(Buffers.begin(), packetTRET);
			}
		}
		DEBUG(ALI(18)<<header<<ALI(15)<<*packetTRET<<(downstream ? "Down) " : "Up)   ")<<"MAKING TRET packet to be embedded RTC ("<<extRTC<<")");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rax
movl 0x430(%rax), %eax
movl %eax, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movl $0x0, -0x14(%rbp)
movq -0xa8(%rbp), %rdi
movslq -0x14(%rbp), %rax
movq %rax, -0xb0(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x22d60
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jae 0x3cfdf
movq -0xa8(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
cmpq $0x0, (%rax)
je 0x3cfcf
movq -0xa8(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
cmpl $0x1, 0x434(%rax)
je 0x3cfcf
movq -0xa8(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
cmpl $0x3, 0x434(%rax)
je 0x3cfcf
movq -0xa8(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq -0xa8(%rbp), %rcx
movq (%rax), %rax
movl 0x430(%rax), %eax
cmpl 0x208(%rcx), %eax
ja 0x3cfcd
movb $0x1, -0x19(%rbp)
jmp 0x3cfdf
jmp 0x3cfd1
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3cf06
testb $0x1, -0x19(%rbp)
je 0x3d3a1
movq -0xa8(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
cmpl $0x0, 0x43c(%rax)
jne 0x3d19c
leaq 0x29511(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3d197
leaq 0x29502(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3d197
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %esi
movq 0x28f89(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xd8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xd8(%rbp), %rdi
movb %al, -0x21(%rbp)
movzbl -0x21(%rbp), %esi
callq 0x4550
movq -0xa8(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xd0(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xd0(%rbp), %rdi
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %esi
callq 0x4440
movq %rax, -0xc8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xc8(%rbp), %rdi
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %esi
callq 0x4550
movq -0xa8(%rbp), %rdi
movq %rax, -0xc0(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq -0xc0(%rbp), %rdi
movq (%rax), %rsi
callq 0x44960
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0xb8(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3d121
leaq 0x16530(%rip), %rax     # 0x53648
movq %rax, -0xe0(%rbp)
jmp 0x3d131
leaq 0x16527(%rip), %rax     # 0x5364f
movq %rax, -0xe0(%rbp)
jmp 0x3d131
movq -0xb8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x16705(%rip), %rsi     # 0x53853
callq 0x4380
movq %rax, %rdi
movl -0x18(%rbp), %esi
callq 0x4610
movq %rax, %rdi
leaq 0x166a1(%rip), %rsi     # 0x53809
callq 0x4380
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x44960
movq %rax, %rdi
leaq 0x171f6(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x28e1e(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x3d367
leaq 0x29385(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3d365
leaq 0x29376(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3d365
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %esi
movq 0x28dfd(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0x108(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x108(%rbp), %rdi
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %esi
callq 0x4550
movq -0xa8(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0x100(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0x100(%rbp), %rdi
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %esi
callq 0x4440
movq %rax, -0xf8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xf8(%rbp), %rdi
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %esi
callq 0x4550
movq -0xa8(%rbp), %rdi
movq %rax, -0xf0(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq -0xf0(%rbp), %rdi
movq (%rax), %rsi
callq 0x44960
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0xe8(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3d2ad
leaq 0x163a4(%rip), %rax     # 0x53648
movq %rax, -0x110(%rbp)
jmp 0x3d2bd
leaq 0x1639b(%rip), %rax     # 0x5364f
movq %rax, -0x110(%rbp)
jmp 0x3d2bd
movq -0xe8(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x16579(%rip), %rsi     # 0x53853
callq 0x4380
movq -0xa8(%rbp), %rdi
movq %rax, -0x118(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movq -0x118(%rbp), %rdi
movq (%rax), %rax
movl 0x43c(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x16511(%rip), %rsi     # 0x5382d
callq 0x4380
movq %rax, %rdi
movl -0x18(%rbp), %esi
callq 0x4610
movq %rax, %rdi
leaq 0x164d3(%rip), %rsi     # 0x53809
callq 0x4380
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x44960
movq %rax, %rdi
leaq 0x17028(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x28c50(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x3d367
movq -0xa8(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, -0x11c(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x14(%rbp), %rsi
callq 0x29940
movl -0x11c(%rbp), %ecx
movq (%rax), %rax
addl 0x43c(%rax), %ecx
movl %ecx, 0x43c(%rax)
jmp 0x3d6c3
movl $0x468, %edi            # imm = 0x468
callq 0x4390
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x128(%rbp)
movq %rsp, %rax
movq $0x0, (%rax)
xorl %r8d, %r8d
movl %r8d, %ecx
movl $0x2, %edx
movl $0x1, %r9d
movl %edx, %esi
callq 0x43a90
jmp 0x3d3dc
movq -0xa8(%rbp), %rax
movq -0x128(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq -0x10(%rbp), %rcx
movl 0x42c(%rcx), %edx
movq -0x48(%rbp), %rcx
movl %edx, 0x42c(%rcx)
movl -0x18(%rbp), %edx
movq -0x48(%rbp), %rcx
movl %edx, 0x43c(%rcx)
testb $0x1, 0x1cc(%rax)
je 0x3d447
movq -0x48(%rbp), %rax
movl $0x0, 0x14(%rax)
jmp 0x3d447
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x468, %esi            # imm = 0x468
callq 0x43a0
jmp 0x3d6cc
movq -0xa8(%rbp), %rax
cmpl $0xb, 0x200(%rax)
jne 0x3d473
movq -0xa8(%rbp), %rdi
addq $0x220, %rdi            # imm = 0x220
leaq -0x48(%rbp), %rsi
callq 0x29ef0
jmp 0x3d55e
movq -0xa8(%rbp), %rax
movb 0x240(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x3d507
movq -0xa8(%rbp), %rdi
movq %rdi, %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq %rax, -0x130(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x299f0
movq -0xa8(%rbp), %rdi
movq %rax, -0x70(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
xorl %eax, %eax
movl %eax, %esi
callq 0x29940
movq (%rax), %rax
movl 0x430(%rax), %eax
movl %eax, %esi
leaq -0x70(%rbp), %rdi
callq 0x29a20
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x29a60
movq -0x130(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x29aa0
movq %rax, -0x78(%rbp)
jmp 0x3d55c
movq -0xa8(%rbp), %rdi
movq %rdi, %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq %rax, -0x138(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x299f0
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x29a60
movq -0x138(%rbp), %rdi
movq -0x80(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x29aa0
movq %rax, -0x90(%rbp)
jmp 0x3d55e
leaq 0x28fc3(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3d6c1
leaq 0x28fb4(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3d6c1
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %esi
movq 0x28a35(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0x158(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x158(%rbp), %rdi
movb %al, -0x95(%rbp)
movzbl -0x95(%rbp), %esi
callq 0x4550
movq -0xa8(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0x150(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0x150(%rbp), %rdi
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %esi
callq 0x4440
movq %rax, -0x148(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x148(%rbp), %rdi
movb %al, -0x9d(%rbp)
movzbl -0x9d(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x44960
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0x140(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3d666
leaq 0x15feb(%rip), %rax     # 0x53648
movq %rax, -0x160(%rbp)
jmp 0x3d676
leaq 0x15fe2(%rip), %rax     # 0x5364f
movq %rax, -0x160(%rbp)
jmp 0x3d676
movq -0x140(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x1619e(%rip), %rsi     # 0x53831
callq 0x4380
movq %rax, %rdi
movl -0x18(%rbp), %esi
callq 0x4610
movq %rax, %rdi
leaq 0x16ccc(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x288f4(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x3d6c3
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x4630
nopw %cs:(%rax,%rax)