// File: /workspace/github_cpp_makefile_star2_O2/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
movslq (%rsi), %rsi
cmpq $-0x1, %rsi
je 0xad3a
movq (%rbx), %rdi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x697a
testl %eax, %eax
jne 0xad1a
movq 0x48(%r14), %rdi
movslq 0x40(%r14), %rsi
movq (%rbx), %rcx
pushq $0x1
popq %rdx
callq 0x698e
cmpq $0x1, %rax
jne 0xad1a
movl 0x8(%r14), %eax
incl %eax
movl %eax, 0x8(%rbx)
cmpl 0x20044(%rbx), %eax
jne 0xae17
leaq 0xa(%rsp), %rdi
orw $-0x1, (%rdi)
movq (%rbx), %rcx
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x698e
movq (%rbx), %rdi
pushq $-0x2
popq %rsi
pushq $0x1
popq %rbp
movl %ebp, %edx
callq 0x697a
jmp 0xae1a
movl (%r14), %ecx
movl 0x40(%r14), %r8d
leaq 0x4e3e(%rip), %rdx      # 0xfb66
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x439c
jmp 0xae1a
cmpl $0x0, 0x20040(%rbx)
jne 0xad4b
movq %rbx, %rdi
callq 0x8142
movl 0x20044(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x80ec
testl %eax, %eax
je 0xadc5
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x8448
testl %eax, %eax
je 0xadc5
movq (%rbx), %rdi
callq 0x697f
movq %rax, %rcx
movl %ecx, (%r14)
movl 0x20044(%rbx), %edx
movl %edx, 0x8(%r14)
cmpl 0x20048(%rbx), %edx
jne 0xadc9
movslq %edx, %rsi
addq $0x1f4, %rsi            # imm = 0x1F4
movl %esi, 0x20048(%rbx)
movq 0x20050(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2d0a
movq %rax, 0x20050(%rbx)
movl 0x20044(%rbx), %edx
movl (%r14), %ecx
jmp 0xadd0
xorl %ebp, %ebp
jmp 0xae1a
movq 0x20050(%rbx), %rax
movslq %edx, %rdx
movq %rdx, %rdi
shlq $0x4, %rdi
movb 0x10(%r14), %sil
movb %sil, (%rax,%rdi)
movb 0x14(%r14), %sil
movb %sil, 0x1(%rax,%rdi)
movb 0xc(%r14), %sil
movb %sil, 0x2(%rax,%rdi)
movslq %ecx, %rsi
movq %rsi, 0x8(%rax,%rdi)
cmpl $0x0, 0x18(%r14)
setne %cl
addb %cl, %cl
movb %cl, 0x3(%rax,%rdi)
incl %edx
movl %edx, 0x20044(%rbx)
jmp 0xacb0
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq