// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movl 0x40(%rsi), %eax
testl %eax, %eax
je 0xab4b
movq %rsi, %rbx
cmpl 0x4(%rsi), %eax
jne 0xab4b
movl %edx, %ebp
movl %edx, %ecx
andl $0x80000001, %ecx       # imm = 0x80000001
cmpl $0x1, %ecx
jne 0xab74
leaq 0x4f31(%rip), %rdx      # 0xfa68
xorl %r15d, %r15d
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x439c
jmp 0xab62
leaq 0x4ee2(%rip), %rdx      # 0xfa34
xorl %r15d, %r15d
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x439c
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r15
cmpl %ebp, %eax
je 0xab62
cmpl $-0x1, (%rbx)
je 0xac45
movq %rdi, %r14
movq (%rdi), %rdi
callq 0x697f
movq %rax, %r12
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x697a
leaq 0x4f05(%rip), %r13      # 0xfaaf
testl %eax, %eax
jne 0xac87
movq %r12, 0x10(%rsp)
movq (%r14), %rcx
leaq 0xe(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x6989
cmpq $0x1, %rax
jne 0xac87
orb $-0x80, 0xf(%rsp)
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %edx, %edx
callq 0x697a
leaq 0x4f1d(%rip), %r13      # 0xfb0a
testl %eax, %eax
jne 0xac87
movq (%r14), %rcx
leaq 0xe(%rsp), %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
callq 0x698e
cmpq $0x1, %rax
jne 0xac87
movq (%r14), %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x697a
movslq 0x8(%rbx), %rax
cmpq $-0x1, %rax
je 0xac41
cmpl $0x0, 0x20040(%r14)
je 0xac41
movq 0x20050(%r14), %rcx
shlq $0x4, %rax
orb $0x1, 0x3(%rcx,%rax)
pushq $0x1
popq %r15
orl $-0x1, (%rbx)
orl $-0x1, 0x8(%rbx)
movl %ebp, 0x4(%rbx)
movq 0x48(%rbx), %rdi
movslq %ebp, %rsi
callq 0x2d0a
movq %rax, %rcx
movq %rax, 0x48(%rbx)
movl %ebp, 0x40(%rbx)
pushq $0x2
popq %rsi
movl %ebp, %eax
cltd
idivl %esi
addl $-0x2, %eax
movb %al, 0x2(%rcx)
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq 0x48(%rbx), %rcx
movb %al, 0x3(%rcx)
jmp 0xab62
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
jmp 0xab5b