// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq $0x0, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
xorl %eax, %eax
movl %eax, %edi
movl $0x3055, %esi           # imm = 0x3055
callq 0x21d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x3326(%rip), %rsi      # 0x625b
callq 0x2cf0
cmpl $0x0, %eax
jne 0x2f77
movq -0x28(%rbp), %rdi
leaq 0x3325(%rip), %rsi      # 0x626f
callq 0x2cf0
cmpl $0x0, %eax
je 0x2f69
movq -0x28(%rbp), %rdi
leaq 0x332b(%rip), %rsi      # 0x628a
callq 0x2cf0
cmpl $0x0, %eax
jne 0x2f77
leaq 0x332f(%rip), %rdi      # 0x629f
movb $0x0, %al
callq 0x4cf0
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %esi
leaq -0x4(%rbp), %rdx
callq *0x62a1(%rip)           # 0x9228
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2f9e
leaq 0x3333(%rip), %rdi      # 0x62ca
movb $0x0, %al
callq 0x4cf0
cmpl $0x1, -0x4(%rbp)
jge 0x2fb2
leaq 0x333d(%rip), %rdi      # 0x62e8
movb $0x0, %al
callq 0x4cf0
movslq -0x4(%rbp), %rdi
movl $0x8, %esi
callq 0x2350
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2fd9
leaq 0x332d(%rip), %rdi      # 0x62ff
movb $0x0, %al
callq 0x4cf0
movq 0x6248(%rip), %rax      # 0x9228
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq -0x4(%rbp), %rdx
callq *%rax
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x3004
leaq 0x32cd(%rip), %rdi      # 0x62ca
movb $0x0, %al
callq 0x4cf0
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x3063
movq 0x6216(%rip), %rax      # 0x9230
movq -0x10(%rbp), %rcx
movslq -0x8(%rbp), %rdx
movq (%rcx,%rdx,8), %rdi
movl $0x3055, %esi           # imm = 0x3055
callq *%rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x32df(%rip), %rsi      # 0x631b
callq 0x2cf0
cmpl $0x0, %eax
je 0x3056
movq -0x10(%rbp), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
jmp 0x3058
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x300b
movq -0x10(%rbp), %rdi
callq 0x21f0
cmpq $0x0, -0x18(%rbp)
jne 0x3081
leaq 0x32b4(%rip), %rdi      # 0x632e
movb $0x0, %al
callq 0x4cf0
movq -0x18(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)