// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/fglut/freeglut_menu.c
// Function: fghCheckMenuStatus
static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
{
    SFG_MenuEntry* menuEntry;
    int x, y;

    /* First of all check any of the active sub menus... */
    for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
         menuEntry;
         menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
    {
        if( menuEntry->SubMenu && menuEntry->IsActive )
        {
            /*
             * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
             * will mean that it caught the mouse cursor and we do not need
             * to regenerate the activity list, and so our parents do...
             */
            GLboolean return_status;

            menuEntry->SubMenu->Window->State.MouseX =
                menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
            menuEntry->SubMenu->Window->State.MouseY =
                menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
            return_status = fghCheckMenuStatus( menuEntry->SubMenu );

            if ( return_status )
                return GL_TRUE;
        }
    }

    /* That much about our sub menus, let's get to checking the current menu: */
    x = menu->Window->State.MouseX;
    y = menu->Window->State.MouseY;

    /* Check if the mouse cursor is contained within the current menu box */
    if( ( x >= FREEGLUT_MENU_BORDER ) &&
        ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&
        ( y >= FREEGLUT_MENU_BORDER ) &&
        ( y < menu->Height - FREEGLUT_MENU_BORDER )  )
    {
        int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;

        /* The mouse cursor is somewhere over our box, check it out. */
        menuEntry = fghFindMenuEntry( menu, menuID + 1 );
        FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
                                      "fghCheckMenuStatus" );

        menuEntry->IsActive = GL_TRUE;
        menuEntry->Ordinal = menuID;

        /*
         * If this is not the same as the last active menu entry, deactivate
         * the previous entry.  Specifically, if the previous active entry
         * was a submenu then deactivate it.
         */
        if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
            if( menu->ActiveEntry->SubMenu )
                fghDeactivateSubMenu( menu->ActiveEntry );

        if( menuEntry != menu->ActiveEntry )
        {
            menu->Window->State.Redisplay = GL_TRUE;
            if( menu->ActiveEntry )
                menu->ActiveEntry->IsActive = GL_FALSE;
        }

        menu->ActiveEntry = menuEntry;
        menu->IsActive = GL_TRUE;  /* XXX Do we need this? */

        /*
         * OKi, we have marked that entry as active, but it would be also
         * nice to have its contents updated, in case it's a sub menu.
         * Also, ignore the return value of the check function:
         */
        if( menuEntry->SubMenu )
        {
            if ( ! menuEntry->SubMenu->IsActive )
            {
                int max_x, max_y;
                SFG_Window *current_window = fgStructure.CurrentWindow;

                /* Set up the initial menu position now... */
                menuEntry->SubMenu->IsActive = GL_TRUE;

                /* Set up the initial submenu position now: */
                fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
                menuEntry->SubMenu->X = menu->X + menu->Width;
                menuEntry->SubMenu->Y = menu->Y +
                    menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;

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

                if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
                {
                    menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
                                               FREEGLUT_MENU_HEIGHT -
                                               2 * FREEGLUT_MENU_BORDER );
                    if( menuEntry->SubMenu->Y < 0 )
                        menuEntry->SubMenu->Y = 0;
                }

                fgSetWindow( menuEntry->SubMenu->Window );
                glutPositionWindow( menuEntry->SubMenu->X,
                                    menuEntry->SubMenu->Y );
                glutReshapeWindow( menuEntry->SubMenu->Width,
                                   menuEntry->SubMenu->Height );
                glutPopWindow( );
                glutShowWindow( );
                menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
                fgSetWindow( current_window );
                menuEntry->SubMenu->Window->State.MouseX =
                    x + menu->X - menuEntry->SubMenu->X;
                menuEntry->SubMenu->Window->State.MouseY =
                    y + menu->Y - menuEntry->SubMenu->Y;
                fghCheckMenuStatus( menuEntry->SubMenu );
            }

            /* Activate it because its parent entry is active */
            menuEntry->SubMenu->IsActive = GL_TRUE;  /* XXX Do we need this? */
        }

        /* Report back that we have caught the menu cursor */
        return GL_TRUE;
    }

    /* Looks like the menu cursor is somewhere else... */
    if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
        ( !menu->ActiveEntry->SubMenu ||
          !menu->ActiveEntry->SubMenu->IsActive ) )
    {
        menu->Window->State.Redisplay = GL_TRUE;
        menu->ActiveEntry->IsActive = GL_FALSE;
        menu->ActiveEntry = NULL;
    }

    return GL_FALSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
leaq 0x20(%rdi), %r14
movq (%r14), %r14
testq %r14, %r14
je 0xbbf61
movq 0x20(%r14), %rdi
testq %rdi, %rdi
je 0xbbf14
cmpb $0x0, 0x28(%r14)
je 0xbbf14
movq 0x60(%rbx), %rax
movq 0x60(%rdi), %rcx
movq 0x60(%rax), %xmm0
movq 0x4c(%rbx), %xmm1
paddd %xmm0, %xmm1
movq 0x4c(%rdi), %xmm0
psubd %xmm0, %xmm1
movq %xmm1, 0x60(%rcx)
callq 0xbbeff
testb %al, %al
je 0xbbf14
movb $0x1, %bpl
jmp 0xbc1aa
movq 0x60(%rbx), %rax
movq 0x60(%rax), %rcx
cmpl $0x2, %ecx
jl 0xbc17d
movq %rcx, %xmm1
movl 0x44(%rbx), %edx
addl $-0x2, %edx
cmpl %edx, %ecx
setl %cl
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
movd %xmm0, %ebp
cmpl $0x2, %ebp
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xbc17d
movl 0x48(%rbx), %ecx
addl $-0x2, %ecx
cmpl %ecx, %ebp
jge 0xbc17d
movdqa %xmm1, 0x20(%rsp)
addl $-0x2, %ebp
leaq 0x7b81e(%rip), %rdi     # 0x1377d8
callq 0xb8cb3
movl %eax, %ecx
addl $0x2, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movl %eax, %r15d
leal 0x1(%r15), %esi
movq %rbx, %rdi
callq 0xbc86a
movq %rax, %r14
testq %rax, %rax
jne 0xbbffc
leaq 0x28c77(%rip), %rdi     # 0xe4c5e
leaq 0x29409(%rip), %rsi     # 0xe53f7
leaq 0x29419(%rip), %rdx     # 0xe540e
xorl %eax, %eax
callq 0xbad73
movb $0x1, 0x28(%r14)
movl %r15d, 0x14(%r14)
movq 0x58(%rbx), %rdi
testq %rdi, %rdi
je 0xbc023
cmpq %rdi, %r14
je 0xbc023
cmpq $0x0, 0x20(%rdi)
je 0xbc023
callq 0xbc4cc
movq 0x58(%rbx), %rdi
cmpq %rdi, %r14
je 0xbc039
movq 0x60(%rbx), %rax
movb $0x1, 0x48(%rax)
testq %rdi, %rdi
je 0xbc039
movb $0x0, 0x28(%rdi)
movq %r14, 0x58(%rbx)
movb $0x1, 0x40(%rbx)
movq 0x20(%r14), %rax
movb $0x1, %bpl
testq %rax, %rax
je 0xbc1aa
cmpb $0x0, 0x40(%rax)
jne 0xbc177
leaq 0x7b82e(%rip), %rcx     # 0x137890
movq 0x30(%rcx), %rcx
movq %rcx, 0x10(%rsp)
movb $0x1, 0x40(%rax)
movq 0x68(%rbx), %rdi
leaq 0x1c(%rsp), %r12
leaq 0xc(%rsp), %rdx
movq %r12, %rsi
callq 0xbcaaa
movl 0x44(%rbx), %eax
addl 0x4c(%rbx), %eax
movq 0x20(%r14), %rcx
movl %eax, 0x4c(%rcx)
movl 0x50(%rbx), %r13d
movl 0x14(%r14), %r15d
leaq 0x7b737(%rip), %rdi     # 0x1377d8
callq 0xb8cb3
addl $0x2, %eax
imull %r15d, %eax
addl %r13d, %eax
movq 0x20(%r14), %rcx
movl %eax, 0x50(%rcx)
movl 0x44(%rcx), %edx
movl 0x4c(%rcx), %esi
addl %edx, %esi
cmpl (%r12), %esi
jle 0xbc0cd
movl 0x4c(%rbx), %esi
subl %edx, %esi
movl %esi, 0x4c(%rcx)
movl 0x48(%rcx), %r12d
addl %r12d, %eax
cmpl 0xc(%rsp), %eax
jle 0xbc103
leaq 0x7b6f7(%rip), %rdi     # 0x1377d8
callq 0xb8cb3
movq 0x20(%r14), %rcx
movl 0x50(%rcx), %edx
subl %r12d, %eax
addl %edx, %eax
addl $0x6, %eax
xorl %edx, %edx
testl %eax, %eax
cmovgl %eax, %edx
movl %edx, 0x50(%rcx)
movq 0x20(%r14), %rcx
movq 0x60(%rcx), %rdi
callq 0xbde17
movq 0x20(%r14), %rax
movl 0x4c(%rax), %edi
movl 0x50(%rax), %esi
callq 0xbe9b7
movq 0x20(%r14), %rax
movl 0x44(%rax), %edi
movl 0x48(%rax), %esi
callq 0xbe8de
callq 0xbeaa7
callq 0xbe5fe
movq 0x20(%r14), %rax
movq 0x60(%rax), %rcx
movq %rax, 0x150(%rcx)
movq 0x10(%rsp), %rdi
callq 0xbde17
movq 0x20(%r14), %rdi
movq 0x60(%rdi), %rax
movq 0x4c(%rbx), %xmm0
paddd 0x20(%rsp), %xmm0
movq 0x4c(%rdi), %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x60(%rax)
callq 0xbbeff
movq 0x20(%r14), %rax
movb $0x1, 0x40(%rax)
jmp 0xbc1aa
movq 0x58(%rbx), %rcx
testq %rcx, %rcx
je 0xbc1a8
cmpb $0x0, 0x28(%rcx)
je 0xbc1a8
movq 0x20(%rcx), %rdx
testq %rdx, %rdx
je 0xbc19b
cmpb $0x0, 0x40(%rdx)
jne 0xbc1a8
movb $0x1, 0x48(%rax)
movb $0x0, 0x28(%rcx)
andq $0x0, 0x58(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq