// File: /workspace/github_c_makefile_star2_O0/aerospike[P]aerospike-common/src/main/aerospike/as_msgpack_serializer.c
// Function: as_msgpack_serializer_serialize
static int as_msgpack_serializer_serialize(as_serializer *s, const as_val *v, as_buffer *buff)
{
	as_packer packer = {
			.buffer = (unsigned char *)cf_malloc(AS_PACKER_BUFFER_SIZE),
			.capacity = AS_PACKER_BUFFER_SIZE,
			.offset = 0,
			.head = 0,
			.tail = 0,
	};

	if (! packer.buffer) {
		return 1;
	}

    int rc = as_pack_val(&packer, v);
	
	if (rc != 0) {
		// Cleanup buffers on error.
		as_packer_buffer *p = packer.head;
		as_packer_buffer *tmp;

		while (p) {
			tmp = p;
			p = p->next;
			
			// Free buffer entry.
			cf_free(tmp->buffer);
			cf_free(tmp);
		}

		// Free main buffer.
		cf_free(packer.buffer);
		return rc;
	}

	if (packer.head) {
		// Combine buffers into a single contiguous buffer.
		as_packer_buffer *p = packer.head;
		int size = packer.offset;

		while (p) {
			size += p->length;
			p = p->next;
		}

		unsigned char *target = (unsigned char *)cf_malloc(size);
		p = packer.head;
		int offset = 0;

		while (p) {
			memcpy(target + offset, p->buffer, p->length);
			offset += p->length;
			
			as_packer_buffer *tmp = p;
			p = p->next;
			
			// Free original buffer entry.
			cf_free(tmp->buffer);
			cf_free(tmp);
		}

		memcpy(target + offset, packer.buffer, packer.offset);

		// Free original main buffer.
		cf_free(packer.buffer);

		// Transfer new buffer.
		buff->data = target;
		buff->size = size;
		buff->capacity = size;
	}
	else {
		// Use existing buffer directly.
		buff->data = packer.buffer;
		buff->size = packer.offset;
		buff->capacity = packer.capacity;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x2000, %edi           # imm = 0x2000
callq 0x8090
movq %rax, -0x30(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x2000, -0x24(%rbp)    # imm = 0x2000
cmpq $0x0, -0x30(%rbp)
jne 0x13516
movl $0x1, -0x4(%rbp)
jmp 0x13699
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x8b90
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x1357a
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x13566
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x8aa0
movq -0x58(%rbp), %rdi
callq 0x8aa0
jmp 0x13534
movq -0x30(%rbp), %rdi
callq 0x8aa0
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13699
cmpq $0x0, -0x40(%rbp)
je 0x13673
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x64(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x135b4
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
jmp 0x13593
movslq -0x64(%rbp), %rdi
callq 0x8090
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x74(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x13630
movq -0x70(%rbp), %rdi
movslq -0x74(%rbp), %rax
addq %rax, %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %edx
callq 0x86b0
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x8aa0
movq -0x80(%rbp), %rdi
callq 0x8aa0
jmp 0x135d0
movq -0x70(%rbp), %rdi
movslq -0x74(%rbp), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movl -0x28(%rbp), %eax
movl %eax, %edx
callq 0x86b0
movq -0x30(%rbp), %rdi
callq 0x8aa0
movq -0x70(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x64(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x64(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x13692
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop