// File: /workspace/github_c_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, 0xe0(%rsp)
movl %edi, %ebp
movl $0x2, %esi
movl $0x1, %edx
callq 0x22a0
testl %eax, %eax
je 0x3688
leaq 0x26ef(%rip), %rdi      # 0x5d70
xorl %eax, %eax
callq 0x4184
movl $0x3, %esi
movl $0x1, %edx
movl %ebp, %edi
callq 0x22a0
testl %eax, %eax
je 0x36ab
leaq 0x26fc(%rip), %rdi      # 0x5da0
xorl %eax, %eax
callq 0x4184
movl %ebp, %edi
callq 0x2380
movq %rax, %r15
testq %rax, %rax
jne 0x36c8
leaq 0x2705(%rip), %rdi      # 0x5dc6
xorl %eax, %eax
callq 0x4184
movslq 0x20(%r15), %rbx
movq %rbx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdi
callq 0x2140
movq %rax, 0xb0(%rsp)
leaq 0x26ff(%rip), %rdi      # 0x5dea
movq $0x0, 0x38(%rsp)
movl %ebx, %esi
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x20(%r15)
jle 0x4029
xorl %r12d, %r12d
movq $0x0, 0x38(%rsp)
movq %r15, 0xb8(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x40(%rsp)
movq $0x0, 0xa0(%rsp)
movq 0x28(%r15), %rax
movl (%rax,%r12,4), %esi
movl %ebp, %edi
callq 0x2060
movq %rax, %r14
testq %rax, %rax
jne 0x377e
leaq 0x269f(%rip), %rdi      # 0x5e13
movl %r12d, %esi
xorl %eax, %eax
callq 0x4184
cmpl $0x1, 0x10(%r14)
jne 0x3969
cmpl $0x0, 0x20(%r14)
jle 0x3969
cmpl $0x0, 0x48(%r14)
jle 0x3969
movq 0x50(%r14), %rax
movl (%rax), %esi
movl %ebp, %edi
callq 0x2310
movq %rax, 0xe8(%rsp)
testq %rax, %rax
jne 0x37cd
movq 0x50(%r14), %rax
movl (%rax), %esi
leaq 0x2689(%rip), %rdi      # 0x5e4f
xorl %eax, %eax
callq 0x4184
movq 0x28(%r15), %rax
movl (%rax,%r12,4), %eax
movl %eax, 0x40(%rsp)
movq 0x28(%r14), %rsi
movl $0x11, %ecx
leaq 0x50(%rsp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movq 0x28(%r15), %rax
movq %r12, 0xa8(%rsp)
movl (%rax,%r12,4), %esi
movl %ebp, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
callq 0x2190
movq %rax, %r13
cmpl $0x0, (%rax)
je 0x405a
xorl %ebx, %ebx
movq 0x8(%r13), %rax
movl (%rax,%rbx,4), %esi
movl %ebp, %edi
callq 0x2170
movq %rax, %r15
testq %rax, %rax
jne 0x3837
leaq 0x26cf(%rip), %rdi      # 0x5eff
xorl %eax, %eax
callq 0x4184
testb $0x10, 0x4(%r15)
je 0x3855
movq %r15, %rsi
addq $0x8, %rsi
leaq 0x263a(%rip), %rdi      # 0x5e86
callq 0x2350
testl %eax, %eax
je 0x386e
movq %r15, %rdi
callq 0x2260
incq %rbx
movl (%r13), %eax
cmpq %rax, %rbx
jb 0x3813
jmp 0x405a
movq 0x10(%r13), %rax
movl (%rax,%rbx,8), %esi
movl %ebp, %edi
callq 0x22c0
movq %rax, %r12
movq %r15, %rdi
callq 0x2260
movq %r13, %rdi
callq 0x2150
movl $0x1, %edi
movl $0x20, %esi
callq 0x2370
movq %rax, %rbx
movq 0x8(%r12), %rsi
movl 0x4(%r12), %edx
movq %rax, %rdi
callq 0x2850
movl %eax, %r15d
movq %r12, %rdi
callq 0x22d0
movq %rbx, 0x98(%rsp)
testl %r15d, %r15d
jne 0x38e4
movq 0x18(%rbx), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
leaq 0x25ae(%rip), %rdi      # 0x5e8b
xorl %eax, %eax
callq 0x2050
movl 0x40(%rsp), %esi
leaq 0x25c2(%rip), %rdi      # 0x5eb1
xorl %eax, %eax
callq 0x2050
movq 0xb8(%rsp), %r15
movl 0x10(%r15), %eax
testl %eax, %eax
movq 0xa8(%rsp), %r12
movq 0xe8(%rsp), %r13
jle 0x394c
xorl %ebx, %ebx
cmpq %rbx, %r12
ja 0x3926
movl 0xc(%r13), %ecx
btl %ebx, %ecx
jb 0x392f
incq %rbx
cmpl %ebx, %eax
jne 0x3918
jmp 0x394c
movq 0x18(%r15), %rax
movl (%rax,%rbx,4), %esi
movl %esi, 0x44(%rsp)
leaq 0x2588(%rip), %rdi      # 0x5ec9
xorl %eax, %eax
callq 0x2050
movl %ebx, 0x48(%rsp)
cmpl $0x0, 0x44(%rsp)
jne 0x3961
leaq 0x2582(%rip), %rdi      # 0x5edc
xorl %eax, %eax
callq 0x4184
movq %r13, %rdi
callq 0x23d0
movq %r14, %rdi
callq 0x20a0
cmpl $0x0, 0x40(%rsp)
je 0x4006
cmpl $0x0, 0x44(%rsp)
je 0x4006
movq %r12, 0xa8(%rsp)
movl %ebp, %edi
callq 0x22f0
movq %rax, %r14
testq %rax, %rax
jne 0x39ac
leaq 0x259e(%rip), %rdi      # 0x5f43
xorl %eax, %eax
callq 0x4184
cmpl $0x0, (%r14)
je 0x3ab9
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %esi
movl %ebp, %edi
callq 0x2090
movq %rax, %rbx
testq %rax, %rax
jne 0x39e1
leaq 0x2595(%rip), %rdi      # 0x5f6c
movl %r13d, %esi
xorl %eax, %eax
callq 0x4184
movl 0x2c(%rbx), %r15d
movq %rbx, %rdi
callq 0x2220
movl 0x48(%rsp), %eax
btl %eax, %r15d
jae 0x3aaa
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %esi
movl %ebp, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
callq 0x2190
movq %rax, %r15
cmpl $0x0, (%rax)
je 0x3a69
xorl %r12d, %r12d
movq 0x8(%r15), %rax
movl (%rax,%r12,4), %esi
movl %ebp, %edi
callq 0x2170
movq %rax, %rbx
testq %rax, %rax
jne 0x3a3f
leaq 0x24c7(%rip), %rdi      # 0x5eff
xorl %eax, %eax
callq 0x4184
movq %rbx, %rsi
addq $0x8, %rsi
leaq 0x2541(%rip), %rdi      # 0x5f8e
callq 0x2350
testl %eax, %eax
je 0x3a88
movq %rbx, %rdi
callq 0x2260
incq %r12
movl (%r15), %eax
cmpq %rax, %r12
jb 0x3a1a
movq %r15, %rdi
callq 0x2150
leaq 0x24a2(%rip), %rdi      # 0x5f1a
leaq 0x250f(%rip), %rsi      # 0x5f8e
xorl %eax, %eax
callq 0x4184
jmp 0x3aaa
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %r12
movq %rbx, %rdi
callq 0x2260
movq %r15, %rdi
callq 0x2150
cmpq $0x1, %r12
je 0x4018
incq %r13
movl (%r14), %eax
cmpq %rax, %r13
jb 0x39b9
movq %r14, %rdi
callq 0x2240
movl 0x4c(%rsp), %esi
leaq 0x24c7(%rip), %rdi      # 0x5f93
xorl %eax, %eax
callq 0x2050
cmpl $0x0, 0x4c(%rsp)
jne 0x3ae8
leaq 0x24c0(%rip), %rdi      # 0x5fa1
xorl %eax, %eax
callq 0x4184
movzwl 0x54(%rsp), %eax
movzwl 0x5e(%rsp), %ecx
movw %ax, 0xa0(%rsp)
movw %cx, 0xa2(%rsp)
movl $0x0, 0xf0(%rsp)
movl $0x44, %edx
movl %ebp, %edi
leaq 0x50(%rsp), %rsi
leaq 0xf0(%rsp), %rcx
callq 0x20d0
testl %eax, %eax
movq 0xb8(%rsp), %r15
je 0x3b40
leaq 0x248a(%rip), %rdi      # 0x5fc3
xorl %eax, %eax
callq 0x4184
movl 0xf0(%rsp), %r13d
leaq 0xf8(%rsp), %rax
pxor %xmm0, %xmm0
movdqu %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movdqa %xmm0, (%rsp)
movl $0x0, 0xc0(%rsp)
movzwl 0xa0(%rsp), %eax
movl %eax, 0xf4(%rsp)
movzwl 0xa2(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl $0x20, 0xf8(%rsp)
movl $0xc01064b3, %eax       # imm = 0xC01064B3
leaq 0xfffff(%rax), %rsi
movl %ebp, %edi
leaq 0xf0(%rsp), %rdx
callq 0x2030
testl %eax, %eax
jns 0x3bc6
leaq 0x2425(%rip), %rdi      # 0x5fe4
xorl %eax, %eax
callq 0x4184
movzwl 0xa0(%rsp), %esi
movzwl 0xa2(%rsp), %edx
movl 0x100(%rsp), %eax
movl 0x104(%rsp), %r9d
movl %ebp, %edi
movl $0x18, %ecx
movl $0x20, %r8d
leaq 0xc0(%rsp), %r10
pushq %r10
pushq %rax
callq 0x2360
addq $0x10, %rsp
testl %eax, %eax
je 0x3c18
leaq 0x23f2(%rip), %rdi      # 0x6003
xorl %eax, %eax
callq 0x4184
movl 0x100(%rsp), %eax
movl %eax, (%rsp)
movl $0xc01064b3, %esi       # imm = 0xC01064B3
movl %ebp, %edi
movq %rsp, %rdx
callq 0x2030
testl %eax, %eax
je 0x3c43
leaq 0x23da(%rip), %rdi      # 0x6016
xorl %eax, %eax
callq 0x4184
movq 0x108(%rsp), %rsi
movq 0x8(%rsp), %r9
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %ebp, %r8d
callq 0x23f0
movq %rax, %rbx
cmpq $-0x1, %rax
jne 0x3c7b
leaq 0x23be(%rip), %rdi      # 0x6032
xorl %eax, %eax
callq 0x4184
movq 0x108(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2080
movl 0xc0(%rsp), %ebx
callq 0x2070
movq %rax, %r14
pxor %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
movdqa %xmm0, (%rsp)
movl $0x0, 0x30(%rsp)
leaq 0x2389(%rip), %rax      # 0x6049
movq %rax, 0xc0(%rsp)
movq %rsp, %rax
movq %rax, 0xc8(%rsp)
leaq 0x2377(%rip), %rax      # 0x6051
movq %rax, 0xd0(%rsp)
leaq 0x4(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x2362(%rip), %rax      # 0x6058
movq %rax, 0xf0(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x234c(%rip), %rax      # 0x605e
movq %rax, 0x100(%rsp)
leaq 0xc(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x2336(%rip), %rax      # 0x6064
movq %rax, 0x110(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x118(%rsp)
leaq 0x2320(%rip), %rax      # 0x606a
movq %rax, 0x120(%rsp)
leaq 0x14(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x230a(%rip), %rax      # 0x6070
movq %rax, 0x130(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x22f5(%rip), %rax      # 0x6077
movq %rax, 0x140(%rsp)
leaq 0x1c(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x22e0(%rip), %rax      # 0x607e
movq %rax, 0x150(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x22cb(%rip), %rax      # 0x6085
movq %rax, 0x160(%rsp)
leaq 0x24(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x22b6(%rip), %rax      # 0x608c
movq %rax, 0x170(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x178(%rsp)
leaq 0x22a0(%rip), %rcx      # 0x6092
movq %rcx, 0x180(%rsp)
leaq 0x2c(%rsp), %rax
movq %rax, 0x188(%rsp)
movq %rcx, 0x190(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x198(%rsp)
movl 0x44(%rsp), %esi
movl $0x2, %r8d
movl %ebp, %edi
movl $0xcccccccc, %edx       # imm = 0xCCCCCCCC
leaq 0xc0(%rsp), %rcx
callq 0x4062
movl 0x4c(%rsp), %esi
movl $0xa, %r8d
movl %ebp, %edi
movl $0xeeeeeeee, %edx       # imm = 0xEEEEEEEE
leaq 0xf0(%rsp), %rcx
callq 0x4062
movl 0x40(%rsp), %esi
movl $0x1, %r8d
movl %ebp, %edi
movl $0xc0c0c0c0, %edx       # imm = 0xC0C0C0C0
leaq 0x190(%rsp), %rcx
callq 0x4062
movl 0x44(%rsp), %esi
movl (%rsp), %edx
movq %r14, %rdi
movq %r13, %rcx
callq 0x2340
movl 0x44(%rsp), %esi
movl 0x4(%rsp), %edx
movl $0x1, %ecx
movq %r14, %rdi
callq 0x2340
movl 0x30(%rsp), %edx
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x8(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0xc(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x10(%rsp), %edx
movl 0x4c(%rsp), %esi
movl 0xa0(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x14(%rsp), %edx
movzwl 0xa2(%rsp), %eax
shll $0x10, %eax
movslq %eax, %rcx
movq %r14, %rdi
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x18(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x1c(%rsp), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x20(%rsp), %edx
movzwl 0xa0(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x24(%rsp), %edx
movzwl 0xa2(%rsp), %ecx
movq %r14, %rdi
callq 0x2340
movl 0x4c(%rsp), %esi
movl 0x28(%rsp), %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x2340
movl 0x44(%rsp), %ecx
movl 0x4c(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r14, %rdi
callq 0x2340
movl %ebp, %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 0x3fb8
leaq 0x1e4d(%rip), %rdi      # 0x5dfe
xorl %eax, %eax
callq 0x4184
movl 0x4c(%rsp), %eax
movq 0x38(%rsp), %rsi
movslq %esi, %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0xb0(%rsp), %rdx
movl %eax, (%rdx,%rcx,8)
movdqa 0xa0(%rsp), %xmm0
punpcklwd 0x1d7c(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1],xmm0[2],mem[2],xmm0[3],mem[3]
movq %xmm0, 0x4(%rdx,%rcx,8)
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rdx,%rcx,8)
incl %esi
movq %rsi, 0x38(%rsp)
movq 0xa8(%rsp), %r12
incq %r12
movslq 0x20(%r15), %rax
cmpq %rax, %r12
jl 0x371c
jmp 0x4029
movq 0x8(%r14), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0x4c(%rsp)
jmp 0x3ab9
movq %r15, %rdi
callq 0x2120
movq 0xe0(%rsp), %rax
movq 0x38(%rsp), %rcx
movl %ecx, (%rax)
movq 0xb0(%rsp), %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x2440