// File: /workspace/github_cpp_makefile_star2_O3/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:
cmpl $0x0, 0x20040(%rdi)
je 0x8711
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x1, 0x20040(%rdi)
movq (%rdi), %rdi
callq 0x6e18
xorl %eax, %eax
movl %eax, 0x8(%rbx)
movl %eax, 0x20384(%rbx)
movq (%rbx), %rdi
callq 0x6e13
movq %rax, %r14
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x8a63
movl $0x0, %r15d
testl %eax, %eax
je 0x8a3a
leaq 0x20360(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
movl 0x20044(%rbx), %ecx
movq 0x20050(%rbx), %rax
cmpl %r15d, %ecx
jne 0x87d4
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x7ef3(%rip), %xmm0     # 0x10698
cvttsd2si %xmm0, %r15d
movslq %r15d, %rsi
addl $0x1f4, %r15d           # imm = 0x1F4
shlq $0x4, %rsi
addq $0x1f40, %rsi           # imm = 0x1F40
movq %rax, %rdi
callq 0x2dcb
movq %rax, 0x20050(%rbx)
movl 0x20044(%rbx), %ecx
movslq %ecx, %rdx
shlq $0x4, %rdx
leaq (%rax,%rdx), %r12
movb 0x8(%rsp), %cl
movb %cl, (%rax,%rdx)
movl 0xc(%rsp), %ecx
movb %cl, 0x1(%rax,%rdx)
movb $0x0, 0x3(%rax,%rdx)
movq %r14, 0x8(%rax,%rdx)
movb $0x1, %al
cmpb $0x0, 0x10(%rbx)
jns 0x8808
movb $0x2, 0x3(%r12)
movb $0x3, %al
cmpb $0x0, 0x11(%rbx)
jns 0x8813
movb %al, 0x3(%r12)
cmpl $0x15, %ecx
ja 0x894f
movl $0x200858, %eax         # imm = 0x200858
btl %ecx, %eax
jae 0x894f
movb $0x2, 0x2(%r12)
testb $0x3, 0x3(%r12)
je 0x8864
incl 0x20044(%rbx)
movq (%rbx), %rdi
callq 0x6e13
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x8a63
testl %eax, %eax
jne 0x8783
jmp 0x8a3a
movl 0xc(%rsp), %esi
subq $0x8, %rsp
movq %rbx, %rdi
xorl %edx, %edx
leaq 0x18(%rsp), %rcx
leaq 0x1c(%rsp), %r8
leaq 0x20(%rsp), %r9
leaq 0x2c(%rsp), %rax
pushq %rax
leaq 0x30(%rsp), %rax
pushq %rax
leaq 0x34(%rsp), %rax
pushq %rax
callq 0x8c8f
addq $0x20, %rsp
testl %eax, %eax
je 0x8838
cmpl $0x0, 0x2035c(%rbx)
je 0x897c
movq 0x20360(%rbx), %xmm0
movq 0x10(%rsp), %xmm1
movdqa %xmm1, %xmm2
movdqa 0x7e1a(%rip), %xmm4     # 0x106e0
pxor %xmm4, %xmm2
movdqa %xmm0, %xmm3
pxor %xmm4, %xmm3
pcmpgtd %xmm3, %xmm2
pand %xmm2, %xmm0
pandn %xmm1, %xmm2
por %xmm0, %xmm2
movq %xmm2, 0x20360(%rbx)
movl 0x18(%rsp), %eax
movl 0x20368(%rbx), %ecx
movl 0x20374(%rbx), %edx
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x20368(%rbx)
movq 0x2036c(%rbx), %xmm0
movq 0x1c(%rsp), %xmm1
movdqa %xmm0, %xmm2
pxor %xmm4, %xmm2
movdqa %xmm1, %xmm3
pxor %xmm4, %xmm3
pcmpgtd %xmm3, %xmm2
pand %xmm2, %xmm0
pandn %xmm1, %xmm2
por %xmm0, %xmm2
movq %xmm2, 0x2036c(%rbx)
movl 0x24(%rsp), %eax
cmpl %eax, %edx
cmoval %edx, %eax
movl %eax, 0x20374(%rbx)
jmp 0x8838
movl %ecx, %edx
xorl $0x5, %edx
movl 0x8(%rsp), %eax
movl %eax, %esi
xorl $0x1, %esi
orl %edx, %esi
jne 0x89a3
movq %rbx, %rdi
callq 0xb002
movq %rax, %rsi
callq 0xac11
movb $0x3, 0x2(%r12)
jmp 0x8830
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rcx)
movl $0x1, 0x2035c(%rbx)
jmp 0x8838
leal -0xf(%rcx), %edx
cmpl $0x1, %edx
ja 0x89b6
movb $0x5, 0x2(%r12)
jmp 0x8830
leal -0x9(%rcx), %edx
cmpl $0xe, %edx
ja 0x89d9
leaq 0x7d7b(%rip), %rcx      # 0x10740
movslq (%rcx,%rdx,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x7, 0x2(%r12)
jmp 0x8830
cmpl $0x25, %ecx
je 0x8a2f
cmpl $0x42, %ecx
jne 0x89f3
cmpl $0x18, %eax
jne 0x89f3
movb $0xa, 0x2(%r12)
jmp 0x8830
movb $0x1, 0x2(%r12)
jmp 0x8830
movq %rbx, %rdi
callq 0xb088
movq %rax, %rsi
callq 0xac11
movb $0x4, 0x2(%r12)
jmp 0x8830
movb $0xc, 0x2(%r12)
jmp 0x8830
movb $0x6, 0x2(%r12)
jmp 0x8830
movb $0x9, 0x2(%r12)
jmp 0x8830
movq (%rbx), %rdi
callq 0x6e18
xorl %eax, %eax
movl %eax, 0x8(%rbx)
movl %eax, 0x20384(%rbx)
movl %r15d, 0x20048(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq