// File: /workspace/github_cpp_makefile_star2_O3/rthoelen[P]NXCore_Icom/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, j;
	int tg;
	int tac_flag;
	in_addr_t tmp_addr;
	int o_bufsize;

	if (tg_lookup(GID, rpt_id) == -1)
	{
		if(debug)
			std::cout << "Blocking TG: " << GID << " sent on Repeater  ->" << r_list[rpt_id] << "<-  , unauthorized TG: " << GID << std::endl;
		return;
	}

	for(i = 0; i < repeater_count; i++)
	{

		if (rpt_id == i)
			continue;

		tg = tg_lookup(GID, i);
		tac_flag = tac_lookup(GID, i);

	 	if(tg != -1)
		{


			if(repeater[i].disable)
				continue;


			// Check if this is an OTAA packet

                	if ((buf[38] == 0x1c) && (buf[39] == 0x21) && (buf[40] == 0xa0) && (repeater[i].tx_otaa == 0)) {
				continue;
			}

                        // 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;
                        }

			// If there is RX activity on this repeater, don't send to the repeater

			// if(repeater[i].rx_activity == 1)
			//	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)
                                        	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)
					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)
                                	std::cout << " Repeater  ->" << r_list[i] << "<-  not geting " << GID << "due to active TX on " << repeater[i].busy_tg << std::endl;
                                continue;
                        }


			// Drop in the ran code
			if ((buf[38] == 0x1c) && (buf[40] == 0x81))
			{
				buf[41] = (char)repeater[i].tx_ran;
			}
			
			if (strt_packet)
			{
				tx_busy_sem = 1;
				repeater[i].tx_busy = 1;
				tx_busy_sem = 0;
				repeater[i].busy_tg = GID;
			}

			repeater[i].time_since_tx = 0;

			if(debug)
				std::cout << "Sending size " << recvlen << " packet to Repeater  ->" << r_list[i] << "<-  with TG: " << GID << std::endl;

			sendto(socket_00, buf, recvlen, 0, (struct sockaddr *)&repeater[i].rpt_addr_00,
		 		sizeof(repeater[i].rpt_addr_00));

			while(1)
			{
				#ifdef __FreeBSD__
				ioctl(socket_00,FIONWRITE, &o_bufsize);
				#else
				ioctl(socket_00,SIOCOUTQ, &o_bufsize);
				#endif
				if(o_bufsize == 0)
					break;
				usleep(250);
			}

		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, -0x48(%rbp)
movl %edx, %ebx
movl %esi, -0x44(%rbp)
movq %rdi, -0x40(%rbp)
movq 0xfc8c(%rip), %rax      # 0x15358
movslq %ecx, %r15
imulq $0x70, %r15, %rdx
movq 0x40(%rax,%rdx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x5ba5
addq $0x4, %rax
cmpl %ebx, %edx
jne 0x56d8
cmpl $0x0, 0xfc87(%rip)      # 0x15378
jle 0x5c63
movslq -0x44(%rbp), %rax
movq %rax, -0x50(%rbp)
movl %ecx, %r14d
xorl %r12d, %r12d
cmpq %r14, %r12
je 0x574d
movq 0xfc47(%rip), %r8       # 0x15358
imulq $0x70, %r12, %r13
movq 0x40(%r8,%r13), %rax
movq %rax, %rdx
movl (%rdx), %ecx
testl %ecx, %ecx
je 0x572b
addq $0x4, %rdx
cmpl %ebx, %ecx
jne 0x571d
movq 0x48(%r8,%r13), %rdx
movl (%rdx), %r15d
testl %r15d, %r15d
je 0x5741
addq $0x4, %rdx
cmpl %ebx, %r15d
jne 0x5730
testl %ecx, %ecx
je 0x574d
cmpl $0x0, 0x60(%r8,%r13)
je 0x5761
incq %r12
movslq 0xfc21(%rip), %rax      # 0x15378
cmpq %rax, %r12
jl 0x5705
jmp 0x5c63
leaq (%r8,%r13), %rcx
movq -0x40(%rbp), %rdx
cmpb $0x1c, 0x26(%rdx)
jne 0x5789
movq -0x40(%rbp), %rdx
cmpb $0x21, 0x27(%rdx)
jne 0x5789
movq -0x40(%rbp), %rdx
cmpb $-0x60, 0x28(%rdx)
jne 0x5789
cmpl $0x0, 0x5c(%rcx)
je 0x574d
testl %r15d, %r15d
je 0x57a0
movl 0x18(%rcx), %edx
cmpl 0x20(%rcx), %edx
je 0x574d
cmpl %ebx, 0x38(%rcx)
jne 0x574d
jmp 0x5843
cmpl %ebx, 0x3c(%rcx)
je 0x5843
movl 0x18(%rcx), %edx
cmpl 0x20(%rcx), %edx
jge 0x5843
cmpl $0x0, 0xfbc0(%rip)      # 0x1537c
je 0x574d
movl $0xd, %edx
movq 0xf806(%rip), %r15      # 0x14fd0
movq %r15, %rdi
leaq 0x99f3(%rip), %rsi      # 0xf1c7
callq 0x4330
movq %r15, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r15
movl $0x15, %edx
movq %rax, %rdi
leaq 0x99e0(%rip), %rsi      # 0xf1d5
callq 0x4330
movq 0xfb5f(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r15, %rdi
callq 0x4330
movq %rax, %r15
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x99d8(%rip), %rsi      # 0xf203
callq 0x4330
movq 0xfb21(%rip), %rax      # 0x15358
addq %r13, %rax
addq $0x3c, %rax
jmp 0x5a28
movq %rax, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl (%rsi), %edi
testl %edi, %edi
je 0x585d
incl %edx
addq $0x4, %rsi
cmpl %ebx, %edi
jne 0x584b
jmp 0x5862
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl 0x30(%r8,%r13), %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl (%rax), %r9d
testl %r9d, %r9d
je 0x5881
incl %esi
addq $0x4, %rax
cmpl %edi, %r9d
jne 0x586c
jmp 0x5886
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpl $0x1, -0x48(%rbp)
jne 0x5979
cmpl %esi, %edx
jge 0x5979
testl %r15d, %r15d
jne 0x5979
cmpl $0x1, 0x2c(%rcx)
jne 0x5979
movl %ebx, 0x30(%rcx)
cmpl $0x0, 0xfac7(%rip)      # 0x1537c
je 0x5979
movl $0xf, %edx
movq 0xf709(%rip), %rdi      # 0x14fd0
leaq 0x9952(%rip), %rsi      # 0xf220
callq 0x4330
movq 0xfa7e(%rip), %rax      # 0x15358
movl 0x30(%rax,%r13), %esi
movq 0xf6ea(%rip), %rdi      # 0x14fd0
callq 0x41d0
movq %rax, -0x38(%rbp)
movl $0x9, %edx
movq %rax, %rdi
leaq 0x9981(%rip), %rsi      # 0xf27f
callq 0x4330
movq -0x38(%rbp), %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, -0x38(%rbp)
movl $0x10, %edx
movq %rax, %rdi
leaq 0x98b9(%rip), %rsi      # 0xf1da
callq 0x4330
movq 0xfa33(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq -0x38(%rbp), %rdi
callq 0x4330
movq %rax, -0x38(%rbp)
movq (%rax), %rcx
movq -0x18(%rcx), %rdi
addq %rax, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq -0x38(%rbp), %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41e0
movq 0xf9df(%rip), %r8       # 0x15358
cmpl $0x1, 0x2c(%r8,%r13)
jne 0x5a61
testl %r15d, %r15d
jne 0x5a61
leaq (%r8,%r13), %rax
cmpl %ebx, 0x30(%rax)
je 0x5a61
cmpl $0x0, 0xf9da(%rip)      # 0x1537c
je 0x574d
movl $0xd, %edx
movq 0xf61c(%rip), %r15      # 0x14fd0
movq %r15, %rdi
leaq 0x981f(%rip), %rsi      # 0xf1dd
callq 0x4330
movq 0xf996(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r15, %rdi
callq 0x4330
movq %rax, %r15
movl $0xf, %edx
movq %rax, %rdi
leaq 0x983c(%rip), %rsi      # 0xf230
callq 0x4330
movq %r15, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r15
movl $0x14, %edx
movq %rax, %rdi
leaq 0x982b(%rip), %rsi      # 0xf240
callq 0x4330
movq 0xf937(%rip), %rax      # 0x15358
addq %r13, %rax
addq $0x30, %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x41d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41e0
jmp 0x574d
movq -0x40(%rbp), %rsi
cmpb $0x1c, 0x26(%rsi)
jne 0x5a79
cmpb $-0x7f, 0x28(%rsi)
jne 0x5a79
movb 0x34(%r8,%r13), %al
movb %al, 0x29(%rsi)
cmpl $0x0, -0x48(%rbp)
je 0x5a97
movl $0x1, 0x2c(%r8,%r13)
movl $0x0, 0xf8f2(%rip)      # 0x15384
movl %ebx, 0x30(%r8,%r13)
movl $0x0, 0x1c(%r8,%r13)
cmpl $0x0, 0xf8d5(%rip)      # 0x1537c
je 0x5b5f
movl $0xd, %edx
movq 0xf517(%rip), %r15      # 0x14fd0
movq %r15, %rdi
leaq 0x9792(%rip), %rsi      # 0xf255
callq 0x4330
movq %r15, %rdi
movl -0x44(%rbp), %esi
callq 0x44c0
movq %rax, %r15
movl $0x17, %edx
movq %rax, %rdi
leaq 0x977e(%rip), %rsi      # 0xf263
callq 0x4330
movq 0xf86f(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r15, %rdi
callq 0x4330
movq %rax, %r15
movl $0xd, %edx
movq %rax, %rdi
leaq 0x9760(%rip), %rsi      # 0xf27b
callq 0x4330
movq %r15, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41e0
movq -0x40(%rbp), %rsi
movq 0xf7f9(%rip), %r8       # 0x15358
movl 0xf81b(%rip), %edi      # 0x15380
addq %r13, %r8
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
movl 0xf801(%rip), %edi      # 0x15380
movl $0x5411, %esi           # imm = 0x5411
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x43e0
cmpl $0x0, -0x2c(%rbp)
je 0x574d
movl $0xfa, %edi
callq 0x4400
jmp 0x5b79
cmpl $0x0, 0xf7d0(%rip)      # 0x1537c
je 0x5c63
movq 0xf417(%rip), %r14      # 0x14fd0
leaq 0x9607(%rip), %rsi      # 0xf1c7
movl $0xd, %edx
movq %r14, %rdi
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r14
leaq 0x95f4(%rip), %rsi      # 0xf1d5
movl $0x15, %edx
movq %rax, %rdi
callq 0x4330
movq 0xf76b(%rip), %rax      # 0x15360
shlq $0x5, %r15
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r14, %rdi
callq 0x4330
movq %rax, %r14
leaq 0x95d7(%rip), %rsi      # 0xf1eb
movl $0x17, %edx
movq %rax, %rdi
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41e0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq