// File: /workspace/github_c_makefile_star2_O0/lukexi[P]eglstreams-mini/src/kms.c
// Function: SetDisplayModes
kms_plane* SetDisplayModes(int drmFd, int *NumPlanes) {

    const uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;

    int ret;
    ret = drmSetClientCap(drmFd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
    if (ret != 0) {
        Fatal("DRM_CLIENT_CAP_UNIVERSAL_PLANES not available.\n");
    }

    ret = drmSetClientCap(drmFd, DRM_CLIENT_CAP_ATOMIC, 1);
    if (ret != 0) {
        Fatal("DRM_CLIENT_CAP_ATOMIC not available.\n");
    }


    drmModeResPtr pModeRes = drmModeGetResources(drmFd);
    if (pModeRes == NULL) {
        Fatal("Unable to query DRM-KMS resources.\n");
    }

    kms_plane* Planes = malloc(sizeof(kms_plane) * pModeRes->count_connectors);
    printf("Num connectors: %i\n", pModeRes->count_connectors);
    int PlaneIndex = 0;
    for (int connIndex = 0; connIndex < pModeRes->count_connectors; connIndex++) {
        struct Config config = { 0 };

        if (!PickConfig(drmFd, pModeRes, connIndex, &config)) {
            continue;
        }

        uint32_t modeID = CreateModeID(drmFd, &config);
        uint32_t fb     = CreateFb(drmFd, &config);

        drmModeAtomicReqPtr pAtomic = drmModeAtomicAlloc();

        AssignAtomicRequest(drmFd, pAtomic, &config, modeID, fb);

        ret = drmModeAtomicCommit(drmFd, pAtomic, flags, NULL /* user_data */);

        drmModeAtomicFree(pAtomic);

        if (ret != 0) {
            Fatal("Failed to set mode.\n");
        }

        Planes[PlaneIndex].PlaneID = config.planeID;
        Planes[PlaneIndex].Width = config.width;
        Planes[PlaneIndex].Height = config.height;
        Planes[PlaneIndex].EDID = config.edid;

        PlaneIndex++;
    }
    drmModeFreeResources(pModeRes);

    *NumPlanes = PlaneIndex;

    return Planes;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x400, -0x14(%rbp)     # imm = 0x400
movl -0x4(%rbp), %edi
movl $0x2, %esi
movl $0x1, %edx
callq 0x2280
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x3e52
leaq 0x2f2e(%rip), %rdi      # 0x6d79
movb $0x0, %al
callq 0x4cf0
movl -0x4(%rbp), %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x2280
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0x3e7b
leaq 0x2f35(%rip), %rdi      # 0x6da9
movb $0x0, %al
callq 0x4cf0
movl -0x4(%rbp), %edi
callq 0x2360
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3e9c
leaq 0x2f3a(%rip), %rdi      # 0x6dcf
movb $0x0, %al
callq 0x4cf0
movq -0x20(%rbp), %rax
movslq 0x20(%rax), %rax
imulq $0x18, %rax, %rdi
callq 0x2120
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x20(%rax), %esi
leaq 0x2f34(%rip), %rdi      # 0x6df3
movb $0x0, %al
callq 0x2050
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x401b
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2080
movl -0x4(%rbp), %edi
movq -0x20(%rbp), %rsi
movl -0x30(%rbp), %edx
leaq -0x98(%rbp), %rcx
callq 0x4040
testb $0x1, %al
jne 0x3f16
jmp 0x400d
movl -0x4(%rbp), %edi
leaq -0x98(%rbp), %rsi
callq 0x40b0
movl %eax, -0x9c(%rbp)
movl -0x4(%rbp), %edi
leaq -0x98(%rbp), %rsi
callq 0x4100
movl %eax, -0xa0(%rbp)
callq 0x2070
movq %rax, -0xa8(%rbp)
movl -0x4(%rbp), %edi
movq -0xa8(%rbp), %rsi
movl -0x9c(%rbp), %ecx
movl -0xa0(%rbp), %r8d
leaq -0x98(%rbp), %rdx
callq 0x4250
movl -0x4(%rbp), %edi
movq -0xa8(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %eax, %eax
movl %eax, %ecx
callq 0x2260
movl %eax, -0x18(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x2310
cmpl $0x0, -0x18(%rbp)
je 0x3faa
leaq 0x2e64(%rip), %rdi      # 0x6e07
movb $0x0, %al
callq 0x4cf0
movl -0x8c(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movzwl -0x38(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movzwl -0x36(%rbp), %ecx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x3ed4
movq -0x20(%rbp), %rdi
callq 0x2100
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)