// File: rpavlik[P]edid-decode/parse-cta-block.cpp
// Function: edid_state::preparse_cta_block(unsigned char const*)
void edid_state::preparse_cta_block(const unsigned char *x)
{
	unsigned version = x[1];
	unsigned offset = x[2];

	if (offset >= 4) {
		const unsigned char *detailed;

		for (detailed = x + offset; detailed + 17 < x + 127; detailed += 18) {
			if (memchk(detailed, 18))
				break;
			if (detailed[0] || detailed[1])
				cta.preparsed_total_dtds++;
		}
	}

	if (version < 3)
		return;

	for (unsigned i = 4; i < offset; i += (x[i] & 0x1f) + 1) {
		bool for_ycbcr420 = false;
		unsigned oui;

		switch ((x[i] & 0xe0) >> 5) {
		case 0x03:
			oui = (x[i + 3] << 16) + (x[i + 2] << 8) + x[i + 1];
			if (oui == 0x000c03) {
				cta.has_hdmi = true;
				cta.preparsed_phys_addr = (x[i + 4] << 8) | x[i + 5];
			}
			break;
		case 0x07:
			if (x[i + 1] == 0x0d)
				cta.has_vfpdb = true;
			if (x[i + 1] == 0x13 && (x[i + 2] & 0x40)) {
				cta.preparsed_speaker_count = 1 + (x[i + 2] & 0x1f);
				cta.preparsed_sld = x[i + 2] & 0x20;
			}
			if (x[i + 1] == 0x14)
				cta_preparse_sldb(x + i + 2, (x[i] & 0x1f) - 1);
			if (x[i + 1] == 0x22)
				cta.preparsed_total_vtdbs++;
			if (x[i + 1] == 0x23)
				cta.preparsed_has_t8vtdb = true;
			if (x[i + 1] == 0x32)
				cta.preparsed_total_vtdbs +=
					((x[i] & 0x1f) - 2) / (6 + ((x[i + 2] & 0x70) >> 4));
			if (x[i + 1] != 0x0e)
				continue;
			for_ycbcr420 = true;
			/* fall-through */
		case 0x02:
			for (unsigned j = 1 + for_ycbcr420; j <= (x[i] & 0x1f); j++) {
				unsigned char vic = x[i + j];

				if ((vic & 0x7f) <= 64)
					vic &= 0x7f;
				cta.preparsed_svds[for_ycbcr420].push_back(vic);
				cta.preparsed_has_vic[for_ycbcr420][vic] = true;
			}
			break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
movl %eax, -0x18(%rbp)
cmpl $0x4, -0x18(%rbp)
jb 0x1d7c7
movq -0x10(%rbp), %rax
movl -0x18(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x11, %rax
movq -0x10(%rbp), %rcx
addq $0x7f, %rcx
cmpq %rcx, %rax
jae 0x1d7c5
movq -0x20(%rbp), %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x7810
testb $0x1, %al
jne 0x1d78d
jmp 0x1d78f
jmp 0x1d7c5
movq -0x20(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x1d7a2
movq -0x20(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x1d7b5
movq -0x40(%rbp), %rax
movl 0x180(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x180(%rax)
jmp 0x1d7b7
movq -0x20(%rbp), %rax
addq $0x12, %rax
movq %rax, -0x20(%rbp)
jmp 0x1d762
jmp 0x1d7c7
cmpl $0x3, -0x14(%rbp)
jae 0x1d7d2
jmp 0x1daf7
movl $0x4, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x1daf7
movb $0x0, -0x25(%rbp)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movzbl (%rax,%rcx), %eax
shrl $0x5, %eax
movl %eax, -0x44(%rbp)
subl $0x2, %eax
je 0x1da28
jmp 0x1d805
movl -0x44(%rbp), %eax
subl $0x3, %eax
je 0x1d820
jmp 0x1d80f
movl -0x44(%rbp), %eax
subl $0x7, %eax
je 0x1d8a2
jmp 0x1dad9
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
shll $0x10, %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0xc03, -0x2c(%rbp)     # imm = 0xC03
jne 0x1d89d
movq -0x40(%rbp), %rax
movb $0x1, 0x2c3(%rax)
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x4, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x10(%rbp), %rdx
movl -0x24(%rbp), %esi
addl $0x5, %esi
movl %esi, %esi
movzbl (%rdx,%rsi), %edx
orl %edx, %ecx
movw %cx, 0x2d0(%rax)
jmp 0x1dad9
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0x1d8c2
movq -0x40(%rbp), %rax
movb $0x1, 0x2c5(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x13, %eax
jne 0x1d931
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x1d931
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
andl $0x1f, %ecx
addl $0x1, %ecx
movl %ecx, 0x2c8(%rax)
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
andl $0x20, %ecx
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, 0x2cd(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x14, %eax
jne 0x1d96e
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x24(%rbp), %eax
addq %rax, %rsi
addq $0x2, %rsi
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movzbl (%rax,%rcx), %edx
andl $0x1f, %edx
subl $0x1, %edx
callq 0x18380
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x22, %eax
jne 0x1d996
movq -0x40(%rbp), %rax
movl 0x1a0(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1a0(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x23, %eax
jne 0x1d9b6
movq -0x40(%rbp), %rax
movb $0x1, 0x1d8(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x32, %eax
jne 0x1da0a
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1f, %eax
subl $0x2, %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
addl $0x2, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
andl $0x70, %ecx
sarl $0x4, %ecx
addl $0x6, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x40(%rbp), %rax
addl 0x1a0(%rax), %ecx
movl %ecx, 0x1a0(%rax)
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xe, %eax
je 0x1da24
jmp 0x1dadb
movb $0x1, -0x25(%rbp)
movb -0x25(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
movl -0x24(%rbp), %edx
movzbl (%rcx,%rdx), %ecx
andl $0x1f, %ecx
cmpl %ecx, %eax
ja 0x1dad7
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
addl -0x30(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
andl $0x7f, %eax
cmpl $0x40, %eax
jg 0x1da77
movzbl -0x31(%rbp), %eax
andl $0x7f, %eax
movb %al, -0x31(%rbp)
movq -0x40(%rbp), %rdi
addq $0x180, %rdi            # imm = 0x180
addq $0x760, %rdi            # imm = 0x760
movb -0x25(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq -0x31(%rbp), %rsi
callq 0x201c0
movq -0x40(%rbp), %rax
addq $0x180, %rax            # imm = 0x180
addq $0x560, %rax            # imm = 0x560
movb -0x25(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
shlq $0x8, %rcx
addq %rcx, %rax
movzbl -0x31(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1da36
jmp 0x1dad9
jmp 0x1dadb
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1f, %eax
addl $0x1, %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x1d7d9
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)