// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f55
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
movq %rdx, %rdi
callq 0x6e13
movq %rax, %r15
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x6e2c
testq %rax, %rax
je 0x2f55
movq %rbx, %rdi
callq 0x2120
movq %rax, %r12
testl %r12d, %r12d
je 0x2f55
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
leal 0x1(%r12), %eax
cmpl %ebp, %eax
jne 0x2f31
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
cmpb $0xd, (%rbx,%rax)
jne 0x2f31
movq %r14, %rdi
callq 0x2490
cmpl $0xa, %eax
je 0x2f31
leal (%r12,%r15), %eax
movslq %eax, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x6e0e
cmpl $0x2, %r12d
jl 0x2f69
leal -0x1(%r12), %eax
cmpb $0xa, (%rbx,%rax)
jne 0x2f69
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addl %r12d, %eax
cmpb $0xd, (%rbx,%rax)
jne 0x2f69
addq %rbx, %rax
jmp 0x2f87
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 0x2f87
cmpl $0xa, %ecx
jne 0x2f8a
movb $0x0, (%rax)
movq %rbx, %rdi
movl $0xd, %esi
callq 0x20e0
testq %rax, %rax
je 0x2f57
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 0x6e0e
movq %r14, %rdi
callq 0x2490
movslq %ebp, %r13
leaq 0xc3db(%rip), %r15      # 0xf3a6
leaq 0xc3d8(%rip), %r12      # 0xf3aa
cmpl $0xd, %eax
je 0x2fdc
cmpl $-0x1, %eax
jne 0x2fed
movq %r14, %rdi
callq 0x6e13
cmpq %r13, %rax
jge 0x2f57
cmpl $0x0, 0x122fc(%rip)     # 0x152f0
jne 0x300d
movl $0x1, 0x122f0(%rip)     # 0x152f0
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x4844
movq %r14, %rdi
callq 0x2490
jmp 0x2fd2