// File: rpavlik[P]edid-decode/edid-decode.cpp
// Function: edid_from_file(char const*, _IO_FILE*)
static int edid_from_file(const char *from_file, FILE *error)
{
#ifdef O_BINARY
	// Windows compatibility
	int flags = O_RDONLY | O_BINARY;
#else
	int flags = O_RDONLY;
#endif
	int fd;

	if (!strcmp(from_file, "-")) {
		from_file = "stdin";
		fd = 0;
	} else if ((fd = open(from_file, flags)) == -1) {
		perror(from_file);
		return -1;
	}

	odd_hex_digits = false;
	if (!extract_edid(fd, error)) {
		if (!state.edid_size) {
			fprintf(error, "EDID of '%s' was empty.\n", from_file);
			return -1;
		}
		fprintf(error, "EDID extract of '%s' failed: ", from_file);
		if (odd_hex_digits)
			fprintf(error, "odd number of hexadecimal digits.\n");
		else
			fprintf(error, "unknown format.\n");
		return -1;
	}
	if (state.edid_size % EDID_PAGE_SIZE) {
		fprintf(error, "EDID length %u is not a multiple of %u.\n",
			state.edid_size, EDID_PAGE_SIZE);
		return -1;
	}
	state.num_blocks = state.edid_size / EDID_PAGE_SIZE;
	if (fd != 0)
		close(fd);

	if (memcmp(edid, "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", 8)) {
		fprintf(error, "No EDID header found in '%s'.\n", from_file);
		return -1;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x2a56d(%rip), %rsi     # 0x3418f
callq 0x4440
cmpl $0x0, %eax
jne 0x9c40
leaq 0x27611(%rip), %rax     # 0x31244
movq %rax, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
jmp 0x9c6d
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movb $0x0, %al
callq 0x4230
movl %eax, -0x20(%rbp)
cmpl $-0x1, %eax
jne 0x9c6b
movq -0x10(%rbp), %rdi
callq 0x42b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9d96
jmp 0x9c6d
movb $0x0, 0x4e9ac(%rip)     # 0x58620
movl -0x20(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0xa220
testb $0x1, %al
jne 0x9d00
cmpl $0x0, 0x41ec5(%rip)     # 0x4bb50
jne 0x9caf
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x275ae(%rip), %rsi     # 0x3124a
movb $0x0, %al
callq 0x4450
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9d96
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x275a5(%rip), %rsi     # 0x31263
movb $0x0, %al
callq 0x4450
testb $0x1, 0x4e954(%rip)     # 0x58620
je 0x9ce2
movq -0x18(%rbp), %rdi
leaq 0x275a8(%rip), %rsi     # 0x31281
movb $0x0, %al
callq 0x4450
jmp 0x9cf4
movq -0x18(%rbp), %rdi
leaq 0x275b7(%rip), %rsi     # 0x312a4
movb $0x0, %al
callq 0x4450
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9d96
movl 0x41e4a(%rip), %eax     # 0x4bb50
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x9d34
movq -0x18(%rbp), %rdi
movl 0x41e38(%rip), %edx     # 0x4bb50
leaq 0x27596(%rip), %rsi     # 0x312b5
movl $0x80, %ecx
movb $0x0, %al
callq 0x4450
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9d96
movl 0x41e16(%rip), %eax     # 0x4bb50
shrl $0x7, %eax
movl %eax, 0x41e11(%rip)     # 0x4bb54
cmpl $0x0, -0x20(%rbp)
je 0x9d51
movl -0x20(%rbp), %edi
callq 0x45b0
movq 0x468c8(%rip), %rax     # 0x50620
movabsq $0xffffffffffff00, %rcx # imm = 0xFFFFFFFFFFFF00
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x9d8f
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x2755f(%rip), %rsi     # 0x312de
movb $0x0, %al
callq 0x4450
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9d96
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nop