// File: rurban[P]dgnlib/dgnread.cpp
// Function: DGNBuildIndex(DGNInfo*)
void DGNBuildIndex( DGNInfo *psDGN )

{
    int nMaxElements, nType, nLevel;
    long nLastOffset;
    GUInt32 anRegion[6];

    if( psDGN->index_built ) 
        return;

    psDGN->index_built = TRUE;
    
    DGNRewind( psDGN );

    nMaxElements = 0;

    nLastOffset = VSIFTell( psDGN->fp );
    while( DGNLoadRawElement( psDGN, &nType, &nLevel ) )
    {
        DGNElementInfo  *psEI;

        if( psDGN->element_count == nMaxElements )
        {
            nMaxElements = (int) (nMaxElements * 1.5) + 500;
            
            psDGN->element_index = (DGNElementInfo *) 
                CPLRealloc( psDGN->element_index, 
                            nMaxElements * sizeof(DGNElementInfo) );
        }

        psEI = psDGN->element_index + psDGN->element_count;
        psEI->level = (unsigned char) nLevel;
        psEI->type = (unsigned char) nType;
        psEI->flags = 0;
        psEI->offset = (long) nLastOffset;

        if( psDGN->abyElem[0] & 0x80 )
            psEI->flags |= DGNEIF_COMPLEX;

        if( psDGN->abyElem[1] & 0x80 )
            psEI->flags |= DGNEIF_DELETED;

        if( nType == DGNT_LINE || nType == DGNT_LINE_STRING
            || nType == DGNT_SHAPE || nType == DGNT_CURVE
            || nType == DGNT_BSPLINE_POLE )
            psEI->stype = DGNST_MULTIPOINT;

        else if( nType == DGNT_GROUP_DATA && nLevel == DGN_GDL_COLOR_TABLE )
        {
            DGNElemCore *psCT = DGNParseColorTable( psDGN );
            DGNFreeElement( (DGNHandle) psDGN, psCT );
            psEI->stype = DGNST_COLORTABLE;
        }
        else if( nType == DGNT_ELLIPSE || nType == DGNT_ARC )
            psEI->stype = DGNST_ARC;
        
        else if( nType == DGNT_COMPLEX_SHAPE_HEADER 
                 || nType == DGNT_COMPLEX_CHAIN_HEADER
                 || nType == DGNT_3DSURFACE_HEADER
                 || nType == DGNT_3DSOLID_HEADER)
            psEI->stype = DGNST_COMPLEX_HEADER;
        
        else if( nType == DGNT_TEXT )
            psEI->stype = DGNST_TEXT;

        else if( nType == DGNT_TAG_VALUE )
            psEI->stype = DGNST_TAG_VALUE;

        else if( nType == DGNT_APPLICATION_ELEM )
        {
            if( nLevel == 24 )
                psEI->stype = DGNST_TAG_SET;
            else
                psEI->stype = DGNST_CORE;
        }
        else if( nType == DGNT_TCB )
        {
            DGNElemCore *psTCB = DGNParseTCB( psDGN );
            DGNFreeElement( (DGNHandle) psDGN, psTCB );
            psEI->stype = DGNST_TCB;
        }
        else if( nType == DGNT_CONE )
            psEI->stype = DGNST_CONE;
        else
            psEI->stype = DGNST_CORE;

        if( !(psEI->flags & DGNEIF_DELETED)
            && !(psEI->flags & DGNEIF_COMPLEX) 
            && DGNGetRawExtents( psDGN, nType, NULL,
                                 anRegion+0, anRegion+1, anRegion+2,
                                 anRegion+3, anRegion+4, anRegion+5 ) )
        {
#ifdef notdef
            printf( "panRegion[%d]=%.1f,%.1f,%.1f,%.1f,%.1f,%.1f\n", 
                    psDGN->element_count,
                    anRegion[0] - 2147483648.0,
                    anRegion[1] - 2147483648.0,
                    anRegion[2] - 2147483648.0,
                    anRegion[3] - 2147483648.0,
                    anRegion[4] - 2147483648.0,
                    anRegion[5] - 2147483648.0 );
#endif            
            if( psDGN->got_bounds )
            {
                psDGN->min_x = MIN(psDGN->min_x, anRegion[0]);
                psDGN->min_y = MIN(psDGN->min_y, anRegion[1]);
                psDGN->min_z = MIN(psDGN->min_z, anRegion[2]);
                psDGN->max_x = MAX(psDGN->max_x, anRegion[3]);
                psDGN->max_y = MAX(psDGN->max_y, anRegion[4]);
                psDGN->max_z = MAX(psDGN->max_z, anRegion[5]);
            }
            else
            {
                memcpy( &(psDGN->min_x), anRegion, sizeof(GInt32) * 6 );
                psDGN->got_bounds = TRUE;
            }
        }

        psDGN->element_count++;

        nLastOffset = VSIFTell( psDGN->fp );
    }

    DGNRewind( psDGN );

    psDGN->max_element_count = nMaxElements;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x20040(%rax)
je 0xcb51
jmp 0xcfca
movq -0x8(%rbp), %rax
movl $0x1, 0x20040(%rax)
movq -0x8(%rbp), %rdi
callq 0x10d20
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x9d00
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
leaq -0x14(%rbp), %rdx
callq 0xcfe0
cmpl $0x0, %eax
je 0xcfb4
movq -0x8(%rbp), %rax
movl 0x20044(%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xcbeb
cvtsi2sdl -0xc(%rbp), %xmm0
movsd 0xd77b(%rip), %xmm1     # 0x1a330
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
addl $0x1f4, %eax            # imm = 0x1F4
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x20050(%rax), %rdi
movslq -0xc(%rbp), %rsi
shlq $0x4, %rsi
callq 0x3120
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x20050(%rax)
movq -0x8(%rbp), %rax
movq 0x20050(%rax), %rax
movq -0x8(%rbp), %rcx
movslq 0x20044(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %eax
movb %al, %cl
movq -0x48(%rbp), %rax
movb %cl, (%rax)
movl -0x10(%rbp), %eax
movb %al, %cl
movq -0x48(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x48(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x20(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xcc57
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x3(%rax)
movq -0x8(%rbp), %rax
movzbl 0x11(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xcc77
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x3(%rax)
cmpl $0x3, -0x10(%rbp)
je 0xcc95
cmpl $0x4, -0x10(%rbp)
je 0xcc95
cmpl $0x6, -0x10(%rbp)
je 0xcc95
cmpl $0xb, -0x10(%rbp)
je 0xcc95
cmpl $0x15, -0x10(%rbp)
jne 0xcca2
movq -0x48(%rbp), %rax
movb $0x2, 0x2(%rax)
jmp 0xcda5
cmpl $0x5, -0x10(%rbp)
jne 0xccd5
cmpl $0x1, -0x14(%rbp)
jne 0xccd5
movq -0x8(%rbp), %rdi
callq 0x113f0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x10be0
movq -0x48(%rbp), %rax
movb $0x3, 0x2(%rax)
jmp 0xcda3
cmpl $0xf, -0x10(%rbp)
je 0xcce1
cmpl $0x10, -0x10(%rbp)
jne 0xccee
movq -0x48(%rbp), %rax
movb $0x5, 0x2(%rax)
jmp 0xcda1
cmpl $0xe, -0x10(%rbp)
je 0xcd06
cmpl $0xc, -0x10(%rbp)
je 0xcd06
cmpl $0x12, -0x10(%rbp)
je 0xcd06
cmpl $0x13, -0x10(%rbp)
jne 0xcd13
movq -0x48(%rbp), %rax
movb $0x7, 0x2(%rax)
jmp 0xcd9f
cmpl $0x11, -0x10(%rbp)
jne 0xcd23
movq -0x48(%rbp), %rax
movb $0x6, 0x2(%rax)
jmp 0xcd9d
cmpl $0x25, -0x10(%rbp)
jne 0xcd33
movq -0x48(%rbp), %rax
movb $0x9, 0x2(%rax)
jmp 0xcd9b
cmpl $0x42, -0x10(%rbp)
jne 0xcd53
cmpl $0x18, -0x14(%rbp)
jne 0xcd49
movq -0x48(%rbp), %rax
movb $0xa, 0x2(%rax)
jmp 0xcd51
movq -0x48(%rbp), %rax
movb $0x1, 0x2(%rax)
jmp 0xcd99
cmpl $0x9, -0x10(%rbp)
jne 0xcd7d
movq -0x8(%rbp), %rdi
callq 0x114c0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x10be0
movq -0x48(%rbp), %rax
movb $0x4, 0x2(%rax)
jmp 0xcd97
cmpl $0x17, -0x10(%rbp)
jne 0xcd8d
movq -0x48(%rbp), %rax
movb $0xc, 0x2(%rax)
jmp 0xcd95
movq -0x48(%rbp), %rax
movb $0x1, 0x2(%rax)
jmp 0xcd97
jmp 0xcd99
jmp 0xcd9b
jmp 0xcd9d
jmp 0xcd9f
jmp 0xcda1
jmp 0xcda3
jmp 0xcda5
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xcf8c
movq -0x48(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0xcf8c
movq -0x8(%rbp), %rdi
movl -0x10(%rbp), %esi
leaq -0x40(%rbp), %rcx
leaq -0x40(%rbp), %r8
addq $0x4, %r8
leaq -0x40(%rbp), %r9
addq $0x8, %r9
leaq -0x40(%rbp), %r11
addq $0xc, %r11
leaq -0x40(%rbp), %r10
addq $0x10, %r10
leaq -0x40(%rbp), %rax
addq $0x14, %rax
xorl %edx, %edx
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xd2e0
cmpl $0x0, %eax
je 0xcf8c
movq -0x8(%rbp), %rax
cmpl $0x0, 0x2035c(%rax)
je 0xcf57
movq -0x8(%rbp), %rax
movl 0x20360(%rax), %eax
cmpl -0x40(%rbp), %eax
jae 0xce4d
movq -0x8(%rbp), %rax
movl 0x20360(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0xce53
movl -0x40(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20360(%rax)
movq -0x8(%rbp), %rax
movl 0x20364(%rax), %eax
cmpl -0x3c(%rbp), %eax
jae 0xce7e
movq -0x8(%rbp), %rax
movl 0x20364(%rax), %eax
movl %eax, -0x60(%rbp)
jmp 0xce84
movl -0x3c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20364(%rax)
movq -0x8(%rbp), %rax
movl 0x20368(%rax), %eax
cmpl -0x38(%rbp), %eax
jae 0xceaf
movq -0x8(%rbp), %rax
movl 0x20368(%rax), %eax
movl %eax, -0x64(%rbp)
jmp 0xceb5
movl -0x38(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20368(%rax)
movq -0x8(%rbp), %rax
movl 0x2036c(%rax), %eax
cmpl -0x34(%rbp), %eax
jbe 0xcee0
movq -0x8(%rbp), %rax
movl 0x2036c(%rax), %eax
movl %eax, -0x68(%rbp)
jmp 0xcee6
movl -0x34(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x2036c(%rax)
movq -0x8(%rbp), %rax
movl 0x20370(%rax), %eax
cmpl -0x30(%rbp), %eax
jbe 0xcf11
movq -0x8(%rbp), %rax
movl 0x20370(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xcf17
movl -0x30(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20370(%rax)
movq -0x8(%rbp), %rax
movl 0x20374(%rax), %eax
cmpl -0x2c(%rbp), %eax
jbe 0xcf42
movq -0x8(%rbp), %rax
movl 0x20374(%rax), %eax
movl %eax, -0x70(%rbp)
jmp 0xcf48
movl -0x2c(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20374(%rax)
jmp 0xcf8a
movq -0x8(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x20360(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x20368(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x20370(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x2035c(%rax)
jmp 0xcf8c
movq -0x8(%rbp), %rax
movl 0x20044(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20044(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x9d00
movq %rax, -0x20(%rbp)
jmp 0xcb7f
movq -0x8(%rbp), %rdi
callq 0x10d20
movl -0xc(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20048(%rax)
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)