// File: /workspace/github_c_makefile_star2_O0/lukexi[P]eglstreams-mini/src/kms.c
// Function: PickConnector
static bool PickConnector(int drmFd,
                          drmModeResPtr pModeRes,
                          int connIndex,
                          struct Config *pConfig) {

    drmModeConnectorPtr pConnector =
        drmModeGetConnector(drmFd, pModeRes->connectors[connIndex]);

    if (pConnector == NULL) {
        Fatal("Unable to query DRM-KMS information for "
              "connector index %d\n", connIndex);
    }

    if ((pConnector->connection == DRM_MODE_CONNECTED) &&
        (pConnector->count_modes > 0) &&
        (pConnector->count_encoders > 0)) {

        drmModeEncoderPtr pEncoder =
            drmModeGetEncoder(drmFd, pConnector->encoders[0]);

        if (pEncoder == NULL) {
            Fatal("Unable to query DRM-KMS information for"
                  "encoder 0x%08x\n", pConnector->encoders[0]);
        }

        pConfig->connectorID = pModeRes->connectors[connIndex];
        pConfig->mode = pConnector->modes[0];

        // Get the EDID data blob
        drmModePropertyBlobPtr edidBlobPtr = GetPropertyBlobValue(drmFd,
            pModeRes->connectors[connIndex],
            DRM_MODE_OBJECT_CONNECTOR,
            "EDID");

        // Parse the EDID blob into useful strings
        drm_edid* edid = calloc(1, sizeof(drm_edid));
        int rc = edid_parse(edid,
                edidBlobPtr->data,
                edidBlobPtr->length);
        // Free the blob; we've extracted what we needed.
        drmModeFreePropertyBlob(edidBlobPtr);

        pConfig->edid = edid;

        if (!rc) {
            printf("Using Display '%s' '%s' Serial# '%s'\n",
                   edid->PNPID,
                   edid->MonitorName,
                   edid->SerialNumber);
        }

        // for (int ModeIndex = 0; ModeIndex < pConnector->count_modes; ModeIndex++) {
        //     printf("Mode %i info: %s clock %i vrefresh %i \n",
        //         ModeIndex,
        //         pConnector->modes[ModeIndex].name,
        //         pConnector->modes[ModeIndex].clock,
        //         pConnector->modes[ModeIndex].vrefresh
        //     );
        // }



        printf("Using Connector ID: %i\n", pConfig->connectorID);
        // printf("Connector Index: %i\n", connIndex);
        // printf("Num CRTCS: %i\n", pModeRes->count_crtcs);
        // printf("Num Connectors: %i\n", pModeRes->count_connectors);
        for (int crtcIndex = 0; crtcIndex < pModeRes->count_crtcs; crtcIndex++) {
            if (crtcIndex < connIndex) {
                continue;
            }

            if ((pEncoder->possible_crtcs & (1 << crtcIndex)) == 0) {
                continue;
            }

            pConfig->crtcID = pModeRes->crtcs[crtcIndex];
            printf("Using CRTC ID: %i\n", pConfig->crtcID);
            pConfig->crtcIndex = crtcIndex;
            break;
        }

        if (pConfig->crtcID == 0) {
            Fatal("Unable to select a suitable CRTC.\n");
        }

        drmModeFreeEncoder(pEncoder);
    }

    drmModeFreeConnector(pConnector);

    return pConfig->connectorID != 0 && pConfig->crtcID != 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x2060
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x4439
movl -0x14(%rbp), %esi
leaq 0x29ea(%rip), %rdi      # 0x6e1c
movb $0x0, %al
callq 0x4cf0
movq -0x28(%rbp), %rax
cmpl $0x1, 0x10(%rax)
jne 0x460e
movq -0x28(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jle 0x460e
movq -0x28(%rbp), %rax
cmpl $0x0, 0x48(%rax)
jle 0x460e
movl -0x4(%rbp), %edi
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %esi
callq 0x22f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4498
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rax
movl (%rax), %esi
leaq 0x29c7(%rip), %rdi      # 0x6e58
movb $0x0, %al
callq 0x4cf0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
movl $0x44, %edx
callq 0x23c0
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %esi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
leaq 0x29aa(%rip), %rcx      # 0x6e8f
callq 0x4780
movq %rax, -0x38(%rbp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2350
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edx
callq 0x2900
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
callq 0x22b0
movq -0x40(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
cmpl $0x0, -0x44(%rbp)
jne 0x455e
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0x293d(%rip), %rdi      # 0x6e94
movb $0x0, %al
callq 0x2050
movq -0x20(%rbp), %rax
movl (%rax), %esi
leaq 0x294f(%rip), %rdi      # 0x6eba
movb $0x0, %al
callq 0x2050
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x45ed
movl -0x48(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x458f
jmp 0x45e2
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movl -0x48(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x45ab
jmp 0x45e2
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %esi
leaq 0x2903(%rip), %rdi      # 0x6ed2
movb $0x0, %al
callq 0x2050
movl -0x48(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x45ed
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4579
movq -0x20(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x4605
leaq 0x28e7(%rip), %rdi      # 0x6ee5
movb $0x0, %al
callq 0x4cf0
movq -0x30(%rbp), %rdi
callq 0x23b0
movq -0x28(%rbp), %rdi
callq 0x20a0
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, (%rcx)
movb %al, -0x49(%rbp)
je 0x4633
movq -0x20(%rbp), %rax
cmpl $0x0, 0x4(%rax)
setne %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
andb $0x1, %al
addq $0x50, %rsp
popq %rbp
retq
nop