// File: jliesch[P]ofxget/pugixml/pugixml.cpp
// Function: pugi::xml_node::append_buffer(void const*, unsigned long, unsigned int, pugi::xml_encoding)
PUGI__FN xml_parse_result xml_node::append_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding)
	{
		// append_buffer is only valid for elements/documents
		if (!impl::allow_insert_child(type(), node_element)) return impl::make_parse_result(status_append_invalid_root);

		// get document node
		impl::xml_document_struct* doc = &impl::get_document(_root);

		// disable document_buffer_order optimization since in a document with multiple buffers comparing buffer pointers does not make sense
		doc->header |= impl::xml_memory_page_contents_shared_mask;

		// get extra buffer element (we'll store the document fragment buffer there so that we can deallocate it later)
		impl::xml_memory_page* page = 0;
		impl::xml_extra_buffer* extra = static_cast<impl::xml_extra_buffer*>(doc->allocate_memory(sizeof(impl::xml_extra_buffer) + sizeof(void*), page));
		(void)page;

		if (!extra) return impl::make_parse_result(status_out_of_memory);

	#ifdef PUGIXML_COMPACT
		// align the memory block to a pointer boundary; this is required for compact mode where memory allocations are only 4b aligned
		// note that this requires up to sizeof(void*)-1 additional memory, which the allocation above takes into account
		extra = reinterpret_cast<impl::xml_extra_buffer*>((reinterpret_cast<uintptr_t>(extra) + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1));
	#endif

		// add extra buffer to the list
		extra->buffer = 0;
		extra->next = doc->extra_buffers;
		doc->extra_buffers = extra;

		// name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level
		impl::name_null_sentry sentry(_root);

		return impl::load_buffer_impl(doc, _root, const_cast<void*>(contents), size, options, encoding, false, false, &extra->buffer);
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x70(%rbp)
movq %rdi, -0x68(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x60(%rbp)
callq 0x42160
movl %eax, %edi
movl $0x2, %esi
callq 0x437f0
testb $0x1, %al
jne 0x44cfb
movq -0x70(%rbp), %rdi
movl $0xf, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x44e10
jmp 0x44df4
movq -0x60(%rbp), %rax
movq (%rax), %rdi
callq 0x42780
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rcx
orq $0x40, %rcx
movq %rcx, (%rax)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rdi
addq $0x40, %rdi
movl $0x18, %esi
leaq -0x30(%rbp), %rdx
callq 0x44e50
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x44d59
movq -0x70(%rbp), %rdi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x44e10
jmp 0x44df4
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rcx)
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rdx
movq -0x38(%rbp), %rcx
movq %rdx, 0x8(%rcx)
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq %rdx, 0x58(%rcx)
movq (%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0x44ed0
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq (%rax), %rdx
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %r8
movl -0x1c(%rbp), %r9d
movl -0x20(%rbp), %r10d
movq -0x38(%rbp), %r11
movq %rsp, %rax
movq %r11, 0x18(%rax)
movl %r10d, (%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
callq 0x44f00
jmp 0x44dd2
leaq -0x48(%rbp), %rdi
callq 0x45060
jmp 0x44df4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x45060
jmp 0x44e01
movq -0x68(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x7600
nopw (%rax,%rax)