// File: /workspace/github_c_makefile_star2_O3/lukexi[P]eglstreams-mini/src/egl.c
// Function: GetEglDevice
EGLDeviceEXT GetEglDevice(void)
{
    EGLint numDevices, i;
    EGLDeviceEXT *devices = NULL;
    EGLDeviceEXT device = EGL_NO_DEVICE_EXT;
    EGLBoolean ret;

    const char *clientExtensionString =
        eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);

    if (!ExtensionIsSupported(clientExtensionString,
                              "EGL_EXT_device_base") &&
        (!ExtensionIsSupported(clientExtensionString,
                               "EGL_EXT_device_enumeration") ||
         !ExtensionIsSupported(clientExtensionString,
                               "EGL_EXT_device_query"))) {
        Fatal("EGL_EXT_device base extensions not found.\n");
    }

    /* Query how many devices are present. */
    ret = pEglQueryDevicesEXT(0, NULL, &numDevices);

    if (!ret) {
        Fatal("Failed to query EGL devices.\n");
    }

    if (numDevices < 1) {
        Fatal("No EGL devices found.\n");
    }

    /* Allocate memory to store that many EGLDeviceEXTs. */
    devices = calloc(numDevices, sizeof(EGLDeviceEXT));

    if (devices == NULL) {
        Fatal("Memory allocation failure.\n");
    }

    /* Query the EGLDeviceEXTs. */
    ret = pEglQueryDevicesEXT(numDevices, devices, &numDevices);

    if (!ret) {
        Fatal("Failed to query EGL devices.\n");
    }

    /*
     * Select which EGLDeviceEXT to use.
     *
     * The EGL_EXT_device_query extension defines the functions:
     *
     *   eglQueryDeviceAttribEXT()
     *   eglQueryDeviceStringEXT()
     *
     * as ways to generically query properties of EGLDeviceEXTs, and
     * separate EGL extensions define EGLDeviceEXT attributes that can
     * be queried through those functions.  E.g.,
     *
     * - EGL_NV_device_cuda lets you query the CUDA device ID
     *   (EGL_CUDA_DEVICE_NV of an EGLDeviceEXT.
     *
     * - EGL_EXT_device_drm lets you query the DRM device file
     *   (EGL_DRM_DEVICE_FILE_EXT) of an EGLDeviceEXT.
     *
     * Future extensions could define other EGLDeviceEXT attributes
     * such as PCI BusID.
     *
     * For now, just choose the first device that supports EGL_EXT_device_drm.
     */

    for (i = 0; i < numDevices; i++) {

        const char *deviceExtensionString =
            pEglQueryDeviceStringEXT(devices[i], EGL_EXTENSIONS);

        if (ExtensionIsSupported(deviceExtensionString, "EGL_EXT_device_drm")) {
            device = devices[i];
            // break;
        }
    }

    free(devices);

    if (device == EGL_NO_DEVICE_EXT) {
        Fatal("No EGL_EXT_device_drm-capable EGL device found.\n");
    }

    return device;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
xorl %edi, %edi
movl $0x3055, %esi           # imm = 0x3055
callq 0x21f0
movq %rax, %rbx
leaq 0x25c6(%rip), %rsi      # 0x51c0
movq %rax, %rdi
callq 0x2a1c
testl %eax, %eax
jne 0x2c3a
leaq 0x25c7(%rip), %rsi      # 0x51d4
movq %rbx, %rdi
callq 0x2a1c
testl %eax, %eax
je 0x2c2c
leaq 0x25cf(%rip), %rsi      # 0x51ef
movq %rbx, %rdi
callq 0x2a1c
testl %eax, %eax
jne 0x2c3a
leaq 0x25d1(%rip), %rdi      # 0x5204
xorl %eax, %eax
callq 0x4184
leaq 0xc(%rsp), %rdx
xorl %edi, %edi
xorl %esi, %esi
callq *0x55e7(%rip)           # 0x8230
testl %eax, %eax
jne 0x2c5b
leaq 0x25db(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x4184
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
jg 0x2c75
leaq 0x25e3(%rip), %rdi      # 0x524d
xorl %eax, %eax
callq 0x4184
movl 0xc(%rsp), %ebp
movslq %ebp, %rdi
movl $0x8, %esi
callq 0x2370
movq %rax, %rbx
testq %rax, %rax
jne 0x2c9c
leaq 0x25d3(%rip), %rdi      # 0x5264
xorl %eax, %eax
callq 0x4184
movl 0xc(%rsp), %ebp
leaq 0xc(%rsp), %rdx
movl %ebp, %edi
movq %rbx, %rsi
callq *0x5584(%rip)           # 0x8230
testl %eax, %eax
jne 0x2cbe
leaq 0x2578(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x4184
cmpl $0x0, 0xc(%rsp)
jle 0x2d10
xorl %r12d, %r12d
leaq 0x25b1(%rip), %r15      # 0x5280
xorl %r14d, %r14d
movq (%rbx,%r12,8), %rdi
movl $0x3055, %esi           # imm = 0x3055
callq *0x5557(%rip)           # 0x8238
movq %rax, %rdi
movq %r15, %rsi
callq 0x2a1c
testl %eax, %eax
je 0x2cf4
movq (%rbx,%r12,8), %r14
incq %r12
movslq 0xc(%rsp), %rax
cmpq %rax, %r12
jl 0x2cd2
movq %rbx, %rdi
callq 0x2210
testq %r14, %r14
jne 0x2d29
jmp 0x2d18
movq %rbx, %rdi
callq 0x2210
leaq 0x2574(%rip), %rdi      # 0x5293
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x4184
movq %r14, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq