// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r14
movl 0x430(%rsi), %ebx
movq 0x1d0(%rdi), %rax
movq 0x1d8(%rdi), %rcx
subq %rax, %rcx
je 0x2372c
sarq $0x3, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %ebp, %ebp
movq (%rax,%rbp,8), %rdx
testq %rdx, %rdx
je 0x23724
movl 0x434(%rdx), %esi
orl $0x2, %esi
cmpl $0x3, %esi
jne 0x2395d
incq %rbp
cmpq %rbp, %rcx
jne 0x23709
leaq 0x1d0(%r14), %r15
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r13
movq $0x0, (%rsp)
movq %rax, %rdi
movl $0x2, %esi
movl $0x2, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x284bc
movq %r13, 0x10(%rsp)
movl 0x42c(%r12), %eax
movl %eax, 0x42c(%r13)
movl %ebx, 0x43c(%r13)
cmpb $0x1, 0x1cc(%r14)
jne 0x23792
movl $0x0, 0x14(%r13)
cmpl $0xb, 0x200(%r14)
jne 0x237b9
movq 0x228(%r14), %rsi
cmpq 0x230(%r14), %rsi
je 0x237e6
movq %r13, (%rsi)
addq $0x8, 0x228(%r14)
jmp 0x237f7
movq 0x1d0(%r14), %rsi
cmpb $0x1, 0x240(%r14)
jne 0x237d7
movq (%rsi), %rax
movl 0x430(%rax), %eax
leaq (%rsi,%rax,8), %rsi
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
callq 0x1a82e
jmp 0x237f7
leaq 0x220(%r14), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1ac5a
leaq 0x18c8a(%rip), %rax     # 0x3c488
cmpb $0x1, (%rax)
jne 0x23c4f
leaq 0x18c7b(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x23c4f
movq 0x187b2(%rip), %rax     # 0x3bfd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x12, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r15
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x2385c
movq %r15, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x20, 0xe0(%r15)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x1875d(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0xf, 0x10(%r15,%rcx)
movq -0x18(%rax), %rax
leaq (%r15,%rax), %r12
cmpb $0x0, 0xe1(%r15,%rax)
jne 0x238bc
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x28bbe
movq %rax, %r15
leaq 0xfe14(%rip), %rax      # 0x336f0
leaq 0xfe14(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r15, %rdi
callq 0x4340
leaq 0xfff7(%rip), %rsi      # 0x338fa
movl $0x27, %edx
movq %r15, %rdi
callq 0x4340
movq %r15, %rdi
movl %ebx, %esi
callq 0x4500
movq %rax, %rbx
leaq 0x10b3d(%rip), %rsi     # 0x34461
movl $0x1, %edx
movq %rax, %rdi
callq 0x4340
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
jmp 0x23c4f
leaq 0x1d0(%r14), %r15
movl 0x430(%rdx), %eax
cmpl 0x208(%r14), %eax
ja 0x23733
cmpl $0x0, 0x43c(%rdx)
leaq 0x18b03(%rip), %rax     # 0x3c488
movb (%rax), %al
je 0x23ada
testb %al, %al
je 0x23c42
leaq 0x18aed(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x23c42
movq 0x18624(%rip), %rax     # 0x3bfd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x12, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r13
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x239ea
movq %r13, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r13)
movb $0x1, 0xe1(%r13)
movb $0x20, 0xe0(%r13)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x185cf(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0xf, 0x10(%r13,%rcx)
movq -0x18(%rax), %rax
leaq (%rax,%r13), %rcx
cmpb $0x0, 0xe1(%r13,%rax)
jne 0x23a52
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movl $0x20, %esi
callq 0x42d0
movq 0x8(%rsp), %rcx
movb %al, 0xe0(%rcx)
movb $0x1, 0xe1(%rcx)
movb $0x20, 0xe0(%rcx)
movq 0x1d0(%r14), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x28bbe
movq %rax, %r13
leaq 0xfc7a(%rip), %rax      # 0x336f0
leaq 0xfc7a(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r13, %rdi
callq 0x4340
leaq 0xfe7f(%rip), %rsi      # 0x3391c
movl $0x5, %edx
movq %r13, %rdi
callq 0x4340
movq 0x1d0(%r14), %rax
movq (%rax,%rbp,8), %rax
movl 0x43c(%rax), %esi
movq %r13, %rdi
callq 0x41e0
movq %rax, %r13
leaq 0xfe29(%rip), %rsi      # 0x338f6
movl $0x3, %edx
movq %rax, %rdi
jmp 0x23bf2
testb %al, %al
je 0x23c42
leaq 0x189a0(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x23c42
movq 0x184d7(%rip), %rax     # 0x3bfd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x12, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r13
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x23b37
movq %r13, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r13)
movb $0x1, 0xe1(%r13)
movb $0x20, 0xe0(%r13)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x18482(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0xf, 0x10(%r13,%rcx)
movq -0x18(%rax), %rax
leaq (%rax,%r13), %rcx
cmpb $0x0, 0xe1(%r13,%rax)
jne 0x23b9f
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movl $0x20, %esi
callq 0x42d0
movq 0x8(%rsp), %rcx
movb %al, 0xe0(%rcx)
movb $0x1, 0xe1(%rcx)
movb $0x20, 0xe0(%rcx)
movq 0x1d0(%r14), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x28bbe
movq %rax, %r13
leaq 0xfb2d(%rip), %rax      # 0x336f0
leaq 0xfb2d(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r13, %rdi
callq 0x4340
leaq 0xfd32(%rip), %rsi      # 0x3391c
movl $0x5, %edx
movq %r13, %rdi
callq 0x4340
movq %r13, %rdi
movl %ebx, %esi
callq 0x4500
movq %rax, %r14
leaq 0xfcc7(%rip), %rsi      # 0x338d2
movl $0x23, %edx
movq %rax, %rdi
callq 0x4340
movq %r14, %rdi
movq %r12, %rsi
callq 0x28bbe
movq %rax, %r14
leaq 0x10834(%rip), %rsi     # 0x34461
movl $0x1, %edx
movq %rax, %rdi
callq 0x4340
movq %r14, %rdi
callq 0x4170
movq (%r15), %rax
movq (%rax,%rbp,8), %rax
addl %ebx, 0x43c(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r13, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4540