// File: rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNWriteElement
int DGNWriteElement( DGNHandle hDGN, DGNElemCore *psElement )

{
    DGNInfo     *psDGN = (DGNInfo *) hDGN;

/* ==================================================================== */
/*      If this element hasn't been positioned yet, place it at the     */
/*      end of the file.                                                */
/* ==================================================================== */
    if( psElement->offset == -1 )
    {
        int nJunk;

        // We must have an index, in order to properly assign the 
        // element id of the newly written element.  Ensure it is built.
        if( !psDGN->index_built )
            DGNBuildIndex( psDGN );

        // Read the current "last" element.
        if( !DGNGotoElement( hDGN, psDGN->element_count-1 ) )
            return FALSE;

        if( !DGNLoadRawElement( psDGN, &nJunk, &nJunk ) )
            return FALSE;

        // Establish the position of the new element.
        psElement->offset = VSIFTell( psDGN->fp );
        psElement->element_id = psDGN->element_count;

        // Grow element buffer if needed.
        if( psDGN->element_count == psDGN->max_element_count )
        {
            psDGN->max_element_count += 500;
            
            psDGN->element_index = (DGNElementInfo *) 
                CPLRealloc( psDGN->element_index, 
                            psDGN->max_element_count * sizeof(DGNElementInfo));
        }

        // Set up the element info
        DGNElementInfo *psInfo;
        
        psInfo = psDGN->element_index + psDGN->element_count;
        psInfo->level = (unsigned char) psElement->level;
        psInfo->type = (unsigned char) psElement->type;
        psInfo->stype = (unsigned char) psElement->stype;
        psInfo->offset = psElement->offset;
        if( psElement->complex )
            psInfo->flags = DGNEIF_COMPLEX;
        else
            psInfo->flags = 0;
    
        psDGN->element_count++;
    }

/* -------------------------------------------------------------------- */
/*      Write out the element.                                          */
/* -------------------------------------------------------------------- */
    if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0 
        || VSIFWrite( psElement->raw_data, psElement->raw_bytes, 
                      1, psDGN->fp) != 1 )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Error seeking or writing new element of %d bytes at %d.",
                  psElement->offset, 
                  psElement->raw_bytes );
        return FALSE;
    }

    psDGN->next_element_id = psElement->element_id + 1;

/* -------------------------------------------------------------------- */
/*      Write out the end of file 0xffff marker (if we were             */
/*      extending the file), but push the file pointer back before      */
/*      this EOF when done.                                             */
/* -------------------------------------------------------------------- */
    if( psDGN->next_element_id == psDGN->element_count )
    {
        unsigned char abyEOF[2];

        abyEOF[0] = 0xff;
        abyEOF[1] = 0xff;

        VSIFWrite( abyEOF, 2, 1, psDGN->fp );
        VSIFSeek( psDGN->fp, -2, SEEK_CUR );
    }
        
    return TRUE;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0x1225f
movq -0x20(%rbp), %rax
cmpl $0x0, 0x20040(%rax)
jne 0x1210b
movq -0x20(%rbp), %rdi
callq 0xcb30
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movl 0x20044(%rax), %esi
subl $0x1, %esi
callq 0xca90
cmpl $0x0, %eax
jne 0x12132
movl $0x0, -0x4(%rbp)
jmp 0x12334
movq -0x20(%rbp), %rdi
leaq -0x24(%rbp), %rdx
movq %rdx, %rsi
callq 0xcfe0
cmpl $0x0, %eax
jne 0x12153
movl $0x0, -0x4(%rbp)
jmp 0x12334
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x9d00
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl 0x20044(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0x20044(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x20048(%rcx), %eax
jne 0x121d1
movq -0x20(%rbp), %rax
movl 0x20048(%rax), %ecx
addl $0x1f4, %ecx            # imm = 0x1F4
movl %ecx, 0x20048(%rax)
movq -0x20(%rbp), %rax
movq 0x20050(%rax), %rdi
movq -0x20(%rbp), %rax
movslq 0x20048(%rax), %rsi
shlq $0x4, %rsi
callq 0x3120
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20050(%rax)
movq -0x20(%rbp), %rax
movq 0x20050(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x20044(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x18(%rbp), %rax
movslq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x12244
movq -0x30(%rbp), %rax
movb $0x2, 0x3(%rax)
jmp 0x1224c
movq -0x30(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x20(%rbp), %rax
movl 0x20044(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20044(%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x9cd0
cmpl $0x0, %eax
jne 0x122a0
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x18(%rbp), %rax
movslq 0x40(%rax), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %edx
callq 0x9d80
cmpq $0x1, %rax
je 0x122cf
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl 0x40(%rax), %r8d
movl $0x3, %edi
movl $0x1, %esi
leaq 0x8473(%rip), %rdx      # 0x1a732
movb $0x0, %al
callq 0x5740
movl $0x0, -0x4(%rbp)
jmp 0x12334
movq -0x18(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x20044(%rcx), %eax
jne 0x1232d
movb $-0x1, -0x32(%rbp)
movb $-0x1, -0x31(%rbp)
leaq -0x32(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x9d80
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq $-0x2, %rsi
movl $0x1, %edx
callq 0x9cd0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)