// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, 0x118(%rsp)
movl %edi, %r15d
movl $0x2, %esi
movl $0x1, %edx
callq 0x22a0
testl %eax, %eax
je 0x3645
leaq 0x271e(%rip), %rdi      # 0x5d5c
xorl %eax, %eax
callq 0x41e8
movl $0x3, %esi
movl $0x1, %edx
movl %r15d, %edi
callq 0x22a0
testl %eax, %eax
je 0x3669
leaq 0x272a(%rip), %rdi      # 0x5d8c
xorl %eax, %eax
callq 0x41e8
movl %r15d, %edi
callq 0x2380
movq %rax, %rbx
testq %rax, %rax
jne 0x3687
leaq 0x2732(%rip), %rdi      # 0x5db2
xorl %eax, %eax
callq 0x41e8
movq %rbx, 0x10(%rsp)
movq 0x10(%rsp), %rax
movslq 0x20(%rax), %rbx
movq %rbx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x2140
movq %rax, 0xe8(%rsp)
leaq 0x2722(%rip), %rdi      # 0x5dd6
movq $0x0, 0x60(%rsp)
movl %ebx, %esi
movq 0x10(%rsp), %rbx
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x20(%rbx)
jle 0x4095
xorl %ecx, %ecx
movq $0x0, 0x60(%rsp)
movl %r15d, 0xc(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0xd0(%rsp)
movq 0x28(%rbx), %rax
movq %rcx, 0x68(%rsp)
movl (%rax,%rcx,4), %esi
movl %r15d, %edi
callq 0x2060
movq %rax, %r13
testq %rax, %rax
jne 0x3750
leaq 0x26bb(%rip), %rdi      # 0x5dff
movq 0x68(%rsp), %rsi
xorl %eax, %eax
callq 0x41e8
cmpl $0x1, 0x10(%r13)
jne 0x3988
cmpl $0x0, 0x20(%r13)
jle 0x3988
cmpl $0x0, 0x48(%r13)
jle 0x3988
movq 0x50(%r13), %rax
movl (%rax), %esi
movl %r15d, %edi
callq 0x2310
movq %rax, %r14
testq %rax, %rax
jne 0x379b
movq 0x50(%r13), %rax
movl (%rax), %esi
leaq 0x26a7(%rip), %rdi      # 0x5e3b
xorl %eax, %eax
callq 0x41e8
movq %r14, 0xe0(%rsp)
movq 0x28(%rbx), %rax
movq 0x68(%rsp), %rdx
movl (%rax,%rdx,4), %eax
movl %eax, 0x70(%rsp)
movq 0x28(%r13), %rsi
movl $0x11, %ecx
leaq 0x80(%rsp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movq 0x28(%rbx), %rax
movl (%rax,%rdx,4), %esi
movl %r15d, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
callq 0x2190
movq %rax, %rbx
cmpl $0x0, (%rax)
setne %r15b
je 0x3877
xorl %r12d, %r12d
movb $0x1, %r15b
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
movq 0x8(%rbx), %rax
movl (%rax,%r12,4), %esi
movl 0xc(%rsp), %edi
callq 0x2170
movq %rax, %r14
testq %rax, %rax
jne 0x3822
leaq 0x26d0(%rip), %rdi      # 0x5eeb
xorl %eax, %eax
callq 0x41e8
testb $0x10, 0x4(%r14)
je 0x385b
movq %r14, %rsi
addq $0x8, %rsi
leaq 0x263b(%rip), %rdi      # 0x5e72
callq 0x2350
testl %eax, %eax
jne 0x385b
movq 0x10(%rbx), %rax
movl (%rax,%r12,8), %esi
movl 0xc(%rsp), %edi
callq 0x22c0
movq %rax, 0x18(%rsp)
movl $0x1, %ebp
movq %r14, %rdi
callq 0x2260
testl %ebp, %ebp
jne 0x3880
incq %r12
movl (%rbx), %eax
cmpq %rax, %r12
setb %r15b
jb 0x37fb
jmp 0x3880
movq $0x0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x2150
testb $0x1, %r15b
jne 0x38a3
leaq 0x2671(%rip), %rdi      # 0x5f06
leaq 0x25d6(%rip), %rsi      # 0x5e72
xorl %eax, %eax
callq 0x41e8
movl $0x1, %edi
movl $0x20, %esi
callq 0x2370
movq %rax, %rbx
movq 0x18(%rsp), %r15
movq 0x8(%r15), %rsi
movl 0x4(%r15), %edx
movq %rax, %rdi
callq 0x282c
movl %eax, %r14d
movq %r15, %rdi
callq 0x22d0
movq %rbx, 0xc8(%rsp)
testl %r14d, %r14d
movl 0xc(%rsp), %r15d
jne 0x3900
movq 0x18(%rbx), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
leaq 0x257e(%rip), %rdi      # 0x5e77
xorl %eax, %eax
callq 0x2050
movl 0x70(%rsp), %esi
leaq 0x2592(%rip), %rdi      # 0x5e9d
xorl %eax, %eax
callq 0x2050
movq 0x10(%rsp), %rax
movl 0x10(%rax), %eax
testl %eax, %eax
movq 0x68(%rsp), %rdx
movq 0xe0(%rsp), %r14
jle 0x3966
xorl %ebx, %ebx
cmpq %rbx, %rdx
ja 0x393b
movl 0xc(%r14), %ecx
btl %ebx, %ecx
jb 0x3944
incq %rbx
cmpl %ebx, %eax
jne 0x392d
jmp 0x3966
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl (%rax,%rbx,4), %esi
movl %esi, 0x74(%rsp)
leaq 0x255a(%rip), %rdi      # 0x5eb5
xorl %eax, %eax
callq 0x2050
movl %ebx, 0x78(%rsp)
cmpl $0x0, 0x74(%rsp)
jne 0x397b
leaq 0x2554(%rip), %rdi      # 0x5ec8
xorl %eax, %eax
callq 0x41e8
movq %r14, %rdi
callq 0x23d0
movq 0x10(%rsp), %rbx
movq %r13, %rdi
callq 0x20a0
cmpl $0x0, 0x70(%rsp)
setne %al
cmpl $0x0, 0x74(%rsp)
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4080
movb %cl, 0xb(%rsp)
movl %r15d, %edi
callq 0x22f0
movq %rax, %r14
testq %rax, %rax
jne 0x39cd
leaq 0x2569(%rip), %rdi      # 0x5f2f
xorl %eax, %eax
callq 0x41e8
cmpl $0x0, (%r14)
je 0x3b1e
xorl %r13d, %r13d
movq %r14, 0xe0(%rsp)
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %esi
movl %r15d, %edi
callq 0x2090
movq %rax, %rbx
testq %rax, %rax
jne 0x3a0b
leaq 0x2557(%rip), %rdi      # 0x5f58
movl %r13d, %esi
xorl %eax, %eax
callq 0x41e8
movl 0x2c(%rbx), %ebp
movq %rbx, %rdi
callq 0x2220
movl 0x78(%rsp), %eax
btl %eax, %ebp
jae 0x3b01
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %esi
movl %r15d, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
callq 0x2190
movq %rax, %rbx
xorl %r12d, %r12d
cmpl $0x0, (%rax)
setne %bpl
je 0x3ad9
movb $0x1, %bpl
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq 0x8(%rbx), %rax
movl (%rax,%r14,4), %esi
movl %r15d, %edi
callq 0x2170
movq %rax, %r15
testq %rax, %rax
jne 0x3a80
leaq 0x2472(%rip), %rdi      # 0x5eeb
xorl %eax, %eax
callq 0x41e8
leaq 0x8(%r15), %rsi
leaq 0x24ef(%rip), %rdi      # 0x5f7a
callq 0x2350
testl %eax, %eax
jne 0x3aa7
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %r12d
movq %r15, %rdi
callq 0x2260
testl %r12d, %r12d
movl 0xc(%rsp), %r15d
jne 0x3ac7
incq %r14
movl (%rbx), %eax
cmpq %rax, %r14
setb %bpl
jb 0x3a5a
cmpq $0x1, 0x18(%rsp)
sete %r12b
movq 0xe0(%rsp), %r14
movq %rbx, %rdi
callq 0x2150
testb $0x1, %bpl
jne 0x3afc
leaq 0x2418(%rip), %rdi      # 0x5f06
leaq 0x2485(%rip), %rsi      # 0x5f7a
xorl %eax, %eax
callq 0x41e8
testb %r12b, %r12b
jne 0x3b12
incq %r13
movl (%r14), %eax
cmpq %rax, %r13
jb 0x39e2
jmp 0x3b1e
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x7c(%rsp)
movq %r14, %rdi
callq 0x2240
movl 0x7c(%rsp), %esi
leaq 0x244e(%rip), %rdi      # 0x5f7f
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x7c(%rsp)
jne 0x3b4d
leaq 0x2447(%rip), %rdi      # 0x5f8d
xorl %eax, %eax
callq 0x41e8
movzwl 0x84(%rsp), %eax
movzwl 0x8e(%rsp), %ecx
movw %ax, 0xd0(%rsp)
movw %cx, 0xd2(%rsp)
cmpb $0x0, 0xb(%rsp)
movq 0x10(%rsp), %rbx
je 0x4080
movl $0x0, 0x120(%rsp)
movl $0x44, %edx
movl %r15d, %edi
leaq 0x80(%rsp), %rsi
leaq 0x120(%rsp), %rcx
callq 0x20d0
testl %eax, %eax
je 0x3bb7
leaq 0x23ff(%rip), %rdi      # 0x5faf
xorl %eax, %eax
callq 0x41e8
movl 0x120(%rsp), %r13d
xorps %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0xf0(%rsp)
movzwl 0xd0(%rsp), %eax
movl %eax, 0x124(%rsp)
movzwl 0xd2(%rsp), %eax
movl %eax, 0x120(%rsp)
movl $0x20, 0x128(%rsp)
movl $0xc01064b3, %eax       # imm = 0xC01064B3
leaq 0xfffff(%rax), %rsi
movl %r15d, %edi
leaq 0x120(%rsp), %rdx
callq 0x2030
testl %eax, %eax
jns 0x3c39
leaq 0x239e(%rip), %rdi      # 0x5fd0
xorl %eax, %eax
callq 0x41e8
movzwl 0xd0(%rsp), %esi
movzwl 0xd2(%rsp), %edx
movl 0x130(%rsp), %eax
movl 0x134(%rsp), %r9d
movl %r15d, %edi
movl $0x18, %ecx
movl $0x20, %r8d
leaq 0xf0(%rsp), %r10
pushq %r10
pushq %rax
callq 0x2360
addq $0x10, %rsp
testl %eax, %eax
je 0x3c8c
leaq 0x236a(%rip), %rdi      # 0x5fef
xorl %eax, %eax
callq 0x41e8
movl 0x130(%rsp), %eax
movl %eax, 0x20(%rsp)
movl $0xc01064b3, %esi       # imm = 0xC01064B3
movl %r15d, %edi
leaq 0x20(%rsp), %rdx
callq 0x2030
testl %eax, %eax
je 0x3cbb
leaq 0x234e(%rip), %rdi      # 0x6002
xorl %eax, %eax
callq 0x41e8
movq 0x138(%rsp), %rsi
movq 0x28(%rsp), %r9
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r15d, %r8d
callq 0x23f0
movq %rax, %rbx
cmpq $-0x1, %rax
jne 0x3cf3
leaq 0x2332(%rip), %rdi      # 0x601e
xorl %eax, %eax
callq 0x41e8
movq 0x138(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2080
movl 0xf0(%rsp), %ebx
callq 0x2070
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0x50(%rsp)
leaq 0x2300(%rip), %rax      # 0x6035
movq %rax, 0xf0(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x22ec(%rip), %rax      # 0x603d
movq %rax, 0x100(%rsp)
leaq 0x24(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x22d7(%rip), %rax      # 0x6044
movq %rax, 0x120(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x22c1(%rip), %rax      # 0x604a
movq %rax, 0x130(%rsp)
leaq 0x2c(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x22ab(%rip), %rax      # 0x6050
movq %rax, 0x140(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x2295(%rip), %rax      # 0x6056
movq %rax, 0x150(%rsp)
leaq 0x34(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x227f(%rip), %rax      # 0x605c
movq %rax, 0x160(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x226a(%rip), %rax      # 0x6063
movq %rax, 0x170(%rsp)
leaq 0x3c(%rsp), %rax
movq %rax, 0x178(%rsp)
leaq 0x2255(%rip), %rax      # 0x606a
movq %rax, 0x180(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x188(%rsp)
leaq 0x2240(%rip), %rax      # 0x6071
movq %rax, 0x190(%rsp)
leaq 0x44(%rsp), %rax
movq %rax, 0x198(%rsp)
leaq 0x222b(%rip), %rax      # 0x6078
movq %rax, 0x1a0(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x1a8(%rsp)
leaq 0x2215(%rip), %rcx      # 0x607e
movq %rcx, 0x1b0(%rsp)
leaq 0x4c(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq %rcx, 0x1c0(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x1c8(%rsp)
movl 0x74(%rsp), %esi
movl $0x2, %r8d
movl %r15d, %edi
movl $0xcccccccc, %edx       # imm = 0xCCCCCCCC
leaq 0xf0(%rsp), %rcx
callq 0x40c6
movl 0x7c(%rsp), %esi
movl $0xa, %r8d
movl %r15d, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
leaq 0x120(%rsp), %rcx
callq 0x40c6
movl 0x70(%rsp), %esi
movl $0x1, %r8d
movl %r15d, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
leaq 0x1c0(%rsp), %rcx
callq 0x40c6
movl 0x74(%rsp), %esi
movl 0x20(%rsp), %edx
movq %r14, %rdi
movq %r13, %rcx
callq 0x2340
movl 0x74(%rsp), %esi
movl 0x24(%rsp), %edx
movl $0x1, %ecx
movq %r14, %rdi
callq 0x2340
movl 0x50(%rsp), %edx
movl 0x70(%rsp), %esi
movl 0x74(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x28(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x30(%rsp), %edx
movl 0x7c(%rsp), %esi
movl 0xd0(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x34(%rsp), %edx
movzwl 0xd2(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x38(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x3c(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x40(%rsp), %edx
movzwl 0xd0(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x44(%rsp), %edx
movzwl 0xd2(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x7c(%rsp), %esi
movl 0x48(%rsp), %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2340
movl 0x74(%rsp), %ecx
movl 0x7c(%rsp), %esi
movl 0x4c(%rsp), %edx
movq %r14, %rdi
callq 0x2340
movl %r15d, %edi
movq %r14, %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x2280
movl %eax, %ebx
movq %r14, %rdi
callq 0x2330
testl %ebx, %ebx
je 0x4034
leaq 0x1dbd(%rip), %rdi      # 0x5dea
xorl %eax, %eax
callq 0x41e8
movl 0x7c(%rsp), %eax
movq 0x60(%rsp), %rsi
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0xe8(%rsp), %rdx
movl %eax, (%rdx,%rcx,8)
movzwl 0xd0(%rsp), %eax
movl %eax, 0x4(%rdx,%rcx,8)
movzwl 0xd2(%rsp), %eax
movl %eax, 0x8(%rdx,%rcx,8)
movq 0xc8(%rsp), %rax
movq %rax, 0x10(%rdx,%rcx,8)
incl %esi
movq %rsi, 0x60(%rsp)
movq 0x10(%rsp), %rbx
movq 0x68(%rsp), %rcx
incq %rcx
movslq 0x20(%rbx), %rax
cmpq %rax, %rcx
jl 0x36e5
movq %rbx, %rdi
callq 0x2120
movq 0x118(%rsp), %rax
movq 0x60(%rsp), %rcx
movl %ecx, (%rax)
movq 0xe8(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq