// File: /workspace/github_cpp_makefile_star2_O3/rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNAddRawAttrLink
int DGNAddRawAttrLink( DGNHandle hDGN, DGNElemCore *psElement, 
                       int nLinkSize, unsigned char *pabyRawLinkData )

{
    int   iLinkage;

    if( nLinkSize % 2 == 1 )
        nLinkSize++;

    if( psElement->size + nLinkSize > 768 )
    {
        CPLError( CE_Failure, CPLE_ElementTooBig, 
                  "Attempt to add %d byte linkage to element exceeds maximum"
                  " element size.", 
                  nLinkSize );
        return -1;
    }
    
/* -------------------------------------------------------------------- */
/*      Ensure the attribute linkage bit is set.                        */
/* -------------------------------------------------------------------- */
    psElement->properties |= DGNPF_ATTRIBUTES;

/* -------------------------------------------------------------------- */
/*      Append the attribute linkage to the linkage area.               */
/* -------------------------------------------------------------------- */
    psElement->attr_bytes += nLinkSize;
    psElement->attr_data = (unsigned char *) 
        CPLRealloc( psElement->attr_data, psElement->attr_bytes );
    
    memcpy( psElement->attr_data + (psElement->attr_bytes-nLinkSize), 
            pabyRawLinkData, nLinkSize );
    
/* -------------------------------------------------------------------- */
/*      Grow the raw data, if we have rawdata.                          */
/* -------------------------------------------------------------------- */
    psElement->raw_bytes = psElement->raw_bytes += nLinkSize;
    psElement->raw_data = (unsigned char *) 
        CPLRealloc( psElement->raw_data, psElement->raw_bytes );
    
    memcpy( psElement->raw_data + (psElement->raw_bytes-nLinkSize), 
            pabyRawLinkData, nLinkSize );
    
/* -------------------------------------------------------------------- */
/*      If the element is a shape or chain complex header, then we      */
/*      need to increase the total complex group size appropriately.    */
/* -------------------------------------------------------------------- */
    if( psElement->stype == DGNST_COMPLEX_HEADER ||
        psElement->stype == DGNST_TEXT_NODE )  // compatible structures
    {
        DGNElemComplexHeader *psCT = (DGNElemComplexHeader *) psElement;

        psCT->totlength += (nLinkSize / 2);

        psElement->raw_data[36] = (unsigned char) (psCT->totlength % 256);
        psElement->raw_data[37] = (unsigned char) (psCT->totlength / 256);
    }

/* -------------------------------------------------------------------- */
/*      Ensure everything is updated properly, including element        */
/*      length and properties.                                          */
/* -------------------------------------------------------------------- */
    DGNUpdateElemCoreExtended( hDGN, psElement );

/* -------------------------------------------------------------------- */
/*      Figure out what the linkage index is.                           */
/* -------------------------------------------------------------------- */
    for( iLinkage = 0; ; iLinkage++ )
    {
        if( DGNGetLinkage( hDGN, psElement, iLinkage, NULL, NULL, NULL, NULL )
            == NULL )
            break;
    }

    return iLinkage-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %edx, %eax
andl $0x80000001, %eax       # imm = 0x80000001
xorl %ebp, %ebp
cmpl $0x1, %eax
sete %bpl
addl %edx, %ebp
movl 0x4(%rsi), %eax
addl %ebp, %eax
cmpl $0x301, %eax            # imm = 0x301
jl 0xd423
leaq 0x38b9(%rip), %rdx      # 0x10cbe
movl $0x3, %edi
movl $0x1, %esi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x460c
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0xd4f8
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r14
orb $0x8, 0x25(%rsi)
movslq 0x34(%rsi), %rsi
movslq %ebp, %r12
addq %r12, %rsi
movl %esi, 0x34(%rbx)
movq 0x38(%rbx), %rdi
callq 0x2dcb
movq %rax, 0x38(%rbx)
movslq 0x34(%rbx), %rdi
subq %r12, %rdi
addq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2230
movslq 0x40(%rbx), %rsi
addq %r12, %rsi
movl %esi, 0x40(%rbx)
movq 0x48(%rbx), %rdi
callq 0x2dcb
movq %rax, 0x48(%rbx)
movslq 0x40(%rbx), %rdi
subq %r12, %rdi
addq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2230
movl 0xc(%rbx), %eax
cmpl $0xd, %eax
je 0xd498
cmpl $0x7, %eax
jne 0xd4c3
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
addl 0x50(%rbx), %eax
movl %eax, 0x50(%rbx)
movq 0x48(%rbx), %rcx
movb %al, 0x24(%rcx)
movl 0x50(%rbx), %eax
leal 0xff(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movq 0x48(%rbx), %rax
movb %ch, 0x25(%rax)
movq %rbx, %rsi
callq 0xbf25
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
leal 0x2(%r15), %edx
movq $0x0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x7209
incl %r15d
testq %rax, %rax
jne 0xd4d1
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq