// File: probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::decode()
void Signal::decode()
{
	typedef void (Signal::* SignalFnc)();
	static const SignalFnc Funcs[] = {
		&Signal::tryXMP,
		&Signal::tryZenith,
		&Signal::tryX10,
		&Signal::tryXX,
		&Signal::tryTDC,		// GD 2009. Needs to be above Q2
		&Signal::trySejin,		// GD 2009
		&Signal::tryQ1,
		&Signal::tryQ2,
		&Signal::tryDirecTV,
		&Signal::trySomfy,
		&Signal::tryGap,		// if this moves, change nGapProtocol value	
		&Signal::tryRC5,
		&Signal::tryRC6,
		&Signal::tryCanalSat,	// GD 2009
		&Signal::tryNokia,
		&Signal::tryPCTV,
		&Signal::tryF12,
		&Signal::trySony,
		&Signal::trySingleBurstSize,
		&Signal::tryGXB,
		&Signal::tryAsync,
		&Signal::tryAirboard,
		&Signal::tryAirAsync,
		&Signal::tryAK,
		&Signal::trySunfire,
		&Signal::tryGrundig16,
		&Signal::tryPid13,
		&Signal::tryLutron,		// GD 2010
		&Signal::tryElan,		// DAR 2012
		&Signal::tryBryston,	// DAR 2012
		&Signal::tryHumax,		// DAR 2012
		&Signal::tryAdNotam		// DAR 2012
	};

	nGapProtocol = 10;

	*pProtocol = 0;
	// GD 2009 Start - Initialize output variables as IRScope does not appear
	// to do so on repeat calls
	/* if ( *pDevice >= 0 ) */ *pDevice = -1;	// Initialize Device only if not carrying ExtraBurstCount
	*pSubDevice = -1;
	*pOBC = -1;
	for (int n = 0; n < 4; n++) pHex[n] = -1;
	*pMisc = 0;
	*pError = 0;
	// GD 2009 End
	int nStart = pContext[0] & 0xFFFFF;
	preemptLength = pContext[0] >> 20;
	nFrameL = (pContext[1] >> 16) + MinFrame;
	nProtocol = (pContext[1] & 0xFF);
	preemptValue = (pContext[1] >> 8) & 0xFF;
	newPreemptValue = 0;
	newPreemptLength = 0;

	int nStartLimit = nSingle + nRepeat;
	int nTot = nStartLimit + nRepeat;
	pMainLimit = pDuration + 2 * nStartLimit;
	pFullLimit = pMainLimit + 2 * nRepeat;
	if (nRepeat < MinFrame)
		nStartLimit = nTot - (MinFrame-1);

 // /* Cover bug in CCF2EFC */ if (nStartLimit>200) nStartLimit=200;

	for (; nStart<nStartLimit; nStart++)
	{
		pFrame = pDuration + nStart*2;
        frameLeft = 1e9;
        if ( nStart )
        {
            frameLeft = pFrame[-1];
            if ( nStart == nSingle )
            {
                float wrap = pDuration[ 2*(nSingle+nRepeat) -1 ];
                if ( wrap >= frameLeft )
                    frameLeft = wrap;
            }
        }
		if ( nStart == 0 || nStart == nSingle || pFrame[0]+pFrame[1] < pFrame[-2]*.5+pFrame[-1]
			|| pFrame[0] > 14000 )  // This last one deals with Lutron, which uses a long lead-in rather than framing
		{
			pFrameEnd = pFrame + nFrameL*2-3;	// Point to Off half of second to last burst (advanced later to be final Off)
			nTotDur = 0;
			for (float* pDur = pFrame; ; )
			{
				sortOn.insert(pDur);
				sortOff.insert(pDur+1);
				sortBurst.insert1(pDur);
				nTotDur += pDur[0] + pDur[1];
				pDur+=2;
				if (pDur >= pFrameEnd)
					break;
			}
            sortBurst.findMid();

			int nMaxL = nTot - nStart;  // Maximum frame length that could start at nStart
			if (nMaxL > MaxFrame)
				nMaxL = MaxFrame;
			for (; ; nFrameL++)  //  Loop over posible frame lengths
			{
				pFrameEnd++;    // Point at last burst (lead out)
				sortOn.insert(pFrameEnd);

				nMaxDur = sortOff.max1;   // Excludes final Off
                if (nMaxDur < sortOn.max1)
					nMaxDur = sortOn.max1;

				nTotDur += *pFrameEnd++;    // On
                
                frame = *pFrameEnd;
                if ( frameLeft <= frame )
                    frame = frameLeft;

				// GD Start


				int xxStart = nStart;
				int xxFrameL = nFrameL;
				// Calc freq for this frame
				nFreq = (nContextLength > 4 && pCounts != NULL) ? getFreq(nStart, nStart+nFrameL) : nFreq_in;
				// GD End

				for ( ;nProtocol < sizeof(Funcs)/sizeof(*Funcs); )
				{
					(this->*(Funcs[nProtocol]))();
					++nProtocol;
					if (*pProtocol)
					{
						if (   newPreemptValue > preemptValue
							||    newPreemptValue == preemptValue
							   && newPreemptLength > preemptLength )
						{
							preemptValue = newPreemptValue;
							preemptLength = newPreemptLength;
						}
						pContext[0] = nStart + (preemptLength<<20);
						pContext[1] = ((nFrameL - MinFrame)<<16) + (preemptValue<<8) + nProtocol;
						return;
					}
				}
				nProtocol = 0;

                if ( nFrameL >= nMaxL )
                    break;

				nTotDur += *pFrameEnd;    // Off
				sortBurst.insert(pFrameEnd-1);
				sortOff.insert(pFrameEnd);
			}	// for (; ; nFrameL++)
			sortOn.clear();
			sortOff.clear();
			sortBurst.clear();
		}	// if ( nStart == 0 || ...
		nFrameL=MinFrame;
		if (--preemptLength <= 0)
		{
			preemptLength = preemptValue = 0;
		}
	}  // for (; nStart<nStartLimit; nStart++)
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0xa, 0x78(%rax)
movq 0x148(%rax), %rcx
movb $0x0, (%rcx)
movq 0x160(%rax), %rcx
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movq 0x168(%rax), %rcx
movl $0xffffffff, (%rcx)     # imm = 0xFFFFFFFF
movq 0x170(%rax), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x0, -0xc(%rbp)
cmpl $0x4, -0xc(%rbp)
jge 0x294a
movq -0x40(%rbp), %rax
movq 0x178(%rax), %rax
movslq -0xc(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x2923
movq -0x40(%rbp), %rax
movq 0x150(%rax), %rcx
movb $0x0, (%rcx)
movq 0x158(%rax), %rcx
movb $0x0, (%rcx)
movq (%rax), %rcx
movl (%rcx), %ecx
andl $0xfffff, %ecx          # imm = 0xFFFFF
movl %ecx, -0x10(%rbp)
movq (%rax), %rcx
movl (%rcx), %ecx
shrl $0x14, %ecx
movl %ecx, 0x50(%rax)
movq (%rax), %rcx
movl 0x4(%rcx), %ecx
shrl $0x10, %ecx
addl $0x2, %ecx
movl %ecx, 0x48(%rax)
movq (%rax), %rcx
movl 0x4(%rcx), %ecx
andl $0xff, %ecx
movl %ecx, 0x4c(%rax)
movq (%rax), %rcx
movl 0x4(%rcx), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movl %ecx, 0x54(%rax)
movl $0x0, 0x5c(%rax)
movl $0x0, 0x58(%rax)
movl 0x20(%rax), %ecx
addl 0x24(%rax), %ecx
movl %ecx, -0x14(%rbp)
movl -0x14(%rbp), %ecx
addl 0x24(%rax), %ecx
movl %ecx, -0x18(%rbp)
movq 0x8(%rax), %rcx
movl -0x14(%rbp), %edx
shll %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, 0x10(%rax)
movq 0x10(%rax), %rcx
movl 0x24(%rax), %edx
shll %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, 0x18(%rax)
cmpl $0x2, 0x24(%rax)
jge 0x2a08
movl -0x18(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2a0a
movl -0x10(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2ed6
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x10(%rbp), %edx
shll %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
movq %rcx, 0x38(%rax)
movss 0x175cf(%rip), %xmm0    # 0x1a008
movss %xmm0, 0x13c(%rax)
cmpl $0x0, -0x10(%rbp)
je 0x2aa7
movq -0x40(%rbp), %rcx
movq 0x38(%rcx), %rax
movss -0x4(%rax), %xmm0
movss %xmm0, 0x13c(%rcx)
movl -0x10(%rbp), %eax
cmpl 0x20(%rcx), %eax
jne 0x2aa5
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
movl 0x20(%rax), %edx
addl 0x24(%rax), %edx
shll %edx
subl $0x1, %edx
movslq %edx, %rdx
movss (%rcx,%rdx,4), %xmm0
movss %xmm0, -0x1c(%rbp)
movss -0x1c(%rbp), %xmm0
ucomiss 0x13c(%rax), %xmm0
jb 0x2aa3
movq -0x40(%rbp), %rax
movss -0x1c(%rbp), %xmm0
movss %xmm0, 0x13c(%rax)
jmp 0x2aa5
jmp 0x2aa7
cmpl $0x0, -0x10(%rbp)
je 0x2b1f
movq -0x40(%rbp), %rcx
movl -0x10(%rbp), %eax
cmpl 0x20(%rcx), %eax
je 0x2b1f
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rcx
movss (%rcx), %xmm0
movq 0x38(%rax), %rcx
addss 0x4(%rcx), %xmm0
cvtss2sd %xmm0, %xmm1
movq 0x38(%rax), %rcx
movss -0x8(%rcx), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x38(%rax), %rax
movss -0x4(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movsd 0x176cc(%rip), %xmm3    # 0x1a1c0
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x2b1f
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rax
movss (%rax), %xmm0
movss 0x174f6(%rip), %xmm1    # 0x1a00c
ucomiss %xmm1, %xmm0
jbe 0x2e9b
movq -0x40(%rbp), %rax
movq 0x38(%rax), %rcx
movl 0x48(%rax), %edx
shll %edx
movslq %edx, %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
addq $-0xc, %rcx
movq %rcx, 0x40(%rax)
xorps %xmm0, %xmm0
movss %xmm0, 0x98(%rax)
movq 0x38(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x40(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
movq -0x28(%rbp), %rsi
callq 0x181c0
movq -0x40(%rbp), %rdi
addq $0x18c, %rdi            # imm = 0x18C
movq -0x28(%rbp), %rsi
addq $0x4, %rsi
callq 0x18270
movq -0x40(%rbp), %rdi
addq $0x198, %rdi            # imm = 0x198
movq -0x28(%rbp), %rsi
callq 0x182f0
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movss (%rax), %xmm0
movq -0x28(%rbp), %rax
addss 0x4(%rax), %xmm0
addss 0x98(%rcx), %xmm0
movss %xmm0, 0x98(%rcx)
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq 0x40(%rcx), %rax
jb 0x2bce
jmp 0x2bd0
jmp 0x2b51
movq -0x40(%rbp), %rdi
addq $0x198, %rdi            # imm = 0x198
callq 0x18490
movl -0x18(%rbp), %eax
subl -0x10(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x64, -0x2c(%rbp)
jle 0x2bf6
movl $0x64, -0x2c(%rbp)
jmp 0x2bf8
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x4, %rcx
movq %rcx, 0x40(%rax)
movq %rax, %rdi
addq $0x180, %rdi            # imm = 0x180
movq 0x40(%rax), %rsi
callq 0x181c0
movq -0x40(%rbp), %rax
movss 0x190(%rax), %xmm0
movss %xmm0, 0x9c(%rax)
movss 0x9c(%rax), %xmm1
movss 0x184(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x2c58
movq -0x40(%rbp), %rax
movss 0x184(%rax), %xmm0
movss %xmm0, 0x9c(%rax)
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rcx
movq %rcx, %rdx
addq $0x4, %rdx
movq %rdx, 0x40(%rax)
movss (%rcx), %xmm0
addss 0x98(%rax), %xmm0
movss %xmm0, 0x98(%rax)
movq 0x40(%rax), %rcx
movss (%rcx), %xmm0
movss %xmm0, 0x140(%rax)
movss 0x13c(%rax), %xmm1
movss 0x140(%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x2cb8
movq -0x40(%rbp), %rax
movss 0x13c(%rax), %xmm0
movss %xmm0, 0x140(%rax)
movq -0x40(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, -0x30(%rbp)
movl 0x48(%rax), %ecx
movl %ecx, -0x34(%rbp)
cmpl $0x4, 0x74(%rax)
jle 0x2cf3
movq -0x40(%rbp), %rax
cmpq $0x0, 0x90(%rax)
je 0x2cf3
movq -0x40(%rbp), %rdi
movl -0x10(%rbp), %esi
movl -0x10(%rbp), %edx
addl 0x48(%rdi), %edx
callq 0x148a0
movl %eax, -0x44(%rbp)
jmp 0x2cfd
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %ecx, 0x2c(%rax)
movq -0x40(%rbp), %rax
movslq 0x4c(%rax), %rax
cmpq $0x20, %rax
jae 0x2dfa
movq -0x40(%rbp), %rcx
movslq 0x4c(%rcx), %rdx
shlq $0x4, %rdx
leaq 0x1be74(%rip), %rsi     # 0x1eba0
movq (%rdx,%rsi), %rax
movq %rax, -0x58(%rbp)
movq 0x8(%rdx,%rsi), %rdx
addq %rdx, %rcx
movq %rcx, -0x50(%rbp)
andq $0x1, %rax
cmpq $0x0, %rax
je 0x2d63
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x60(%rbp)
jmp 0x2d6b
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rax
callq *%rax
movq -0x40(%rbp), %rax
movl 0x4c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4c(%rax)
movq 0x148(%rax), %rax
cmpb $0x0, (%rax)
je 0x2df5
movq -0x40(%rbp), %rcx
movl 0x5c(%rcx), %eax
cmpl 0x54(%rcx), %eax
jg 0x2db2
movq -0x40(%rbp), %rcx
movl 0x5c(%rcx), %eax
cmpl 0x54(%rcx), %eax
jne 0x2dc2
movq -0x40(%rbp), %rcx
movl 0x58(%rcx), %eax
cmpl 0x50(%rcx), %eax
jle 0x2dc2
movq -0x40(%rbp), %rax
movl 0x5c(%rax), %ecx
movl %ecx, 0x54(%rax)
movl 0x58(%rax), %ecx
movl %ecx, 0x50(%rax)
movq -0x40(%rbp), %rax
movl -0x10(%rbp), %edx
movl 0x50(%rax), %ecx
shll $0x14, %ecx
addl %ecx, %edx
movq (%rax), %rcx
movl %edx, (%rcx)
movl 0x48(%rax), %ecx
subl $0x2, %ecx
shll $0x10, %ecx
movl 0x54(%rax), %edx
shll $0x8, %edx
addl %edx, %ecx
addl 0x4c(%rax), %ecx
movq (%rax), %rax
movl %ecx, 0x4(%rax)
jmp 0x2ed6
jmp 0x2d07
movq -0x40(%rbp), %rax
movl $0x0, 0x4c(%rax)
movl 0x48(%rax), %eax
cmpl -0x2c(%rbp), %eax
jl 0x2e0f
jmp 0x2e6b
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rcx
movss (%rcx), %xmm0
addss 0x98(%rax), %xmm0
movss %xmm0, 0x98(%rax)
movq %rax, %rdi
addq $0x198, %rdi            # imm = 0x198
movq 0x40(%rax), %rsi
addq $-0x4, %rsi
callq 0x18570
movq -0x40(%rbp), %rax
movq %rax, %rdi
addq $0x18c, %rdi            # imm = 0x18C
movq 0x40(%rax), %rsi
callq 0x18270
movq -0x40(%rbp), %rax
movl 0x48(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x48(%rax)
jmp 0x2bf8
movq -0x40(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
callq 0x187b0
movq -0x40(%rbp), %rdi
addq $0x18c, %rdi            # imm = 0x18C
callq 0x187e0
movq -0x40(%rbp), %rdi
addq $0x198, %rdi            # imm = 0x198
callq 0x18810
movq -0x40(%rbp), %rcx
movl $0x2, 0x48(%rcx)
movl 0x50(%rcx), %eax
addl $-0x1, %eax
movl %eax, 0x50(%rcx)
cmpl $0x0, %eax
jg 0x2ec6
movq -0x40(%rbp), %rax
movl $0x0, 0x54(%rax)
movl $0x0, 0x50(%rax)
jmp 0x2ec8
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x2a0a
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)