// File: RC2K7[P]SDisk-FileSystem/filesystem.cpp
// Function: FileSystem::addBlock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int FileSystem::addBlock(string fileName, string buffer)
{
	// Handles FileNames Greater Than 5 Chars
	if (fileName.length() > 5)
		fileName = fileName.substr(0, 5);

	// There Is No Free Blocks
	if (this->fat[0] == 0)
		return 0;

	int alloc = this->fat[0];

	// Run Sqitch On First Block
	switch (this->getFirstBlock(fileName))
	{
        // File Does Not Exit
		case -1:
			return 0;
			break;
        // File Is Currently Empty
		case 0:
			for (int i = 0; i < this->rootFileName.size(); i++)
				if (this->rootFileName[i] == fileName) {
					this->rootFirstBlock[i] = alloc;
					this->fat[0] = this->fat[alloc];
					this->fat[alloc] = 0;

                    // Write The Buffer To SDisk
                    this->putBlock(alloc, buffer);

                    return this->fsSynch();
				}
			break;
        // File Exists And Contains Blocks
		default:
			int curB = this->getFirstBlock(fileName);
			while (this->fat[curB] != 0)
				curB = this->fat[curB];
			this->fat[curB] = alloc;
			this->fat[0] = this->fat[alloc];
			this->fat[alloc] = 0;

            // Write The Buffer To SDisk
            this->putBlock(alloc, buffer);

			break;
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rsi, -0x100(%rbp)
movq %rdi, %rax
movq -0x100(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
movq %rdx, -0xf0(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
callq 0x3410
cmpq $0x5, %rax
jbe 0x7cde
movq -0xf8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movl $0x5, %ecx
callq 0x32b0
movq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3360
leaq -0x40(%rbp), %rdi
callq 0x3170
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8760
cmpl $0x0, (%rax)
jne 0x7d03
movl $0x0, -0x4(%rbp)
jmp 0x8089
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8760
movq -0xf8(%rbp), %rsi
movl (%rax), %eax
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x3100
movq -0xe8(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x7a10
movl %eax, -0x104(%rbp)
jmp 0x7d4e
leaq -0x68(%rbp), %rdi
callq 0x3170
movl -0x104(%rbp), %eax
subl $-0x1, %eax
je 0x7d8d
jmp 0x7d64
movl -0x104(%rbp), %eax
testl %eax, %eax
je 0x7d99
jmp 0x7f00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3170
jmp 0x8095
movl $0x0, -0x4(%rbp)
jmp 0x8089
movl $0x0, -0x78(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0x78(%rbp), %rax
movq %rax, -0x118(%rbp)
addq $0x38, %rdi
callq 0x5a60
movq %rax, %rcx
movq -0x118(%rbp), %rax
cmpq %rcx, %rax
jae 0x7efb
movq -0xe8(%rbp), %rdi
addq $0x38, %rdi
movslq -0x78(%rbp), %rsi
callq 0x5a80
movq -0xf8(%rbp), %rsi
movq %rax, %rdi
callq 0x87a0
testb $0x1, %al
jne 0x7dfa
jmp 0x7eeb
movq -0xe8(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x138(%rbp)
addq $0x50, %rdi
movslq -0x78(%rbp), %rsi
callq 0x8760
movl -0x138(%rbp), %ecx
movq -0xe8(%rbp), %rdi
movl %ecx, (%rax)
addq $0x68, %rdi
movq %rdi, -0x130(%rbp)
movslq -0x44(%rbp), %rsi
callq 0x8760
movq -0x130(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x134(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x8760
movl -0x134(%rbp), %ecx
movq -0x130(%rbp), %rdi
movl %ecx, (%rax)
movslq -0x44(%rbp), %rsi
callq 0x8760
movq -0xf0(%rbp), %rsi
movl $0x0, (%rax)
movl -0x44(%rbp), %eax
movl %eax, -0x124(%rbp)
leaq -0x98(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x3100
movq -0xe8(%rbp), %rdi
movl -0x124(%rbp), %esi
movq -0x120(%rbp), %rdx
callq 0x99d0
jmp 0x7eae
leaq -0x98(%rbp), %rdi
callq 0x3170
movq -0xe8(%rbp), %rdi
callq 0x70c0
movl %eax, -0x4(%rbp)
jmp 0x8089
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x3170
jmp 0x8095
jmp 0x7eed
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x7da0
jmp 0x8082
movq -0xf8(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x3100
movq -0xe8(%rbp), %rdi
movq -0x148(%rbp), %rsi
callq 0x7a10
movl %eax, -0x13c(%rbp)
jmp 0x7f35
leaq -0xc0(%rbp), %rdi
callq 0x3170
movl -0x13c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
movslq -0x9c(%rbp), %rsi
callq 0x8760
cmpl $0x0, (%rax)
je 0x7fa7
movq -0xe8(%rbp), %rdi
addq $0x68, %rdi
movslq -0x9c(%rbp), %rsi
callq 0x8760
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
jmp 0x7f4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x3170
jmp 0x8095
movq -0xe8(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x168(%rbp)
addq $0x68, %rdi
movq %rdi, -0x160(%rbp)
movslq -0x9c(%rbp), %rsi
callq 0x8760
movl -0x168(%rbp), %ecx
movq -0x160(%rbp), %rdi
movl %ecx, (%rax)
movslq -0x44(%rbp), %rsi
callq 0x8760
movq -0x160(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x164(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x8760
movl -0x164(%rbp), %ecx
movq -0x160(%rbp), %rdi
movl %ecx, (%rax)
movslq -0x44(%rbp), %rsi
callq 0x8760
movq -0xf0(%rbp), %rsi
movl $0x0, (%rax)
movl -0x44(%rbp), %eax
movl %eax, -0x154(%rbp)
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x3100
movq -0xe8(%rbp), %rdi
movl -0x154(%rbp), %esi
movq -0x150(%rbp), %rdx
callq 0x99d0
jmp 0x805a
leaq -0xe0(%rbp), %rdi
callq 0x3170
jmp 0x8082
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x3170
jmp 0x8095
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x3470
nop