// File: rpavlik[P]edid-decode/edid-decode.cpp
// Function: extract_edid(int, _IO_FILE*)
static bool extract_edid(int fd, FILE *error)
{
	std::vector<char> edid_data;
	char buf[EDID_PAGE_SIZE];

	for (;;) {
		ssize_t i = read(fd, buf, sizeof(buf));

		if (i < 0)
			return false;
		if (i == 0)
			break;
		edid_data.insert(edid_data.end(), buf, buf + i);
	}

	if (edid_data.empty()) {
		state.edid_size = 0;
		return false;
	}

	const char *data = &edid_data[0];
	const char *start;

	/* Look for edid-decode output */
	start = strstr(data, "EDID (hex):");
	if (!start)
		start = strstr(data, "edid-decode (hex):");
	if (start)
		return extract_edid_hex(strchr(start, ':'));

	/* Look for C-array */
	start = strstr(data, "unsigned char edid[] = {");
	if (start)
		return extract_edid_hex(strchr(start, '{') + 1, false);

	/* Look for QuantumData EDID output */
	start = strstr(data, "<BLOCK");
	if (start)
		return extract_edid_quantumdata(start);

	/* Look for xrandr --verbose output (lines of 16 hex bytes) */
	start = strstr(data, "EDID_DATA:");
	if (!start)
		start = strstr(data, "EDID:");
	if (start)
		return extract_edid_xrandr(start);

	/* Look for an EDID in an Xorg.0.log file */
	start = strstr(data, "EDID (in hex):");
	if (start)
		start = strstr(start, "(II)");
	if (start)
		return extract_edid_xorg(start);

	unsigned i;

	/* Is the EDID provided in hex? */
	for (i = 0; i < 32 && (isspace(data[i]) || strchr(ignore_chars, data[i]) ||
			       tolower(data[i]) == 'x' || isxdigit(data[i])); i++);

	if (i == 32)
		return extract_edid_hex(data);

	/* Assume binary */
	if (edid_data.size() > sizeof(edid)) {
		fprintf(error, "Binary EDID length %zu is greater than %zu.\n",
			edid_data.size(), sizeof(edid));
		return false;
	}
	memcpy(edid, data, edid_data.size());
	state.edid_size = edid_data.size();
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcaf0
movl -0x8(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movl $0x80, %edx
callq 0x4460
movq %rax, -0x100(%rbp)
jmp 0xa258
movq -0x100(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jge 0xa2a3
movb $0x0, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc0(%rbp)
movl %eax, -0xc4(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcca0
jmp 0xa75e
cmpq $0x0, -0xb8(%rbp)
jne 0xa2af
jmp 0xa31e
leaq -0x28(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0xcbb0
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xcbe0
movq -0x110(%rbp), %rdi
movq -0xb8(%rbp), %rax
leaq -0xb0(%rbp,%rax), %rcx
movq -0xd0(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0xcb10
movq %rax, -0x108(%rbp)
jmp 0xa30b
movq -0x108(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0xa23b
leaq -0x28(%rbp), %rdi
callq 0xcc20
testb $0x1, %al
jne 0xa32d
jmp 0xa34a
movl $0x0, 0x41819(%rip)     # 0x4bb50
movb $0x0, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcc60
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
leaq 0x26f91(%rip), %rsi     # 0x312fd
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0xa39c
movq -0xe8(%rbp), %rdi
leaq 0x26f79(%rip), %rsi     # 0x31309
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa3e6
movq -0xf0(%rbp), %rdi
movl $0x3a, %esi
callq 0x4120
movq %rax, %rdi
movl $0x1, %esi
callq 0xa770
movb %al, -0x111(%rbp)
jmp 0xa3cc
movb -0x111(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq -0xe8(%rbp), %rdi
leaq 0x26f28(%rip), %rsi     # 0x3131c
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa44a
movq -0xf0(%rbp), %rdi
movl $0x7b, %esi
callq 0x4120
movq %rax, %rdi
incq %rdi
xorl %esi, %esi
callq 0xa770
movb %al, -0x112(%rbp)
jmp 0xa430
movb -0x112(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq -0xe8(%rbp), %rdi
leaq 0x26edd(%rip), %rsi     # 0x31335
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa49c
movq -0xf0(%rbp), %rdi
callq 0xa8d0
movb %al, -0x113(%rbp)
jmp 0xa482
movb -0x113(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq -0xe8(%rbp), %rdi
leaq 0x26e92(%rip), %rsi     # 0x3133c
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0xa4da
movq -0xe8(%rbp), %rdi
leaq 0x26e79(%rip), %rsi     # 0x31347
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa512
movq -0xf0(%rbp), %rdi
callq 0xa9c0
movb %al, -0x114(%rbp)
jmp 0xa4f8
movb -0x114(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq -0xe8(%rbp), %rdi
leaq 0x26e2d(%rip), %rsi     # 0x3134d
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa550
movq -0xf0(%rbp), %rdi
leaq 0x26e18(%rip), %rsi     # 0x3135c
callq 0x40b0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xa588
movq -0xf0(%rbp), %rdi
callq 0xab30
movb %al, -0x115(%rbp)
jmp 0xa56e
movb -0x115(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movl $0x0, -0xf4(%rbp)
xorl %eax, %eax
cmpl $0x20, -0xf4(%rbp)
movb %al, -0x116(%rbp)
jae 0xa64d
movq -0xe8(%rbp), %rax
movl -0xf4(%rbp), %ecx
movsbl (%rax,%rcx), %edi
callq 0x4060
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x117(%rbp)
jne 0xa641
movq 0x410ad(%rip), %rdi     # 0x4b680
movq -0xe8(%rbp), %rax
movl -0xf4(%rbp), %ecx
movsbl (%rax,%rcx), %esi
callq 0x4120
movq %rax, %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, -0x117(%rbp)
jne 0xa641
movq -0xe8(%rbp), %rax
movl -0xf4(%rbp), %ecx
movsbl (%rax,%rcx), %edi
callq 0x4470
movl %eax, %ecx
movb $0x1, %al
cmpl $0x78, %ecx
movb %al, -0x117(%rbp)
je 0xa641
movq -0xe8(%rbp), %rax
movl -0xf4(%rbp), %ecx
movsbl (%rax,%rcx), %edi
callq 0x40c0
cmpl $0x0, %eax
setne %al
movb %al, -0x117(%rbp)
movb -0x117(%rbp), %al
movb %al, -0x116(%rbp)
movb -0x116(%rbp), %al
testb $0x1, %al
jne 0xa659
jmp 0xa66f
jmp 0xa65b
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0xa592
cmpl $0x20, -0xf4(%rbp)
jne 0xa6ab
movq -0xe8(%rbp), %rdi
movl $0x1, %esi
callq 0xa770
movb %al, -0x118(%rbp)
jmp 0xa691
movb -0x118(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
leaq -0x28(%rbp), %rdi
callq 0xcc80
cmpq $0x8000, %rax           # imm = 0x8000
jbe 0xa6fd
movq -0x10(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcc80
movq -0x120(%rbp), %rdi
movq %rax, %rdx
leaq 0x26c80(%rip), %rsi     # 0x31361
movl $0x8000, %ecx           # imm = 0x8000
movb $0x0, %al
callq 0x4450
movb $0x0, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
jmp 0xa747
movq -0xe8(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcc80
movq -0x128(%rbp), %rsi
movq %rax, %rdx
leaq 0x45efb(%rip), %rdi     # 0x50620
callq 0x42a0
leaq -0x28(%rbp), %rdi
callq 0xcc80
movl %eax, 0x41417(%rip)     # 0x4bb50
movb $0x1, -0x1(%rbp)
movl $0x1, -0xc8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xcca0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0xc0(%rbp), %rdi
callq 0x4550
nopw (%rax,%rax)