// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0xf4b4
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movzbl 0x4(%rsi), %eax
leal -0x3(%rax), %ecx
cmpl $0x2, %ecx
jae 0xf3a0
testq %r14, %r14
jne 0xf333
movq 0x10cad(%rip), %r14     # 0x1ffe0
movq 0x20(%rbx), %rdi
movl 0x18(%rbx), %esi
leaq 0x14(%rsp), %r12
movq %r15, %rdx
movq %r12, %rcx
movl $0x1, %r8d
callq 0xf214
movl (%r12), %r13d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r13
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0xf4b4
testb %al, %al
je 0xf3b9
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
movq %r15, (%rax,%r13)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r13)
jmp 0xf4b2
cmpl $0x9, %eax
jne 0xf4b4
cmpl $0x4, 0x1c(%r15)
je 0xf327
jmp 0xf4b4
leal 0x3e8(%r13), %ecx
movl 0x18(%rbx), %eax
cmpl %eax, %ecx
jbe 0xf42e
movl 0x1c(%rbx), %esi
cmpl %esi, %eax
jne 0xf3ed
addl %esi, %esi
movl %esi, 0x1c(%rbx)
movq 0x20(%rbx), %rdi
shlq $0x4, %rsi
callq 0x8510
testq %rax, %rax
je 0xf4b4
movq %rax, 0x20(%rbx)
movq 0x20(%rbx), %rsi
leal 0x1(%r13), %edi
shlq $0x4, %rdi
addq %rsi, %rdi
movq %r13, %r12
shlq $0x4, %r12
addq %r12, %rsi
movl 0x18(%rbx), %edx
subl %r13d, %edx
shlq $0x4, %rdx
callq 0x8850
movq 0x20(%rbx), %rax
movq %r15, (%rax,%r12)
movq 0x20(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
incl 0x18(%rbx)
jmp 0xf4b2
cmpq $0x0, 0x30(%rbx)
jne 0xf451
movl $0x3e80, %edi           # imm = 0x3E80
callq 0x8080
movq %rax, 0x30(%rbx)
movl $0xfa0, %edi            # imm = 0xFA0
callq 0x8080
movq %rax, 0x38(%rbx)
movq 0x30(%rbx), %rdi
movl 0x28(%rbx), %esi
leaq 0x10(%rsp), %r12
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf214
movl (%r12), %r12d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r12
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
je 0xf4b4
testb %al, %al
je 0xf4c5
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
movq %r15, (%rax,%r12)
movq 0x30(%rbx), %rax
movq %r14, 0x8(%rax,%r12)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rbx), %edx
cmpl $0x3e8, %edx            # imm = 0x3E8
je 0xf4b4
movq 0x30(%rbx), %rsi
leal 0x1(%r12), %edi
movq %rdi, 0x8(%rsp)
shlq $0x4, %rdi
addq %rsi, %rdi
movq %r12, %rbp
shlq $0x4, %rbp
addq %rbp, %rsi
subl %r12d, %edx
shlq $0x4, %rdx
callq 0x8850
movq 0x30(%rbx), %rax
movq %r15, (%rax,%rbp)
movq 0x30(%rbx), %rax
movq %r14, 0x8(%rax,%rbp)
movq 0x38(%rbx), %rax
movq 0x8(%rsp), %rcx
leaq (%rax,%rcx,4), %rdi
leaq (%rax,%r12,4), %rsi
movl 0x28(%rbx), %edx
subl %r12d, %edx
shlq $0x2, %rdx
callq 0x8850
movq 0x38(%rbx), %rax
movl %r13d, (%rax,%r12,4)
movl 0x28(%rbx), %eax
incl %eax
movl %eax, 0x28(%rbx)
xorl %ebp, %ebp
cmpl $0x3e8, %eax            # imm = 0x3E8
jne 0xf4b4
movq %rbx, %rdi
callq 0xf556
jmp 0xf4b4