// File: /workspace/github_cpp_makefile_star2_O3/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
leaq 0x1d0(%rdi), %r15
movq 0x1d0(%rdi), %rax
movq 0x1d8(%rdi), %rcx
subq %rax, %rcx
je 0x22fe9
sarq $0x3, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %ebp, %ebp
movq (%rax,%rbp,8), %rdx
testq %rdx, %rdx
je 0x22fe1
movl 0x434(%rdx), %esi
orl $0x2, %esi
cmpl $0x3, %esi
jne 0x23204
incq %rbp
cmpq %rbp, %rcx
jne 0x22fc6
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 0x27c44
movq %r13, 0x10(%rsp)
movl 0x42c(%r12), %eax
movl %eax, 0x42c(%r13)
movl %ebx, 0x43c(%r13)
cmpb $0x1, 0x1cc(%r14)
jne 0x23048
movl $0x0, 0x14(%r13)
cmpl $0xb, 0x200(%r14)
jne 0x2306f
movq 0x228(%r14), %rsi
cmpq 0x230(%r14), %rsi
je 0x2309c
movq %r13, (%rsi)
addq $0x8, 0x228(%r14)
jmp 0x230ad
movq 0x1d0(%r14), %rsi
cmpb $0x1, 0x240(%r14)
jne 0x2308d
movq (%rsi), %rax
movl 0x430(%rax), %eax
leaq (%rsi,%rax,8), %rsi
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
callq 0x1a0a8
jmp 0x230ad
leaq 0x220(%r14), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1a462
leaq 0x183d4(%rip), %rax     # 0x3b488
cmpb $0x1, (%rax)
jne 0x234d5
leaq 0x183c5(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x234d5
movq 0x17efc(%rip), %rax     # 0x3afd0
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 0x2310b
movq %r15, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r15)
movb $0x20, 0xe0(%r15)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x17eae(%rip), %rdi     # 0x3afd0
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 0x23163
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x28270
movq %rax, %r15
leaq 0xf5f5(%rip), %rax      # 0x32778
leaq 0xf5f5(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r15, %rdi
callq 0x4340
leaq 0xf7cc(%rip), %rsi      # 0x32976
movl $0x27, %edx
movq %r15, %rdi
callq 0x4340
movq %r15, %rdi
movl %ebx, %esi
callq 0x4500
movq %rax, %rbx
leaq 0x10316(%rip), %rsi     # 0x334e1
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 0x234d5
movl 0x430(%rdx), %eax
cmpl 0x208(%r14), %eax
ja 0x22fe9
cmpl $0x0, 0x43c(%rdx)
leaq 0x18263(%rip), %rax     # 0x3b488
movb (%rax), %al
je 0x2336d
testb %al, %al
je 0x234c8
leaq 0x1824d(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x234c8
movq 0x17d84(%rip), %rax     # 0x3afd0
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 0x23283
movq %r13, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r13)
movb $0x20, 0xe0(%r13)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x17d36(%rip), %rdi     # 0x3afd0
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 0x232e5
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movl $0x20, %esi
callq 0x42d0
movq 0x8(%rsp), %rcx
movb $0x1, 0xe1(%rcx)
movb $0x20, 0xe0(%rcx)
movq 0x1d0(%r14), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x28270
movq %rax, %r13
leaq 0xf46f(%rip), %rax      # 0x32778
leaq 0xf46f(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r13, %rdi
callq 0x4340
leaq 0xf668(%rip), %rsi      # 0x32998
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 0xf612(%rip), %rsi      # 0x32972
movl $0x3, %edx
movq %rax, %rdi
jmp 0x23478
testb %al, %al
je 0x234c8
leaq 0x1810d(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x234c8
movq 0x17c44(%rip), %rax     # 0x3afd0
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 0x233c3
movq %r13, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r13)
movb $0x20, 0xe0(%r13)
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq 0x17bf6(%rip), %rdi     # 0x3afd0
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 0x23425
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rdi
movl $0x20, %esi
callq 0x42d0
movq 0x8(%rsp), %rcx
movb $0x1, 0xe1(%rcx)
movb $0x20, 0xe0(%rcx)
movq 0x1d0(%r14), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x28270
movq %rax, %r13
leaq 0xf32f(%rip), %rax      # 0x32778
leaq 0xf32f(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%r14)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r13, %rdi
callq 0x4340
leaq 0xf528(%rip), %rsi      # 0x32998
movl $0x5, %edx
movq %r13, %rdi
callq 0x4340
movq %r13, %rdi
movl %ebx, %esi
callq 0x4500
movq %rax, %r14
leaq 0xf4bd(%rip), %rsi      # 0x3294e
movl $0x23, %edx
movq %rax, %rdi
callq 0x4340
movq %r14, %rdi
movq %r12, %rsi
callq 0x28270
movq %rax, %r14
leaq 0x1002e(%rip), %rsi     # 0x334e1
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