// File: /workspace/github_cpp_makefile_star2_O2/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 0xc7fc
leaq 0x34ea(%rip), %rdx      # 0xfcce
pushq $0x3
popq %rdi
pushq $0x1
popq %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x439c
pushq $-0x1
popq %r15
jmp 0xc8c8
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 0x2d0a
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 0x2d0a
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 0xc871
cmpl $0x7, %eax
jne 0xc898
pushq $0x2
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
addl 0x50(%rbx), %eax
movl %eax, 0x50(%rbx)
movq 0x48(%rbx), %rcx
movb %al, 0x24(%rcx)
movl 0x50(%rbx), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movq 0x48(%rbx), %rcx
movb %al, 0x25(%rcx)
movq %rbx, %rsi
callq 0xb380
pushq $-0x2
popq %r15
leal 0x2(%r15), %edx
andq $0x0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x6d35
incl %r15d
testq %rax, %rax
jne 0xc8a4
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq