// File: /workspace/github_c_makefile_star2_O3/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
testq %rdi, %rdi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
je 0xf4f0
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x4(%rsi), %eax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0xf57c
testq %r14, %r14
jne 0xf511
movq 0x10acf(%rip), %r14     # 0x1ffe0
movq 0x20(%rbx), %rdi
movl 0x18(%rbx), %esi
leaq 0x4(%rsp), %r12
movq %r15, %rdx
movl $0x1, %r8d
movq %r12, %rcx
callq 0xf424
movl (%r12), %r13d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %rcx, %r13
je 0xf4df
testb %al, %al
je 0xf595
movq 0x20(%rbx), %rax
shlq $0x4, %r13
movq (%rax,%r13), %rdi
callq 0x84b0
movq 0x20(%rbx), %rax
movq 0x8(%rax,%r13), %rdi
callq 0x84b0
movq 0x20(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r13)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r13)
jmp 0xf4df
cmpl $0x9, %eax
jne 0xf4df
cmpl $0x4, 0x1c(%r15)
jne 0xf4df
jmp 0xf505
movl 0x18(%rbx), %edx
leal 0x3e8(%r13), %eax
cmpl %edx, %eax
jbe 0xf609
cmpl 0x1c(%rbx), %edx
movq 0x20(%rbx), %rsi
jne 0xf5d3
addl %edx, %edx
movq %rsi, %rdi
movl %edx, 0x1c(%rbx)
shlq $0x4, %rdx
movq %rdx, %rsi
callq 0x8510
testq %rax, %rax
je 0xf4df
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 0x8850
movq 0x20(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r12)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
incl 0x18(%rbx)
jmp 0xf4df
movq 0x30(%rbx), %rdi
testq %rdi, %rdi
jne 0xf632
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 0xf424
movl (%r12), %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
je 0xf4df
testb %al, %al
je 0xf698
movq 0x30(%rbx), %rax
shlq $0x4, %r12
movq (%rax,%r12), %rdi
callq 0x84b0
movq 0x30(%rbx), %rax
movq 0x8(%rax,%r12), %rdi
callq 0x84b0
movq 0x30(%rbx), %rax
xorl %ebp, %ebp
movq %r15, (%rax,%r12)
movq 0x30(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
jmp 0xf4df
movl 0x28(%rbx), %edx
cmpl $0x3e8, %edx            # imm = 0x3E8
je 0xf4df
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 0x8850
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 0x8850
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 0xf4df
movq %rbx, %rdi
callq 0xf724
jmp 0xf4df