// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
je 0x11e85
movq -0x18(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x4(%rcx), %eax
je 0x11ea9
movl $0x3, %edi
movl $0x1, %esi
leaq 0x876a(%rip), %rdx      # 0x1a600
movb $0x0, %al
callq 0x5740
movl $0x0, -0x4(%rbp)
jmp 0x120b9
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x11ee0
movl -0x1c(%rbp), %ecx
movl $0x3, %edi
movl $0x1, %esi
leaq 0x8767(%rip), %rdx      # 0x1a634
movb $0x0, %al
callq 0x5740
movl $0x0, -0x4(%rbp)
jmp 0x120b9
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jne 0x11ef8
movl $0x1, -0x4(%rbp)
jmp 0x120b9
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x1202c
movq -0x28(%rbp), %rax
movq (%rax), %rdi
callq 0x9d00
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x9cd0
cmpl $0x0, %eax
jne 0x11f4e
leaq -0x2e(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x9d40
cmpq $0x1, %rax
je 0x11f72
movl $0x3, %edi
movl $0x1, %esi
leaq 0x871c(%rip), %rdx      # 0x1a67b
movb $0x0, %al
callq 0x5740
movl $0x0, -0x4(%rbp)
jmp 0x120b9
movzbl -0x2d(%rbp), %eax
orl $0x80, %eax
movb %al, -0x2d(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x9cd0
cmpl $0x0, %eax
jne 0x11fb8
leaq -0x2e(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movl $0x2, %esi
movl $0x1, %edx
callq 0x9d80
cmpq $0x1, %rax
je 0x11fdc
movl $0x3, %edi
movl $0x1, %esi
leaq 0x870d(%rip), %rdx      # 0x1a6d6
movb $0x0, %al
callq 0x5740
movl $0x0, -0x4(%rbp)
jmp 0x120b9
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x9cd0
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0x1202a
movq -0x28(%rbp), %rax
cmpl $0x0, 0x20040(%rax)
je 0x1202a
movq -0x28(%rbp), %rax
movq 0x20050(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movzbl 0x3(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x3(%rax)
jmp 0x1202c
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rdi
movslq -0x1c(%rbp), %rsi
callq 0x3120
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x40(%rax)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
subl $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %dl, %cl
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movb %cl, 0x2(%rax)
movl -0x34(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movb %al, %cl
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movb %cl, 0x3(%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)