// File: /workspace/github_c_makefile_star2_O0/codeniko[P]CS214-Malloc-Free/mymalloc.c
// Function: mymalloc
void *mymalloc(unsigned int size, char *file, int line)
{
	static int initialized = 0;
	struct MemEntry *p;
	struct MemEntry *next;
	static struct MemEntry *root;
	
	if (size == 0) {
		fprintf(stderr, "Unable to allocate 0 bytes in FILE: '%s' on LINE: '%d'\n", file, line);
		return 0;
	}

	if(!initialized)	// 1st time called
	{
		// create a root chunk at the beginning of the memory block
		root = (struct MemEntry*) memblock;
		root->prev = root->next = 0;
		root->size = MEMSIZE - sizeof(struct MemEntry);
		root->isfree = 1;
		initialized = 1;
		memEntries[getFreeIndex()] = memblock;
	}

	p = root;
	do
	{
		if(p->size < size || !p->isfree) {
			// the current chunk is smaller, go to the next chunk
			// or this chunk was taken, go to the next
			p = p->next;
		}
		else if(p->size < (size + sizeof(struct MemEntry))) {
			// this chunk is free and large enough to hold data, 
			// but there's not enough memory to hold the HEADER of the next chunk
			// don't create any more chunks after this one
			p->isfree = 0;
			return (char*)p + sizeof(struct MemEntry);
		}
		else {
			// take the needed memory and create the header of the next chunk
			next = (struct MemEntry*)((char*)p + sizeof(struct MemEntry) + size);
			next->prev = p;
			next->next = p->next;
			next->size = p->size - sizeof(struct MemEntry) - size;
			next->isfree = 1;
			memEntries[getFreeIndex()] = next;
			p->size = size;
			p->isfree = 0;
			p->next = next;
			return (char*)p + sizeof(struct MemEntry);
		}
	} while (p != 0);

	fprintf(stderr, "Insufficient memory space requested (%d bytes) in FILE: '%s' on LINE: '%d'\n", size, file, line);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x11b4
movq 0x2e69(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
leaq 0xe6c(%rip), %rsi       # 0x200c
movb $0x0, %al
callq 0x1030
movq $0x0, -0x8(%rbp)
jmp 0x1363
cmpl $0x0, 0x2e85(%rip)      # 0x4040
jne 0x1228
leaq 0x2e8c(%rip), %rax      # 0x4050
movq %rax, 0x2e7d(%rip)      # 0x4048
movq 0x2e76(%rip), %rax      # 0x4048
movq $0x0, 0x8(%rax)
movq 0x2e67(%rip), %rax      # 0x4048
movq $0x0, (%rax)
movq 0x2e59(%rip), %rax      # 0x4048
movl $0x6190, 0x14(%rax)     # imm = 0x6190
movq 0x2e4b(%rip), %rax      # 0x4048
movl $0x1, 0x10(%rax)
movl $0x1, 0x2e32(%rip)      # 0x4040
callq 0x1530
movslq %eax, %rcx
leaq 0x8fe3(%rip), %rax      # 0xa200
leaq 0x2e2c(%rip), %rdx      # 0x4050
movq %rdx, (%rax,%rcx,8)
movq 0x2e19(%rip), %rax      # 0x4048
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0xc(%rbp), %eax
jb 0x1249
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x125a
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x132b
movq -0x28(%rbp), %rax
movslq 0x14(%rax), %rax
movl -0xc(%rbp), %ecx
addq $0x18, %rcx
cmpq %rcx, %rax
jae 0x128a
movq -0x28(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x28(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x8(%rbp)
jmp 0x1363
movq -0x28(%rbp), %rax
addq $0x18, %rax
movl -0xc(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movslq 0x14(%rax), %rax
subq $0x18, %rax
movl -0xc(%rbp), %ecx
subq %rcx, %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x30(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
callq 0x1530
movq -0x38(%rbp), %rdx
movslq %eax, %rcx
leaq 0x8f08(%rip), %rax      # 0xa200
movq %rdx, (%rax,%rcx,8)
movl -0xc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x8(%rbp)
jmp 0x1363
jmp 0x132d
cmpq $0x0, -0x28(%rbp)
jne 0x1233
movq 0x2cb9(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
leaq 0xcf0(%rip), %rsi       # 0x2044
movb $0x0, %al
callq 0x1030
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)