// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x10(%rsp)
movb 0x1(%rsi), %bl
movq %rsi, 0x20(%rsp)
movzbl 0x2(%rsi), %eax
movl %eax, 0x1c(%rsp)
cmpl $0x4, %eax
jb 0x111ed
movl 0x1c(%rsp), %r14d
pushq $0x12
popq %rbp
cmpq $0x6d, %r14
ja 0x111ed
movq 0x20(%rsp), %rax
leaq (%rax,%r14), %r15
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x4fa5
testb %al, %al
jne 0x111ed
cmpb $0x0, (%r15)
jne 0x111dc
movq 0x20(%rsp), %rax
cmpb $0x0, 0x1(%rax,%r14)
je 0x111e7
movq 0x10(%rsp), %rax
incl 0x180(%rax)
addq $0x12, %r14
jmp 0x111aa
cmpb $0x3, %bl
jae 0x11201
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
leaq 0x8e0(%rax), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x6e0(%rax), %rbp
pushq $0x4
popq %rdx
leaq 0xf(%rsp), %r15
cmpl 0x1c(%rsp), %edx
jae 0x111f2
movl %edx, %eax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %r14
movzbl (%rcx,%rax), %eax
shrl $0x5, %eax
cmpl $0x2, %eax
movq %rdx, 0x30(%rsp)
je 0x11371
cmpl $0x7, %eax
je 0x11291
cmpl $0x3, %eax
jne 0x1135c
movzwl 0x2(%r14), %eax
shll $0x8, %eax
movzbl 0x1(%r14), %ecx
orl %eax, %ecx
cmpl $0xc03, %ecx            # imm = 0xC03
jne 0x1135c
movq 0x10(%rsp), %rcx
movb $0x1, 0x2c3(%rcx)
movzwl 0x4(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2d0(%rcx)
jmp 0x1135c
movb 0x1(%r14), %al
cmpb $0xd, %al
jne 0x112a9
movq 0x10(%rsp), %rax
movb $0x1, 0x2c5(%rax)
movb 0x1(%r14), %al
cmpb $0x13, %al
jne 0x112dd
movzbl 0x2(%r14), %eax
testb $0x40, %al
je 0x1135c
andl $0x1f, %eax
incl %eax
movq 0x10(%rsp), %rcx
movl %eax, 0x2c8(%rcx)
movb 0x2(%r14), %al
shrb $0x5, %al
andb $0x1, %al
movb %al, 0x2cd(%rcx)
movb 0x1(%r14), %al
cmpb $0x14, %al
jne 0x112fc
leaq 0x2(%r14), %rsi
movzbl (%r14), %edx
andl $0x1f, %edx
decl %edx
movq 0x10(%rsp), %rdi
callq 0xdb28
movb 0x1(%r14), %al
cmpb $0x22, %al
jne 0x1130f
movq 0x10(%rsp), %rax
incl 0x1a0(%rax)
movb 0x1(%r14), %al
cmpb $0x23, %al
jne 0x11323
movq 0x10(%rsp), %rax
movb $0x1, 0x1d8(%rax)
movb 0x1(%r14), %al
cmpb $0x32, %al
jne 0x11351
movb (%r14), %al
movb 0x2(%r14), %cl
andb $0x1f, %al
addb $-0x2, %al
shrb $0x4, %cl
andb $0x7, %cl
addb $0x6, %cl
cbtw
idivb %cl
movzbl %al, %eax
movq 0x10(%rsp), %rcx
addl %eax, 0x1a0(%rcx)
movb 0x1(%r14), %al
pushq $0x1
popq %rbx
pushq $0x2
popq %r13
cmpb $0xe, %al
je 0x11377
movzbl (%r14), %eax
andl $0x1f, %eax
movq 0x30(%rsp), %rdx
addl %eax, %edx
incl %edx
jmp 0x11221
xorl %ebx, %ebx
pushq $0x1
popq %r13
imulq $0x18, %rbx, %r12
addq 0x28(%rsp), %r12
movzbl (%r14), %eax
andl $0x1f, %eax
cmpq %rax, %r13
ja 0x1135c
movzbl (%r14,%r13), %eax
movl %eax, %ecx
andb $0x7f, %cl
cmpb $0x41, %cl
movzbl %cl, %ecx
cmovael %eax, %ecx
movb %cl, 0xf(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x11efa
movzbl 0xf(%rsp), %eax
movl %ebx, %ecx
shll $0x8, %ecx
addq %rbp, %rcx
movb $0x1, (%rax,%rcx)
incq %r13
jmp 0x11380