// File: /workspace/github_cpp_makefile_star2_O2/rurban[P]dgnlib/cpl_conv.cpp
// Function: CPLFGets
char *CPLFGets( char *pszBuffer, int nBufferSize, FILE * fp )

{
    int nActuallyRead, nOriginalOffset;

    if ( nBufferSize == 0 || pszBuffer == NULL || fp == NULL )
        return NULL;

/* -------------------------------------------------------------------- */
/*      Let the OS level call read what it things is one line.  This    */
/*      will include the newline.  On windows, if the file happens      */
/*      to be in text mode, the CRLF will have been converted to        */
/*      just the newline (LF).  If it is in binary mode it may well     */
/*      have both.                                                      */
/* -------------------------------------------------------------------- */
    nOriginalOffset = VSIFTell( fp );
    if( VSIFGets( pszBuffer, nBufferSize, fp ) == NULL )
        return NULL;
    
    nActuallyRead = strlen(pszBuffer);
    if ( nActuallyRead == 0 )
        return NULL;

/* -------------------------------------------------------------------- */
/*      If we found \r and out buffer is full, it is possible there     */
/*      is also a pending \n.  Check for it.                            */
/* -------------------------------------------------------------------- */
    if( nBufferSize == nActuallyRead+1
        && pszBuffer[nActuallyRead-1] == 13 )
    {
        int chCheck;
        chCheck = fgetc( fp );
        if( chCheck != 10 )
        {
            // unget the character.
            VSIFSeek( fp, nOriginalOffset+nActuallyRead, SEEK_SET );
        }
    }

/* -------------------------------------------------------------------- */
/*      Trim off \n, \r or \r\n if it appears at the end.  We don't     */
/*      need to do any "seeking" since we want the newline eaten.       */
/* -------------------------------------------------------------------- */
    if( nActuallyRead > 1 
        && pszBuffer[nActuallyRead-1] == 10 
        && pszBuffer[nActuallyRead-2] == 13 )
    {
        pszBuffer[nActuallyRead-2] = '\0';
    }
    else if( pszBuffer[nActuallyRead-1] == 10 
             || pszBuffer[nActuallyRead-1] == 13 )
    {
        pszBuffer[nActuallyRead-1] = '\0';
    }

/* -------------------------------------------------------------------- */
/*      Search within the string for a \r (MacOS convention             */
/*      apparently), and if we find it we need to trim the string,      */
/*      and seek back.                                                  */
/* -------------------------------------------------------------------- */
    char *pszExtraNewline = strchr( pszBuffer, 13 );
    
    if( pszExtraNewline != NULL )
    {
        int chCheck;

        nActuallyRead = pszExtraNewline - pszBuffer + 1;
        
        *pszExtraNewline = '\0';
        VSIFSeek( fp, nOriginalOffset + nActuallyRead - 1, SEEK_SET );

        /* 
         * This hackery is necessary to try and find our correct
         * spot on win32 systems with text mode line translation going 
         * on.  Sometimes the fseek back overshoots, but it doesn't
         * "realize it" till a character has been read. Try to read till
         * we get to the right spot and get our CR. 
         */ 
        chCheck = fgetc( fp );
        while( (chCheck != 13 && chCheck != EOF)
               || VSIFTell(fp) < nOriginalOffset + nActuallyRead )
        {
            static volatile int bWarned = FALSE;

            if( !bWarned )
            {
                bWarned = TRUE;
                CPLDebug( "CPL", "CPLFGets() correcting for DOS text mode translation seek problem." );
            }
            chCheck = fgetc( fp );
        }
    }

    return pszBuffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testl %esi, %esi
sete %al
testq %rdi, %rdi
sete %cl
orb %al, %cl
testq %rdx, %rdx
sete %al
orb %cl, %al
jne 0x2e87
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x697f
movq %rax, %r15
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x6998
testq %rax, %rax
je 0x2e87
movq %rbx, %rdi
callq 0x2120
movq %rax, %r12
testl %r12d, %r12d
je 0x2e87
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
leal 0x1(%r12), %eax
cmpl %ebp, %eax
jne 0x2e63
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
cmpb $0xd, (%rbx,%rax)
jne 0x2e63
movq %r14, %rdi
callq 0x2490
cmpl $0xa, %eax
je 0x2e63
leal (%r12,%r15), %eax
movslq %eax, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x697a
cmpl $0x2, %r12d
jl 0x2e9b
leal -0x1(%r12), %eax
cmpb $0xa, (%rbx,%rax)
jne 0x2e9b
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addl %r12d, %eax
cmpb $0xd, (%rbx,%rax)
jne 0x2e9b
addq %rbx, %rax
jmp 0x2eb9
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
shlq $0x20, %r12
addq %r13, %r12
sarq $0x20, %r12
leaq (%rbx,%r12), %rax
movzbl (%rbx,%r12), %ecx
cmpl $0xd, %ecx
je 0x2eb9
cmpl $0xa, %ecx
jne 0x2ebc
movb $0x0, (%rax)
pushq $0xd
popq %rsi
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x2e89
movl %eax, %ecx
subl %ebx, %ecx
movb $0x0, (%rax)
leal (%rcx,%r15), %ebp
incl %ebp
addl %r15d, %ecx
movslq %ecx, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x697a
movq %r14, %rdi
callq 0x2490
movslq %ebp, %r13
leaq 0xb4ab(%rip), %r15      # 0xe3a6
leaq 0xb4a8(%rip), %r12      # 0xe3aa
cmpl $0xd, %eax
je 0x2f0c
cmpl $-0x1, %eax
jne 0x2f1d
movq %r14, %rdi
callq 0x697f
cmpq %r13, %rax
jge 0x2e89
cmpl $0x0, 0x123cc(%rip)     # 0x152f0
jne 0x2f3d
movl $0x1, 0x123c0(%rip)     # 0x152f0
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x45a8
movq %r14, %rdi
callq 0x2490
jmp 0x2f02