// File: /workspace/github_c_makefile_star2_O2/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 $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, 0x108(%rsp)
movl %edi, %r15d
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x22a0
testl %eax, %eax
je 0x35f1
leaq 0x2786(%rip), %rdi      # 0x5d70
xorl %eax, %eax
callq 0x40d8
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movl %r15d, %edi
callq 0x22a0
testl %eax, %eax
je 0x3611
leaq 0x2796(%rip), %rdi      # 0x5da0
xorl %eax, %eax
callq 0x40d8
movl %r15d, %edi
callq 0x2380
testq %rax, %rax
movq %rax, 0x8(%rsp)
jne 0x3636
leaq 0x279c(%rip), %rdi      # 0x5dc6
xorl %eax, %eax
callq 0x40d8
movq 0x8(%rsp), %rax
movslq 0x20(%rax), %rbx
imulq $0x18, %rbx, %rdi
callq 0x2140
movq %rax, 0xd8(%rsp)
leaq 0x2798(%rip), %rdi      # 0x5dea
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
movq 0x8(%rsp), %rdi
xorl %ecx, %ecx
movl $0x0, 0x4(%rsp)
movl %r15d, (%rsp)
movslq 0x20(%rdi), %rax
cmpq %rax, %rcx
jge 0x3f9d
pxor %xmm0, %xmm0
movdqa %xmm0, 0xb0(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
andq $0x0, 0xc0(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movq 0x28(%rdi), %rax
movq %rcx, 0x50(%rsp)
movl (%rax,%rcx,4), %esi
movl %r15d, %edi
callq 0x2060
movq %rax, %r13
testq %rax, %rax
jne 0x36e7
leaq 0x2738(%rip), %rdi      # 0x5e13
movq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x40d8
cmpl $0x1, 0x10(%r13)
jne 0x38f5
cmpl $0x0, 0x20(%r13)
jle 0x38f5
cmpl $0x0, 0x48(%r13)
jle 0x38f5
movq 0x50(%r13), %rax
movl (%rax), %esi
movl %r15d, %edi
callq 0x2310
movq %rax, 0xd0(%rsp)
testq %rax, %rax
jne 0x3737
movq 0x50(%r13), %rax
movl (%rax), %esi
leaq 0x271f(%rip), %rdi      # 0x5e4f
xorl %eax, %eax
callq 0x40d8
movq 0x8(%rsp), %rdx
movq 0x28(%rdx), %rax
movq 0x50(%rsp), %r8
movl (%rax,%r8,4), %eax
movl %eax, 0x60(%rsp)
movq 0x28(%r13), %rsi
pushq $0x11
popq %rcx
leaq 0x70(%rsp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movq 0x28(%rdx), %rax
movl (%rax,%r8,4), %esi
movl %r15d, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
callq 0x2190
movq %rax, %r14
xorl %eax, %eax
movq $0x0, 0x58(%rsp)
movq %rax, %rbx
movl (%r14), %r15d
cmpq %r15, %rax
jae 0x37f7
movq 0x8(%r14), %rax
movl (%rax,%rbx,4), %esi
movl (%rsp), %edi
callq 0x2170
movq %rax, %r12
testq %rax, %rax
jne 0x37ae
leaq 0x2758(%rip), %rdi      # 0x5eff
xorl %eax, %eax
callq 0x40d8
movb $0x1, %bpl
testb $0x10, 0x4(%r12)
je 0x37e6
movq %r12, %rsi
addq $0x8, %rsi
leaq 0x26bf(%rip), %rdi      # 0x5e86
callq 0x2350
testl %eax, %eax
jne 0x37e6
movq 0x10(%r14), %rax
movl (%rax,%rbx,8), %esi
movl (%rsp), %edi
callq 0x22c0
movq %rax, 0x58(%rsp)
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x2260
leaq 0x1(%rbx), %rax
testb %bpl, %bpl
jne 0x377e
movq %r14, %rdi
callq 0x2150
cmpq %r15, %rbx
jb 0x3819
leaq 0x270f(%rip), %rdi      # 0x5f1a
leaq 0x2674(%rip), %rsi      # 0x5e86
xorl %eax, %eax
callq 0x40d8
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0x2370
movq %rax, %rbx
movq 0x58(%rsp), %r15
movq 0x8(%r15), %rsi
movl 0x4(%r15), %edx
movq %rax, %rdi
callq 0x2824
movl %eax, %r14d
movq %r15, %rdi
callq 0x22d0
movq %rbx, 0xb8(%rsp)
testl %r14d, %r14d
jne 0x386d
movq 0x18(%rbx), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
leaq 0x2625(%rip), %rdi      # 0x5e8b
xorl %eax, %eax
callq 0x2050
movl 0x60(%rsp), %esi
leaq 0x2639(%rip), %rdi      # 0x5eb1
xorl %eax, %eax
callq 0x2050
movq 0x8(%rsp), %rdx
movl 0x10(%rdx), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ebx, %ebx
movl (%rsp), %r15d
movq 0x50(%rsp), %rsi
movq 0xd0(%rsp), %r14
cmpl %ebx, %eax
je 0x38d8
cmpq %rbx, %rsi
ja 0x38b6
movl 0xc(%r14), %ecx
btl %ebx, %ecx
jb 0x38bb
incq %rbx
jmp 0x38a4
movq 0x18(%rdx), %rax
movl (%rax,%rbx,4), %esi
movl %esi, 0x64(%rsp)
leaq 0x25fc(%rip), %rdi      # 0x5ec9
xorl %eax, %eax
callq 0x2050
movl %ebx, 0x68(%rsp)
cmpl $0x0, 0x64(%rsp)
jne 0x38ed
leaq 0x25f6(%rip), %rdi      # 0x5edc
xorl %eax, %eax
callq 0x40d8
movq %r14, %rdi
callq 0x23d0
movq %r13, %rdi
callq 0x20a0
cmpl $0x0, 0x60(%rsp)
je 0x3f8b
cmpl $0x0, 0x64(%rsp)
je 0x3f8b
movl %r15d, %edi
callq 0x22f0
movq %rax, %r14
testq %rax, %rax
jne 0x3931
leaq 0x2619(%rip), %rdi      # 0x5f43
xorl %eax, %eax
callq 0x40d8
xorl %r12d, %r12d
movq %r14, 0xd0(%rsp)
movl (%r14), %eax
cmpq %rax, %r12
jae 0x3a5b
movq 0x8(%r14), %rax
movl (%rax,%r12,4), %esi
movl %r15d, %edi
callq 0x2090
movq %rax, %rbx
testq %rax, %rax
jne 0x3971
leaq 0x2605(%rip), %rdi      # 0x5f6c
movl %r12d, %esi
xorl %eax, %eax
callq 0x40d8
movl 0x2c(%rbx), %ebp
movq %rbx, %rdi
callq 0x2220
movl 0x68(%rsp), %eax
btl %eax, %ebp
jae 0x3a47
movq 0x8(%r14), %rax
movq %r12, 0x58(%rsp)
movl (%rax,%r12,4), %esi
movl %r15d, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
callq 0x2190
movq %rax, %r12
xorl %eax, %eax
xorl %ebp, %ebp
movq %rax, %r14
movl (%r12), %r13d
cmpq %r13, %rax
jae 0x3a12
movq 0x8(%r12), %rax
movl (%rax,%r14,4), %esi
movl %r15d, %edi
callq 0x2170
movq %rax, %rbx
testq %rax, %rax
jne 0x39dd
leaq 0x2529(%rip), %rdi      # 0x5eff
xorl %eax, %eax
callq 0x40d8
leaq 0x8(%rbx), %rsi
leaq 0x25a6(%rip), %rdi      # 0x5f8e
callq 0x2350
movl %eax, %r15d
testl %eax, %eax
jne 0x39fd
movq 0x10(%r12), %rax
movq (%rax,%r14,8), %rbp
movq %rbx, %rdi
callq 0x2260
leaq 0x1(%r14), %rax
testl %r15d, %r15d
movl (%rsp), %r15d
jne 0x39aa
movq %r12, %rdi
callq 0x2150
cmpq %r13, %r14
jb 0x3a34
leaq 0x24f4(%rip), %rdi      # 0x5f1a
leaq 0x2561(%rip), %rsi      # 0x5f8e
xorl %eax, %eax
callq 0x40d8
cmpq $0x1, %rbp
movq 0xd0(%rsp), %r14
movq 0x58(%rsp), %r12
je 0x3a4f
incq %r12
jmp 0x393c
movq 0x8(%r14), %rax
movl (%rax,%r12,4), %eax
movl %eax, 0x6c(%rsp)
movq %r14, %rdi
callq 0x2240
movl 0x6c(%rsp), %esi
leaq 0x2525(%rip), %rdi      # 0x5f93
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x6c(%rsp)
jne 0x3a8a
leaq 0x251e(%rip), %rdi      # 0x5fa1
xorl %eax, %eax
callq 0x40d8
movzwl 0x74(%rsp), %eax
movzwl 0x7e(%rsp), %ecx
movw %ax, 0xc0(%rsp)
movw %cx, 0xc2(%rsp)
andl $0x0, 0x110(%rsp)
movl %r15d, %edi
leaq 0x70(%rsp), %rsi
pushq $0x44
popq %rdx
leaq 0x110(%rsp), %rcx
callq 0x20d0
testl %eax, %eax
je 0x3ad6
leaq 0x24f4(%rip), %rdi      # 0x5fc3
xorl %eax, %eax
callq 0x40d8
movl 0x110(%rsp), %r12d
leaq 0x118(%rsp), %rax
andq $0x0, 0x10(%rax)
andl $0x0, 0xe0(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, (%rax)
movdqa %xmm0, 0x10(%rsp)
movzwl 0xc0(%rsp), %eax
movl %eax, 0x114(%rsp)
movzwl 0xc2(%rsp), %eax
movl %eax, 0x110(%rsp)
movl $0x20, 0x118(%rsp)
movl $0xc01064b3, %eax       # imm = 0xC01064B3
leaq 0xfffff(%rax), %rsi
movl %r15d, %edi
leaq 0x110(%rsp), %rdx
callq 0x2030
testl %eax, %eax
jns 0x3b58
leaq 0x2493(%rip), %rdi      # 0x5fe4
xorl %eax, %eax
callq 0x40d8
movzwl 0xc0(%rsp), %esi
movzwl 0xc2(%rsp), %edx
movl 0x120(%rsp), %eax
movl 0x124(%rsp), %r9d
movl %r15d, %edi
pushq $0x18
popq %rcx
pushq $0x20
popq %r8
leaq 0xe0(%rsp), %r10
pushq %r10
pushq %rax
callq 0x2360
addq $0x10, %rsp
testl %eax, %eax
je 0x3ba7
leaq 0x2463(%rip), %rdi      # 0x6003
xorl %eax, %eax
callq 0x40d8
movl 0x120(%rsp), %eax
movl %eax, 0x10(%rsp)
movl $0xc01064b3, %esi       # imm = 0xC01064B3
movl %r15d, %edi
leaq 0x10(%rsp), %rdx
callq 0x2030
testl %eax, %eax
je 0x3bd6
leaq 0x2447(%rip), %rdi      # 0x6016
xorl %eax, %eax
callq 0x40d8
movq 0x128(%rsp), %rsi
movq 0x18(%rsp), %r9
xorl %edi, %edi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl %r15d, %r8d
callq 0x23f0
movq %rax, %rbx
cmpq $-0x1, %rax
jne 0x3c0a
leaq 0x242f(%rip), %rdi      # 0x6032
xorl %eax, %eax
callq 0x40d8
movq 0x128(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2080
movl 0xe0(%rsp), %r13d
callq 0x2070
movq %rax, %r14
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
andl $0x0, 0x40(%rsp)
movdqa %xmm0, 0x10(%rsp)
leaq 0x23fb(%rip), %rax      # 0x6049
movq %rax, 0xe0(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x23e7(%rip), %rax      # 0x6051
movq %rax, 0xf0(%rsp)
leaq 0x14(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x23d2(%rip), %rax      # 0x6058
movq %rax, 0x110(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x23bc(%rip), %rax      # 0x605e
movq %rax, 0x120(%rsp)
leaq 0x1c(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x23a6(%rip), %rax      # 0x6064
movq %rax, 0x130(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x2390(%rip), %rax      # 0x606a
movq %rax, 0x140(%rsp)
leaq 0x24(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x237a(%rip), %rax      # 0x6070
movq %rax, 0x150(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x2365(%rip), %rax      # 0x6077
movq %rax, 0x160(%rsp)
leaq 0x2c(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x2350(%rip), %rax      # 0x607e
movq %rax, 0x170(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x178(%rsp)
leaq 0x233b(%rip), %rax      # 0x6085
movq %rax, 0x180(%rsp)
leaq 0x34(%rsp), %rax
movq %rax, 0x188(%rsp)
leaq 0x2326(%rip), %rax      # 0x608c
movq %rax, 0x190(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0x198(%rsp)
leaq 0x2310(%rip), %rcx      # 0x6092
movq %rcx, 0x1a0(%rsp)
leaq 0x3c(%rsp), %rax
movq %rax, 0x1a8(%rsp)
movq %rcx, 0x1b0(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movl 0x64(%rsp), %esi
movl %r15d, %edi
movl $0xcccccccc, %edx       # imm = 0xCCCCCCCC
leaq 0xe0(%rsp), %rcx
pushq $0x2
popq %r8
callq 0x3fca
movl 0x6c(%rsp), %esi
movl %r15d, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
leaq 0x110(%rsp), %rcx
pushq $0xa
popq %r8
callq 0x3fca
movl 0x60(%rsp), %esi
movl %r15d, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
leaq 0x1b0(%rsp), %rcx
pushq $0x1
popq %rbx
movq %rbx, %r8
callq 0x3fca
movl 0x64(%rsp), %esi
movl 0x10(%rsp), %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x2340
movl 0x64(%rsp), %esi
movl 0x14(%rsp), %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2340
movl 0x40(%rsp), %edx
movl 0x60(%rsp), %esi
movl 0x64(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x18(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x1c(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x20(%rsp), %edx
movl 0x6c(%rsp), %esi
movl 0xc0(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x24(%rsp), %edx
movzwl 0xc2(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x28(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x30(%rsp), %edx
movzwl 0xc0(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x34(%rsp), %edx
movzwl 0xc2(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x6c(%rsp), %esi
movl 0x38(%rsp), %edx
movq %r14, %rdi
movq %r13, %rcx
callq 0x2340
movl 0x64(%rsp), %ecx
movl 0x6c(%rsp), %esi
movl 0x3c(%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 0x3f47
leaq 0x1ebe(%rip), %rdi      # 0x5dfe
xorl %eax, %eax
callq 0x40d8
movl 0x6c(%rsp), %eax
movl 0x4(%rsp), %esi
movslq %esi, %rcx
imulq $0x18, %rcx, %rcx
movq 0xd8(%rsp), %rdx
movl %eax, (%rdx,%rcx)
movdqa 0xc0(%rsp), %xmm0
punpcklwd 0x1dee(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1],xmm0[2],mem[2],xmm0[3],mem[3]
movq %xmm0, 0x4(%rdx,%rcx)
movq 0xb8(%rsp), %rax
movq %rax, 0x10(%rdx,%rcx)
incl %esi
movl %esi, 0x4(%rsp)
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %rdi
jmp 0x366e
callq 0x2120
movq 0x108(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0xd8(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq