// File: 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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x238(%rbp)
movb $0x0, -0x1a1(%rbp)
movb $0x0, -0x1a2(%rbp)
leaq -0x1a0(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c20
movl $0x0, -0x1a8(%rbp)
cmpl $0xa, -0x1a8(%rbp)
jae 0x5061
leaq -0x1a0(%rbp), %rdi
addq $0x8, %rdi
movl -0x1a8(%rbp), %eax
imulq $0xc, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x3480
cmpl $0x0, %eax
jne 0x504b
movq -0x238(%rbp), %rdi
leaq -0x1a0(%rbp), %rax
addq $0x8, %rax
movl -0x1a8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x4470
testb $0x1, %al
jne 0x502f
movq -0x238(%rbp), %rdi
movb $0x1, -0x1a2(%rbp)
leaq -0x1a0(%rbp), %rax
addq $0x8, %rax
movl -0x1a8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
leaq -0x228(%rbp), %rdx
movswl 0xa(%rax), %esi
callq 0x3c20
movq $0x0, -0x230(%rbp)
movq -0x230(%rbp), %rax
movl -0x224(%rbp), %ecx
shrl $0x7, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x4fa3
movq -0x238(%rbp), %rdi
movl -0x1a8(%rbp), %eax
movswl -0x220(%rbp,%rax,2), %esi
callq 0x3b70
movl -0x1a8(%rbp), %eax
movw $0x0, -0x220(%rbp,%rax,2)
movl $0x0, -0x224(%rbp)
movq -0x230(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x230(%rbp)
jmp 0x4f41
movq -0x238(%rbp), %rdi
leaq -0x1a0(%rbp), %rax
addq $0x8, %rax
movl -0x1a8(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x3b70
movq -0x238(%rbp), %rax
leaq -0x1a0(%rbp), %rcx
addq $0x8, %rcx
movl -0x1a8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movb $0x0, (%rcx)
leaq -0x1a0(%rbp), %rcx
addq $0x8, %rcx
movl -0x1a8(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movw $0x0, 0xa(%rcx)
movl -0x19c(%rbp), %ecx
addl $-0x1, %ecx
movl %ecx, -0x19c(%rbp)
leaq -0x1a0(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c50
jmp 0x5049
leaq -0x120(%rbp), %rdi
leaq 0x7180(%rip), %rsi      # 0xc1bd
callq 0x3320
movb $0x1, -0x1a1(%rbp)
jmp 0x504b
jmp 0x504d
movl -0x1a8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1a8(%rbp)
jmp 0x4e99
testb $0x1, -0x1a2(%rbp)
jne 0x508f
testb $0x1, -0x1a1(%rbp)
jne 0x508f
leaq -0x120(%rbp), %rdi
leaq 0x7091(%rip), %rsi      # 0xc112
callq 0x3320
movb $0x1, -0x1a1(%rbp)
jmp 0x50a2
leaq -0x120(%rbp), %rdi
leaq 0x7038(%rip), %rsi      # 0xc0d5
callq 0x3320
movq -0x238(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x120(%rbp), %rsi
movl $0x109, %edx            # imm = 0x109
xorl %ecx, %ecx
callq 0x31d0
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopl (%rax,%rax)