// File: /workspace/github_cpp_makefile_star2_O2/rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNCreateCellHeaderFromGroup
DGNElemCore *
DGNCreateCellHeaderFromGroup( DGNHandle hDGN, const char *pszName, 
                              short nClass, short *panLevels, 
                              int nNumElems, DGNElemCore **papsElems,
                              DGNPoint *psOrigin, 
                              double dfXScale, double dfYScale,
                              double dfRotation )

{
    int         nTotalLength;
    int         i, nLevel;
    DGNElemCore *psCH;
    DGNPoint    sMin={0.0,0.0,0.0}, sMax={0.0,0.0,0.0};
    unsigned char abyLevelsOccuring[8] = {0,0,0,0,0,0,0,0};
    DGNInfo *psInfo = (DGNInfo *) hDGN;

    DGNLoadTCB( hDGN );

    if( nNumElems < 1 || papsElems == NULL )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Need at least one element to form a cell." );
        return NULL;
    }

    if( psInfo->dimension == 2 )
        nTotalLength = 27;
    else
        nTotalLength = 43;

/* -------------------------------------------------------------------- */
/*      Collect the total size, and bounds.                             */
/* -------------------------------------------------------------------- */

    for( i = 0; i < nNumElems; i++ )
    {
        DGNPoint sThisMin, sThisMax;
        int  nLevel;

        nTotalLength += papsElems[i]->raw_bytes / 2;

        /* mark as complex */
        papsElems[i]->complex = TRUE;
        papsElems[i]->raw_data[0] |= 0x80;

        /* establish level */
        nLevel = papsElems[i]->level;
        abyLevelsOccuring[nLevel >> 3] |= (0x1 << ((nLevel-1)&0x7));
        
        DGNGetElementExtents( hDGN, papsElems[i], &sThisMin, &sThisMax );
        if( i == 0 )
        {
            sMin = sThisMin;
            sMax = sThisMax;
        }
        else
        {
            sMin.x = MIN(sMin.x,sThisMin.x);
            sMin.y = MIN(sMin.y,sThisMin.y);
            sMin.z = MIN(sMin.z,sThisMin.z);
            sMax.x = MAX(sMax.x,sThisMax.x);
            sMax.y = MAX(sMax.y,sThisMax.y);
            sMax.z = MAX(sMax.z,sThisMax.z);
        }
    }

/* -------------------------------------------------------------------- */
/*      It seems that the range needs to be adjusted according to       */
/*      the rotation and scaling.                                       */
/*                                                                      */
/*      NOTE: Omitting code ... this is already done in                 */
/*      DGNInverseTransformPoint() called from DGNWriteBounds().        */
/* -------------------------------------------------------------------- */
#ifdef notdef
    sMin.x -= psOrigin->x;
    sMin.y -= psOrigin->y;
    sMin.z -= psOrigin->z;
    sMax.x -= psOrigin->x;
    sMax.y -= psOrigin->y;
    sMax.z -= psOrigin->z;

    sMin.x /= ((DGNInfo *) hDGN)->scale;
    sMin.y /= ((DGNInfo *) hDGN)->scale;
    sMin.z /= ((DGNInfo *) hDGN)->scale;
    sMax.x /= ((DGNInfo *) hDGN)->scale;
    sMax.y /= ((DGNInfo *) hDGN)->scale;
    sMax.z /= ((DGNInfo *) hDGN)->scale;
#endif

/* -------------------------------------------------------------------- */
/*      Create the corresponding cell header.                           */
/* -------------------------------------------------------------------- */
    if( panLevels == NULL )
        panLevels = (short *) abyLevelsOccuring + 0;

    psCH = DGNCreateCellHeaderElem( hDGN, nTotalLength, pszName, 
                                    nClass, panLevels, 
                                    &sMin, &sMax, psOrigin, 
                                    dfXScale, dfYScale, dfRotation );
    DGNWriteBounds( (DGNInfo *) hDGN, psCH, &sMin, &sMax );
    
    return psCH;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm2, 0x78(%rsp)
movsd %xmm1, 0x70(%rsp)
movsd %xmm0, 0x68(%rsp)
movq %r9, %r12
movl %r8d, %ebp
movq %rcx, %r13
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
andq $0x0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x50(%rsp)
callq 0xa500
testl %ebp, %ebp
setg %al
testq %r12, %r12
setne %cl
testb %cl, %al
jne 0xd182
leaq 0x2b37(%rip), %rdx      # 0xfca4
xorl %r14d, %r14d
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x439c
jmp 0xd310
movq %r13, 0x58(%rsp)
movl %r15d, 0xc(%rsp)
movq %r14, 0x60(%rsp)
xorl %r13d, %r13d
movq %rbx, 0x48(%rsp)
cmpl $0x2, 0x20018(%rbx)
setne %r13b
shll $0x4, %r13d
addl $0x1b, %r13d
movl %ebp, %r14d
xorl %ebp, %ebp
pushq $0x1
popq %rbx
cmpq %rbp, %r14
je 0xd2a7
movq (%r12,%rbp,8), %rcx
movl 0x40(%rcx), %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %r15d
movl %ebx, 0x18(%rcx)
movq 0x48(%rcx), %rax
orb $-0x80, (%rax)
movq (%r12,%rbp,8), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
andb $0x7, %cl
movl %ebx, %edx
shll %cl, %edx
sarl $0x3, %eax
cltq
orb %dl, 0x50(%rsp,%rax)
movq 0x48(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
callq 0x84ea
testq %rbp, %rbp
je 0xd266
movapd 0x30(%rsp), %xmm0
minpd 0xa0(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
movsd 0x40(%rsp), %xmm0
minsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movapd 0x10(%rsp), %xmm0
maxpd 0x80(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movsd 0x20(%rsp), %xmm0
maxsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0xd29c
movq 0xb0(%rsp), %rax
movq %rax, 0x40(%rsp)
movaps 0xa0(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movapd 0x80(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
addl %r15d, %r13d
incq %rbp
jmp 0xd1b4
movq 0x58(%rsp), %rax
testq %rax, %rax
leaq 0x50(%rsp), %r8
cmovneq %rax, %r8
leaq 0x10(%rsp), %r15
movswl 0xc(%rsp), %ecx
leaq 0x30(%rsp), %r12
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
movq 0x60(%rsp), %rdx
movq %r12, %r9
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
pushq 0xf0(%rsp)
pushq %r15
callq 0xcd76
popq %rcx
popq %rdx
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xb6bf
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq