// File: /workspace/github_c_makefile_star2_O2/aerospike[P]aerospike-common/src/main/aerospike/as_orderedmap.c
// Function: as_orderedmap_set
int
as_orderedmap_set(as_orderedmap* map, const as_val* key, const as_val* val)
{
	if (map == NULL || ! is_valid_key_type(key)) {
		return -1;
	}

	as_val* cval = (as_val*)(val != NULL ? val : &as_nil);
	as_val* ckey = (as_val*)key;
	uint32_t ix;
	bool found = key_find(map->table, map->count, key, &ix, true);

	if (ix == UINT32_MAX) {
		return -1;
	}

	if (found) {
		as_val_destroy(map->table[ix].key);
		as_val_destroy(map->table[ix].value);
		map->table[ix].key = ckey;
		map->table[ix].value = cval;

		return 0;
	}

	if (ix + HOLD_TABLE_CAP > map->count) {
		// Near end of main table - insert directly.

		if (map->count == map->capacity) {
			map->capacity *= 2;

			map_entry* table = (map_entry*)cf_realloc(map->table,
					map->capacity * sizeof(map_entry));

			if (table == NULL) {
				return -1;
			}

			map->table = table;
		}

		memmove(&map->table[ix + 1], &map->table[ix],
				sizeof(map_entry) * (map->count - ix));
		map->table[ix].key = ckey;
		map->table[ix].value = cval;
		map->count++;

		return 0;
	}

	// Far from end of main table - insert in hold table.

	if (map->hold_table == NULL) {
		map->hold_table = cf_malloc(HOLD_TABLE_CAP * sizeof(map_entry));
		map->hold_locations = cf_malloc(HOLD_TABLE_CAP * sizeof(uint32_t));
	}

	uint32_t hold_ix;

	found = key_find(map->hold_table, map->hold_count, key, &hold_ix, false);

	if (hold_ix == UINT32_MAX) {
		return -1;
	}

	if (found) {
		as_val_destroy(map->hold_table[hold_ix].key);
		as_val_destroy(map->hold_table[hold_ix].value);
		map->hold_table[hold_ix].key = ckey;
		map->hold_table[hold_ix].value = cval;

		return 0;
	}

	if (map->hold_count == HOLD_TABLE_CAP) {
		return -1; // previous merge failed
	}

	memmove(&map->hold_table[hold_ix + 1], &map->hold_table[hold_ix],
			sizeof(map_entry) * (map->hold_count - hold_ix));
	map->hold_table[hold_ix].key = ckey;
	map->hold_table[hold_ix].value = cval;

	memmove(&map->hold_locations[hold_ix + 1], &map->hold_locations[hold_ix],
			sizeof(uint32_t) * (map->hold_count - hold_ix));
	map->hold_locations[hold_ix] = ix;

	map->hold_count++;

	if (map->hold_count == HOLD_TABLE_CAP) {
		as_orderedmap_merge(map);
		// Ignore merge allocation failure, next insert will fail.
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $-0x1
popq %rbp
testq %rdi, %rdi
je 0xd99c
movq %rdi, %rbx
movq %rsi, %rdi
movq %rdx, %r14
movq %rsi, %r15
callq 0xd6bb
testb %al, %al
je 0xd99c
testq %r14, %r14
jne 0xd7b4
movq 0x1082c(%rip), %r14     # 0x1dfe0
movq 0x20(%rbx), %rdi
movl 0x18(%rbx), %esi
leaq 0x4(%rsp), %r12
pushq $0x1
popq %r8
movq %r15, %rdx
movq %r12, %rcx
callq 0xd6df
movl (%r12), %r13d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r13
pushq $-0x1
popq %rbp
je 0xd99c
testb %al, %al
je 0xd81f
movq 0x20(%rbx), %rax
shlq $0x4, %r13
movq (%rax,%r13), %rdi
callq 0x84e0
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r13), %rdi
callq 0x84e0
movq 0x20(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r13)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r13)
jmp 0xd99c
movl 0x18(%rbx), %edx
leal 0x3e8(%r13), %eax
cmpl %edx, %eax
jbe 0xd893
cmpl 0x1c(%rbx), %edx
movq 0x20(%rbx), %rsi
jne 0xd85d
addl %edx, %edx
movq %rsi, %rdi
movl %edx, 0x1c(%rbx)
shlq $0x4, %rdx
movq %rdx, %rsi
callq 0x8540
testq %rax, %rax
je 0xd99c
movq %rax, 0x20(%rbx)
movq %rax, %rsi
movl 0x18(%rbx), %edx
movq %r13, %r12
subl %r13d, %edx
shlq $0x4, %r12
shlq $0x4, %rdx
leaq 0x10(%r12,%rsi), %rdi
addq %r12, %rsi
callq 0x88f0
movq 0x20(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r12)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
incl 0x18(%rbx)
jmp 0xd99c
movq 0x30(%rbx), %rdi
testq %rdi, %rdi
jne 0xd8bc
movl $0x3e80, %edi           # imm = 0x3E80
callq 0x8080
movl $0xfa0, %edi            # imm = 0xFA0
movq %rax, 0x30(%rbx)
callq 0x8080
movq %rax, 0x38(%rbx)
movq 0x30(%rbx), %rdi
movl 0x28(%rbx), %esi
movq %rsp, %r12
movq %r15, %rdx
xorl %r8d, %r8d
movq %r12, %rcx
callq 0xd6df
movl (%r12), %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
pushq $-0x1
popq %rbp
je 0xd99c
testb %al, %al
je 0xd91d
movq 0x30(%rbx), %rax
shlq $0x4, %r12
movq (%rax,%r12), %rdi
callq 0x84e0
movq 0x30(%rbx), %rax
movq 0x8(%rax,%r12), %rdi
callq 0x84e0
movq 0x30(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r12)
movq 0x30(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
jmp 0xd99c
movl 0x28(%rbx), %edx
cmpl $0x3e8, %edx            # imm = 0x3E8
je 0xd99c
movq 0x30(%rbx), %rsi
leaq 0x1(%r12), %rdi
movq %r12, %rbp
subl %r12d, %edx
shlq $0x4, %rdi
shlq $0x4, %rbp
shlq $0x4, %rdx
addq %rsi, %rdi
addq %rbp, %rsi
callq 0x88f0
movq 0x30(%rbx), %rax
movq %r15, (%rax,%rbp)
movq 0x30(%rbx), %rax
movq %r14, 0x8(%rax,%rbp)
movl 0x28(%rbx), %edx
movq 0x38(%rbx), %rax
subl %r12d, %edx
leaq 0x4(%rax,%r12,4), %rdi
leaq (%rax,%r12,4), %rsi
shlq $0x2, %rdx
callq 0x88f0
movq 0x38(%rbx), %rax
xorl %ebp, %ebp
movl %r13d, (%rax,%r12,4)
movl 0x28(%rbx), %eax
incl %eax
cmpl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0x28(%rbx)
jne 0xd99c
movq %rbx, %rdi
callq 0xd9ad
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq