// File: tomfunkhouser[P]gaps/pkgs/fglut/freeglut_window.c
// Function: fghCreateNewContext
static GLXContext fghCreateNewContext( SFG_Window* window )
{
  /* for color model calculation */
  int menu = ( window->IsMenu && !fgStructure.MenuContext );
  int index_mode = ( fgState.DisplayMode & GLUT_INDEX );

  /* "classic" context creation */
  Display *dpy = fgDisplay.Display;
  GLXFBConfig config = *(window->Window.FBConfig);
  int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
  GLXContext share_list = NULL;
  Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
  GLXContext context;

  /* new context creation */
  int attributes[9];
  CreateContextAttribsProc createContextAttribs;

  /* If nothing fancy has been required, simply use the old context creation GLX API entry */
  if ( fghIsLegacyContextRequested() )
  {
    context = glXCreateNewContext( dpy, config, render_type, share_list, direct );
    if ( context == NULL ) {
      fghContextCreationError();
    }
    return context;
  }

  /* color index mode is not available anymore with OpenGL 3.0 */
  if ( render_type == GLX_COLOR_INDEX_TYPE ) {
    fgWarning( "color index mode is deprecated, using RGBA mode" );
  }

  fghFillContextAttributes( attributes );

  createContextAttribs = (CreateContextAttribsProc) fghGetProcAddress( "glXCreateContextAttribsARB" );
  if ( createContextAttribs == NULL ) {
    fgError( "glXCreateContextAttribsARB not found" );
  }

  context = createContextAttribs( dpy, config, share_list, direct, attributes );
  if ( context == NULL ) {
    fghContextCreationError();
  }
  return context;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq 0x78(%rsp), %rax
movzbl 0x170(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x7(%rsp)
je 0x184048
leaq 0x7bea6(%rip), %rax     # 0x1ffee0
cmpq $0x0, 0x40(%rax)
setne %al
xorb $-0x1, %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
leaq 0x76334(%rip), %rax     # 0x1fa390
movl 0x18(%rax), %eax
andl $0x1, %eax
movl %eax, 0x70(%rsp)
leaq 0x7bdc3(%rip), %rax     # 0x1ffe30
movq (%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, 0x60(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x74(%rsp)
movb %al, 0x6(%rsp)
jne 0x18409f
cmpl $0x0, 0x70(%rsp)
setne %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %dl
movl $0x8014, %eax           # imm = 0x8014
movl $0x8015, %ecx           # imm = 0x8015
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, 0x5c(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x762c9(%rip), %rax     # 0x1fa390
cmpl $0x0, 0x20(%rax)
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x4c(%rsp)
callq 0x184df0
cmpl $0x0, %eax
je 0x184122
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x5c(%rsp), %edx
movq 0x50(%rsp), %rcx
movl 0x4c(%rsp), %r8d
callq 0x22dc0
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x184110
callq 0x184e40
movq 0x40(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x1841a9
cmpl $0x8015, 0x5c(%rsp)     # imm = 0x8015
jne 0x18413a
leaq 0x2ae42(%rip), %rdi     # 0x1aef75
movb $0x0, %al
callq 0x17dfa0
leaq 0x10(%rsp), %rdi
callq 0x184e90
leaq 0x2ae5a(%rip), %rdi     # 0x1aefa5
callq 0x1852a0
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x18416b
leaq 0x2ae5c(%rip), %rdi     # 0x1aefc0
movb $0x0, %al
callq 0x17de70
movq 0x8(%rsp), %rax
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x50(%rsp), %rdx
movl 0x4c(%rsp), %ecx
leaq 0x10(%rsp), %r8
callq *%rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x18419c
callq 0x184e40
movq 0x40(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
addq $0x88, %rsp
retq
nopl (%rax)