// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/fglut/freeglut_menu.c
// Function: fgCheckActiveMenu
GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
                              int mouse_x, int mouse_y )
{
    /*
     * Near as I can tell, this is the menu behaviour:
     *  - Down-click the menu button, menu not active:  activate
     *    the menu with its upper left-hand corner at the mouse
     *    location.
     *  - Down-click any button outside the menu, menu active:
     *    deactivate the menu
     *  - Down-click any button inside the menu, menu active:
     *    select the menu entry and deactivate the menu
     *  - Up-click the menu button, menu not active:  nothing happens
     *  - Up-click the menu button outside the menu, menu active:
     *    nothing happens
     *  - Up-click the menu button inside the menu, menu active:
     *    select the menu entry and deactivate the menu
     * Since menus can have submenus, we need to check this recursively.
     */
    if( window->ActiveMenu )
    {
        if( window == window->ActiveMenu->ParentWindow )
        {
            window->ActiveMenu->Window->State.MouseX =
                                       mouse_x - window->ActiveMenu->X;
            window->ActiveMenu->Window->State.MouseY =
                                       mouse_y - window->ActiveMenu->Y;
        }

        /* In the menu, invoke the callback and deactivate the menu */
        if( fghCheckMenuStatus( window->ActiveMenu ) )
        {
            /*
             * Save the current window and menu and set the current
             * window to the window whose menu this is
             */
            SFG_Window *save_window = fgStructure.CurrentWindow;
            SFG_Menu *save_menu = fgStructure.CurrentMenu;
            SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
            fgSetWindow( parent_window );
            fgStructure.CurrentMenu = window->ActiveMenu;

            /* Execute the menu callback */
            fghExecuteMenuCallback( window->ActiveMenu );
            fgDeactivateMenu( parent_window );

            /* Restore the current window and menu */
            fgSetWindow( save_window );
            fgStructure.CurrentMenu = save_menu;
        }
        else if( pressed )
            /*
             * Outside the menu, deactivate if it's a downclick
             *
             * XXX This isn't enough.  A downclick outside of
             * XXX the interior of our freeglut windows should also
             * XXX deactivate the menu.  This is more complicated.
             */
            fgDeactivateMenu( window->ActiveMenu->ParentWindow );

        /*
         * XXX Why does an active menu require a redisplay at
         * XXX this point?  If this can come out cleanly, then
         * XXX it probably should do so; if not, a comment should
         * XXX explain it.
         */
        if( ! window->IsMenu )
            window->State.Redisplay = GL_TRUE;

        return GL_TRUE;
    }

    /* No active menu, let's check whether we need to activate one. */
    if( ( 0 <= button ) &&
        ( FREEGLUT_MAX_MENUS > button ) &&
        ( window->Menu[ button ] ) &&
        pressed )
    {
        /* XXX Posting a requisite Redisplay seems bogus. */
        window->State.Redisplay = GL_TRUE;
        fghActivateMenu( window, button );
        return GL_TRUE;
    }

    return GL_FALSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rdi, %rbx
movq 0x150(%rdi), %rdi
testq %rdi, %rdi
je 0xd13e0
cmpq %rbx, 0x68(%rdi)
jne 0xd1350
subl 0x4c(%rdi), %ecx
movq 0x60(%rdi), %rax
movl %ecx, 0x60(%rax)
subl 0x50(%rdi), %r8d
movl %r8d, 0x64(%rax)
callq 0xd103d
testb %al, %al
je 0xd153d
leaq 0x7550c(%rip), %r12     # 0x146870
movq 0x30(%r12), %r14
movq 0x38(%r12), %r13
movq 0x150(%rbx), %rax
movq 0x68(%rax), %r15
movq %r15, %rdi
callq 0xd314d
movq 0x150(%rbx), %rbp
movq %rbp, 0x38(%r12)
movq %rbp, %rdx
movq %rdx, %rax
leaq 0x20(%rdx), %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0xd13c6
cmpb $0x0, 0x28(%rcx)
je 0xd1397
movq 0x20(%rcx), %rdx
testq %rdx, %rdx
jne 0xd1390
movq 0x30(%rax), %rdx
testq %rdx, %rdx
je 0xd13c6
movq %rax, 0x38(%r12)
movl 0x10(%rcx), %edi
callq *%rdx
movq %rbp, 0x38(%r12)
movq %r15, %rdi
callq 0xd1570
movq %r14, %rdi
callq 0xd314d
movq %r13, 0x38(%r12)
jmp 0xd1552
cmpl $0x2, %esi
ja 0xd1539
movl %esi, %eax
movq 0x138(%rbx,%rax,8), %r14
testq %r14, %r14
sete %al
testb %bpl, %bpl
sete %cl
orb %al, %cl
jne 0xd1539
movb $0x1, 0x48(%rbx)
leaq 0x7545e(%rip), %rax     # 0x146870
movq 0x68(%r14), %rcx
testq %rcx, %rcx
je 0xd1426
movq $0x0, 0x150(%rcx)
movq 0x30(%rax), %r15
movq %r14, 0x150(%rbx)
movb $0x1, 0x40(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd15fe
leaq 0x70d78(%rip), %rax     # 0x1421c0
incl 0x70(%rax)
movq 0x68(%r14), %rdi
leaq 0x4(%rsp), %r12
movq %rsp, %rdx
movq %r12, %rsi
callq 0xd1c4e
movq %rbx, %rdi
callq 0xd314d
movl 0x60(%rbx), %ebp
movl $0x64, %edi
callq 0xd1dc4
addl %ebp, %eax
movl %eax, 0x4c(%r14)
movl 0x64(%rbx), %ebp
movl $0x65, %edi
callq 0xd1dc4
addl %ebp, %eax
movl %eax, 0x50(%r14)
movl 0x44(%r14), %edx
movl 0x4c(%r14), %ecx
leal (%rdx,%rcx), %esi
cmpl (%r12), %esi
jle 0xd14a4
subl %edx, %ecx
movl %ecx, 0x4c(%r14)
movl 0x48(%r14), %ecx
leal (%rcx,%rax), %edx
cmpl (%rsp), %edx
jle 0xd14bd
subl %ecx, %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
movl %ecx, 0x50(%r14)
movl 0x60(%rbx), %ebp
movl $0x64, %edi
callq 0xd1dc4
addl %ebp, %eax
subl 0x4c(%r14), %eax
movq 0x60(%r14), %rcx
movl %eax, 0x60(%rcx)
movl 0x64(%rbx), %ebx
movl $0x65, %edi
callq 0xd1dc4
addl %ebx, %eax
subl 0x50(%r14), %eax
movq 0x60(%r14), %rdi
movl %eax, 0x64(%rdi)
callq 0xd314d
movl 0x4c(%r14), %edi
movl 0x50(%r14), %esi
callq 0xd3d61
movl 0x44(%r14), %edi
movl 0x48(%r14), %esi
callq 0xd3c88
callq 0xd3e51
callq 0xd39a8
movq 0x60(%r14), %rax
movq %r14, 0x150(%rax)
movq %r14, %rdi
callq 0xd103d
movq %r15, %rdi
callq 0xd314d
movb $0x1, %al
jmp 0xd1561
xorl %eax, %eax
jmp 0xd1561
testb %bpl, %bpl
je 0xd1552
movq 0x150(%rbx), %rax
movq 0x68(%rax), %rdi
callq 0xd1570
movb $0x1, %al
cmpb $0x0, 0x170(%rbx)
jne 0xd1561
movb $0x1, 0x48(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq