// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movl %edx, %ebx
movq %r9, %r15
movl %r8d, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movsd %xmm2, (%rsp)
movapd %xmm1, 0x20(%rsp)
movapd %xmm0, 0x30(%rsp)
movq %rdi, %rbp
xorl %r12d, %r12d
movq %rsi, %rdi
xorl %esi, %esi
callq 0xd464
testq %rax, %rax
je 0xb0bb
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xd581
movq %r14, %rdi
callq 0x8731
movq %rax, %r12
leaq 0x4d13(%rip), %rsi      # 0xfb9e
movq %rbp, %rdi
callq 0x6970
testq %rax, %rax
je 0xaf37
movq %rax, %r13
movq %rbp, 0x18(%rsp)
movslq 0x40(%r12), %rdi
callq 0x2cb3
movq %rax, %rbp
movq 0x48(%r12), %rsi
movslq 0x40(%r12), %rdx
movq %rax, %rdi
callq 0x2230
testb $0x1, %bl
jne 0xaf56
movq 0x80(%rsp), %rax
movzwl (%r15), %ecx
movw %cx, 0x460(%rbp)
movzwl (%rax), %eax
movw %ax, 0x462(%rbp)
movl 0x10(%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 0xc(%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 0xaf96
leaq 0x4c63(%rip), %rdx      # 0xfba1
xorl %r12d, %r12d
pushq $0x3
popq %rdi
pushq $0x4
popq %rsi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x439c
jmp 0xb0bb
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 0xb001
imull %edx, %ecx
cvtsi2sd %ecx, %xmm0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
leaq 0x4d8(%rbp), %rdi
leaq 0x4e0(%rbp), %rax
movq %rax, (%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x30(%rsp), %xmm2
unpcklpd 0x20(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
mulpd %xmm0, %xmm2
movupd %xmm2, 0x4d8(%rbp)
movq %rbp, %r15
addq $0x4e8, %r15            # imm = 0x4E8
movsd %xmm1, 0x4e8(%rbp)
callq 0x6b8a
movq (%rsp), %rdi
callq 0x6b8a
movq %r15, %rdi
callq 0x6b8a
movslq 0x40(%r12), %rsi
pushq $0x1
popq %r15
movq %rbp, %rdi
movq %r15, %rdx
movq %r13, %rcx
callq 0x698e
movq %rbp, %rdi
callq 0x6a49
leaq 0x16(%rsp), %rdi
orw $-0x1, (%rdi)
pushq $0x2
popq %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x698e
movq %r14, %rdi
movq %r12, %rsi
callq 0xa2bd
movq %r13, %rdi
callq 0x6975
pushq $0x1
popq %rsi
movq 0x18(%rsp), %rdi
callq 0xd464
movq %rax, %r12
movq %r14, %rdi
callq 0x8731
testq %rax, %rax
je 0xb0b3
movq %rax, %r15
testb $0x8, %bl
jne 0xb082
testb $0x4, %bl
je 0xb07b
cmpl $0x3, 0xc(%r15)
je 0xb082
cmpl $0x2, 0x8(%r15)
jg 0xb0a6
movq %r12, %rsi
movq %r15, %rdx
callq 0xb0cd
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0xac95
movq %r12, %rdi
movq %r13, %rsi
callq 0xa2bd
movq %r14, %rdi
movq %r15, %rsi
callq 0xa2bd
jmp 0xb05a
movq %r14, %rdi
callq 0xd6c6
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq