// File: /workspace/github_cpp_makefile_star2_O1/rthoelen[P]NXCore_Kenwood/main.cc
// Function: timing_thread(void*)
void *timing_thread(void *t_id)
{
	int i;
	int minor_cycle=0;
	unsigned int seconds = 0;
	char h_buf[59];
	unsigned char nx_packet[28] = { 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x4e, 0x58, 0x44, 0x4e, 0x4e, 0x31, 0x58, 0x44,
				 0x4e, 0x43, 0x54, 0x00, 0x00, 0x00, 0x00, 0x01,
				0x00, 0x00, 0x00, 0x00 };
	char buf_64[80];

	
	
        memset((char *)&h_buf[0], 0, sizeof(h_buf));
	h_buf[21] = 0x99;
	for(;;)
	{

		// minor cycle ~ 80ms


		// send shutdown packets if needed

		for ( i = 0; i < repeater_count; i++)
		{
			if((repeater[i].keydown > 0)&&((minor_cycle % 2) == 1))
				shutdown_64001(i);
		}	

		// clear out 64001 packets

		recv(socket_01,buf_64, 80, MSG_DONTWAIT);

		// major loop ~ 1 second

		if(minor_cycle == 0)
		{

			for( i = 0; i < repeater_count; i++)
			{
				repeater[i].time_since_rx++;
				if(repeater[i].time_since_rx > repeater[i].hold_time)
				{
					repeater[i].time_since_rx = repeater[i].hold_time;
					repeater[i].rx_activity = 0;
				}

				repeater[i].time_since_tx++;
		
				while(tx_busy_sem == 1)
					usleep(1);
				if(repeater[i].time_since_tx > repeater[i].tx_hold_time)
				{
					repeater[i].time_since_tx = repeater[i].tx_hold_time;
					repeater[i].tx_busy = 0;
				}
			}	
	
			if((++seconds % 20) == 0 )
			{
				for( i = 0; i < repeater_count; i++)
				{
					if ( repeater[i].stealth && repeater[i].tx_busy == 0)
					{
						while(tx_sem==1)
							usleep(1);
						tx_sem=1;
						sendto(socket_00, h_buf, sizeof(h_buf), 0, (struct sockaddr *)&repeater[i].rpt_addr_00,
							sizeof(repeater[i].rpt_addr_00));

						sendto(socket_01, nx_packet, sizeof(nx_packet), 0, (struct sockaddr *)&repeater[i].rpt_addr_01,
							sizeof(repeater[i].rpt_addr_01));
						tx_sem=0;

					}	
				}

			}	

		}

		usleep(80000);
		if(++minor_cycle > 11)
			minor_cycle = 0;
	}
	return (void *)0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movups 0x9a31(%rip), %xmm0     # 0xfc2c
leaq -0x50(%rbp), %rax
movups %xmm0, 0xc(%rax)
movaps 0x9a16(%rip), %xmm0     # 0xfc20
movaps %xmm0, (%rax)
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movaps %xmm0, 0x20(%rax)
movups %xmm0, 0x2b(%rax)
movb $-0x67, 0x15(%rax)
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpl $0x0, 0xf1a1(%rip)      # 0x153d8
jle 0x6285
movl %r15d, %ebx
andl $0x80000001, %ebx       # imm = 0x80000001
movl $0x74, %r14d
xorl %r12d, %r12d
cmpl $0x1, %ebx
sete %al
movq 0xf178(%rip), %rcx      # 0x153d0
cmpl $0x0, (%rcx,%r14)
setg %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x626f
movl %r12d, %edi
callq 0x643a
incq %r12
movslq 0xf15f(%rip), %rax      # 0x153d8
addq $0x90, %r14
cmpq %rax, %r12
jl 0x624b
movl 0xf175(%rip), %edi      # 0x15400
movl $0x50, %edx
leaq -0xe0(%rbp), %rsi
movl $0x40, %ecx
callq 0x40d0
testl %r15d, %r15d
jne 0x6418
cmpl $0x0, 0xf127(%rip)      # 0x153d8
jle 0x632e
xorl %r12d, %r12d
movq 0xf113(%rip), %rax      # 0x153d0
leaq (%r12,%r12,8), %rbx
shlq $0x4, %rbx
movl 0x28(%rax,%rbx), %edx
movl 0x30(%rax,%rbx), %ecx
incl %edx
movl %edx, 0x28(%rax,%rbx)
cmpl %ecx, %edx
jle 0x62e6
leaq (%rax,%rbx), %rdx
movl %ecx, 0x28(%rdx)
movl $0x0, 0x38(%rax,%rbx)
incl 0x2c(%rax,%rbx)
cmpl $0x1, 0xf117(%rip)      # 0x15408
jne 0x62ff
movl $0x1, %edi
callq 0x4450
jmp 0x62ea
movq 0xf0ca(%rip), %rax      # 0x153d0
movl 0x34(%rax,%rbx), %ecx
cmpl %ecx, 0x2c(%rax,%rbx)
jle 0x631f
leaq (%rax,%rbx), %rdx
movl %ecx, 0x2c(%rdx)
movl $0x0, 0x3c(%rax,%rbx)
incq %r12
movslq 0xf0af(%rip), %rax      # 0x153d8
cmpq %rax, %r12
jl 0x62b6
incl %r13d
imull $0xcccccccd, %r13d, %eax # imm = 0xCCCCCCCD
rorl $0x2, %eax
cmpl $0xccccccc, %eax        # imm = 0xCCCCCCC
ja 0x6418
cmpl $0x0, 0xf08b(%rip)      # 0x153d8
jle 0x6418
xorl %r12d, %r12d
movq 0xf073(%rip), %rax      # 0x153d0
leaq (%r12,%r12,8), %rcx
shlq $0x4, %rcx
cmpl $0x0, 0x44(%rax,%rcx)
je 0x6405
addq %rcx, %rax
cmpl $0x0, 0x3c(%rax)
jne 0x6405
jmp 0x6389
movl $0x1, %edi
callq 0x4450
cmpl $0x1, 0xf074(%rip)      # 0x15404
je 0x637f
movl $0x1, 0xf068(%rip)      # 0x15404
movl 0xf05a(%rip), %edi      # 0x153fc
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,8), %rbx
movq 0xf01c(%rip), %r8       # 0x153d0
addq %rbx, %r8
movl $0x3b, %edx
leaq -0x90(%rbp), %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
movl 0xf02a(%rip), %edi      # 0x15400
movq 0xeff3(%rip), %rax      # 0x153d0
leaq (%rax,%rbx), %r8
addq $0x10, %r8
movl $0x1c, %edx
leaq -0x50(%rbp), %rsi
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x4060
movl $0x0, 0xefff(%rip)      # 0x15404
incq %r12
movslq 0xefc9(%rip), %rax      # 0x153d8
cmpq %rax, %r12
jl 0x6356
movl $0x13880, %edi          # imm = 0x13880
callq 0x4450
cmpl $0xb, %r15d
leal 0x1(%r15), %eax
movl $0x0, %ecx
cmovgel %ecx, %eax
movl %eax, %r15d
jmp 0x6230