// File: /workspace/github_c_makefile_star2_O2/lukexi[P]eglstreams-mini/src/edid.c
// Function: edid_parse
int edid_parse(drm_edid* edid, const uint8_t* data, size_t length) {
   char* tmp;
   int i;
   int rc = 0;
   uint32_t serial;

   /* check header */
   if (length < 128) {
       rc = -1;
       goto out;
   }
   if (data[0] != 0x00 || data[1] != 0xff) {
       rc = -1;
       goto out;
   }

   /* decode the PNP ID from three 5 bit words packed into 2 bytes
    * /--08--\/--09--\
    * 7654321076543210
    * |\---/\---/\---/
    * R  C1   C2   C3 */
   edid->PNPID = malloc(4);
   edid->PNPID[0] = 'A' + ((data[EDID_OFFSET_PNPID+0] & 0x7c) / 4) - 1;
   edid->PNPID[1] = 'A' + ((data[EDID_OFFSET_PNPID+0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID+1] & 0xe0) / 32) - 1;
   edid->PNPID[2] = 'A' + (data[EDID_OFFSET_PNPID+1] & 0x1f) - 1;
   edid->PNPID[3] = '\0';;

   /* maybe there isn't a ASCII serial number descriptor, so use this instead */
   serial = (int32_t) data[EDID_OFFSET_SERIAL+0];
   serial += (int32_t) data[EDID_OFFSET_SERIAL+1] * 0x100;
   serial += (int32_t) data[EDID_OFFSET_SERIAL+2] * 0x10000;
   serial += (int32_t) data[EDID_OFFSET_SERIAL+3] * 0x1000000;
   if (serial > 0) {
       edid->SerialNumber = malloc(9);
       sprintf (edid->SerialNumber, "%li", (long int) serial);
   }

   /* parse EDID data */
   for (i = EDID_OFFSET_DATA_BLOCKS;
        i <= EDID_OFFSET_LAST_BLOCK;
        i += 18) {
       /* ignore pixel clock data */
       if (data[i] != 0)
           continue;
       if (data[i+2] != 0)
           continue;

       /* any useful blocks? */
       if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
           tmp = edid_parse_string (&data[i+5]);
           if (tmp != NULL) {
               free (edid->MonitorName);
               edid->MonitorName = tmp;
           }
       } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
           tmp = edid_parse_string (&data[i+5]);
           if (tmp != NULL) {
               free (edid->SerialNumber);
               edid->SerialNumber = tmp;
           }
       } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
           tmp = edid_parse_string (&data[i+5]);
           if (tmp != NULL) {
               free (edid->EISAID);
               edid->EISAID = tmp;
           }
       }
   }
out:
   return rc;
}

// Assembly:
pushq $-0x1
popq %rax
cmpq $0x80, %rdx
jb 0x2965
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %rbx
cmpb $0x0, (%rsi)
jne 0x295d
cmpb $-0x1, 0x1(%rbx)
jne 0x295d
movq %rdi, %r14
pushq $0x4
popq %rdi
callq 0x2140
movq %rax, 0x18(%r14)
movb 0x8(%rbx), %cl
movl %ecx, %edx
shrb $0x2, %dl
andb $0x1f, %dl
orb $0x40, %dl
movb %dl, (%rax)
movb 0x9(%rbx), %dl
shlb $0x3, %cl
movl %edx, %esi
shrb $0x5, %sil
orb %cl, %sil
andb $0x1f, %sil
orb $0x40, %sil
movb %sil, 0x1(%rax)
andb $0x1f, %dl
orb $0x40, %dl
movb %dl, 0x2(%rax)
movb $0x0, 0x3(%rax)
movl 0xc(%rbx), %ebp
testl %ebp, %ebp
je 0x28bf
pushq $0x9
popq %rdi
callq 0x2140
movq %rax, 0x8(%r14)
movl %ebp, %edx
leaq 0x27bc(%rip), %rsi      # 0x5071
movq %rax, %rdi
xorl %eax, %eax
callq 0x22b0
addq $0x3b, %rbx
pushq $0x36
popq %r12
cmpq $0x6c, %r12
ja 0x295b
cmpb $0x0, -0x5(%rbx)
jne 0x28dd
cmpb $0x0, -0x3(%rbx)
je 0x28e7
addq $0x12, %r12
addq $0x12, %rbx
jmp 0x28c7
movzbl -0x2(%rbx), %eax
cmpl $0xff, %eax
je 0x293c
cmpl $0xfe, %eax
je 0x291d
cmpl $0xfc, %eax
jne 0x28dd
movq %rbx, %rdi
callq 0x2966
testq %rax, %rax
je 0x28dd
movq %rax, %r15
movq (%r14), %rdi
callq 0x2210
movq %r15, (%r14)
jmp 0x28dd
movq %rbx, %rdi
callq 0x2966
testq %rax, %rax
je 0x28dd
movq %rax, %r15
movq 0x10(%r14), %rdi
callq 0x2210
movq %r15, 0x10(%r14)
jmp 0x28dd
movq %rbx, %rdi
callq 0x2966
testq %rax, %rax
je 0x28dd
movq %rax, %r15
movq 0x8(%r14), %rdi
callq 0x2210
movq %r15, 0x8(%r14)
jmp 0x28dd
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq