// File: 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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movw %dx, %ax
movq 0x10(%rbp), %rdx
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, -0x48(%rbp)
movsd %xmm2, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2170
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2170
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x2170
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11190
cmpl $0x1, -0x2c(%rbp)
jl 0x171d0
cmpq $0x0, -0x38(%rbp)
jne 0x171f5
movl $0x3, %edi
movl $0x1, %esi
leaq 0x368f(%rip), %rdx      # 0x1a870
movb $0x0, %al
callq 0x5740
movq $0x0, -0x8(%rbp)
jmp 0x17541
movq -0xa8(%rbp), %rax
cmpl $0x2, 0x20018(%rax)
jne 0x1720e
movl $0x1b, -0x54(%rbp)
jmp 0x17215
movl $0x2b, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x174c9
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x40(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x1, 0x18(%rax)
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x48(%rax), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x10(%rax), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %ecx
subl $0x1, %ecx
andl $0x7, %ecx
movl $0x1, %edx
shll %cl, %edx
movl -0xdc(%rbp), %eax
sarl $0x3, %eax
cltq
movzbl -0xa0(%rbp,%rax), %ecx
orl %edx, %ecx
movb %cl, -0xa0(%rbp,%rax)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0xc0(%rbp), %rdx
leaq -0xd8(%rbp), %rcx
callq 0xd100
cmpl $0x0, -0x58(%rbp)
jne 0x17330
movq -0xc0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x174b9
movsd -0x80(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17352
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x17362
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movsd -0xe8(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x78(%rbp), %xmm1
movsd -0xb8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17391
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
jmp 0x173a1
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x70(%rbp), %xmm1
movsd -0xb0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x173d0
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xf8(%rbp)
jmp 0x173e0
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0xf8(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x98(%rbp), %xmm0
ucomisd -0xd8(%rbp), %xmm0
jbe 0x17411
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
jmp 0x17421
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x100(%rbp)
movsd -0x100(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x90(%rbp), %xmm0
ucomisd -0xd0(%rbp), %xmm0
jbe 0x17455
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
jmp 0x17465
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x88(%rbp), %xmm0
ucomisd -0xc8(%rbp), %xmm0
jbe 0x17499
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
jmp 0x174a9
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0x174bb
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x1721c
cmpq $0x0, -0x28(%rbp)
jne 0x174db
leaq -0xa0(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x54(%rbp), %esi
movq -0x18(%rbp), %rdx
movw -0x1a(%rbp), %cx
movq -0x28(%rbp), %r8
movq 0x10(%rbp), %rax
movsd -0x40(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
leaq -0x80(%rbp), %r9
leaq -0x98(%rbp), %r10
movswl %cx, %ecx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x16a20
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
leaq -0x80(%rbp), %rdx
leaq -0x98(%rbp), %rcx
callq 0x13630
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nop