// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r14
pushq %rbx
subq $0x10, %rsp
movq %rsi, %r14
movq %rdi, %rbx
cmpl $-0x1, (%rsi)
je 0xbdd0
movq (%rbx), %rdi
movslq (%r14), %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x6f4e
testl %eax, %eax
jne 0xbdac
movq 0x48(%r14), %rdi
movslq 0x40(%r14), %rsi
movq (%rbx), %rcx
movl $0x1, %edx
callq 0x6f62
cmpq $0x1, %rax
jne 0xbdac
movl 0x8(%r14), %eax
incl %eax
movl %eax, 0x8(%rbx)
movl $0x1, %ebp
cmpl 0x20044(%rbx), %eax
jne 0xbeaa
leaq 0xa(%rsp), %rdi
movw $0xffff, (%rdi)         # imm = 0xFFFF
movq (%rbx), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x6f62
movq (%rbx), %rdi
movl $0x1, %ebp
movq $-0x2, %rsi
movl $0x1, %edx
callq 0x6f4e
jmp 0xbeaa
movl (%r14), %ecx
movl 0x40(%r14), %r8d
leaq 0x5cec(%rip), %rdx      # 0x11aa6
movl $0x3, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x46dc
jmp 0xbeaa
cmpl $0x0, 0x20040(%rbx)
jne 0xbde1
movq %rbx, %rdi
callq 0x8847
movl 0x20044(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x87ec
testl %eax, %eax
je 0xbea8
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x8b80
testl %eax, %eax
je 0xbea8
movq (%rbx), %rdi
callq 0x6f53
movl %eax, (%r14)
movl 0x20044(%rbx), %eax
movl %eax, 0x8(%r14)
movslq 0x20048(%rbx), %rsi
cmpl %esi, %eax
jne 0xbe55
addq $0x1f4, %rsi            # imm = 0x1F4
movl %esi, 0x20048(%rbx)
movq 0x20050(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2df9
movq %rax, 0x20050(%rbx)
movq 0x20050(%rbx), %rax
movslq 0x20044(%rbx), %rcx
leal 0x1(%rcx), %edx
shlq $0x4, %rcx
movb 0x10(%r14), %sil
movb %sil, (%rax,%rcx)
movb 0x14(%r14), %sil
movb %sil, 0x1(%rax,%rcx)
movb 0xc(%r14), %sil
movb %sil, 0x2(%rax,%rcx)
movslq (%r14), %rsi
movq %rsi, 0x8(%rax,%rcx)
cmpl $0x0, 0x18(%r14)
setne %sil
addb %sil, %sil
movb %sil, 0x3(%rax,%rcx)
movl %edx, 0x20044(%rbx)
jmp 0xbd2a
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq