// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::rm(char const*)
void FileSys::rm(const char *name)
{
  char buffer[MAX_FNAME_SIZE+256];
  dirblock_t curr_block;
  bool error = false;
  bool found = false;
  bfs.read_block(curr_dir,(void*)& curr_block);

  for (unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    if (strcmp(curr_block.dir_entries[i].name, name) == 0){
      if (!is_directory(curr_block.dir_entries[i].block_num)){
        found = true;
        inode_t inode_block;
        bfs.read_block(curr_block.dir_entries[i].block_num,
                       (void*)&inode_block);
        for (size_t j = 0; j < (inode_block.size / BLOCK_SIZE) + 1; j++){
          bfs.reclaim_block(inode_block.blocks[i]);
          inode_block.blocks[i] = 0;
          inode_block.size = 0;
        }
        bfs.reclaim_block(curr_block.dir_entries[i].block_num);

        curr_block.dir_entries[i].name[0] = '\0';
        curr_block.dir_entries[i].block_num = 0;

        curr_block.num_entries--;
        bfs.write_block(curr_dir,(void*)& curr_block);
      }
      else{
        strcpy(buffer, "501: File is a directory\r\n");
        error = true;
      }
    }
  }
  if(!found && !error){
    strcpy(buffer, "503 File does not exist\r\n");
    error = true;
  }else{
    strcpy(buffer, "200 OK\r\nLength: 0\r\n\r\n");
  }
  //send buffer to client
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rsi, %r14
movq %rdi, %rbx
movswl 0x4(%rdi), %esi
leaq 0x90(%rsp), %rdx
callq 0x380e
xorl %r12d, %r12d
pushq $0x1
popq %r15
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
cmpq $0xa, %r12
je 0x441b
imulq $0xc, %r12, %r13
leaq 0x98(%rsp), %rax
addq %rax, %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3350
testl %eax, %eax
jne 0x4413
movswl 0xa(%r13), %esi
movq %rbx, %rdi
callq 0x3d22
testb %al, %al
je 0x4398
movups 0x3e47(%rip), %xmm0     # 0x81bf
movups %xmm0, 0x11b(%rsp)
movups 0x3e2d(%rip), %xmm0     # 0x81b4
movaps %xmm0, 0x110(%rsp)
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x4413
movswl 0xa(%r13), %esi
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x380e
movl 0x14(%rsp), %eax
shrl $0x7, %eax
incq %rax
xorl %ebp, %ebp
cmpq %rax, %rbp
jae 0x43dd
movswl 0x18(%rsp,%r12,2), %esi
movq %rbx, %rdi
callq 0x37b6
andw $0x0, 0x18(%rsp,%r12,2)
andl $0x0, 0x14(%rsp)
incq %rbp
movq %r15, %rax
jmp 0x43b6
movswl 0xa(%r13), %esi
movq %rbx, %rdi
callq 0x37b6
movb $0x0, (%r13)
andw $0x0, 0xa(%r13)
decl 0x94(%rsp)
movswl 0x4(%rbx), %esi
movq %rbx, %rdi
leaq 0x90(%rsp), %rdx
callq 0x3814
movb $0x1, %bpl
incq %r12
jmp 0x4334
orb 0x8(%rsp), %bpl
testb $0x1, %bpl
je 0x4449
movups 0x3ca8(%rip), %xmm0     # 0x80d5
movaps %xmm0, 0x110(%rsp)
movabsq $0xa0d0a0d30203a, %rax  # imm = 0xA0D0A0D30203A
movq %rax, 0x11e(%rsp)
jmp 0x4467
movups 0x3cc3(%rip), %xmm0     # 0x8113
movups %xmm0, 0x11a(%rsp)
movups 0x3caa(%rip), %xmm0     # 0x8109
movaps %xmm0, 0x110(%rsp)
movl 0x8(%rbx), %edi
leaq 0x110(%rsp), %rsi
movl $0x109, %edx            # imm = 0x109
xorl %ecx, %ecx
callq 0x3170
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq