// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
xorl %edi, %edi
movl $0x3055, %esi           # imm = 0x3055
callq 0x21f0
movq %rax, %rbx
leaq 0x25e1(%rip), %rsi      # 0x51c0
movq %rax, %rdi
callq 0x29fc
testl %eax, %eax
jne 0x2c1f
leaq 0x25e2(%rip), %rsi      # 0x51d4
movq %rbx, %rdi
callq 0x29fc
testl %eax, %eax
je 0x2c11
leaq 0x25ea(%rip), %rsi      # 0x51ef
movq %rbx, %rdi
callq 0x29fc
testl %eax, %eax
jne 0x2c1f
leaq 0x25ec(%rip), %rdi      # 0x5204
xorl %eax, %eax
callq 0x41e8
leaq 0x4(%rsp), %rdx
xorl %edi, %edi
xorl %esi, %esi
callq *0x5602(%rip)           # 0x8230
testl %eax, %eax
jne 0x2c40
leaq 0x25f6(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x41e8
cmpl $0x0, 0x4(%rsp)
jg 0x2c55
leaq 0x25ff(%rip), %rdi      # 0x524d
xorl %eax, %eax
callq 0x41e8
movslq 0x4(%rsp), %rdi
movl $0x8, %esi
callq 0x2370
movq %rax, %rbx
testq %rax, %rax
jne 0x2c7a
leaq 0x25f1(%rip), %rdi      # 0x5264
xorl %eax, %eax
callq 0x41e8
leaq 0x4(%rsp), %rdx
movl (%rdx), %edi
movq %rbx, %rsi
callq *0x55a6(%rip)           # 0x8230
testl %eax, %eax
jne 0x2c9c
leaq 0x259a(%rip), %rdi      # 0x522f
xorl %eax, %eax
callq 0x41e8
cmpl $0x0, 0x4(%rsp)
jle 0x2ce1
xorl %r12d, %r12d
leaq 0x25d3(%rip), %r15      # 0x5280
xorl %r14d, %r14d
movq (%rbx,%r12,8), %rdi
movl $0x3055, %esi           # imm = 0x3055
callq *0x5579(%rip)           # 0x8238
movq %rax, %rdi
movq %r15, %rsi
callq 0x29fc
testl %eax, %eax
je 0x2cd2
movq (%rbx,%r12,8), %r14
incq %r12
movslq 0x4(%rsp), %rax
cmpq %rax, %r12
jl 0x2cb0
jmp 0x2ce4
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x2210
testq %r14, %r14
jne 0x2cff
leaq 0x259b(%rip), %rdi      # 0x5293
xorl %eax, %eax
callq 0x41e8
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq