// File: estwings57[P]CasHMC/sources/LinkMaster.cpp
// Function: CasHMC::FindAvailableLink(int&, std::vector<CasHMC::LinkMaster*, std::allocator<CasHMC::LinkMaster*>>&)
int FindAvailableLink(int &link, vector<LinkMaster *> &LM)
{
	switch(LINK_PRIORITY) {
		case ROUND_ROBIN:
			if(++link >= NUM_LINKS)
				link=0;
			return link;
			break;
		case BUFFER_AWARE:
			unsigned minBufferSize = MAX_LINK_BUF;
			unsigned minBufferLink = 0;
			for(int l=0; l<NUM_LINKS; l++) {
				if(LM[l]->currentState != ACTIVE && LM[l]->currentState != LINK_RETRY) {
					continue;
				}
				else {
					int bufSizeTemp = LM[l]->Buffers.size();
					for(int i=0; i<LM[l]->linkRxTx.size(); i++) {
						if(LM[l]->linkRxTx[i] != NULL) {
							bufSizeTemp += LM[l]->linkRxTx[i]->LNG;
						}
					}
					if(bufSizeTemp < minBufferSize) {
						minBufferSize = bufSizeTemp;
						minBufferLink = l;
					}
				}
			}
			return minBufferLink;
			break;
	}
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq 0x262fd(%rip), %rax     # 0x66584
movl (%rax), %eax
movl %eax, -0x30(%rbp)
testl %eax, %eax
je 0x4029f
jmp 0x40292
movl -0x30(%rbp), %eax
subl $0x1, %eax
je 0x402cd
jmp 0x40419
movq -0x10(%rbp), %rcx
movl (%rcx), %eax
addl $0x1, %eax
movl %eax, (%rcx)
leaq 0x26297(%rip), %rcx     # 0x66548
cmpl (%rcx), %eax
jl 0x402bf
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x40420
leaq 0x26284(%rip), %rax     # 0x66558
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
leaq 0x26257(%rip), %rcx     # 0x66548
cmpl (%rcx), %eax
jge 0x40411
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rax
cmpl $0x0, 0x200(%rax)
je 0x40330
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rax
cmpl $0xb, 0x200(%rax)
je 0x40330
jmp 0x40403
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x22d60
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x22d60
movq %rax, %rcx
movq -0x38(%rbp), %rax
cmpq %rcx, %rax
jae 0x403eb
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movslq -0x2c(%rbp), %rsi
callq 0x29940
cmpq $0x0, (%rax)
je 0x403db
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0x17770
movq (%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movslq -0x2c(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
movl 0x430(%rax), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x403dd
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x40356
movl -0x28(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x403ff
movl -0x28(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x40401
jmp 0x40403
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x402e7
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x40420
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)