// File: /workspace/github_cpp_makefile_star2_O1/smallmuou[P]tinyhtml/tinyxml.cpp
// Function: TiXmlDocument::LoadFile(_IO_FILE*, TiXmlEncoding)
bool TiXmlDocument::LoadFile( FILE* file, TiXmlEncoding encoding )
{
	if ( !file ) 
	{
		SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Delete the existing data:
	Clear();
	location.Clear();

	// Get the file size, so we can pre-allocate the string. HUGE speed impact.
	long length = 0;
	fseek( file, 0, SEEK_END );
	length = ftell( file );
	fseek( file, 0, SEEK_SET );

	// Strange case, but good to handle up front.
	if ( length <= 0 )
	{
		SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Subtle bug here. TinyXml did use fgets. But from the XML spec:
	// 2.11 End-of-Line Handling
	// <snip>
	// <quote>
	// ...the XML processor MUST behave as if it normalized all line breaks in external 
	// parsed entities (including the document entity) on input, before parsing, by translating 
	// both the two-character sequence #xD #xA and any #xD that is not followed by #xA to 
	// a single #xA character.
	// </quote>
	//
	// It is not clear fgets does that, and certainly isn't clear it works cross platform. 
	// Generally, you expect fgets to translate from the convention of the OS to the c/unix
	// convention, and not work generally.

	/*
	while( fgets( buf, sizeof(buf), file ) )
	{
		data += buf;
	}
	*/

	char* buf = new char[ length+1 ];
	buf[0] = 0;

	if ( fread( buf, length, 1, file ) != 1 ) {
		delete [] buf;
		SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
		return false;
	}

	// Process the buffer in place to normalize new lines. (See comment above.)
	// Copies from the 'p' to 'q' pointer, where p can advance faster if
	// a newline-carriage return is hit.
	//
	// Wikipedia:
	// Systems based on ASCII or a compatible character set use either LF  (Line feed, '\n', 0x0A, 10 in decimal) or 
	// CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A)...
	//		* LF:    Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
    //		* CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
    //		* CR:    Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9

	const char* p = buf;	// the read head
	char* q = buf;			// the write head
	const char CR = 0x0d;
	const char LF = 0x0a;

	buf[length] = 0;
	while( *p ) {
		assert( p < (buf+length) );
		assert( q <= (buf+length) );
		assert( q <= p );

		if ( *p == CR ) {
			*q++ = LF;
			p++;
			if ( *p == LF ) {		// check for CR+LF (and skip LF)
				p++;
			}
		}
		else {
			*q++ = *p++;
		}
	}
	assert( q <= (buf+length) );
	*q = 0;

	Parse( buf, 0, encoding );

	delete [] buf;
	return !Error();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
testq %rsi, %rsi
je 0x516e
movl %edx, %ebp
movq %rsi, %r15
leaq 0x28(%rbx), %r14
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x505e
movq (%rdi), %rax
movq 0x48(%rdi), %r12
callq *0x8(%rax)
movq %r12, %rdi
testq %r12, %r12
jne 0x504c
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq $-0x1, 0x8(%rbx)
movq %r15, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3170
movq %r15, %rdi
callq 0x3030
movq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3170
testq %r12, %r12
jle 0x5159
leaq 0x1(%r12), %rdi
callq 0x3040
movq %rax, %r14
movb $0x0, (%rax)
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x3210
cmpq $0x1, %rax
jne 0x5166
movq %r14, %rax
addq %r12, %rax
movb $0x0, (%r14,%r12)
movb (%r14), %sil
movq %r14, %rcx
testb %sil, %sil
je 0x512b
movq %r14, %rcx
movq %r14, %rdx
cmpq %rax, %rdx
jae 0x51cf
cmpq %rax, %rcx
ja 0x5191
cmpq %rdx, %rcx
ja 0x51b0
cmpb $0xd, %sil
jne 0x5114
movb $0xa, (%rcx)
cmpb $0xa, 0x1(%rdx)
je 0x511c
incq %rdx
jmp 0x5120
incq %rdx
movb %sil, (%rcx)
jmp 0x5120
addq $0x2, %rdx
incq %rcx
movb (%rdx), %sil
testb %sil, %sil
jne 0x50e5
cmpq %rax, %rcx
ja 0x51ee
movb $0x0, (%rcx)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %ebp, %ecx
callq *0x18(%rax)
movq %r14, %rdi
callq 0x31d0
movb 0x50(%rbx), %r14b
xorb $0x1, %r14b
jmp 0x5185
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0xc, %esi
jmp 0x5179
movq %r14, %rdi
callq 0x31d0
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x77f6
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5164(%rip), %rdi      # 0xa2fc
leaq 0x4e98(%rip), %rsi      # 0xa037
leaq 0x5122(%rip), %rcx      # 0xa2c8
movl $0x424, %edx            # imm = 0x424
callq 0x30e0
leaq 0x5157(%rip), %rdi      # 0xa30e
leaq 0x4e79(%rip), %rsi      # 0xa037
leaq 0x5103(%rip), %rcx      # 0xa2c8
movl $0x425, %edx            # imm = 0x425
callq 0x30e0
leaq 0x50e1(%rip), %rdi      # 0xa2b7
leaq 0x4e5a(%rip), %rsi      # 0xa037
leaq 0x50e4(%rip), %rcx      # 0xa2c8
movl $0x423, %edx            # imm = 0x423
callq 0x30e0
leaq 0x5107(%rip), %rdi      # 0xa2fc
leaq 0x4e3b(%rip), %rsi      # 0xa037
leaq 0x50c5(%rip), %rcx      # 0xa2c8
movl $0x432, %edx            # imm = 0x432
callq 0x30e0
nop