// File: /workspace/github_cpp_makefile_star2_O1/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 0xbbb8
movq %rsi, %rbx
cmpl 0x4(%rsi), %eax
jne 0xbbb8
movl %edx, %ebp
movl %edx, %ecx
andl $0x80000001, %ecx       # imm = 0x80000001
cmpl $0x1, %ecx
jne 0xbbe5
leaq 0x5e08(%rip), %rdx      # 0x119a8
xorl %r15d, %r15d
movl $0x3, %edi
movl $0x1, %esi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x46dc
jmp 0xbbd3
leaq 0x5db5(%rip), %rdx      # 0x11974
xorl %r15d, %r15d
movl $0x3, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x46dc
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 0xbbd3
cmpl $-0x1, (%rbx)
je 0xbcb5
movq %rdi, %r14
movq (%rdi), %rdi
callq 0x6f53
movq %rax, %r12
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x6f4e
leaq 0x5dd2(%rip), %r13      # 0x119ef
testl %eax, %eax
jne 0xbd01
movq (%r14), %rcx
leaq 0x6(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x6f5d
cmpq $0x1, %rax
jne 0xbd01
orb $-0x80, 0x7(%rsp)
movq (%r14), %rdi
movslq (%rbx), %rsi
xorl %edx, %edx
callq 0x6f4e
leaq 0x5deb(%rip), %r13      # 0x11a4a
testl %eax, %eax
jne 0xbd01
movq (%r14), %rcx
leaq 0x6(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x6f62
cmpq $0x1, %rax
jne 0xbd01
movq (%r14), %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x6f4e
movslq 0x8(%rbx), %rax
cmpq $-0x1, %rax
je 0xbcb5
cmpl $0x0, 0x20040(%r14)
je 0xbcb5
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 0x2df9
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 0xbbd3
movl $0x3, %edi
movl $0x1, %esi
movq %r13, %rdx
jmp 0xbbcc