// File: /workspace/github_cpp_makefile_star2_O3/probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::tryLutron()
void Signal::tryLutron()
{
	if ( nFrameL < 4 || nFrameL > 10)	
		return;

	if (   pFrame[0] < 14000
		|| pFrame[0] > 30000
		|| nTotDur < 26*2300		// Nominal min is 29 units
		|| nTotDur > 35*2300 		// Nominal max is 32 units
		|| sortOn.min1 < 2100		// Nominal min is 2100
		|| sortOff.min1 < 2100 )	// Nominal min is 2100
	{
		return;
	}
		
	int bParityError = true;
	// Number of bits between lead-in and lead-out can be anything from 18 to 24
	for (int nBitCnt = 18; bParityError && nBitCnt < 25; nBitCnt++)
	{
		// Work out m_rawUnit from the total duration of these 18 to 24 bits
		m_rawUnit =  float(nBitCnt) / (nTotDur - pFrame[0]);
		m_rawPulseAdjust = .4f;				// Pulse from -.4 to +.2
		m_rawGapAdjust = .2f;				// Gap from -.2 to +.4
		m_rawErrorLimit = .6f;

		cleanup();
		pBit += 1;
		if ( ! decodeRaw(nBitCnt+4) || pBit < pFrameEnd ) continue;
		makeMsb();
		// Check that nBitCnt really is the number of bits before the lead-out.
		if ( ! (getMsb(nBitCnt-1, 1) == 1 && getMsb(nBitCnt, 8) == 0 ) ) continue;

		// Do not rely on the length of the lead-in distinguishing correctly between it being
		// 8,9,10 or 11 units, i.e. concatenated with 0 to 3 data 1-bits.  So loop through 
		// the possibilities until we get a valid decode.
		for (int nOnes=0; bParityError && nOnes<25-nBitCnt; nOnes++)
		{
			// Store the 18-bit decode of the 24-bit data starting at cBits[4]
			cBits[4] = cBits[5] = cBits[6] = 0;
			int nBitPos = 0;	// Next position within 18-bit decode
			bParityError = false;
			for (int i=0; i<24; i++)
			{
				// To decode 3-bit reversed binary with odd parity into 3-bit binary, each binary
				// bit starting msb is XOR of all the reversed binary bits from msb to the corresponding
				// bit, e.g. 1101 decodes to 100 since 1=1, 0=1^1, 0=1^1^0.  The parity check requires
				// XOR of all four bits to be 1, so finally test 1^1^0^1 == 1.
				if ( i<nOnes ? 1 : getMsb(i-nOnes, 1) )
				{
					for (int n = nBitPos; n < (i/4)*3 + 4; n++) { cBits[4+(n>>3)] ^= 0x80>>(n&7); }
				}
				if ((i&3)==3)
				{
					bParityError |= (getMsb(nBitPos+32,1) != 1);
					// Clear the bit position where the parity check was temporarily stored.
					cBits[4+(nBitPos>>3)] &= (0xFF - (0x80>>(nBitPos&7)));
				}
				else nBitPos++;
			}

			// Now check the two overall parity bits of the 18-bit decode
			int nOverallParity = 0;
			for (int i=0; i < 9; i++) nOverallParity ^= getMsb(32+2*i, 2);
			bParityError |= (nOverallParity != 0);
			// If all OK, check that number of initial 1-bits is consistent with the lead-in duration.
			// Note that overall parity means that if nOnes is in error then it can only be out by 2.
			if (	pFrame[0]*m_rawUnit < 7.2 + nOnes
				||	pFrame[0]*m_rawUnit > 8.8 + nOnes ) continue;
/*
			char str[100];
			sprintf(str, "Lutron bitcnt=%d nOnes=%d data = %X", nBitCnt, nOnes, getMsb(32,18));
			MessageBox(0,str,"DecodeIR",0);
*/
		}
	}
	if (bParityError) return;
	strcpy(pProtocol,"Lutron");
	*pDevice = getMsb(32,8);
	*pOBC = getMsb(40,8);		// Hex is 2-byte so cannot be shown
}

// Assembly:
movl 0x48(%rdi), %eax
addl $-0xb, %eax
cmpl $-0x7, %eax
jb 0xe744
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movss (%rax), %xmm0
movss 0x40bb(%rip), %xmm1     # 0x124f0
ucomiss %xmm0, %xmm1
ja 0xe736
ucomiss 0x4153(%rip), %xmm0     # 0x12598
ja 0xe736
movss 0x98(%rbx), %xmm0
movss 0x4259(%rip), %xmm1     # 0x126b4
ucomiss %xmm0, %xmm1
ja 0xe736
ucomiss 0x424d(%rip), %xmm0     # 0x126b8
ja 0xe736
movss 0x4243(%rip), %xmm0     # 0x126bc
ucomiss 0x180(%rbx), %xmm0
ja 0xe736
ucomiss 0x18c(%rbx), %xmm0
ja 0xe736
leaq 0xa0(%rbx), %r12
movl $0x12, %r13d
movabsq $0x3e4ccccd3ecccccd, %r15 # imm = 0x3E4CCCCD3ECCCCCD
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
movss 0x98(%rbx), %xmm1
movq 0x38(%rbx), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xfc(%rbx)
movq %r15, 0x100(%rbx)
movl $0x3f19999a, 0x108(%rbx) # imm = 0x3F19999A
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq $0x0, 0xc0(%rbx)
addq $0x4, %rax
movq %rax, 0xc8(%rbx)
leal 0x4(%r13), %esi
movq %rbx, %rdi
callq 0x1003a
movb $0x1, %r14b
testl %eax, %eax
je 0xe6e7
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jb 0xe6e7
movl 0xc0(%rbx), %eax
addl $0x7, %eax
sarl $0x3, %eax
testl %eax, %eax
movl $0x33000000, %esi       # imm = 0x33000000
movl $0x55000000, %edi       # imm = 0x55000000
jle 0xe589
movl %eax, %eax
movzbl 0x9f(%rbx,%rax), %ecx
bswapl %ecx
movl %ecx, %edx
shll $0x4, %edx
shrl $0x4, %ecx
andl $0xf000000, %ecx        # imm = 0xF000000
orl %edx, %ecx
movl %ecx, %edx
andl %esi, %edx
shrl $0x2, %ecx
andl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movl %ecx, %edx
andl %edi, %edx
shrl %ecx
andl %edi, %ecx
leal (%rcx,%rdx,2), %ecx
shrl $0x18, %ecx
movb %cl, 0x9f(%rbx,%rax)
leaq -0x1(%rax), %rcx
cmpq $0x1, %rax
movq %rcx, %rax
ja 0xe541
leal -0x1(%r13), %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xfc9e
cmpl $0x1, %eax
jne 0xe6e7
movq %rbx, %rdi
movl %r13d, %esi
movl $0x8, %edx
callq 0xfc9e
testl %eax, %eax
jne 0xe6e7
movl $0x19, %eax
movq %r13, -0x38(%rbp)
subl %r13d, %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movb $0x0, 0xa6(%rbx)
movw $0x0, 0xa4(%rbx)
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %r15d, %esi
subl -0x2c(%rbp), %esi
jb 0xe603
movq %rbx, %rdi
movl $0x1, %edx
callq 0xfc9e
testl %eax, %eax
je 0xe636
movl %r15d, %eax
shrl $0x2, %eax
leal (%rax,%rax,2), %eax
addl $0x4, %eax
cmpl %eax, %r13d
jge 0xe636
movl %r13d, %edx
movl %edx, %ecx
andb $0x7, %cl
movl $0x80, %esi
shrl %cl, %esi
movl %edx, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
xorb %sil, 0x4(%rcx,%r12)
incl %edx
cmpl %eax, %edx
jl 0xe617
movl %r15d, %eax
notl %eax
testb $0x3, %al
jne 0xe67a
leal 0x20(%r13), %esi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xfc9e
xorl %edx, %edx
cmpl $0x1, %eax
setne %dl
movl %r13d, %ecx
andb $0x7, %cl
movl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
sarl %cl, %eax
orl %edx, %r14d
movl %r13d, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
andb %al, 0xa4(%rbx,%rcx)
jmp 0xe67d
incl %r13d
incl %r15d
cmpl $0x18, %r15d
jne 0xe5ea
xorl %ecx, %ecx
movl $0x20, %r15d
movl %ecx, %r13d
movq %rbx, %rdi
movl %r15d, %esi
movl $0x2, %edx
callq 0xfc9e
movl %eax, %ecx
xorl %r13d, %ecx
addl $0x2, %r15d
cmpl $0x32, %r15d
jne 0xe692
xorl %ecx, %ecx
cmpl %r13d, %eax
setne %cl
orl %ecx, %r14d
je 0xe6d2
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpl -0x30(%rbp), %eax
jb 0xe5d1
testl %r14d, %r14d
setne %r14b
movabsq $0x3e4ccccd3ecccccd, %r15 # imm = 0x3E4CCCCD3ECCCCCD
movq -0x38(%rbp), %r13
testb %r14b, %r14b
je 0xe6fd
leal 0x1(%r13), %eax
cmpl $0x18, %r13d
movl %eax, %r13d
jb 0xe4aa
testb %r14b, %r14b
jne 0xe736
movq 0x148(%rbx), %rax
movl $0x6e6f72, 0x3(%rax)    # imm = 0x6E6F72
movl $0x7274754c, (%rax)     # imm = 0x7274754C
movzbl 0xa4(%rbx), %eax
movq 0x160(%rbx), %rcx
movl %eax, (%rcx)
movzbl 0xa5(%rbx), %eax
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop