// File: rurban[P]dgnlib/dgnopen.cpp
// Function: DGNOpen
DGNHandle DGNOpen( const char * pszFilename, int bUpdate )

{
    DGNInfo     *psDGN;
    FILE        *fp;

/* -------------------------------------------------------------------- */
/*      Open the file.                                                  */
/* -------------------------------------------------------------------- */
    if( bUpdate )
        fp = VSIFOpen( pszFilename, "rb+" );
    else
        fp = VSIFOpen( pszFilename, "rb" );
    if( fp == NULL )
    {
        CPLError( CE_Failure, CPLE_OpenFailed, 
                  "Unable to open `%s' for read access.\n", 
                  pszFilename );
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Verify the format ... add later.                                */
/* -------------------------------------------------------------------- */
    GByte       abyHeader[512];

    VSIFRead( abyHeader, 1, sizeof(abyHeader), fp );
    if( !DGNTestOpen( abyHeader, sizeof(abyHeader) ) )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                  "File `%s' does not have expected DGN header.\n", 
                  pszFilename );
        VSIFClose( fp );
        return NULL;
    }

    VSIRewind( fp );

/* -------------------------------------------------------------------- */
/*      Create the info structure.                                      */
/* -------------------------------------------------------------------- */
    psDGN = (DGNInfo *) CPLCalloc(sizeof(DGNInfo),1);
    psDGN->fp = fp;
    psDGN->next_element_id = 0;

    psDGN->got_tcb = FALSE;
    psDGN->scale = 1.0;
    psDGN->origin_x = 0.0;
    psDGN->origin_y = 0.0;
    psDGN->origin_z = 0.0;                                             

    psDGN->index_built = FALSE;
    psDGN->element_count = 0;
    psDGN->element_index = NULL;

    psDGN->got_bounds = FALSE;

    if( abyHeader[0] == 0xC8 )
        psDGN->dimension = 3;
    else
        psDGN->dimension = 2;

    psDGN->has_spatial_filter = FALSE;
    psDGN->sf_converted_to_uor = FALSE;
    psDGN->select_complex_group = FALSE;
    psDGN->in_complex_group = FALSE;

    return (DGNHandle) psDGN;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x177ee
movq -0x10(%rbp), %rdi
leaq 0x1c41(%rip), %rsi      # 0x19424
callq 0x9c80
movq %rax, -0x28(%rbp)
jmp 0x17802
movq -0x10(%rbp), %rdi
leaq 0x1c28(%rip), %rsi      # 0x19421
callq 0x9c80
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x17832
movq -0x10(%rbp), %rcx
movl $0x3, %edi
movl $0x4, %esi
leaq 0x3121(%rip), %rdx      # 0x1a93f
movb $0x0, %al
callq 0x5740
movq $0x0, -0x8(%rbp)
jmp 0x179ba
leaq -0x230(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x9d40
leaq -0x230(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x17700
cmpl $0x0, %eax
jne 0x17894
movq -0x10(%rbp), %rcx
movl $0x3, %edi
movl $0x1, %esi
leaq 0x30ee(%rip), %rdx      # 0x1a965
movb $0x0, %al
callq 0x5740
movq -0x28(%rbp), %rdi
callq 0x9cb0
movq $0x0, -0x8(%rbp)
jmp 0x179ba
movq -0x28(%rbp), %rdi
callq 0x9d20
movl $0x203b8, %edi          # imm = 0x203B8
movl $0x1, %esi
callq 0x2fc0
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20014(%rax)
movq -0x20(%rbp), %rax
movsd 0x2000(%rip), %xmm0     # 0x198e0
movsd %xmm0, 0x20020(%rax)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20028(%rax)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20030(%rax)
movq -0x20(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20038(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20040(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20044(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x20050(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x2035c(%rax)
movzbl -0x230(%rbp), %eax
cmpl $0xc8, %eax
jne 0x1796c
movq -0x20(%rbp), %rax
movl $0x3, 0x20018(%rax)
jmp 0x1797a
movq -0x20(%rbp), %rax
movl $0x2, 0x20018(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20378(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x2037c(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20380(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20384(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nopw (%rax,%rax)