// File: /workspace/github_cpp_makefile_star2_O2/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 0x814c
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x1, 0x20040(%rdi)
callq 0xa35c
movq (%rbx), %rdi
callq 0x697f
movq %rax, %r14
leaq 0x20360(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0xc(%rsp), %r12
leaq 0x8(%rsp), %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x8448
testl %eax, %eax
je 0x842a
movl 0x20044(%rbx), %ecx
movq 0x20050(%rbx), %rax
cmpl %r15d, %ecx
jne 0x81f4
cvtsi2sd %r15d, %xmm0
mulsd 0x74d3(%rip), %xmm0     # 0xf698
cvttsd2si %xmm0, %r15d
movslq %r15d, %rsi
addl $0x1f4, %r15d           # imm = 0x1F4
shlq $0x4, %rsi
addq $0x1f40, %rsi           # imm = 0x1F40
movq %rax, %rdi
callq 0x2d0a
movq %rax, 0x20050(%rbx)
movl 0x20044(%rbx), %ecx
movslq %ecx, %rdx
shlq $0x4, %rdx
leaq (%rax,%rdx), %rbp
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 0x8226
movb $0x2, 0x3(%rbp)
movb $0x3, %al
cmpb $0x0, 0x11(%rbx)
jns 0x822f
movb %al, 0x3(%rbp)
cmpl $0x15, %ecx
ja 0x8351
movl $0x200858, %eax         # imm = 0x200858
btl %ecx, %eax
jae 0x8351
movb $0x2, 0x2(%rbp)
testb $0x3, 0x3(%rbp)
je 0x8266
incl 0x20044(%rbx)
movq (%rbx), %rdi
callq 0x697f
movq %rax, %r14
jmp 0x8190
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 0x860f
addq $0x20, %rsp
testl %eax, %eax
je 0x8250
cmpl $0x0, 0x2035c(%rbx)
je 0x837c
movq 0x20360(%rbx), %xmm0
movq 0x10(%rsp), %xmm1
movdqa %xmm1, %xmm2
movdqa 0x7418(%rip), %xmm4     # 0xf6e0
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 0x8250
movl %ecx, %edx
xorl $0x5, %edx
movl 0x8(%rsp), %eax
movl %eax, %esi
xorl $0x1, %esi
orl %edx, %esi
jne 0x83a3
movq %rbx, %rdi
callq 0xa687
movq %rax, %rsi
callq 0xa2bd
movb $0x3, 0x2(%rbp)
jmp 0x824a
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
movapd 0x10(%rsp), %xmm0
movupd %xmm0, (%rcx)
movl $0x1, 0x2035c(%rbx)
jmp 0x8250
leal -0xf(%rcx), %edx
cmpl $0x1, %edx
ja 0x83b4
movb $0x5, 0x2(%rbp)
jmp 0x824a
leal -0x9(%rcx), %edx
cmpl $0xe, %edx
ja 0x83d5
leaq 0x738d(%rip), %rcx      # 0xf750
movslq (%rcx,%rdx,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x7, 0x2(%rbp)
jmp 0x824a
cmpl $0x25, %ecx
je 0x8421
cmpl $0x42, %ecx
jne 0x83ed
cmpl $0x18, %eax
jne 0x83ed
movb $0xa, 0x2(%rbp)
jmp 0x824a
movb $0x1, 0x2(%rbp)
jmp 0x824a
movq %rbx, %rdi
callq 0xa70b
movq %rax, %rsi
callq 0xa2bd
movb $0x4, 0x2(%rbp)
jmp 0x824a
movb $0xc, 0x2(%rbp)
jmp 0x824a
movb $0x6, 0x2(%rbp)
jmp 0x824a
movb $0x9, 0x2(%rbp)
jmp 0x824a
movq %rbx, %rdi
callq 0xa35c
movl %r15d, 0x20048(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq