// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/fglut/freeglut_window.c
// Function: fgChooseFBConfig
GLXFBConfig* fgChooseFBConfig( void )
{
  GLboolean wantIndexedMode = GL_FALSE;
  int attributes[ 100 ];
  int where = 0, numAuxBuffers;

  /* First we have to process the display mode settings... */
  if( fgState.DisplayMode & GLUT_INDEX ) {
    ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
    /*  Buffer size is selected later.  */

    ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
    wantIndexedMode = GL_TRUE;
  } else {
    ATTRIB_VAL( GLX_RED_SIZE,   1 );
    ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
    ATTRIB_VAL( GLX_BLUE_SIZE,  1 );
    if( fgState.DisplayMode & GLUT_ALPHA ) {
      ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
    }
  }

  if( fgState.DisplayMode & GLUT_DOUBLE ) {
    ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
  }

  if( fgState.DisplayMode & GLUT_STEREO ) {
    ATTRIB_VAL( GLX_STEREO, True );
  }

  if( fgState.DisplayMode & GLUT_DEPTH ) {
    ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
  }

  if( fgState.DisplayMode & GLUT_STENCIL ) {
    ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
  }

  if( fgState.DisplayMode & GLUT_ACCUM ) {
    ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
    ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
    ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
    if( fgState.DisplayMode & GLUT_ALPHA ) {
      ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
    }
  }

  numAuxBuffers = fghNumberOfAuxBuffersRequested();
  if ( numAuxBuffers > 0 ) {
    ATTRIB_VAL( GLX_AUX_BUFFERS, numAuxBuffers );
  }

  if( fgState.DisplayMode & GLUT_SRGB ) {
    ATTRIB_VAL( GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, True );
  }

  if (fgState.DisplayMode & GLUT_MULTISAMPLE) {
    ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1);
    ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber);
  }

  /* Push a terminator at the end of the list */
  ATTRIB( None );

    {
        GLXFBConfig * fbconfigArray;  /*  Array of FBConfigs  */
        GLXFBConfig * fbconfig;       /*  The FBConfig we want  */
        int fbconfigArraySize;        /*  Number of FBConfigs in the array  */


        /*  Get all FBConfigs that match "attributes".  */
        fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
                                           fgDisplay.Screen,
                                           attributes,
                                           &fbconfigArraySize );

        if (fbconfigArray != NULL)
        {
            if( wantIndexedMode )
            {
                /*
                 * In index mode, we want the largest buffer size, i.e. visual
                 * depth.  Here, FBConfigs are sorted by increasing buffer size
                 * first, so FBConfigs with the largest size come last.
                 */

                int bufferSizeMin, bufferSizeMax;

                /*  Get bufferSizeMin.  */
                glXGetFBConfigAttrib( fgDisplay.Display,
                                        fbconfigArray[0],
                                        GLX_BUFFER_SIZE,
                                        &bufferSizeMin );
                /*  Get bufferSizeMax.  */
                glXGetFBConfigAttrib( fgDisplay.Display,
                                        fbconfigArray[fbconfigArraySize - 1],
                                        GLX_BUFFER_SIZE,
                                        &bufferSizeMax );

                if (bufferSizeMax > bufferSizeMin)
                {
                    /* 
                     * Free and reallocate fbconfigArray, keeping only FBConfigs
                     * with the largest buffer size.
                     */
                    XFree(fbconfigArray);

                    /*  Add buffer size token at the end of the list.  */
                    where--;
                    ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
                    ATTRIB( None );

                    fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
                                                       fgDisplay.Screen,
                                                       attributes,
                                                       &fbconfigArraySize );
                }
            }

            /*
             * We now have an array of FBConfigs, the first one being the "best"
             * one.  So we should return only this FBConfig:
             *
             * int fbconfigXID;
             *
             *  - pick the XID of the FBConfig we want
             * result = glXGetFBConfigAttrib( fgDisplay.Display,
             *                                fbconfigArray[0],
             *                                GLX_FBCONFIG_ID,
             *                                &fbconfigXID );
             *
             * - free the array
             * XFree(fbconfigArray);
             *
             * - reset "attributes" with the XID
             * where = 0;
             * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
             * ATTRIB( None );
             *
             * - get our FBConfig only
             * fbconfig = glXChooseFBConfig( fgDisplay.Display,
             *                               fgDisplay.Screen,
             *                               attributes,
             *                               &fbconfigArraySize );
             *
             * However, for some configurations (for instance multisampling with
             * Mesa 6.5.2 and ATI drivers), this does not work:
             * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
             * XID.  Further investigation is needed.
             *
             * So, for now, we return the whole array of FBConfigs.  This should
             * not produce any side effects elsewhere.
             */
            fbconfig = fbconfigArray;
        }
        else
        {
           fbconfig = NULL;
        }

        return fbconfig;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
leaq 0x7562c(%rip), %rcx     # 0x1331e0
movl 0x18(%rcx), %eax
testb $0x1, %al
jne 0xbdbde
movaps 0x27c2e(%rip), %xmm0    # 0xe57f0
movaps %xmm0, 0x10(%rsp)
movabsq $0x10000000a, %rdx      # imm = 0x10000000A
movq %rdx, 0x20(%rsp)
testb $0x8, %al
jne 0xbdbf0
pushq $0x6
jmp 0xbdc01
pushq $0x4
popq %r12
movaps 0x27bf7(%rip), %xmm0    # 0xe57e0
movaps %xmm0, 0x10(%rsp)
jmp 0xbdc03
movabsq $0x10000000b, %rdx      # imm = 0x10000000B
movq %rdx, 0x28(%rsp)
pushq $0x8
popq %r12
testb $0x2, %al
je 0xbdc1e
movl %r12d, %edx
movl $0x5, 0x10(%rsp,%rdx,4)
addl $0x2, %r12d
movl $0x1, 0x14(%rsp,%rdx,4)
btl $0x8, %eax
jae 0xbdc3a
movl %r12d, %edx
movabsq $0x100000006, %rsi      # imm = 0x100000006
movq %rsi, 0x10(%rsp,%rdx,4)
addl $0x2, %r12d
testb $0x10, %al
je 0xbdc54
movl %r12d, %edx
movabsq $0x10000000c, %rsi      # imm = 0x10000000C
movq %rsi, 0x10(%rsp,%rdx,4)
addl $0x2, %r12d
testb $0x20, %al
je 0xbdc6e
movl %r12d, %edx
movabsq $0x10000000d, %rsi      # imm = 0x10000000D
movq %rsi, 0x10(%rsp,%rdx,4)
addl $0x2, %r12d
testb $0x4, %al
je 0xbdcb4
movaps 0x27b87(%rip), %xmm0    # 0xe5800
movl %r12d, %edx
movups %xmm0, 0x10(%rsp,%rdx,4)
movabsq $0x100000010, %rsi      # imm = 0x100000010
movq %rsi, 0x20(%rsp,%rdx,4)
leal 0x6(%r12), %esi
testb $0x8, %al
jne 0xbdc9e
movl %esi, %r12d
jmp 0xbdcb4
movl %esi, %esi
movl $0x11, 0x10(%rsp,%rsi,4)
addl $0x8, %r12d
movl $0x1, 0x2c(%rsp,%rdx,4)
testw %ax, %ax
js 0xbdce1
btl $0xe, %eax
jb 0xbdce5
btl $0xd, %eax
jb 0xbdce9
movl %eax, %esi
shrl $0xc, %esi
movl 0xb4(%rcx), %edx
testl %edx, %edx
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
je 0xbdcec
jmp 0xbdcff
pushq $0x4
jmp 0xbdceb
pushq $0x3
jmp 0xbdceb
pushq $0x2
popq %rdx
movl %r12d, %esi
movl $0x7, 0x10(%rsp,%rsi,4)
addl $0x2, %r12d
movl %edx, 0x14(%rsp,%rsi,4)
movl %eax, %edx
andl $0x1000, %edx           # imm = 0x1000
testl %edx, %edx
je 0xbdd21
movl %r12d, %edx
movabsq $0x1000020b2, %rsi      # imm = 0x1000020B2
movq %rsi, 0x10(%rsp,%rdx,4)
addl $0x2, %r12d
testb %al, %al
jns 0xbdd4d
movl %r12d, %edx
movabsq $0x1000186a0, %rsi      # imm = 0x1000186A0
movq %rsi, 0x10(%rsp,%rdx,4)
movl $0x186a1, 0x18(%rsp,%rdx,4) # imm = 0x186A1
movl 0xb8(%rcx), %ecx
addl $0x4, %r12d
movl %ecx, 0x1c(%rsp,%rdx,4)
testb $0x1, %al
sete %bpl
movl %r12d, %r13d
andl $0x0, 0x10(%rsp,%r13,4)
leaq 0x79a8d(%rip), %r14     # 0x1377f0
movq (%r14), %rdi
movl 0x8(%r14), %esi
leaq 0x10(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x21cd0
movq %rax, %rbx
testq %rax, %rax
sete %al
orb %bpl, %al
jne 0xbde02
movq (%r14), %rdi
movq (%rbx), %rsi
pushq $0x2
popq %rbp
leaq 0xc(%rsp), %rcx
movl %ebp, %edx
callq 0x21290
movq (%r14), %rdi
movslq 0x4(%rsp), %rax
movq -0x8(%rbx,%rax,8), %rsi
leaq 0x8(%rsp), %r15
movl %ebp, %edx
movq %r15, %rcx
callq 0x21290
movl (%r15), %eax
leaq 0xc(%rsp), %rcx
cmpl (%rcx), %eax
jle 0xbde02
incl %r12d
movq %rbx, %rdi
callq 0x21bf0
movl $0x2, 0x10(%rsp,%r13,4)
movl 0x8(%rsp), %eax
movl %r12d, %ecx
movl %eax, 0x10(%rsp,%rcx,4)
andl $0x0, 0x18(%rsp,%r13,4)
movq (%r14), %rdi
movl 0x8(%r14), %esi
leaq 0x10(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x21cd0
movq %rax, %rbx
movq %rbx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq