// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
cmpq $0x6, 0x8(%rsi)
jb 0x58be
leaq -0xe0(%rbp), %r14
movl $0x5, %ecx
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x21e0
movq %r13, %rdi
movq %r14, %rsi
callq 0x2220
movq (%r14), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x58be
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%r15), %rax
movslq (%rax), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x5ad2
movq %rcx, -0x30(%rbp)
leaq -0xb0(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
callq 0x4218
movq %r15, %rdi
movq %r14, %rsi
callq 0x560e
movl %eax, %r14d
movq -0xc0(%rbp), %rdi
cmpq %r12, %rdi
je 0x5923
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x21c0
cmpl $-0x1, %r14d
movl $0x0, %eax
je 0x5ad2
testl %r14d, %r14d
jne 0x59a0
movq %rbx, -0x40(%rbp)
movq 0x38(%r15), %r12
movq 0x40(%r15), %rbx
subq %r12, %rbx
je 0x5acd
movq %r15, -0x38(%rbp)
sarq $0x5, %rbx
movq 0x8(%r13), %r14
cmpq $0x1, %rbx
adcq $0x0, %rbx
addq $0x8, %r12
xorl %r15d, %r15d
cmpq %r14, (%r12)
jne 0x598f
testq %r14, %r14
je 0x5a51
movq (%r13), %rsi
movq -0x8(%r12), %rdi
movq %r14, %rdx
callq 0x21d0
testl %eax, %eax
je 0x5a51
incq %r15
addq $0x20, %r12
cmpq %r15, %rbx
jne 0x5967
jmp 0x5acd
leaq -0x70(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x4218
movq %r15, %rdi
movq %r14, %rsi
callq 0x560e
movl %eax, %r14d
movq -0x80(%rbp), %rdi
cmpq %r12, %rdi
je 0x59e3
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%r15), %rax
movslq %r14d, %rcx
movl (%rax,%rcx,4), %r14d
testl %r14d, %r14d
jne 0x59e7
movq -0x30(%rbp), %r13
movl %r13d, (%rax,%rcx,4)
movl (%rax,%r13,4), %ecx
movl %ecx, (%rax)
movl $0x0, (%rax,%r13,4)
leaq -0x50(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x4218
movq %r15, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x650c
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x5acd
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x21c0
jmp 0x5acd
movq -0x38(%rbp), %rbx
movq 0x50(%rbx), %rax
movq -0x30(%rbp), %r13
movl %r13d, (%rax,%r15,4)
movq 0x68(%rbx), %rax
movl (%rax,%r13,4), %ecx
movl %ecx, (%rax)
movl $0x0, (%rax,%r13,4)
leaq -0x90(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x40(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x4218
movq %rbx, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x650c
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0x5ac5
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x4f1e
movl $0x1, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x5b0c
movq %rax, %rbx
movq -0x60(%rbp), %rdi
jmp 0x5b0c
movq %rax, %rbx
movq -0x80(%rbp), %rdi
jmp 0x5b0c
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
cmpq %r12, %rdi
je 0x5b1d
movq (%r12), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2300
nop