// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x4, 0x48(%rax)
jl 0x13a84
movq -0x30(%rbp), %rax
cmpl $0xa, 0x48(%rax)
jle 0x13a89
jmp 0x13ec3
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss 0x6573(%rip), %xmm0     # 0x1a00c
ucomiss (%rax), %xmm0
ja 0x13b0f
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movss (%rax), %xmm0
movss 0x65f6(%rip), %xmm1     # 0x1a0a8
ucomiss %xmm1, %xmm0
ja 0x13b0f
movq -0x30(%rbp), %rax
movss 0x66dd(%rip), %xmm0     # 0x1a1a0
ucomiss 0x98(%rax), %xmm0
ja 0x13b0f
movq -0x30(%rbp), %rax
movss 0x98(%rax), %xmm0
movss 0x66c4(%rip), %xmm1     # 0x1a1a4
ucomiss %xmm1, %xmm0
ja 0x13b0f
movq -0x30(%rbp), %rax
movss 0x66b7(%rip), %xmm0     # 0x1a1a8
ucomiss 0x180(%rax), %xmm0
ja 0x13b0f
movq -0x30(%rbp), %rax
movss 0x66a2(%rip), %xmm0     # 0x1a1a8
ucomiss 0x18c(%rax), %xmm0
jbe 0x13b14
jmp 0x13ec3
movl $0x1, -0xc(%rbp)
movl $0x12, -0x10(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x31(%rbp)
je 0x13b37
cmpl $0x19, -0x10(%rbp)
setl %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0x13b43
jmp 0x13e64
movq -0x30(%rbp), %rdi
cvtsi2ssl -0x10(%rbp), %xmm0
movss 0x98(%rdi), %xmm1
movq 0x38(%rdi), %rax
subss (%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xfc(%rdi)
movss 0x662c(%rip), %xmm0     # 0x1a19c
movss %xmm0, 0x100(%rdi)
movss 0x6618(%rip), %xmm0     # 0x1a198
movss %xmm0, 0x104(%rdi)
movss 0x6604(%rip), %xmm0     # 0x1a194
movss %xmm0, 0x108(%rdi)
callq 0x16180
movq -0x30(%rbp), %rdi
movq 0xc8(%rdi), %rax
addq $0x4, %rax
movq %rax, 0xc8(%rdi)
movl -0x10(%rbp), %esi
addl $0x4, %esi
callq 0x16540
cmpl $0x0, %eax
je 0x13bd4
movq -0x30(%rbp), %rcx
movq 0xc8(%rcx), %rax
cmpq 0x40(%rcx), %rax
jae 0x13bd9
jmp 0x13e56
movq -0x30(%rbp), %rdi
callq 0x16110
movq -0x30(%rbp), %rdi
movl -0x10(%rbp), %esi
subl $0x1, %esi
movl $0x1, %edx
callq 0x15f50
cmpl $0x1, %eax
jne 0x13c11
movq -0x30(%rbp), %rdi
movl -0x10(%rbp), %esi
movl $0x8, %edx
callq 0x15f50
cmpl $0x0, %eax
je 0x13c16
jmp 0x13e56
movl $0x0, -0x14(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x32(%rbp)
je 0x13c3b
movl -0x14(%rbp), %eax
movl $0x19, %ecx
subl -0x10(%rbp), %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x32(%rbp)
movb -0x32(%rbp), %al
testb $0x1, %al
jne 0x13c47
jmp 0x13e54
movq -0x30(%rbp), %rax
movb $0x0, 0xa6(%rax)
movb $0x0, 0xa5(%rax)
movb $0x0, 0xa4(%rax)
movl $0x0, -0x18(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x18, -0x1c(%rbp)
jge 0x13d96
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x13c91
movl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x13ca8
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %esi
subl -0x14(%rbp), %esi
movl $0x1, %edx
callq 0x15f50
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl $0x0, %eax
je 0x13d11
movl -0x18(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x3c(%rbp), %eax
imull $0x3, %ecx, %ecx
addl $0x4, %ecx
cmpl %ecx, %eax
jge 0x13d0f
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
andl $0x7, %ecx
movl $0x80, %esi
sarl %cl, %esi
movl -0x20(%rbp), %ecx
sarl $0x3, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzbl 0xa0(%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, 0xa0(%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x13cb6
jmp 0x13d11
movl -0x1c(%rbp), %eax
andl $0x3, %eax
cmpl $0x3, %eax
jne 0x13d7d
movq -0x30(%rbp), %rdi
movl -0x18(%rbp), %esi
addl $0x20, %esi
movl $0x1, %edx
callq 0x15f50
movl %eax, %ecx
movq -0x30(%rbp), %rax
cmpl $0x1, %ecx
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl -0xc(%rbp), %ecx
movl %ecx, -0xc(%rbp)
movl -0x18(%rbp), %ecx
andl $0x7, %ecx
movl $0x80, %edx
sarl %cl, %edx
movl %edx, %ecx
movl $0xff, %esi
subl %ecx, %esi
movl -0x18(%rbp), %ecx
sarl $0x3, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzbl 0xa0(%rax,%rcx), %edx
andl %esi, %edx
movb %dl, 0xa0(%rax,%rcx)
jmp 0x13d86
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x13d88
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x13c75
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x9, -0x28(%rbp)
jge 0x13dd1
movq -0x30(%rbp), %rdi
movl -0x28(%rbp), %esi
shll %esi
addl $0x20, %esi
movl $0x2, %edx
callq 0x15f50
xorl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x13da4
movq -0x30(%rbp), %rax
cmpl $0x0, -0x24(%rbp)
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl -0xc(%rbp), %ecx
movl %ecx, -0xc(%rbp)
movq 0x38(%rax), %rcx
movss (%rcx), %xmm0
mulss 0xfc(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
cvtsi2sdl -0x14(%rbp), %xmm2
movsd 0x6927(%rip), %xmm0     # 0x1a730
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x13e42
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rcx
movss (%rcx), %xmm0
mulss 0xfc(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
cvtsi2sdl -0x14(%rbp), %xmm2
movsd 0x6900(%rip), %xmm1     # 0x1a738
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x13e44
jmp 0x13e46
jmp 0x13e46
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x13c1d
jmp 0x13e56
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x13b22
cmpl $0x0, -0xc(%rbp)
je 0x13e6c
jmp 0x13ec3
movq -0x30(%rbp), %rax
movq 0x148(%rax), %rdi
leaq 0x6c49(%rip), %rsi      # 0x1aac7
callq 0x2120
movq -0x30(%rbp), %rdi
movl $0x20, %esi
movl $0x8, %edx
callq 0x15f50
movq -0x30(%rbp), %rdi
movl %eax, %ecx
movq 0x160(%rdi), %rax
movl %ecx, (%rax)
movl $0x28, %esi
movl $0x8, %edx
callq 0x15f50
movl %eax, %ecx
movq -0x30(%rbp), %rax
movq 0x170(%rax), %rax
movl %ecx, (%rax)
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)