// File: /workspace/github_cpp_makefile_star2_O1/rurban[P]dgnlib/dgnwrite.cpp
// Function: DGNCreate
DGNHandle
      DGNCreate( const char *pszNewFilename, const char *pszSeedFile, 
                 int nCreationFlags, 
                 double dfOriginX, double dfOriginY, double dfOriginZ,
                 int nSubUnitsPerMasterUnit, int nUORPerSubUnit, 
                 const char *pszMasterUnits, const char *pszSubUnits )

{
    DGNInfo *psSeed, *psDGN;
    FILE    *fpNew;
    DGNElemCore *psSrcTCB;

/* -------------------------------------------------------------------- */
/*      Open seed file, and read TCB element.                           */
/* -------------------------------------------------------------------- */
    psSeed = (DGNInfo *) DGNOpen( pszSeedFile, FALSE );
    if( psSeed == NULL )
        return NULL;

    DGNSetOptions( psSeed, DGNO_CAPTURE_RAW_DATA );
    
    psSrcTCB = DGNReadElement( psSeed );

    CPLAssert( psSrcTCB->raw_bytes >= 1536 );
    
/* -------------------------------------------------------------------- */
/*      Open output file.                                               */
/* -------------------------------------------------------------------- */
    fpNew = VSIFOpen( pszNewFilename, "wb" );
    if( fpNew == NULL )
    {
        CPLError( CE_Failure, CPLE_OpenFailed, 
                  "Failed to open output file: %s", pszNewFilename );
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Modify TCB appropriately for the output file.                   */
/* -------------------------------------------------------------------- */
    GByte *pabyRawTCB = (GByte *) CPLMalloc(psSrcTCB->raw_bytes);
    
    memcpy( pabyRawTCB, psSrcTCB->raw_data, psSrcTCB->raw_bytes );

    if( !(nCreationFlags & DGNCF_USE_SEED_UNITS) )
    {
        memcpy( pabyRawTCB+1120, pszMasterUnits, 2 );
        memcpy( pabyRawTCB+1122, pszSubUnits, 2 );

        DGN_WRITE_INT32( nUORPerSubUnit, pabyRawTCB+1116 );
        DGN_WRITE_INT32( nSubUnitsPerMasterUnit,pabyRawTCB+1112);
    }
    else
    {
        nUORPerSubUnit = DGN_INT32( pabyRawTCB+1116 );
        nSubUnitsPerMasterUnit = DGN_INT32( pabyRawTCB+1112 );
    }

    if( !(nCreationFlags & DGNCF_USE_SEED_ORIGIN) )
    {
        dfOriginX *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);
        dfOriginY *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);
        dfOriginZ *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);

        memcpy( pabyRawTCB+1240, &dfOriginX, 8 );
        memcpy( pabyRawTCB+1248, &dfOriginY, 8 );
        memcpy( pabyRawTCB+1256, &dfOriginZ, 8 );

        IEEE2DGNDouble( pabyRawTCB+1240 );
        IEEE2DGNDouble( pabyRawTCB+1248 );
        IEEE2DGNDouble( pabyRawTCB+1256 );
    }

/* -------------------------------------------------------------------- */
/*      Write TCB and EOF to new file.                                  */
/* -------------------------------------------------------------------- */
    unsigned char abyEOF[2];
    
    VSIFWrite( pabyRawTCB, psSrcTCB->raw_bytes, 1, fpNew );
    CPLFree( pabyRawTCB );

    abyEOF[0] = 0xff;
    abyEOF[1] = 0xff;
    
    VSIFWrite( abyEOF, 2, 1, fpNew );

    DGNFreeElement( psSeed, psSrcTCB );

/* -------------------------------------------------------------------- */
/*      Close and re-open using DGN API.                                */
/* -------------------------------------------------------------------- */
    VSIFClose( fpNew );

    psDGN = (DGNInfo *) DGNOpen( pszNewFilename, TRUE );

/* -------------------------------------------------------------------- */
/*      Now copy over elements according to options in effect.          */
/* -------------------------------------------------------------------- */
    DGNElemCore *psSrcElement, *psDstElement;

    while( (psSrcElement = DGNReadElement( psSeed )) != NULL )
    {
        if( (nCreationFlags & DGNCF_COPY_WHOLE_SEED_FILE)
            || (psSrcElement->stype == DGNST_COLORTABLE 
                && nCreationFlags & DGNCF_COPY_SEED_FILE_COLOR_TABLE)
            || psSrcElement->element_id <= 2 )
        {
            psDstElement = DGNCloneElement( psSeed, psDGN, psSrcElement );
            DGNWriteElement( psDGN, psDstElement );
            DGNFreeElement( psDGN, psDstElement );
        }

        DGNFreeElement( psSeed, psSrcElement );
    }

    DGNClose( psSeed );
    
    return psDGN;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebx
movq %r9, %r15
movl %r8d, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbp
xorl %r12d, %r12d
movq %rsi, %rdi
xorl %esi, %esi
callq 0xe918
testq %rax, %rax
je 0xc16c
movq %rax, %r14
movq %rax, %rdi
movl $0x1, %esi
callq 0xea43
movq %r14, %rdi
callq 0x8f14
movq %rax, %r12
leaq 0x5bc0(%rip), %rsi      # 0x11ade
movq %rbp, %rdi
callq 0x6f44
testq %rax, %rax
je 0xbfc7
movq %rax, %r13
movq %rbp, 0x20(%rsp)
movslq 0x40(%r12), %rdi
callq 0x2d9b
movq %rax, %rbp
movq 0x48(%r12), %rsi
movslq 0x40(%r12), %rdx
movq %rax, %rdi
callq 0x2240
testb $0x1, %bl
jne 0xbfea
movq 0x70(%rsp), %rax
movzwl (%r15), %ecx
movw %cx, 0x460(%rbp)
movzwl (%rax), %eax
movw %ax, 0x462(%rbp)
movl 0x18(%rsp), %edx
movl %edx, %eax
shrl $0x10, %eax
movb %al, 0x45c(%rbp)
movl %edx, %eax
shrl $0x18, %eax
movb %al, 0x45d(%rbp)
movb %dl, 0x45e(%rbp)
movb %dh, 0x45f(%rbp)
movl 0x14(%rsp), %ecx
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x458(%rbp)
movl %ecx, %eax
shrl $0x18, %eax
movb %al, 0x459(%rbp)
movb %cl, 0x45a(%rbp)
movb %ch, 0x45b(%rbp)
jmp 0xc02a
leaq 0x5b13(%rip), %rdx      # 0x11ae1
xorl %r12d, %r12d
movl $0x3, %edi
movl $0x4, %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x46dc
jmp 0xc16c
movzwl 0x45e(%rbp), %eax
movzbl 0x45d(%rbp), %ecx
shll $0x18, %ecx
orl %eax, %ecx
movzbl 0x45c(%rbp), %edx
shll $0x10, %edx
orl %ecx, %edx
movzwl 0x45a(%rbp), %eax
movzbl 0x459(%rbp), %ecx
shll $0x18, %ecx
orl %eax, %ecx
movzbl 0x458(%rbp), %eax
shll $0x10, %eax
orl %ecx, %eax
movl %eax, %ecx
testb $0x2, %bl
jne 0xc09f
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd 0x28(%rsp), %xmm3
mulsd %xmm0, %xmm3
movsd 0x30(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
leaq 0x4d8(%rbp), %rdi
movsd %xmm3, 0x4d8(%rbp)
leaq 0x4e0(%rbp), %r15
movsd %xmm2, 0x4e0(%rbp)
movq %rbp, %rax
addq $0x4e8, %rax            # imm = 0x4E8
movq %rax, 0x8(%rsp)
movsd %xmm1, 0x4e8(%rbp)
callq 0x716d
movq %r15, %rdi
callq 0x716d
movq 0x8(%rsp), %rdi
callq 0x716d
movslq 0x40(%r12), %rsi
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rcx
callq 0x6f62
movq %rbp, %rdi
callq 0x701d
leaq 0x1e(%rsp), %rdi
movw $0xffff, (%rdi)         # imm = 0xFFFF
movl $0x2, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x6f62
movq %r14, %rdi
movq %r12, %rsi
callq 0xb0b0
movq %r13, %rdi
callq 0x6f49
movq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0xe918
movq %rax, %r12
movq %r14, %rdi
callq 0x8f14
testq %rax, %rax
je 0xc164
movq %rax, %r15
testb $0x8, %bl
jne 0xc125
testb $0x4, %bl
je 0xc11e
cmpl $0x3, 0xc(%r15)
je 0xc125
cmpl $0x2, 0x8(%r15)
jg 0xc149
movq %r12, %rsi
movq %r15, %rdx
callq 0xc17e
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0xbd13
movq %r12, %rdi
movq %r13, %rsi
callq 0xb0b0
movq %r14, %rdi
movq %r15, %rsi
callq 0xb0b0
movq %r14, %rdi
callq 0x8f14
movq %rax, %r15
testq %rax, %rax
jne 0xc10d
movq %r14, %rdi
callq 0xeba1
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq