// File: rpavlik[P]edid-decode/parse-base-block.cpp
// Function: extract_string(unsigned char const*, unsigned int)
char *extract_string(const unsigned char *x, unsigned len)
{
	static char s[EDID_PAGE_SIZE];
	int seen_newline = 0;
	unsigned i;

	memset(s, 0, sizeof(s));

	for (i = 0; i < len; i++) {
		if (isgraph(x[i])) {
			s[i] = x[i];
		} else if (!seen_newline) {
			if (x[i] == 0x0a) {
				seen_newline = 1;
				if (!i)
					fail("Empty string.\n");
				else if (s[i - 1] == 0x20)
					fail("One or more trailing spaces.\n");
			} else if (x[i] == 0x20) {
				s[i] = x[i];
			} else {
				fail("Non-printable character.\n");
				return s;
			}
		} else if (x[i] != 0x20) {
			fail("Non-space after newline.\n");
			return s;
		}
	}
	/* Does the string end with a space? */
	if (!seen_newline && s[len - 1] == 0x20)
		fail("One or more trailing spaces.\n");

	return s;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
leaq 0x490d3(%rip), %rdi     # 0x58630
xorl %esi, %esi
movl $0x80, %edx
callq 0x41c0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0xf6a6
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %edi
callq 0x42f0
cmpl $0x0, %eax
je 0xf5af
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movb (%rax,%rcx), %dl
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x49089(%rip), %rax     # 0x58630
movb %dl, (%rax,%rcx)
jmp 0xf696
cmpl $0x0, -0x18(%rbp)
jne 0xf665
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xf616
movl $0x1, -0x18(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0xf5e8
xorl %edi, %edi
leaq 0x24462(%rip), %rsi     # 0x33a41
movb $0x0, %al
callq 0x47d0
jmp 0xf614
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x49037(%rip), %rax     # 0x58630
movsbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jne 0xf612
xorl %edi, %edi
leaq 0x24445(%rip), %rsi     # 0x33a50
movb $0x0, %al
callq 0x47d0
jmp 0xf614
jmp 0xf663
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jne 0xf641
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movb (%rax,%rcx), %dl
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x48ff4(%rip), %rax     # 0x58630
movb %dl, (%rax,%rcx)
jmp 0xf661
xorl %edi, %edi
leaq 0x24424(%rip), %rsi     # 0x33a6e
movb $0x0, %al
callq 0x47d0
leaq 0x48fd8(%rip), %rax     # 0x58630
movq %rax, -0x8(%rbp)
jmp 0xf6e1
jmp 0xf663
jmp 0xf694
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x20, %eax
je 0xf692
xorl %edi, %edi
leaq 0x2440a(%rip), %rsi     # 0x33a88
movb $0x0, %al
callq 0x47d0
leaq 0x48fa4(%rip), %rax     # 0x58630
movq %rax, -0x8(%rbp)
jmp 0xf6e1
jmp 0xf694
jmp 0xf696
jmp 0xf698
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf570
cmpl $0x0, -0x18(%rbp)
jne 0xf6d6
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x48f73(%rip), %rax     # 0x58630
movsbl (%rax,%rcx), %eax
cmpl $0x20, %eax
jne 0xf6d6
xorl %edi, %edi
leaq 0x24381(%rip), %rsi     # 0x33a50
movb $0x0, %al
callq 0x47d0
leaq 0x48f53(%rip), %rax     # 0x58630
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)