// File: /workspace/github_cpp_makefile_star2_O1/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 0x8851
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 0x6f58
xorl %eax, %eax
movl %eax, 0x8(%rbx)
movl %eax, 0x20384(%rbx)
movq (%rbx), %rdi
callq 0x6f53
movq %rax, %r14
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x8b80
movl $0x0, %r15d
testl %eax, %eax
je 0x8b57
leaq 0x20360(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0xc(%rsp), %r13
leaq 0x8(%rsp), %rbp
cmpl %r15d, 0x20044(%rbx)
jne 0x8909
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x8d84(%rip), %xmm0     # 0x11660
cvttsd2si %xmm0, %r15d
movslq %r15d, %rsi
addl $0x1f4, %r15d           # imm = 0x1F4
shlq $0x4, %rsi
movq 0x20050(%rbx), %rdi
addq $0x1f40, %rsi           # imm = 0x1F40
callq 0x2df9
movq %rax, 0x20050(%rbx)
movq 0x20050(%rbx), %rcx
movslq 0x20044(%rbx), %rdx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %r12
movb 0x8(%rsp), %al
movb %al, (%rcx,%rdx)
movl 0xc(%rsp), %eax
movb %al, 0x1(%rcx,%rdx)
movb $0x0, 0x3(%rcx,%rdx)
movq %r14, 0x8(%rcx,%rdx)
cmpb $0x0, 0x10(%rbx)
jns 0x8944
movb $0x2, 0x3(%r12)
cmpb $0x0, 0x11(%rbx)
jns 0x8950
orb $0x1, 0x3(%r12)
cmpl $0x15, %eax
ja 0x8a6c
movl $0x200858, %ecx         # imm = 0x200858
btl %eax, %ecx
jae 0x8a6c
movb $0x2, 0x2(%r12)
testb $0x3, 0x3(%r12)
je 0x89a1
incl 0x20044(%rbx)
movq (%rbx), %rdi
callq 0x6f53
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x8b80
testl %eax, %eax
jne 0x88c3
jmp 0x8b57
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 0x8de6
addq $0x20, %rsp
testl %eax, %eax
je 0x8975
cmpl $0x0, 0x2035c(%rbx)
je 0x8a99
movl 0x10(%rsp), %eax
movl 0x20360(%rbx), %ecx
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl 0x20364(%rbx), %ecx
movl %eax, 0x20360(%rbx)
movl 0x14(%rsp), %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0x20364(%rbx)
movl 0x20368(%rbx), %eax
movl 0x18(%rsp), %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x20368(%rbx)
movl 0x2036c(%rbx), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x2036c(%rbx)
movl 0x20370(%rbx), %eax
movl 0x20(%rsp), %ecx
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x20370(%rbx)
movl 0x20374(%rbx), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0x20374(%rbx)
jmp 0x8975
movl %eax, %edx
xorl $0x5, %edx
movl 0x8(%rsp), %ecx
movl %ecx, %esi
xorl $0x1, %esi
orl %edx, %esi
jne 0x8ac0
movq %rbx, %rdi
callq 0xb531
movq %rax, %rsi
callq 0xb0b0
movb $0x3, 0x2(%r12)
jmp 0x896d
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rcx)
movl $0x1, 0x2035c(%rbx)
jmp 0x8975
leal -0xf(%rax), %edx
cmpl $0x1, %edx
ja 0x8ad3
movb $0x5, 0x2(%r12)
jmp 0x896d
leal -0x9(%rax), %edx
cmpl $0xe, %edx
ja 0x8af6
leaq 0x8bc6(%rip), %rcx      # 0x116a8
movslq (%rcx,%rdx,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x7, 0x2(%r12)
jmp 0x896d
cmpl $0x25, %eax
je 0x8b4c
cmpl $0x42, %eax
jne 0x8b10
cmpl $0x18, %ecx
jne 0x8b10
movb $0xa, 0x2(%r12)
jmp 0x896d
movb $0x1, 0x2(%r12)
jmp 0x896d
movq %rbx, %rdi
callq 0xb5b7
movq %rax, %rsi
callq 0xb0b0
movb $0x4, 0x2(%r12)
jmp 0x896d
movb $0xc, 0x2(%r12)
jmp 0x896d
movb $0x6, 0x2(%r12)
jmp 0x896d
movb $0x9, 0x2(%r12)
jmp 0x896d
movq (%rbx), %rdi
callq 0x6f58
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