// File: flastest[P]HW2_Systems_flaster_strand/cache.cc
// Function: Cache::Impl::set(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, char const*, unsigned int)
void set(key_type key, Cache::val_type val, Cache::size_type size)
	{

	    // Don't allow items that are bigger than the cache, or else everything will get evicted and the new item will not be added
	    if (size > mMaxmem){
	        return;
	    }

		if (memory_used + size < mMaxmem)  // TODO: fix this
		{
            //first we ned to add something to the heap
            //this needs to point to a new thing in the heap
            cache_val_type new_cache_item_pointer(new byte_type[size]);

            //now we need to copy val into new_cache_item_pointer
            std::copy(val, val+size, new_cache_item_pointer.get());

            //add key to the key map
            mCache[key] = map_val_type(size, new_cache_item_pointer	);

            //if there's an evictor, use it!
            if (mEvictor) mEvictor->touch_key(key);

            memory_used += size;

		}
		else if(mEvictor){
			// if there's no room, and an evictor, use the evictor!

			// first, pop something from the evictor.
			key_type key_to_evict = mEvictor->evict();

			// keep evicting until key_to_evict is actually something in the cache
			while (mCache.find(key_to_evict) == mCache.cend()) key_to_evict = mEvictor->evict();

			del(key_to_evict);

			//now try to set again. if more space is needed, another item should be evicted
			set(key, val, size);

		}
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rsi, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0xf0(%rbp)
movl -0x1c(%rbp), %eax
cmpl (%rcx), %eax
jbe 0x29e8
jmp 0x2ca5
movq -0xf0(%rbp), %rcx
movl 0x30(%rcx), %eax
addl -0x1c(%rbp), %eax
cmpl (%rcx), %eax
jae 0x2b20
movl -0x1c(%rbp), %eax
movl %eax, %edi
callq 0x2030
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x3c40
movq -0x110(%rbp), %rdi
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movl -0x1c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
callq 0x2dd0
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rsi
movq %rax, %rdx
callq 0x3c70
jmp 0x2a56
leaq -0x58(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x3cb0
jmp 0x2a69
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdi
addq $0x38, %rdi
callq 0x3cf0
movq %rax, -0x118(%rbp)
jmp 0x2a89
movq -0x118(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3d20
leaq -0x58(%rbp), %rdi
callq 0x39b0
movq -0xf0(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2af4
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x2acd
jmp 0x2af4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x2b12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x39b0
jmp 0x2b12
movq -0xf0(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl 0x30(%rax), %ecx
movl %ecx, 0x30(%rax)
leaq -0x30(%rbp), %rdi
callq 0x2df0
jmp 0x2ca5
leaq -0x30(%rbp), %rdi
callq 0x2df0
jmp 0x2cae
movq -0xf0(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2ca3
movq -0xf0(%rbp), %rax
movq 0x8(%rax), %rsi
movq (%rsi), %rax
leaq -0x78(%rbp), %rdi
callq *0x18(%rax)
movq -0xf0(%rbp), %rdi
addq $0x38, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3d90
movq %rax, -0x120(%rbp)
jmp 0x2b64
movq -0xf0(%rbp), %rdi
movq -0x120(%rbp), %rax
movq %rax, -0x80(%rbp)
addq $0x38, %rdi
callq 0x3dc0
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x3d60
testb $0x1, %al
jne 0x2b9c
jmp 0x2beb
movq -0xf0(%rbp), %rax
movq 0x8(%rax), %rsi
movq (%rsi), %rax
movq 0x18(%rax), %rax
leaq -0xa8(%rbp), %rdi
callq *%rax
jmp 0x2bb9
leaq -0x78(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2230
leaq -0xa8(%rbp), %rdi
callq 0x2100
jmp 0x2b47
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x2c98
leaq -0xc8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x20b0
jmp 0x2bfd
movq -0xf0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x2e10
jmp 0x2c12
leaq -0xc8(%rbp), %rdi
callq 0x2100
movq -0xf8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x20b0
jmp 0x2c33
movq -0xf0(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
leaq -0xe8(%rbp), %rsi
callq 0x29b0
jmp 0x2c4f
leaq -0xe8(%rbp), %rdi
callq 0x2100
leaq -0x78(%rbp), %rdi
callq 0x2100
jmp 0x2ca3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x2100
jmp 0x2c98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x2100
leaq -0x78(%rbp), %rdi
callq 0x2100
jmp 0x2cae
jmp 0x2ca5
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x22d0
nopw (%rax,%rax)