// File: rurban[P]dgnlib/dgnhelp.cpp
// Function: DGNGetLinkage
unsigned char *DGNGetLinkage( DGNHandle hDGN, DGNElemCore *psElement, 
                              int iIndex, int *pnLinkageType,
                              int *pnEntityNum, int *pnMSLink, int *pnLength )
    
{
    int nAttrOffset;
    int iLinkage, nLinkSize;

    for( iLinkage=0, nAttrOffset=0;
         (nLinkSize = DGNGetAttrLinkSize( hDGN, psElement, nAttrOffset)) != 0;
         iLinkage++, nAttrOffset += nLinkSize )
    {
        if( iLinkage == iIndex )
        {
            int  nLinkageType=0, nEntityNum=0, nMSLink = 0;
            CPLAssert( nLinkSize > 4 );

            if( psElement->attr_data[nAttrOffset+0] == 0x00
                && (psElement->attr_data[nAttrOffset+1] == 0x00
                    || psElement->attr_data[nAttrOffset+1] == 0x80) )
            {
                nLinkageType = DGNLT_DMRS;
                nEntityNum = psElement->attr_data[nAttrOffset+2] 
                    + psElement->attr_data[nAttrOffset+3] * 256;
                nMSLink = psElement->attr_data[nAttrOffset+4] 
                    + psElement->attr_data[nAttrOffset+5] * 256
                    + psElement->attr_data[nAttrOffset+6] * 65536;
            }
            else
                nLinkageType = psElement->attr_data[nAttrOffset+2] 
                    + psElement->attr_data[nAttrOffset+3] * 256;

            // Possibly an external database linkage?
            if( nLinkSize == 16 && nLinkageType != DGNLT_SHAPE_FILL )
            {
                nEntityNum = psElement->attr_data[nAttrOffset+6] 
                    + psElement->attr_data[nAttrOffset+7] * 256;
                nMSLink = psElement->attr_data[nAttrOffset+8] 
                    + psElement->attr_data[nAttrOffset+9] * 256
                    + psElement->attr_data[nAttrOffset+10] * 65536
                    + psElement->attr_data[nAttrOffset+11] * 65536 * 256;
                
            }

            if( pnLinkageType != NULL )
                *pnLinkageType = nLinkageType;
            if( pnEntityNum != NULL )
                *pnEntityNum = nEntityNum;
            if( pnMSLink != NULL )
                *pnMSLink = nMSLink;
            if( pnLength != NULL )
                *pnLength = nLinkSize;

            return psElement->attr_data + nAttrOffset;
        }
    }
             
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0xc500
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
je 0xa9ac
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0xa993
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0xa85d
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xa7d6
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x80, %eax
jne 0xa85d
movl $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x3, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x5, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x6, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
jmp 0xa88f
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x3, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
cmpl $0x10, -0x44(%rbp)
jne 0xa93e
cmpl $0x41, -0x48(%rbp)
je 0xa93e
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x6, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x7, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl -0x3c(%rbp), %ecx
addl $0x8, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0x9, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0xa, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movq -0x18(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl -0x3c(%rbp), %edx
addl $0xb, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x10, %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xa94e
movl -0x48(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0xa95e
movl -0x4c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x38(%rbp)
je 0xa96e
movl -0x50(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x10(%rbp)
je 0xa97e
movl -0x44(%rbp), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x3c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0xa9b4
jmp 0xa995
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xa741
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop