// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x59b3
movq -0x50(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
movq %rcx, %rdx
callq 0x9c50
movb $0x0, -0x1(%rbp)
jmp 0x5c31
movq -0x50(%rbp), %rdi
callq 0x3810
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
callq 0x7c50
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x3170
movq -0x18(%rbp), %rdi
callq 0x3030
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3170
cmpq $0x0, -0x28(%rbp)
jg 0x5a27
movq -0x50(%rbp), %rdi
movl $0xc, %esi
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
movq %rcx, %rdx
callq 0x9c50
movb $0x0, -0x1(%rbp)
jmp 0x5c31
movq -0x28(%rbp), %rdi
addq $0x1, %rdi
callq 0x3040
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x3200
cmpq $0x1, %rax
je 0x5a93
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
je 0x5a72
movq -0x58(%rbp), %rdi
callq 0x31c0
movq -0x50(%rbp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
xorl %r8d, %r8d
movq %rcx, %rdx
callq 0x9c50
movb $0x0, -0x1(%rbp)
jmp 0x5c31
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movb $0xd, -0x41(%rbp)
movb $0xa, -0x42(%rbp)
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rax
cmpb $0x0, (%rax)
je 0x5bbe
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
addq -0x28(%rbp), %rcx
cmpq %rcx, %rax
jae 0x5ad7
jmp 0x5af6
leaq 0x87d9(%rip), %rdi      # 0xe2b7
leaq 0x8552(%rip), %rsi      # 0xe037
movl $0x423, %edx            # imm = 0x423
leaq 0x87d7(%rip), %rcx      # 0xe2c8
callq 0x30e0
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
addq -0x28(%rbp), %rcx
cmpq %rcx, %rax
ja 0x5b09
jmp 0x5b28
leaq 0x87ec(%rip), %rdi      # 0xe2fc
leaq 0x8520(%rip), %rsi      # 0xe037
movl $0x424, %edx            # imm = 0x424
leaq 0x87a5(%rip), %rcx      # 0xe2c8
callq 0x30e0
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
ja 0x5b34
jmp 0x5b53
leaq 0x87d3(%rip), %rdi      # 0xe30e
leaq 0x84f5(%rip), %rsi      # 0xe037
movl $0x425, %edx            # imm = 0x425
leaq 0x877a(%rip), %rcx      # 0xe2c8
callq 0x30e0
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x5b97
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x40(%rbp)
movb $0xa, (%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x5b95
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x5bb9
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb (%rax), %cl
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb %cl, (%rax)
jmp 0x5ab7
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
addq -0x28(%rbp), %rcx
cmpq %rcx, %rax
ja 0x5bd1
jmp 0x5bf0
leaq 0x8724(%rip), %rdi      # 0xe2fc
leaq 0x8458(%rip), %rsi      # 0xe037
movl $0x432, %edx            # imm = 0x432
leaq 0x86dd(%rip), %rcx      # 0xe2c8
callq 0x30e0
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rax
movb $0x0, (%rax)
movq -0x30(%rbp), %rsi
movl -0x1c(%rbp), %ecx
movq (%rdi), %rax
xorl %edx, %edx
callq *0x18(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x5c21
movq -0x60(%rbp), %rdi
callq 0x31c0
movq -0x50(%rbp), %rdi
callq 0x7c70
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)