// File: /workspace/github_cpp_makefile_star2_O3/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:
movzbl 0x2(%rsi), %eax
cmpl $0x4, %eax
jb 0x14d45
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movb 0x1(%rsi), %bpl
movq %rax, 0x10(%rsp)
cmpb $0x6d, %al
ja 0x14aeb
movq 0x10(%rsp), %r14
leaq (%rbx,%r14), %r15
movq %r15, %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x5ba5
testb %al, %al
jne 0x14aeb
cmpb $0x0, (%r15)
jne 0x14ad6
cmpb $0x0, 0x1(%rbx,%r14)
je 0x14ade
incl 0x180(%r12)
leaq 0x12(%r14), %rax
cmpq $0x5c, %r14
movq %rax, %r14
jb 0x14ab1
cmpb $0x3, %bpl
setb %al
movq 0x10(%rsp), %rsi
cmpl $0x4, %esi
sete %cl
orb %al, %cl
jne 0x14d37
leaq 0x8e0(%r12), %rax
movq %rax, 0x28(%rsp)
leaq 0x6e0(%r12), %rax
movq %rax, 0x20(%rsp)
leaq 0x2(%rbx), %rax
movq %rax, 0x18(%rsp)
movl $0x4, %eax
movq %r12, 0x30(%rsp)
movl %eax, %ebp
leaq (%rbx,%rbp), %r15
movzbl (%rbx,%rbp), %eax
movl %eax, %ecx
shrl $0x5, %ecx
cmpl $0x2, %ecx
je 0x14bb8
cmpl $0x7, %ecx
je 0x14b8c
cmpl $0x3, %ecx
jne 0x14d26
movzwl 0x2(%r15), %ecx
shll $0x8, %ecx
movzbl 0x1(%r15), %edx
orl %ecx, %edx
cmpl $0xc03, %edx            # imm = 0xC03
jne 0x14d26
movb $0x1, 0x2c3(%r12)
movzwl 0x4(%r15), %eax
rolw $0x8, %ax
movw %ax, 0x2d0(%r12)
jmp 0x14bb0
movb 0x1(%r15), %cl
cmpb $0xd, %cl
jne 0x14ba2
movb $0x1, 0x2c5(%r12)
movb 0x1(%r15), %cl
cmpb $0x13, %cl
jne 0x14be7
movzbl 0x2(%r15), %eax
testb $0x40, %al
jne 0x14bc5
movb (%r15), %al
jmp 0x14d26
movl $0x1, %ecx
xorl %r13d, %r13d
jmp 0x14c96
andl $0x1f, %eax
incl %eax
movl %eax, 0x2c8(%r12)
movb 0x2(%r15), %al
shrb $0x5, %al
andb $0x1, %al
movb %al, 0x2cd(%r12)
movb 0x1(%r15), %cl
cmpb $0x14, %cl
jne 0x14c30
movzbl (%r15), %eax
andl $0x1f, %eax
decl %eax
movb $0x1, 0x2ce(%r12)
cmpl $0x2, %eax
jb 0x14c2c
movq 0x18(%rsp), %rcx
addq %rbp, %rcx
cmpl $0x5, %eax
jb 0x14c15
testb $0x40, (%rcx)
jne 0x14c23
addl $-0x2, %eax
addq $0x2, %rcx
cmpl $0x1, %eax
ja 0x14c0b
jmp 0x14c2c
movb $0x1, 0x2cc(%r12)
movb 0x1(%r15), %cl
cmpb $0x22, %cl
jne 0x14c41
incl 0x1a0(%r12)
movb 0x1(%r15), %cl
cmpb $0x23, %cl
jne 0x14c53
movb $0x1, 0x1d8(%r12)
movb 0x1(%r15), %cl
movb (%r15), %al
cmpb $0x32, %cl
jne 0x14c82
andb $0x1f, %al
addb $-0x2, %al
movb 0x2(%r15), %cl
shrb $0x4, %cl
andb $0x7, %cl
addb $0x6, %cl
cbtw
idivb %cl
movzbl %al, %eax
addl %eax, 0x1a0(%r12)
movb (%r15), %al
movb 0x1(%r15), %cl
cmpb $0xe, %cl
jne 0x14d26
movl $0x2, %ecx
movl $0x1, %r13d
movl %eax, %edx
andb $0x1f, %dl
movzbl %dl, %edx
cmpl %edx, %ecx
ja 0x14d26
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x28(%rsp), %rdx
leaq (%rdx,%rax,8), %r12
movl %ecx, %r14d
decq %r14
shll $0x8, %r13d
addq 0x20(%rsp), %r13
movzbl 0x1(%r15,%r14), %ecx
movl %ecx, %eax
andb $0x7f, %al
cmpb $0x41, %al
movzbl %al, %eax
cmovael %ecx, %eax
movb %al, 0xf(%rsp)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x14cf4
movb %al, (%rsi)
incq 0x8(%r12)
jmp 0x14d01
movq %r12, %rdi
leaq 0xf(%rsp), %rdx
callq 0x15f1a
movzbl 0xf(%rsp), %eax
movb $0x1, (%rax,%r13)
movzbl (%r15), %eax
movl %eax, %ecx
andl $0x1f, %ecx
incq %r14
cmpq %rcx, %r14
jb 0x14cc9
movq 0x30(%rsp), %r12
movq 0x10(%rsp), %rsi
andb $0x1f, %al
movzbl %al, %eax
addl %ebp, %eax
incl %eax
cmpl %esi, %eax
jb 0x14b32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq