// File: rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNCreateComplexHeaderFromGroup
DGNElemCore *
DGNCreateComplexHeaderFromGroup( DGNHandle hDGN, int nType, 
                                 int nNumElems, DGNElemCore **papsElems )

{
    int         nTotalLength = 5;
    int         i, nLevel;
    DGNElemCore *psCH;
    DGNPoint    sMin = {0.0,0.0,0.0}, sMax = {0.0,0.0,0.0};

    DGNLoadTCB( hDGN );

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

/* -------------------------------------------------------------------- */
/*      Collect the total size, and bounds.                             */
/* -------------------------------------------------------------------- */
    nLevel = papsElems[0]->level;

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

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

        papsElems[i]->complex = TRUE;
        papsElems[i]->raw_data[0] |= 0x80;

        if( papsElems[i]->level != nLevel )
        {
            CPLError( CE_Warning, CPLE_AppDefined, 
                      "Not all level values matching in a complex set group!");
        }

        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);
        }
    }

/* -------------------------------------------------------------------- */
/*      Create the corresponding complex header.                        */
/* -------------------------------------------------------------------- */
    psCH = DGNCreateComplexHeaderElem( hDGN, nType, nTotalLength, nNumElems );
    DGNUpdateElemCore( hDGN, psCH, papsElems[0]->level, psCH->graphic_group,
                       psCH->color, psCH->weight, psCH->style );

    DGNWriteBounds( (DGNInfo *) hDGN, psCH, &sMin, &sMax );
    
    return psCH;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2170
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2170
movq -0x10(%rbp), %rdi
callq 0x11190
cmpl $0x1, -0x18(%rbp)
jl 0x161f6
cmpq $0x0, -0x20(%rbp)
jne 0x1621b
movl $0x3, %edi
movl $0x1, %esi
leaq 0x45d5(%rip), %rdx      # 0x1a7dc
movb $0x0, %al
callq 0x5740
movq $0x0, -0x8(%rbp)
jmp 0x164ed
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x16482
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x40(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x1, 0x18(%rax)
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x48(%rax), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x10(%rax), %eax
cmpl -0x2c(%rbp), %eax
je 0x162b2
movl $0x2, %edi
movl $0x1, %esi
leaq 0x4564(%rip), %rdx      # 0x1a80f
movb $0x0, %al
callq 0x5740
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x80(%rbp), %rdx
leaq -0x98(%rbp), %rcx
callq 0xd100
cmpl $0x0, -0x28(%rbp)
jne 0x16316
movq -0x80(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x16472
movsd -0x50(%rbp), %xmm1
movsd -0x80(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x16335
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
jmp 0x16342
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x48(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1636e
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0x1637b
movsd -0x78(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x40(%rbp), %xmm1
movsd -0x70(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x163a7
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
jmp 0x163b4
movsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x68(%rbp), %xmm0
ucomisd -0x98(%rbp), %xmm0
jbe 0x163df
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
jmp 0x163ef
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x60(%rbp), %xmm0
ucomisd -0x90(%rbp), %xmm0
jbe 0x1641a
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0x1642a
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x58(%rbp), %xmm0
ucomisd -0x88(%rbp), %xmm0
jbe 0x16455
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
jmp 0x16465
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
jmp 0x16474
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1622f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x24(%rbp), %edx
movl -0x18(%rbp), %ecx
callq 0x15e50
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl 0x10(%rax), %edx
movq -0x38(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x38(%rbp), %rax
movl 0x28(%rax), %r8d
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %r9d
movq -0x38(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, (%rsp)
callq 0x12ee0
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x13630
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)