// File: jliesch[P]ofxget/pugixml/pugixml.cpp
// Function: pugi::impl::(anonymous namespace)::guess_buffer_encoding(unsigned char const*, unsigned long)
PUGI__FN xml_encoding guess_buffer_encoding(const uint8_t* data, size_t size)
	{
		// skip encoding autodetection if input buffer is too small
		if (size < 4) return encoding_utf8;

		uint8_t d0 = data[0], d1 = data[1], d2 = data[2], d3 = data[3];

		// look for BOM in first few bytes
		if (d0 == 0 && d1 == 0 && d2 == 0xfe && d3 == 0xff) return encoding_utf32_be;
		if (d0 == 0xff && d1 == 0xfe && d2 == 0 && d3 == 0) return encoding_utf32_le;
		if (d0 == 0xfe && d1 == 0xff) return encoding_utf16_be;
		if (d0 == 0xff && d1 == 0xfe) return encoding_utf16_le;
		if (d0 == 0xef && d1 == 0xbb && d2 == 0xbf) return encoding_utf8;

		// look for <, <? or <?xm in various encodings
		if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0x3c) return encoding_utf32_be;
		if (d0 == 0x3c && d1 == 0 && d2 == 0 && d3 == 0) return encoding_utf32_le;
		if (d0 == 0 && d1 == 0x3c && d2 == 0 && d3 == 0x3f) return encoding_utf16_be;
		if (d0 == 0x3c && d1 == 0 && d2 == 0x3f && d3 == 0) return encoding_utf16_le;

		// look for utf16 < followed by node name (this may fail, but is better than utf8 since it's zero terminated so early)
		if (d0 == 0 && d1 == 0x3c) return encoding_utf16_be;
		if (d0 == 0x3c && d1 == 0) return encoding_utf16_le;

		// no known BOM detected; parse declaration
		const uint8_t* enc = 0;
		size_t enc_length = 0;

		if (d0 == 0x3c && d1 == 0x3f && d2 == 0x78 && d3 == 0x6d && parse_declaration_encoding(data, size, enc, enc_length))
		{
			// iso-8859-1 (case-insensitive)
			if (enc_length == 10
				&& (enc[0] | ' ') == 'i' && (enc[1] | ' ') == 's' && (enc[2] | ' ') == 'o'
				&& enc[3] == '-' && enc[4] == '8' && enc[5] == '8' && enc[6] == '5' && enc[7] == '9'
				&& enc[8] == '-' && enc[9] == '1')
				return encoding_latin1;

			// latin1 (case-insensitive)
			if (enc_length == 6
				&& (enc[0] | ' ') == 'l' && (enc[1] | ' ') == 'a' && (enc[2] | ' ') == 't'
				&& (enc[3] | ' ') == 'i' && (enc[4] | ' ') == 'n'
				&& enc[5] == '1')
				return encoding_latin1;
		}

		return encoding_utf8;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x4, -0x18(%rbp)
jae 0x505f3
movl $0x1, -0x4(%rbp)
jmp 0x50968
movq -0x10(%rbp), %rax
movb (%rax), %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movb 0x1(%rax), %al
movb %al, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movb 0x2(%rax), %al
movb %al, -0x1b(%rbp)
movq -0x10(%rbp), %rax
movb 0x3(%rax), %al
movb %al, -0x1c(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x5064e
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x5064e
movzbl -0x1b(%rbp), %eax
cmpl $0xfe, %eax
jne 0x5064e
movzbl -0x1c(%rbp), %eax
cmpl $0xff, %eax
jne 0x5064e
movl $0x6, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0xff, %eax
jne 0x50682
movzbl -0x1a(%rbp), %eax
cmpl $0xfe, %eax
jne 0x50682
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
jne 0x50682
movzbl -0x1c(%rbp), %eax
cmpl $0x0, %eax
jne 0x50682
movl $0x5, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0xfe, %eax
jne 0x506a4
movzbl -0x1a(%rbp), %eax
cmpl $0xff, %eax
jne 0x506a4
movl $0x3, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0xff, %eax
jne 0x506c6
movzbl -0x1a(%rbp), %eax
cmpl $0xfe, %eax
jne 0x506c6
movl $0x2, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0xef, %eax
jne 0x506f3
movzbl -0x1a(%rbp), %eax
cmpl $0xbb, %eax
jne 0x506f3
movzbl -0x1b(%rbp), %eax
cmpl $0xbf, %eax
jne 0x506f3
movl $0x1, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x50723
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x50723
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
jne 0x50723
movzbl -0x1c(%rbp), %eax
cmpl $0x3c, %eax
jne 0x50723
movl $0x6, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x3c, %eax
jne 0x50753
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x50753
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
jne 0x50753
movzbl -0x1c(%rbp), %eax
cmpl $0x0, %eax
jne 0x50753
movl $0x5, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x50783
movzbl -0x1a(%rbp), %eax
cmpl $0x3c, %eax
jne 0x50783
movzbl -0x1b(%rbp), %eax
cmpl $0x0, %eax
jne 0x50783
movzbl -0x1c(%rbp), %eax
cmpl $0x3f, %eax
jne 0x50783
movl $0x3, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x3c, %eax
jne 0x507b3
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x507b3
movzbl -0x1b(%rbp), %eax
cmpl $0x3f, %eax
jne 0x507b3
movzbl -0x1c(%rbp), %eax
cmpl $0x0, %eax
jne 0x507b3
movl $0x2, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x0, %eax
jne 0x507d1
movzbl -0x1a(%rbp), %eax
cmpl $0x3c, %eax
jne 0x507d1
movl $0x3, -0x4(%rbp)
jmp 0x50968
movzbl -0x19(%rbp), %eax
cmpl $0x3c, %eax
jne 0x507ef
movzbl -0x1a(%rbp), %eax
cmpl $0x0, %eax
jne 0x507ef
movl $0x2, -0x4(%rbp)
jmp 0x50968
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x3c, %eax
jne 0x50961
movzbl -0x1a(%rbp), %eax
cmpl $0x3f, %eax
jne 0x50961
movzbl -0x1b(%rbp), %eax
cmpl $0x78, %eax
jne 0x50961
movzbl -0x1c(%rbp), %eax
cmpl $0x6d, %eax
jne 0x50961
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x50980
testb $0x1, %al
jne 0x50851
jmp 0x50961
cmpq $0xa, -0x30(%rbp)
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
orl $0x20, %eax
cmpl $0x69, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
orl $0x20, %eax
cmpl $0x73, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
orl $0x20, %eax
cmpl $0x6f, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x2d, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x38, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x38, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x35, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x7(%rax), %eax
cmpl $0x39, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x2d, %eax
jne 0x508f3
movq -0x28(%rbp), %rax
movzbl 0x9(%rax), %eax
cmpl $0x31, %eax
jne 0x508f3
movl $0x9, -0x4(%rbp)
jmp 0x50968
cmpq $0x6, -0x30(%rbp)
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
orl $0x20, %eax
cmpl $0x6c, %eax
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
orl $0x20, %eax
cmpl $0x61, %eax
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
orl $0x20, %eax
cmpl $0x74, %eax
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl 0x3(%rax), %eax
orl $0x20, %eax
cmpl $0x69, %eax
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %eax
orl $0x20, %eax
cmpl $0x6e, %eax
jne 0x5095f
movq -0x28(%rbp), %rax
movzbl 0x5(%rax), %eax
cmpl $0x31, %eax
jne 0x5095f
movl $0x9, -0x4(%rbp)
jmp 0x50968
jmp 0x50961
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)