// File: /workspace/github_c_makefile_star2_O2/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 0x2609(%rip), %rsi      # 0x51c0
movq %rax, %rdi
callq 0x29e0
testl %eax, %eax
jne 0x2bf7
leaq 0x260a(%rip), %rsi      # 0x51d4
movq %rbx, %rdi
callq 0x29e0
testl %eax, %eax
je 0x2be9
leaq 0x2612(%rip), %rsi      # 0x51ef
movq %rbx, %rdi
callq 0x29e0
testl %eax, %eax
jne 0x2bf7
leaq 0x2614(%rip), %rdi      # 0x5204
xorl %eax, %eax
callq 0x40d8
leaq 0xc(%rsp), %rdx
xorl %edi, %edi
xorl %esi, %esi
callq *0x562a(%rip)           # 0x8230
testl %eax, %eax
jne 0x2c18
leaq 0x261e(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x40d8
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
jg 0x2c32
leaq 0x2626(%rip), %rdi      # 0x524d
xorl %eax, %eax
callq 0x40d8
movl 0xc(%rsp), %ebp
movslq %ebp, %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, %rbx
testq %rax, %rax
jne 0x2c57
leaq 0x2618(%rip), %rdi      # 0x5264
xorl %eax, %eax
callq 0x40d8
movl 0xc(%rsp), %ebp
leaq 0xc(%rsp), %rdx
movl %ebp, %edi
movq %rbx, %rsi
callq *0x55c9(%rip)           # 0x8230
testl %eax, %eax
jne 0x2c79
leaq 0x25bd(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x40d8
xorl %r12d, %r12d
leaq 0x25fd(%rip), %r15      # 0x5280
xorl %r14d, %r14d
movslq 0xc(%rsp), %rax
cmpq %rax, %r12
jge 0x2cb7
movq (%rbx,%r12,8), %rdi
movl $0x3055, %esi           # imm = 0x3055
callq *0x5599(%rip)           # 0x8238
movq %rax, %rdi
movq %r15, %rsi
callq 0x29e0
testl %eax, %eax
je 0x2cb2
movq (%rbx,%r12,8), %r14
incq %r12
jmp 0x2c86
movq %rbx, %rdi
callq 0x2210
testq %r14, %r14
jne 0x2cd2
leaq 0x25c8(%rip), %rdi      # 0x5293
xorl %eax, %eax
callq 0x40d8
movq %r14, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq