// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
cmpq $0x6, 0x8(%rsi)
jb 0x4614
leaq -0xd0(%rbp), %r14
pushq $0x5
popq %rcx
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2200
movq %r13, %rdi
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20e0
movq 0x68(%rbx), %rax
movslq (%rax), %r15
xorl %eax, %eax
testq %r15, %r15
je 0x4760
movq %r12, -0x30(%rbp)
leaq -0xb0(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2290
movq %rbx, %rdi
movq %r14, %rsi
callq 0x444a
movl %eax, %r12d
leaq -0xb0(%rbp), %rdi
callq 0x20e0
cmpl $-0x1, %r12d
movl $0x0, %eax
je 0x4760
testl %r12d, %r12d
jne 0x46f1
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x38(%rbx), %rdi
movq 0x40(%rbx), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x475d
addq %r14, %rdi
movq %r13, %rsi
callq 0x4aba
incq %r12
addq $0x20, %r14
testb %al, %al
je 0x4674
movq 0x50(%rbx), %rax
movl %r15d, -0x4(%rax,%r12,4)
movq 0x68(%rbx), %rax
movl (%rax,%r15,4), %ecx
movl %ecx, (%rax)
andl $0x0, (%rax,%r15,4)
leaq -0x90(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x2290
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x50d6
leaq -0x90(%rbp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x3f46
jmp 0x475d
leaq -0x70(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2290
movq %rbx, %rdi
movq %r14, %rsi
callq 0x444a
movl %eax, %r12d
leaq -0x70(%rbp), %rdi
callq 0x20e0
movq 0x68(%rbx), %rax
movslq %r12d, %rcx
movl (%rax,%rcx,4), %r12d
testl %r12d, %r12d
jne 0x471b
movl %r15d, (%rax,%rcx,4)
movl (%rax,%r15,4), %ecx
movl %ecx, (%rax)
andl $0x0, (%rax,%r15,4)
leaq -0x50(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x2290
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x50d6
leaq -0x50(%rbp), %rdi
callq 0x20e0
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4778
jmp 0x4778
jmp 0x4778
movq %rax, %rbx
movq %r14, %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2330
nop