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

{
    DGNInfo     *psDGN = (DGNInfo *) hDGN;

/* -------------------------------------------------------------------- */
/*      Check various conditions.                                       */
/* -------------------------------------------------------------------- */
    if( psElement->raw_bytes == 0 
        || psElement->raw_bytes != psElement->size )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Raw bytes not loaded, or not matching element size." );
        return FALSE;
    }

    if( nNewSize % 2 == 1 )
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "DGNResizeElement(%d): "
                  "can't change to odd (not divisible by two) size.", 
                  nNewSize );
        return FALSE;
    }

    if( nNewSize == psElement->raw_bytes )
        return TRUE;

/* -------------------------------------------------------------------- */
/*      Mark the existing element as deleted if the element has to      */
/*      move to the end of the file.                                    */
/* -------------------------------------------------------------------- */

    if( psElement->offset != -1 )
    {
        int nOldFLoc = VSIFTell( psDGN->fp );
        unsigned char abyLeader[2];
        
        if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0
            || VSIFRead( abyLeader, sizeof(abyLeader), 1, psDGN->fp ) != 1 )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "Failed seek or read when trying to mark existing\n"
                      "element as deleted in DGNResizeElement()\n" );
            return FALSE;
        }

        abyLeader[1] |= 0x80;
        
        if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0
            || VSIFWrite( abyLeader, sizeof(abyLeader), 1, psDGN->fp ) != 1 )
        {
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "Failed seek or write when trying to mark existing\n"
                      "element as deleted in DGNResizeElement()\n" );
            return FALSE;
        }

        VSIFSeek( psDGN->fp, SEEK_SET, nOldFLoc );

        if( psElement->element_id != -1 && psDGN->index_built )
            psDGN->element_index[psElement->element_id].flags 
                |= DGNEIF_DELETED;
    }

    psElement->offset = -1; /* move to end of file. */
    psElement->element_id = -1;

/* -------------------------------------------------------------------- */
/*      Set the new size information, and realloc the raw data buffer.  */
/* -------------------------------------------------------------------- */
    psElement->size = nNewSize;
    psElement->raw_data = (unsigned char *) 
        CPLRealloc( psElement->raw_data, nNewSize );
    psElement->raw_bytes = nNewSize;

/* -------------------------------------------------------------------- */
/*      Update the size information within the raw buffer.              */
/* -------------------------------------------------------------------- */
    int nWords = (nNewSize / 2) - 2;

    psElement->raw_data[2] = (unsigned char) (nWords % 256);
    psElement->raw_data[3] = (unsigned char) (nWords / 256);

    return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x40(%rsi), %eax
testl %eax, %eax
je 0xb674
movq %rsi, %rbx
cmpl 0x4(%rsi), %eax
jne 0xb674
movl %edx, %ebp
movl %edx, %ecx
andl $0x80000001, %ecx       # imm = 0x80000001
cmpl $0x1, %ecx
jne 0xb6a1
leaq 0x53fc(%rip), %rdx      # 0x10a58
xorl %r15d, %r15d
movl $0x3, %edi
movl $0x1, %esi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x460c
jmp 0xb68f
leaq 0x53a9(%rip), %rdx      # 0x10a24
xorl %r15d, %r15d
movl $0x3, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x460c
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r15d
cmpl %ebp, %eax
je 0xb68f
cmpl $-0x1, (%rbx)
je 0xb771
movq %rdi, %r14
movq (%rdi), %rdi
callq 0x6e13
movq %rax, %r12
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x6e0e
leaq 0x53c6(%rip), %r13      # 0x10a9f
testl %eax, %eax
jne 0xb7bd
movq (%r14), %rcx
leaq 0x6(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x6e1d
cmpq $0x1, %rax
jne 0xb7bd
orb $-0x80, 0x7(%rsp)
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %edx, %edx
callq 0x6e0e
leaq 0x53df(%rip), %r13      # 0x10afa
testl %eax, %eax
jne 0xb7bd
movq (%r14), %rcx
leaq 0x6(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x6e22
cmpq $0x1, %rax
jne 0xb7bd
movq (%r14), %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x6e0e
movslq 0x8(%rbx), %rax
cmpq $-0x1, %rax
je 0xb771
cmpl $0x0, 0x20040(%r14)
je 0xb771
movq 0x20050(%r14), %rcx
shlq $0x4, %rax
orb $0x1, 0x3(%rcx,%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rbx)
movl %eax, 0x8(%rbx)
movl %ebp, 0x4(%rbx)
movq 0x48(%rbx), %rdi
movslq %ebp, %rsi
callq 0x2dcb
movq %rax, 0x48(%rbx)
movl %ebp, 0x40(%rbx)
movl %ebp, %ecx
shrl $0x1f, %ecx
addl %ebp, %ecx
sarl %ecx
leal -0x2(%rcx), %edx
addl $0xfd, %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
movb %dl, 0x2(%rax)
movq 0x48(%rbx), %rax
movb %ch, 0x3(%rax)
movl $0x1, %r15d
jmp 0xb68f
movl $0x3, %edi
movl $0x1, %esi
movq %r13, %rdx
jmp 0xb688