// File: jliesch[P]ofxget/pugixml/pugixml.cpp
// Function: pugi::impl::(anonymous namespace)::convert_buffer_latin1(char*&, unsigned long&, void const*, unsigned long, bool)
PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable)
	{
		const uint8_t* data = static_cast<const uint8_t*>(contents);
		size_t data_length = size;

		// get size of prefix that does not need utf8 conversion
		size_t prefix_length = get_latin1_7bit_prefix_length(data, data_length);
		assert(prefix_length <= data_length);

		const uint8_t* postfix = data + prefix_length;
		size_t postfix_length = data_length - prefix_length;

		// if no conversion is needed, just return the original buffer
		if (postfix_length == 0) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable);

		// first pass: get length in utf8 units
		size_t length = prefix_length + latin1_decoder::process(postfix, postfix_length, 0, utf8_counter());

		// allocate buffer of suitable length
		char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t)));
		if (!buffer) return false;

		// second pass: convert latin1 input to utf8
		memcpy(buffer, data, prefix_length);

		uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer);
		uint8_t* oend = latin1_decoder::process(postfix, postfix_length, obegin + prefix_length, utf8_writer());

		assert(oend == obegin + length);
		*oend = 0;

		out_buffer = buffer;
		out_length = length + 1;

		return true;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x51e90
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
ja 0x51320
jmp 0x5133f
leaq 0x1b3a5(%rip), %rdi     # 0x6c6cc
leaq 0x1a027(%rip), %rsi     # 0x6b355
movl $0x8a7, %edx            # imm = 0x8A7
leaq 0x1b3af(%rip), %rcx     # 0x6c6e9
callq 0x7180
movq -0x38(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x51386
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %r8d
callq 0x50e20
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x51465
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x51ef0
movq %rax, %rcx
movq -0x90(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq 0x4462e(%rip), %rax     # 0x959e8
movq -0x60(%rbp), %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq *%rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0x513dc
movb $0x0, -0x1(%rbp)
jmp 0x51465
movq -0x70(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x70b0
movq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq -0x48(%rbp), %rdx
callq 0x51f50
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
addq -0x60(%rbp), %rcx
cmpq %rcx, %rax
jne 0x51421
jmp 0x51440
leaq 0x1af46(%rip), %rdi     # 0x6c36e
leaq 0x19f26(%rip), %rsi     # 0x6b355
movl $0x8bc, %edx            # imm = 0x8BC
leaq 0x1b2ae(%rip), %rcx     # 0x6c6e9
callq 0x7180
movq -0x80(%rbp), %rax
movb $0x0, (%rax)
movq -0x70(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
addq $0x1, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)