// File: /workspace/github_cpp_makefile_star2_O3/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 0x50e0
movl %edx, %ebp
movq %rsi, %r15
leaq 0x28(%rbx), %r14
movq 0x28(%rbx), %rdi
testq %rdi, %rdi
je 0x4fd0
movq (%rdi), %rax
movq 0x48(%rdi), %r12
callq *0x8(%rax)
movq %r12, %rdi
testq %r12, %r12
jne 0x4fbe
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 0x50cb
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 0x50d8
movq %r14, %rax
addq %r12, %rax
movb $0x0, (%r14,%r12)
movb (%r14), %sil
movq %r14, %rcx
testb %sil, %sil
je 0x509d
movq %r14, %rcx
movq %r14, %rdx
cmpq %rax, %rdx
jae 0x5141
cmpq %rax, %rcx
ja 0x5103
cmpq %rdx, %rcx
ja 0x5122
cmpb $0xd, %sil
jne 0x5086
movb $0xa, (%rcx)
cmpb $0xa, 0x1(%rdx)
je 0x508e
incq %rdx
jmp 0x5092
incq %rdx
movb %sil, (%rcx)
jmp 0x5092
addq $0x2, %rdx
incq %rcx
movb (%rdx), %sil
testb %sil, %sil
jne 0x5057
cmpq %rax, %rcx
ja 0x5160
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 0x50f7
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0xc, %esi
jmp 0x50eb
movq %r14, %rdi
callq 0x31d0
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x768c
movl %r14d, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x51f2(%rip), %rdi      # 0xa2fc
leaq 0x4f26(%rip), %rsi      # 0xa037
leaq 0x51b0(%rip), %rcx      # 0xa2c8
movl $0x424, %edx            # imm = 0x424
callq 0x30e0
leaq 0x51e5(%rip), %rdi      # 0xa30e
leaq 0x4f07(%rip), %rsi      # 0xa037
leaq 0x5191(%rip), %rcx      # 0xa2c8
movl $0x425, %edx            # imm = 0x425
callq 0x30e0
leaq 0x516f(%rip), %rdi      # 0xa2b7
leaq 0x4ee8(%rip), %rsi      # 0xa037
leaq 0x5172(%rip), %rcx      # 0xa2c8
movl $0x423, %edx            # imm = 0x423
callq 0x30e0
leaq 0x5195(%rip), %rdi      # 0xa2fc
leaq 0x4ec9(%rip), %rsi      # 0xa037
leaq 0x5153(%rip), %rcx      # 0xa2c8
movl $0x432, %edx            # imm = 0x432
callq 0x30e0
nop