// File: /workspace/github_cpp_makefile_star2_O1/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 %edx, %ebx
movq 0xfcf7(%rip), %rax      # 0x15358
movslq %ecx, %r15
imulq $0x70, %r15, %rdx
movq 0x40(%rax,%rdx), %rax
movl (%rax), %edx
testl %edx, %edx
je 0x5b61
addq $0x4, %rax
cmpl %ebx, %edx
jne 0x566d
testl %edx, %edx
je 0x5b61
movq %rdi, -0x40(%rbp)
movl %esi, -0x44(%rbp)
movl %r8d, -0x48(%rbp)
cmpl $0x0, 0xfcdf(%rip)      # 0x15378
jle 0x5c1f
movslq -0x44(%rbp), %rax
movq %rax, -0x50(%rbp)
movl %ecx, %r13d
xorl %r12d, %r12d
cmpq %r13, %r12
je 0x56f5
movq 0xfc9f(%rip), %rcx      # 0x15358
imulq $0x70, %r12, %r15
movq 0x40(%rcx,%r15), %rax
movq %rax, %rsi
movl (%rsi), %edx
testl %edx, %edx
je 0x56d3
addq $0x4, %rsi
cmpl %ebx, %edx
jne 0x56c5
movq 0x48(%rcx,%r15), %rsi
movl (%rsi), %r14d
testl %r14d, %r14d
je 0x56e9
addq $0x4, %rsi
cmpl %ebx, %r14d
jne 0x56d8
testl %edx, %edx
je 0x56f5
cmpl $0x0, 0x60(%rcx,%r15)
je 0x5709
incq %r12
movslq 0xfc79(%rip), %rax      # 0x15378
cmpq %rax, %r12
jl 0x56ad
jmp 0x5c1f
leaq (%rcx,%r15), %rdx
movq -0x40(%rbp), %rsi
cmpb $0x1c, 0x26(%rsi)
jne 0x5731
movq -0x40(%rbp), %rsi
cmpb $0x21, 0x27(%rsi)
jne 0x5731
movq -0x40(%rbp), %rsi
cmpb $-0x60, 0x28(%rsi)
jne 0x5731
cmpl $0x0, 0x5c(%rdx)
je 0x56f5
testl %r14d, %r14d
je 0x5743
movl 0x18(%rdx), %esi
cmpl 0x20(%rdx), %esi
je 0x56f5
cmpl %ebx, 0x38(%rdx)
jne 0x56f5
testl %r14d, %r14d
jne 0x57ef
cmpl %ebx, 0x3c(%rdx)
je 0x57ef
movl 0x18(%rdx), %esi
cmpl 0x20(%rdx), %esi
jge 0x57ef
cmpl $0x0, 0xfc14(%rip)      # 0x1537c
je 0x56f5
movl $0xd, %edx
movq 0xf85a(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x9a63(%rip), %rsi      # 0xf1e3
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r14
movl $0x15, %edx
movq %rax, %rdi
leaq 0x9a50(%rip), %rsi      # 0xf1f1
callq 0x4330
movq 0xfbb3(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4330
movq %rax, %r14
movl $0x1c, %edx
movq %rax, %rdi
leaq 0x9a48(%rip), %rsi      # 0xf21f
callq 0x4330
movq 0xfb75(%rip), %rax      # 0x15358
addq %r15, %rax
addq $0x3c, %rax
jmp 0x59d7
movq %rax, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl (%rdi), %r8d
testl %r8d, %r8d
je 0x580c
incl %esi
addq $0x4, %rdi
cmpl %ebx, %r8d
jne 0x57f7
jmp 0x5811
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl 0x30(%rcx,%r15), %edi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl (%rax), %r8d
testl %r8d, %r8d
je 0x5830
incl %ecx
addq $0x4, %rax
cmpl %edi, %r8d
jne 0x581b
jmp 0x5835
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x1, -0x48(%rbp)
jne 0x5921
cmpl %ecx, %esi
jge 0x5921
testl %r14d, %r14d
jne 0x5921
cmpl $0x1, 0x2c(%rdx)
jne 0x5921
movl %ebx, 0x30(%rdx)
cmpl $0x0, 0xfb18(%rip)      # 0x1537c
je 0x5921
movl $0xf, %edx
movq 0xf75a(%rip), %rdi      # 0x14fd0
leaq 0x99bf(%rip), %rsi      # 0xf23c
callq 0x4330
movq 0xfacf(%rip), %rax      # 0x15358
movl 0x30(%rax,%r15), %esi
movq 0xf73b(%rip), %rdi      # 0x14fd0
callq 0x41d0
movq %rax, -0x38(%rbp)
movl $0x9, %edx
movq %rax, %rdi
leaq 0x99ee(%rip), %rsi      # 0xf29b
callq 0x4330
movq -0x38(%rbp), %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, -0x38(%rbp)
movl $0x10, %edx
movq %rax, %rdi
leaq 0x9926(%rip), %rsi      # 0xf1f6
callq 0x4330
movq 0xfa84(%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 0xfa30(%rip), %rax      # 0x15358
cmpl $0x1, 0x2c(%rax,%r15)
jne 0x5a10
testl %r14d, %r14d
jne 0x5a10
leaq (%rax,%r15), %rcx
cmpl %ebx, 0x30(%rcx)
je 0x5a10
cmpl $0x0, 0xfa2b(%rip)      # 0x1537c
je 0x56f5
movl $0xd, %edx
movq 0xf66d(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x988c(%rip), %rsi      # 0xf1f9
callq 0x4330
movq 0xf9e7(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4330
movq %rax, %r14
movl $0xf, %edx
movq %rax, %rdi
leaq 0x98a9(%rip), %rsi      # 0xf24c
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r14
movl $0x14, %edx
movq %rax, %rdi
leaq 0x9898(%rip), %rsi      # 0xf25c
callq 0x4330
movq 0xf988(%rip), %rax      # 0x15358
addq %r15, %rax
addq $0x30, %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x41d0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41e0
jmp 0x56f5
movq -0x40(%rbp), %r14
cmpb $0x1c, 0x26(%r14)
jne 0x5a2b
cmpb $-0x7f, 0x28(%r14)
jne 0x5a2b
movb 0x34(%rax,%r15), %cl
movb %cl, 0x29(%r14)
cmpl $0x0, -0x48(%rbp)
je 0x5a50
movl $0x1, %ecx
movl %ecx, 0xf948(%rip)      # 0x15384
movl %ecx, 0x2c(%rax,%r15)
movl $0x0, 0xf939(%rip)      # 0x15384
movl %ebx, 0x30(%rax,%r15)
movl $0x0, 0x1c(%rax,%r15)
cmpl $0x0, 0xf91c(%rip)      # 0x1537c
je 0x5b11
movl $0xd, %edx
movq 0xf55e(%rip), %r14      # 0x14fd0
movq %r14, %rdi
leaq 0x97f5(%rip), %rsi      # 0xf271
callq 0x4330
movq %r14, %rdi
movl -0x44(%rbp), %esi
callq 0x44c0
movq %rax, %r14
movl $0x17, %edx
movq %rax, %rdi
leaq 0x97e1(%rip), %rsi      # 0xf27f
callq 0x4330
movq 0xf8b6(%rip), %rax      # 0x15360
movq %r12, %rcx
shlq $0x5, %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
movq %r14, %rdi
callq 0x4330
movq %rax, %r14
movl $0xd, %edx
movq %rax, %rdi
leaq 0x97c3(%rip), %rsi      # 0xf297
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42b0
movsbl %al, %esi
movq %r14, %rdi
movq -0x40(%rbp), %r14
callq 0x4040
movq %rax, %rdi
callq 0x41e0
movl 0xf869(%rip), %edi      # 0x15380
addq 0xf83a(%rip), %r15      # 0x15358
movq %r14, %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
movq %r15, %r8
movl $0x10, %r9d
callq 0x4060
movl 0xf845(%rip), %edi      # 0x15380
movl $0x5411, %esi           # imm = 0x5411
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x43e0
cmpl $0x0, -0x2c(%rbp)
je 0x56f5
movl $0xfa, %edi
callq 0x4400
jmp 0x5b35
cmpl $0x0, 0xf814(%rip)      # 0x1537c
je 0x5c1f
movq 0xf45b(%rip), %r14      # 0x14fd0
leaq 0x9667(%rip), %rsi      # 0xf1e3
movl $0xd, %edx
movq %r14, %rdi
callq 0x4330
movq %r14, %rdi
movl %ebx, %esi
callq 0x44c0
movq %rax, %r14
leaq 0x9654(%rip), %rsi      # 0xf1f1
movl $0x15, %edx
movq %rax, %rdi
callq 0x4330
movq 0xf7af(%rip), %rax      # 0x15360
shlq $0x5, %r15
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r14, %rdi
callq 0x4330
movq %rax, %r14
leaq 0x9637(%rip), %rsi      # 0xf207
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