// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movl 0x430(%rsi), %ebx
leaq 0x1d0(%rdi), %r14
movq 0x1d0(%rdi), %rax
movq 0x1d8(%rdi), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorl %r13d, %r13d
cmpq %r13, %rcx
je 0x1a6f7
movq (%rax,%r13,8), %rdx
testq %rdx, %rdx
je 0x1a6df
movl 0x434(%rdx), %esi
orl $0x2, %esi
cmpl $0x3, %esi
jne 0x1a6e4
incq %r13
jmp 0x1a6c3
movl 0x430(%rdx), %eax
cmpl 0x208(%r15), %eax
jbe 0x1a85c
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %r13
andq $0x0, (%rsp)
pushq $0x2
popq %rdx
pushq $0x1
popq %r9
movq %rax, %rdi
movl %edx, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1dd04
movq %r13, 0x10(%rsp)
movl 0x42c(%r12), %eax
movl %eax, 0x42c(%r13)
movl %ebx, 0x43c(%r13)
cmpb $0x1, 0x1cc(%r15)
jne 0x1a749
andl $0x0, 0x14(%r13)
cmpl $0xb, 0x200(%r15)
jne 0x1a766
leaq 0x220(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x13442
jmp 0x1a791
movq 0x1d0(%r15), %rsi
cmpb $0x1, 0x240(%r15)
jne 0x1a784
movq (%rsi), %rax
movl 0x430(%rax), %eax
leaq (%rsi,%rax,8), %rsi
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x132ba
leaq 0x16cf0(%rip), %rax     # 0x31488
cmpb $0x1, (%rax)
jne 0x1aa25
leaq 0x16ce1(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1aa25
movq 0x16818(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbp
movl %ebp, %esi
callq 0x4440
leaq 0x20(%r15), %rsi
movq %rax, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4440
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x1e2c8
leaq 0xbf5c(%rip), %rcx      # 0x26768
leaq 0xbf5c(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%r15)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xc138(%rip), %rsi      # 0x26966
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl %ebx, %esi
callq 0x44f0
leaq 0xcc8a(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
jmp 0x1aa25
cmpl $0x0, 0x43c(%rdx)
leaq 0x16c1e(%rip), %rax     # 0x31488
movb (%rax), %al
je 0x1a93a
testb %al, %al
je 0x1aa18
leaq 0x16c08(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1aa18
movq 0x1673f(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbp
movl %ebp, %esi
callq 0x4440
leaq 0x20(%r15), %rsi
movq %rax, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4440
movq 0x1d0(%r15), %rcx
movq (%rcx,%r13,8), %rsi
movq %rax, %rdi
callq 0x1e2c8
leaq 0xbe7d(%rip), %rcx      # 0x26768
leaq 0xbe7d(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%r15)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xc07b(%rip), %rsi      # 0x26988
movq %rax, %rdi
callq 0x4300
movq 0x1d0(%r15), %rcx
movq (%rcx,%r13,8), %rcx
movl 0x43c(%rcx), %esi
movq %rax, %rdi
callq 0x4210
leaq 0xc02d(%rip), %rsi      # 0x26962
jmp 0x1a9d5
testb %al, %al
je 0x1aa18
leaq 0x16b40(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1aa18
movq 0x16677(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbp
movl %ebp, %esi
callq 0x4440
leaq 0x20(%r15), %rsi
movq %rax, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4440
movq 0x1d0(%r15), %rcx
movq (%rcx,%r13,8), %rsi
movq %rax, %rdi
callq 0x1e2c8
leaq 0xbdb5(%rip), %rcx      # 0x26768
leaq 0xbdb5(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%r15)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xbfb3(%rip), %rsi      # 0x26988
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl %ebx, %esi
callq 0x44f0
leaq 0xbf50(%rip), %rsi      # 0x2693e
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movq %r12, %rsi
callq 0x1e2c8
leaq 0xcac9(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
movq (%r14), %rax
movq (%rax,%r13,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 0x4320
movq %rbx, %rdi
callq 0x4520