// File: /workspace/github_cpp_makefile_star2_O1/rthoelen[P]NXCore_Kenwood/main.cc
// Function: snd_packet(unsigned char*, int, int, int, int)
void snd_packet(unsigned char buf[], int recvlen, int GID, int rpt_id, int strt_packet)
{
	int i;
	int tg;
	int tac_flag;
	int o_bufsize;


	// This blocks talkgroups received on a repeater that don't match
	// the talkgroup list

	if (tg_lookup(GID, rpt_id) == -1)
	{
		if(debug)
		{
			ptime();
			std::cout << "Repeater  ->" << r_list[rpt_id] 
			<< "<-  blocked, unauthorized talkgroup: " 
			<< GID << std::endl;
		}

		return;
	}


	// Sending selection logic

	for(i = 0; i < repeater_count; i++)
	{
	
		repeater[i].snd_queue = 0;

		// Don't reflect our own packets back

		if (rpt_id == i)
			continue;

		if (repeater[i].disable)
			continue;

		// Block OTAA if needed

		if((buf[20] == 0x0a) && (buf[21] == 0x05) && (buf[22] == 0x0a) &&
			 ((buf[23] == 0x01) || (buf[23] == 0x80)) &&
				(repeater[i].tx_otaa == 0))
		{
			continue;
		}

		// Get the flag from the Tactical group list
		tac_flag = tac_lookup(GID, i);

		// Is the talkgroup being sent in this repeater's list? If not, stop here

		tg = tg_lookup(GID, i);
		if (tg != -1)

		{		
		

			// Process TAC groups first
			// The only way to let the TAC group(s) through is if the RX active group matches
	

			if(tac_flag != -1)
			{
				if((repeater[i].time_since_rx == repeater[i].hold_time) || (repeater[i].active_tg != GID))
					continue;
			}


			// First, if this particular repeater just had RX activity, if the packet 
			// doesn't match the last talkgroup, drop it.  This should solve most contention
			// issues

			if((repeater[i].last_tg != GID) && (tac_flag == -1))
			{
				if(repeater[i].time_since_rx < repeater[i].hold_time)
				{
					if(debug)
					{
						ptime();
						std::cout << "Blocking TG: " << GID 
							<< " sent on Repeater  ->" << r_list[i]
							<< "<-  due to recent RX on TG: " 
							<< repeater[i].last_tg << std::endl;
					}

					continue;
				}
			}


			// Next, we need to determine if we need to preempt a talkgroup
			// Talkgroups that are on the left in the NXCore.ini list get higher priority

			if((tg_lookup(GID, i) < tg_lookup(repeater[i].busy_tg, i)) && (strt_packet==1)&&(repeater[i].tx_busy==1) &&
				(tac_flag == -1))
			{
				repeater[i].busy_tg = GID;

				if(debug)
				{
					ptime();
					std::cout << "Overriding TG: " << repeater[i].busy_tg 
						<< " with  TG: " << GID 
						<< " on Repeater  ->" << r_list[i] << std::endl;
				}
			}

			// Next, if repeater is considered busy, only send the talkgroup it has been assigned

			if((repeater[i].tx_busy == 1) && (repeater[i].busy_tg!=GID) && (tac_flag == -1))
			{
				if(debug)
				{
					ptime();
					std::cout << " Repeater  ->" << r_list[i] << "<-  not geting " 
						<< GID << "due to active TX on " 
						<< repeater[i].busy_tg << std::endl;
				}

				continue;	
			}


			if(strt_packet == 1)
			{
				repeater[i].keydown = 0;
				rpton_64001(i);
				tx_busy_sem=1;
				repeater[i].time_since_tx = 0;
				repeater[i].tx_busy = 1;
				tx_busy_sem=0;
				repeater[i].busy_tg = GID;
			}
			else
			{
				if((buf[3] % 3))
					rpton_64001(i);
				repeater[i].keydown = 0;
			}	

			// Need to rewrite IP address for len 47 and 59 packets it is 8,9,10,11
			buf[8] = (char)(tempaddr >> 24) & 0xff;
			buf[9] = (char)(tempaddr >> 16) & 0xff;
			buf[10] = (char)(tempaddr >> 8) & 0xff;
			buf[11] = (char)tempaddr & 0xff;

			if((recvlen == 47)&&(strt_packet == 1))
			{
				buf[24] = repeater[i].tx_ran;
			}

			repeater[i].tx_uid = repeater[rpt_id].uid;

			if(debug)
			{
				ptime();
				std::cout << "Sending datagram to repeater  ->" << r_list[i] << std::endl;
			}


			repeater[i].snd_queue = 1;

			repeater[i].time_since_tx = 0;

		}
				
	}

	for(i = 0; i < repeater_count; i++)
	{
		if(repeater[i].snd_queue==1)
		{

			if(strt_packet == 1)
				rpton_64001(i);
			while(tx_sem==1)
				usleep(1);
			tx_sem=1;

			if(sendto(socket_00, buf, recvlen, 0, (struct sockaddr *)&repeater[i].rpt_addr_00,
		 		sizeof(repeater[i].rpt_addr_00)) < 0)
			{
				ptime();
				std::cout << "sendto error..." << std::endl;
			}
			tx_sem=0;
			
			while(1)
			{

				#ifdef __FreeBSD__
				ioctl(socket_00,FIONWRITE,&o_bufsize);
				#else
				ioctl(socket_00,SIOCOUTQ,&o_bufsize);
				#endif
				if(o_bufsize == 0)
					break;
				usleep(500);
			}
	
			repeater[i].snd_queue = 0;

			if(repeater[rpt_id].rx_activity == 0)
			{		
				repeater[i].keydown=3;
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, -0x34(%rbp)
movl %edx, %r15d
movq %rdi, -0x40(%rbp)
movq 0xfa50(%rip), %rax      # 0x153d0
movslq %ecx, %r14
leaq (%r14,%r14,8), %rdx
shlq $0x4, %rdx
movq %rdx, -0x58(%rbp)
movq 0x58(%rax,%rdx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x5ec3
addq $0x4, %rax
cmpl %r15d, %edx
jne 0x5994
testl %edx, %edx
je 0x5ec3
cmpl $0x0, 0xfa22(%rip)      # 0x153d8
jle 0x5f66
movl -0x34(%rbp), %eax
xorl $0x1, %eax
movl %esi, %edx
xorl $0x2f, %edx
orl %eax, %edx
sete -0x29(%rbp)
movl %ecx, %r12d
xorl %r13d, %r13d
movq 0xf9f6(%rip), %rax      # 0x153d0
leaq (,%r13,8), %rbx
addq %r13, %rbx
shlq $0x4, %rbx
movl $0x0, 0x78(%rax,%rbx)
cmpq %r12, %r13
je 0x59fd
cmpl $0x0, 0x7c(%rax,%rbx)
je 0x5a11
incq %r13
movslq 0xf9d1(%rip), %rax      # 0x153d8
cmpq %rax, %r13
jl 0x59d3
jmp 0x5f66
movq -0x40(%rbp), %rcx
cmpb $0xa, 0x14(%rcx)
jne 0x5a4b
movq -0x40(%rbp), %rcx
cmpb $0x5, 0x15(%rcx)
jne 0x5a4b
movq -0x40(%rbp), %rcx
cmpb $0xa, 0x16(%rcx)
jne 0x5a4b
movq -0x40(%rbp), %rcx
movzbl 0x17(%rcx), %ecx
cmpl $0x80, %ecx
je 0x5a44
cmpl $0x1, %ecx
jne 0x5a4b
cmpl $0x0, 0x70(%rax,%rbx)
je 0x59fd
movq 0x60(%rax,%rbx), %rcx
movl (%rcx), %r14d
testl %r14d, %r14d
je 0x5a61
addq $0x4, %rcx
cmpl %r15d, %r14d
jne 0x5a50
movq 0x58(%rax,%rbx), %rcx
movq %rcx, %rdx
movl (%rdx), %edi
testl %edi, %edi
je 0x59fd
addq $0x4, %rdx
cmpl %r15d, %edi
jne 0x5a69
testl %edi, %edi
je 0x59fd
testl %r14d, %r14d
je 0x5a9d
movl 0x28(%rax,%rbx), %edx
cmpl 0x30(%rax,%rbx), %edx
je 0x59fd
leaq (%rax,%rbx), %rdx
cmpl %r15d, 0x4c(%rdx)
jne 0x59fd
leaq (%rax,%rbx), %rdx
testl %r14d, %r14d
movl %esi, -0x30(%rbp)
jne 0x5b59
cmpl %r15d, 0x50(%rax,%rbx)
je 0x5b59
movl 0x28(%rdx), %edi
cmpl 0x30(%rdx), %edi
jge 0x5b59
cmpl $0x0, 0xf911(%rip)      # 0x153dc
je 0x59fd
callq 0x479c
movl $0xd, %edx
movq 0xf4ee(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x97e4(%rip), %rsi      # 0xf2d0
callq 0x4370
movq %r14, %rdi
movl %r15d, %esi
callq 0x4520
movq %rax, %r14
movl $0x15, %edx
movq %rax, %rdi
leaq 0x97d0(%rip), %rsi      # 0xf2de
callq 0x4370
movq 0xf8c6(%rip), %rax      # 0x153e0
movq %r13, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4370
movq %rax, %r14
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x97b0(%rip), %rsi      # 0xf2f4
callq 0x4370
movq 0xf880(%rip), %rax      # 0x153d0
movl 0x50(%rax,%rbx), %esi
jmp 0x5d50
movq %rcx, %rdi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl (%rdi), %r8d
testl %r8d, %r8d
je 0x5b78
incl %r9d
addq $0x4, %rdi
cmpl %r15d, %r8d
jne 0x5b62
jmp 0x5b7e
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl 0x40(%rax,%rbx), %edi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl (%rcx), %r8d
testl %r8d, %r8d
je 0x5b9c
incl %eax
addq $0x4, %rcx
cmpl %edi, %r8d
jne 0x5b87
jmp 0x5ba1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, -0x34(%rbp)
jne 0x5c97
cmpl %eax, %r9d
jge 0x5c97
testl %r14d, %r14d
jne 0x5c97
cmpl $0x1, 0x3c(%rdx)
jne 0x5c97
movl %r15d, 0x40(%rdx)
cmpl $0x0, 0xf80a(%rip)      # 0x153dc
je 0x5c97
callq 0x479c
movl $0xf, %edx
movq 0xf3e7(%rip), %rdi      # 0x14fd0
leaq 0x9721(%rip), %rsi      # 0xf311
callq 0x4370
movq 0xf7d4(%rip), %rax      # 0x153d0
movl 0x40(%rax,%rbx), %esi
movq 0xf3c9(%rip), %rdi      # 0x14fd0
callq 0x4520
movq %rax, -0x48(%rbp)
movl $0xb, %edx
movq %rax, %rdi
leaq 0x9702(%rip), %rsi      # 0xf321
callq 0x4370
movq -0x48(%rbp), %rdi
movl %r15d, %esi
callq 0x4520
movq %rax, -0x48(%rbp)
movl $0x10, %edx
movq %rax, %rdi
leaq 0x96a0(%rip), %rsi      # 0xf2e3
callq 0x4370
movq 0xf791(%rip), %rax      # 0x153e0
movq %r13, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq -0x48(%rbp), %rdi
callq 0x4370
movq %rax, -0x48(%rbp)
movq (%rax), %rcx
movq -0x18(%rcx), %rdi
addq %rax, %rdi
movl $0xa, %esi
callq 0x42f0
movsbl %al, %esi
movq -0x48(%rbp), %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movl -0x30(%rbp), %esi
movq 0xf732(%rip), %rax      # 0x153d0
cmpl $0x1, 0x3c(%rax,%rbx)
jne 0x5d87
testl %r14d, %r14d
jne 0x5d87
leaq (%rax,%rbx), %rcx
cmpl %r15d, 0x40(%rcx)
je 0x5d87
cmpl $0x0, 0xf715(%rip)      # 0x153dc
je 0x59fd
callq 0x479c
movl $0xd, %edx
movq 0xf2f2(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x95fe(%rip), %rsi      # 0xf2e6
callq 0x4370
movq 0xf6ec(%rip), %rax      # 0x153e0
movq %r13, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4370
movq %rax, %r14
movl $0xf, %edx
movq %rax, %rdi
leaq 0x960f(%rip), %rsi      # 0xf32d
callq 0x4370
movq %r14, %rdi
movl %r15d, %esi
callq 0x4520
movq %rax, %r14
movl $0x14, %edx
movq %rax, %rdi
leaq 0x95fd(%rip), %rsi      # 0xf33d
callq 0x4370
movq 0xf684(%rip), %rax      # 0x153d0
movl 0x40(%rax,%rbx), %esi
movq %r14, %rdi
callq 0x4520
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
jmp 0x5ebb
cmpl $0x1, -0x34(%rbp)
jne 0x5dd2
movq %r13, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,8), %r14
xorl %ecx, %ecx
movl %ecx, 0x74(%rax,%r14)
movl %r13d, %edi
callq 0x611d
movl $0x1, %ecx
movl %ecx, 0xf656(%rip)      # 0x15408
movq 0xf617(%rip), %rax      # 0x153d0
xorl %edx, %edx
movl %edx, 0x2c(%rax,%r14)
movl %ecx, 0x3c(%rax,%r14)
movl %edx, 0xf63d(%rip)      # 0x15408
movl %r15d, 0x40(%rax,%r14)
jmp 0x5df8
movq -0x40(%rbp), %rax
movzbl 0x3(%rax), %eax
imull $-0x55, %eax, %eax
cmpb $0x56, %al
jb 0x5de9
movl %r13d, %edi
callq 0x611d
movq 0xf5e0(%rip), %rax      # 0x153d0
movl $0x0, 0x74(%rax,%rbx)
movl 0xf5fa(%rip), %eax      # 0x153f8
bswapl %eax
movq -0x40(%rbp), %rcx
movl %eax, 0x8(%rcx)
cmpb $0x0, -0x29(%rbp)
je 0x5e1f
movq 0xf5bc(%rip), %rax      # 0x153d0
movb 0x48(%rax,%rbx), %al
movq -0x40(%rbp), %rcx
movb %al, 0x18(%rcx)
movq 0xf5aa(%rip), %rax      # 0x153d0
movq -0x58(%rbp), %rcx
movl 0x68(%rax,%rcx), %ecx
movl %ecx, 0x6c(%rax,%rbx)
cmpl $0x0, 0xf5a3(%rip)      # 0x153dc
je 0x5ea4
callq 0x479c
movl $0x20, %edx
movq 0xf184(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x94fc(%rip), %rsi      # 0xf352
callq 0x4370
movq 0xf57e(%rip), %rax      # 0x153e0
movq %r13, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4370
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42f0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movq 0xf525(%rip), %rax      # 0x153d0
movl $0x1, 0x78(%rax,%rbx)
movl $0x0, 0x2c(%rax,%rbx)
movl -0x30(%rbp), %esi
jmp 0x59fd
cmpl $0x0, 0xf512(%rip)      # 0x153dc
je 0x609c
callq 0x479c
movq 0xf0f4(%rip), %rbx      # 0x14fd0
leaq 0x9404(%rip), %rsi      # 0xf2e7
movl $0xc, %edx
movq %rbx, %rdi
callq 0x4370
movq 0xf4e9(%rip), %rax      # 0x153e0
shlq $0x5, %r14
movq (%rax,%r14), %rsi
movq 0x8(%rax,%r14), %rdx
movq %rbx, %rdi
callq 0x4370
movq %rax, %rbx
leaq 0x9394(%rip), %rsi      # 0xf2aa
movl $0x25, %edx
movq %rax, %rdi
callq 0x4370
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4520
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41f0
cmpl $0x0, 0xf46b(%rip)      # 0x153d8
jle 0x609c
movslq %esi, %r15
movq 0xf053(%rip), %r12      # 0x14fd0
leaq -0x4c(%rbp), %r13
xorl %ebx, %ebx
movq 0xf446(%rip), %rax      # 0x153d0
leaq (%rbx,%rbx,8), %r14
shlq $0x4, %r14
cmpl $0x1, 0x78(%rax,%r14)
jne 0x6089
cmpl $0x1, -0x34(%rbp)
jne 0x5fb7
movl %ebx, %edi
callq 0x611d
jmp 0x5fb7
movl $0x1, %edi
callq 0x4450
cmpl $0x1, 0xf446(%rip)      # 0x15404
je 0x5fad
movl $0x1, 0xf43a(%rip)      # 0x15404
movl 0xf42c(%rip), %edi      # 0x153fc
movq 0xf3f9(%rip), %r8       # 0x153d0
addq %r14, %r8
movq -0x40(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
testq %rax, %rax
jns 0x6034
callq 0x479c
movl $0xf, %edx
movq %r12, %rdi
leaq 0x936c(%rip), %rsi      # 0xf373
callq 0x4370
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x42f0
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movl $0x0, 0xf3c6(%rip)      # 0x15404
movl 0xf3b8(%rip), %edi      # 0x153fc
movl $0x5411, %esi           # imm = 0x5411
movq %r13, %rdx
xorl %eax, %eax
callq 0x4430
cmpl $0x0, -0x4c(%rbp)
je 0x6065
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x4450
jmp 0x603e
movq 0xf364(%rip), %rax      # 0x153d0
movl $0x0, 0x78(%rax,%r14)
movq -0x58(%rbp), %rcx
cmpl $0x0, 0x38(%rax,%rcx)
jne 0x6089
movl $0x3, 0x74(%rax,%r14)
incq %rbx
movslq 0xf345(%rip), %rax      # 0x153d8
cmpq %rax, %rbx
jl 0x5f83
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq