// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rsi, %r13
movq %rdi, %r15
cmpq $0x6, 0x8(%rsi)
jb 0x588c
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 0x588c
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%r15), %rax
movslq (%rax), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x5aaf
movq %rcx, -0x38(%rbp)
leaq -0xb0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
callq 0x41ce
movq %r15, %rdi
movq %r14, %rsi
callq 0x55f4
movl %eax, %r14d
movq -0xc0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x58f0
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x21c0
cmpl $-0x1, %r14d
movl $0x0, %eax
je 0x5aaf
testl %r14d, %r14d
jne 0x5970
movq 0x38(%r15), %rbx
movq 0x40(%r15), %r14
movl $0x1, %eax
subq %rbx, %r14
je 0x5aaf
movq %r12, -0x30(%rbp)
movq %r15, -0x40(%rbp)
sarq $0x5, %r14
movq 0x8(%r13), %r12
cmpq $0x1, %r14
adcq $0x0, %r14
addq $0x8, %rbx
xorl %r15d, %r15d
movq (%rbx), %rdx
cmpq %r12, %rdx
jne 0x595f
testq %rdx, %rdx
je 0x5a2c
movq (%r13), %rsi
movq -0x8(%rbx), %rdi
callq 0x21d0
testl %eax, %eax
je 0x5a2c
incq %r15
addq $0x20, %rbx
cmpq %r15, %r14
jne 0x5939
jmp 0x5aaa
movq %r12, -0x30(%rbp)
leaq -0x70(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x41ce
movq %r15, %rdi
movq %r14, %rsi
callq 0x55f4
movl %eax, %r12d
movq -0x80(%rbp), %rdi
cmpq %rbx, %rdi
je 0x59b6
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%r15), %rax
movslq %r12d, %rcx
movl (%rax,%rcx,4), %r12d
testl %r12d, %r12d
jne 0x59ba
movq -0x38(%rbp), %r12
movl %r12d, (%rax,%rcx,4)
movl (%rax,%r12,4), %ecx
movl %ecx, (%rax)
movl $0x0, (%rax,%r12,4)
leaq -0x50(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x41ce
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x6518
movq -0x60(%rbp), %rdi
movl $0x1, %eax
cmpq %rbx, %rdi
je 0x5aaf
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x21c0
jmp 0x5aaa
movq -0x40(%rbp), %rcx
movq 0x50(%rcx), %rax
movq -0x38(%rbp), %r12
movl %r12d, (%rax,%r15,4)
movq %rcx, %r15
movq 0x68(%rcx), %rax
movl (%rax,%r12,4), %ecx
movl %ecx, (%rax)
movl $0x0, (%rax,%r12,4)
leaq -0x90(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x41ce
movq %r15, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x6518
movq -0xa0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5aa2
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x21c0
movq %r15, %rdi
callq 0x4ed8
movl $0x1, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq -0xa0(%rbp), %rdi
jmp 0x5ae9
movq %rax, %r14
movq -0x60(%rbp), %rdi
jmp 0x5ae9
movq %rax, %r14
movq -0x80(%rbp), %rdi
jmp 0x5ae9
movq %rax, %r14
movq -0xc0(%rbp), %rdi
cmpq %rbx, %rdi
je 0x5af9
movq (%rbx), %rsi
incq %rsi
callq 0x21c0
movq %r14, %rdi
callq 0x2300
nop