// File: /workspace/github_c_makefile_star2_O2/lukexi[P]eglstreams-mini/acquire-thread-per-display.c
// Function: main
int main() {
    GetTime();

    egl_state* EGL = SetupEGL();
    EnableGLDebug();



    fps MainLoopFPS = MakeFPS("Main Loop");
    fps* DisplayFPS = calloc(EGL->DisplaysCount, sizeof(fps));
    for (int DisplayIndex = 0; DisplayIndex < EGL->DisplaysCount; DisplayIndex++) {
        egl_display* Display = &EGL->Displays[DisplayIndex];

        pthread_t AcquireThread;
        pthread_create(&AcquireThread, NULL, AcquireThreadMain, Display);

        DisplayFPS[DisplayIndex] = MakeFPS(Display->EDID->MonitorName);
    }

    while (1) {

        EGLUpdateVSync(EGL);

        for (int DisplayIndex = 0; DisplayIndex < EGL->DisplaysCount; DisplayIndex++) {
            egl_display* Display = &EGL->Displays[DisplayIndex];

            if (Display->PageFlipPending) {
                continue;
            }

            eglMakeCurrent(Display->DisplayDevice,
                Display->Surface, Display->Surface,
                Display->Context);

            PrintDisplayLayerSwapInterval(Display);

            glViewport(0, 0,
                (GLint)Display->Width,
                (GLint)Display->Height);

            glClearColor(
                        (sin(GetTime()*3)/2+0.5) * 0.8,
                        (sin(GetTime()*5)/2+0.5) * 0.8,
                        (sin(GetTime()*7)/2+0.5) * 0.8,
                        1);
            glClear(GL_COLOR_BUFFER_BIT);

            NEWTIME(eglSwapBuffers);
            eglSwapBuffers(Display->DisplayDevice, Display->Surface);
            ENDTIME(eglSwapBuffers);

            TickFPS(&DisplayFPS[DisplayIndex]);
        }



        GLCheck("Display Thread");

        TickFPS(&MainLoopFPS);
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x41c3
xorl %eax, %eax
callq 0x33f4
movq %rax, %rbx
xorl %eax, %eax
callq 0x4304
leaq 0x2a7c(%rip), %rdi      # 0x5049
callq 0x4222
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movl 0x8(%rbx), %r14d
movslq %r14d, %rdi
pushq $0x10
popq %rsi
callq 0x2370
movq %rax, 0x8(%rsp)
movq %rax, %r13
addq $0x8, %r13
leaq 0x20(%rsp), %r15
xorl %ebp, %ebp
movslq %r14d, %rax
cmpq %rax, %rbp
jge 0x2643
movq (%rbx), %r14
leaq (%r14,%r12), %rcx
movq %r15, %rdi
xorl %esi, %esi
leaq -0xed(%rip), %rdx       # 0x252c
callq 0x21e0
movq (%r14,%r12), %rax
movq (%rax), %rdi
callq 0x4222
movq %rax, -0x8(%r13)
movq %rdx, (%r13)
incq %rbp
movl 0x8(%rbx), %r14d
addq $0x10, %r13
addq $0x58, %r12
jmp 0x25fe
movq %rbx, %rdi
callq 0x3018
xorl %r13d, %r13d
movq 0x8(%rsp), %r14
xorl %r15d, %r15d
movslq 0x8(%rbx), %rax
cmpq %rax, %r15
jge 0x27d7
movq (%rbx), %r12
cmpb $0x0, 0x50(%r12,%r13)
jne 0x27c7
leaq (%r12,%r13), %rbp
movq 0x30(%r12,%r13), %rdi
movq 0x20(%r12,%r13), %rdx
movq 0x28(%r12,%r13), %rcx
movq %rdx, %rsi
callq 0x21b0
movq %rbp, %rdi
xorl %eax, %eax
callq 0x2fdb
movl 0x8(%r12,%r13), %edx
movl 0xc(%r12,%r13), %ecx
xorl %edi, %edi
xorl %esi, %esi
callq 0x23b0
xorl %eax, %eax
callq 0x41c3
mulss 0x294f(%rip), %xmm0     # 0x5008
cvtss2sd %xmm0, %xmm0
callq 0x2400
movsd 0x294e(%rip), %xmm1     # 0x5018
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd 0x2946(%rip), %xmm1     # 0x5020
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsp)
xorl %eax, %eax
callq 0x41c3
mulss 0x2916(%rip), %xmm0     # 0x500c
cvtss2sd %xmm0, %xmm0
callq 0x2400
movsd 0x2911(%rip), %xmm1     # 0x5018
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
mulsd 0x2909(%rip), %xmm0     # 0x5020
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
xorl %eax, %eax
callq 0x41c3
mulss 0x28e0(%rip), %xmm0     # 0x5010
cvtss2sd %xmm0, %xmm0
callq 0x2400
movsd 0x28d7(%rip), %xmm1     # 0x5018
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
mulsd 0x28cf(%rip), %xmm0     # 0x5020
cvtsd2ss %xmm0, %xmm2
movss (%rsp), %xmm0
movss 0x4(%rsp), %xmm1
movss 0x28ac(%rip), %xmm3     # 0x5014
callq 0x2180
movl $0x4000, %edi           # imm = 0x4000
callq 0x20c0
xorl %eax, %eax
callq 0x41c3
movss %xmm0, (%rsp)
movq 0x20(%r12,%r13), %rsi
movq 0x30(%r12,%r13), %rdi
callq 0x2270
xorl %eax, %eax
callq 0x41c3
subss (%rsp), %xmm0
mulss 0x285e(%rip), %xmm0     # 0x5004
cvtss2sd %xmm0, %xmm0
leaq 0x2877(%rip), %rdi      # 0x5028
leaq 0x289b(%rip), %rsi      # 0x5053
movb $0x1, %al
callq 0x2050
movq %r14, %rdi
callq 0x424d
incq %r15
addq $0x10, %r14
addq $0x58, %r13
jmp 0x2656
leaq 0x2884(%rip), %rdi      # 0x5062
callq 0x4296
leaq 0x10(%rsp), %rdi
callq 0x424d
jmp 0x2643
nop