// File: /workspace/github_cpp_makefile_star2_O3/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 0xe0d6
testq %rax, %rax
je 0xbc22
movq %rax, %r14
movq %rax, %rdi
movl $0x1, %esi
callq 0xe247
movq %r14, %rdi
callq 0x8db3
movq %rax, %r12
leaq 0x51b3(%rip), %rsi      # 0x10b8e
movq %rbp, %rdi
callq 0x6e04
testq %rax, %rax
je 0xba87
movq %rax, %r13
movq %rbp, 0x18(%rsp)
movslq 0x40(%r12), %rdi
callq 0x2d6b
movq %rax, %rbp
movq 0x48(%r12), %rsi
movslq 0x40(%r12), %rdx
movq %rax, %rdi
callq 0x2230
testb $0x1, %bl
jne 0xbaaa
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 0xbaea
leaq 0x5103(%rip), %rdx      # 0x10b91
xorl %r12d, %r12d
movl $0x3, %edi
movl $0x4, %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x460c
jmp 0xbc22
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 0xbb55
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 0x7022
movq (%rsp), %rdi
callq 0x7022
movq %r15, %rdi
callq 0x7022
movslq 0x40(%r12), %rsi
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rcx
callq 0x6e22
movq %rbp, %rdi
callq 0x6edd
leaq 0x16(%rsp), %rdi
movw $0xffff, (%rdi)         # imm = 0xFFFF
movl $0x2, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x6e22
movq %r14, %rdi
movq %r12, %rsi
callq 0xac11
movq %r13, %rdi
callq 0x6e09
movq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0xe0d6
movq %rax, %r12
movq %r14, %rdi
callq 0x8db3
testq %rax, %rax
je 0xbc1a
movq %rax, %r15
testb $0x8, %bl
jne 0xbbdb
testb $0x4, %bl
je 0xbbd4
cmpl $0x3, 0xc(%r15)
je 0xbbdb
cmpl $0x2, 0x8(%r15)
jg 0xbbff
movq %r12, %rsi
movq %r15, %rdx
callq 0xbc34
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
callq 0xb7cf
movq %r12, %rdi
movq %r13, %rsi
callq 0xac11
movq %r14, %rdi
movq %r15, %rsi
callq 0xac11
movq %r14, %rdi
callq 0x8db3
movq %rax, %r15
testq %rax, %rax
jne 0xbbc3
movq %r14, %rdi
callq 0xe38f
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq