// File: tomfunkhouser[P]gaps/pkgs/fglut/freeglut_menu.c
// Function: fghActivateMenu
static void fghActivateMenu( SFG_Window* window, int button )
{
    int max_x, max_y;

    /* We'll be referencing this menu a lot, so remember its address: */
    SFG_Menu* menu = window->Menu[ button ];
    SFG_Window* current_window = fgStructure.CurrentWindow;

    /* If the menu is already active in another window, deactivate it there */
    if ( menu->ParentWindow )
      menu->ParentWindow->ActiveMenu = NULL ;

    /* Mark the menu as active, so that it gets displayed: */
    window->ActiveMenu = menu;
    menu->IsActive = GL_TRUE;
    fghSetMenuParentWindow ( window, menu );
    fgState.ActiveMenus++;

    /* Set up the initial menu position now: */
    fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
    fgSetWindow( window );
    menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
    menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );

    if( menu->X + menu->Width > max_x )
        menu->X -=menu->Width;

    if( menu->Y + menu->Height > max_y )
    {
        menu->Y -=menu->Height;
        if( menu->Y < 0 )
            menu->Y = 0;
    }

    menu->Window->State.MouseX =
        window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
    menu->Window->State.MouseY =
        window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;

    fgSetWindow( menu->Window );
    glutPositionWindow( menu->X, menu->Y );
    glutReshapeWindow( menu->Width, menu->Height );
    glutPopWindow( );
    glutShowWindow( );
    menu->Window->ActiveMenu = menu;
    fghCheckMenuStatus( menu );
    fgSetWindow( current_window );
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq 0x138(%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
leaq 0x7f8d5(%rip), %rax     # 0x1ffee0
movq 0x30(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x180634
movq 0x18(%rsp), %rax
movq 0x68(%rax), %rax
movq $0x0, 0x150(%rax)
movq 0x18(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x150(%rax)
movq 0x18(%rsp), %rax
movb $0x1, 0x40(%rax)
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x180820
leaq 0x79d2c(%rip), %rax     # 0x1fa390
movl 0x70(%rax), %ecx
addl $0x1, %ecx
leaq 0x79d1f(%rip), %rax     # 0x1fa390
movl %ecx, 0x70(%rax)
movq 0x18(%rsp), %rax
movq 0x68(%rax), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x24(%rsp), %rdx
callq 0x181160
movq 0x30(%rsp), %rdi
callq 0x1838e0
movq 0x30(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x8(%rsp)
movl $0x64, %edi
callq 0x181410
movl 0x8(%rsp), %ecx
addl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x4c(%rax)
movq 0x30(%rsp), %rax
movl 0x64(%rax), %eax
movl %eax, 0xc(%rsp)
movl $0x65, %edi
callq 0x181410
movl 0xc(%rsp), %ecx
addl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x50(%rax)
movq 0x18(%rsp), %rax
movl 0x4c(%rax), %eax
movq 0x18(%rsp), %rcx
addl 0x44(%rcx), %eax
cmpl 0x28(%rsp), %eax
jle 0x180709
movq 0x18(%rsp), %rax
movl 0x44(%rax), %edx
movq 0x18(%rsp), %rax
movl 0x4c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4c(%rax)
movq 0x18(%rsp), %rax
movl 0x50(%rax), %eax
movq 0x18(%rsp), %rcx
addl 0x48(%rcx), %eax
cmpl 0x24(%rsp), %eax
jle 0x18074d
movq 0x18(%rsp), %rax
movl 0x48(%rax), %edx
movq 0x18(%rsp), %rax
movl 0x50(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x50(%rax)
movq 0x18(%rsp), %rax
cmpl $0x0, 0x50(%rax)
jge 0x18074b
movq 0x18(%rsp), %rax
movl $0x0, 0x50(%rax)
jmp 0x18074d
movq 0x30(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, (%rsp)
movl $0x64, %edi
callq 0x181410
movl (%rsp), %ecx
addl %eax, %ecx
movq 0x18(%rsp), %rax
subl 0x4c(%rax), %ecx
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rax
movl %ecx, 0x60(%rax)
movq 0x30(%rsp), %rax
movl 0x64(%rax), %eax
movl %eax, 0x4(%rsp)
movl $0x65, %edi
callq 0x181410
movl 0x4(%rsp), %ecx
addl %eax, %ecx
movq 0x18(%rsp), %rax
subl 0x50(%rax), %ecx
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rax
movl %ecx, 0x64(%rax)
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rdi
callq 0x1838e0
movq 0x18(%rsp), %rax
movl 0x4c(%rax), %edi
movq 0x18(%rsp), %rax
movl 0x50(%rax), %esi
callq 0x184ae0
movq 0x18(%rsp), %rax
movl 0x44(%rax), %edi
movq 0x18(%rsp), %rax
movl 0x48(%rax), %esi
callq 0x1849c0
callq 0x184bf0
callq 0x184620
movq 0x18(%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x60(%rax), %rax
movq %rcx, 0x150(%rax)
movq 0x18(%rsp), %rdi
callq 0x17fd20
movq 0x10(%rsp), %rdi
callq 0x1838e0
addq $0x38, %rsp
retq
nopl (%rax,%rax)