// File: /workspace/github_cpp_makefile_star2_O3/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 0xb88d
movq (%rbx), %rdi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x6e0e
testl %eax, %eax
jne 0xb869
movq 0x48(%r14), %rdi
movslq 0x40(%r14), %rsi
movq (%rbx), %rcx
movl $0x1, %edx
callq 0x6e22
cmpq $0x1, %rax
jne 0xb869
movl 0x8(%r14), %eax
incl %eax
movl %eax, 0x8(%rbx)
movl $0x1, %ebp
cmpl 0x20044(%rbx), %eax
jne 0xb91a
leaq 0xa(%rsp), %rdi
movw $0xffff, (%rdi)         # imm = 0xFFFF
movq (%rbx), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x6e22
movq (%rbx), %rdi
movl $0x1, %ebp
movq $-0x2, %rsi
movl $0x1, %edx
callq 0x6e0e
jmp 0xb91a
movl (%r14), %ecx
movl 0x40(%r14), %r8d
leaq 0x52df(%rip), %rdx      # 0x10b56
movl $0x3, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x460c
jmp 0xb91a
cmpl $0x0, 0x20040(%rbx)
jne 0xb89e
movq %rbx, %rdi
callq 0x8707
movl 0x20044(%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x86ac
testl %eax, %eax
je 0xb918
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x8a63
testl %eax, %eax
je 0xb918
movq (%rbx), %rdi
callq 0x6e13
movq %rax, %rcx
movl %ecx, (%r14)
movl 0x20044(%rbx), %edx
movl %edx, 0x8(%r14)
cmpl 0x20048(%rbx), %edx
jne 0xb925
movslq %edx, %rsi
addq $0x1f4, %rsi            # imm = 0x1F4
movl %esi, 0x20048(%rbx)
movq 0x20050(%rbx), %rdi
shlq $0x4, %rsi
callq 0x2dcb
movq %rax, 0x20050(%rbx)
movl 0x20044(%rbx), %edx
movl (%r14), %ecx
jmp 0xb92c
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq
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 0xb7ea