// File: dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::ls()
void FileSys::ls()
{
  char bufferStart[] = "200 OK\r\n";
  string buffer = "";
  char msg[2048];
  char msgLength[80];
  dirblock_t curr_block_ptr;
  char curr_file_name[MAX_FNAME_SIZE + 1];
  //read the data held at the current directory from the disk
  bfs.read_block(curr_dir, (void*)&curr_block_ptr);
  for (unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    if(curr_block_ptr.dir_entries[i].block_num > 0){
      if(is_directory(curr_block_ptr.dir_entries[i].block_num)){
        string temp(curr_block_ptr.dir_entries[i].name);
        buffer += temp;
        buffer += "/ ";
      }
      else{
        string temp2(curr_block_ptr.dir_entries[i].name);
        buffer += temp2;
        buffer += " ";
      }
    }
  }
  strcpy(msg, bufferStart);
  sprintf(msgLength, "Length: %d", sizeof(buffer.c_str()));
  strcat(msg, msgLength);
  strcat(msg, "\r\n\r\n");
  strcat(msg, buffer.c_str());
  send(fs_sock, msg, sizeof(msg), 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x9d0, %rsp            # imm = 0x9D0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x990(%rbp)
movabsq $0xa0d4b4f20303032, %rax # imm = 0xA0D4B4F20303032
movq %rax, -0x18(%rbp)
movb $0x0, -0x10(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x988(%rbp)
callq 0x3570
movq -0x988(%rbp), %rdx
leaq 0x78b0(%rip), %rsi      # 0xc0ea
leaq -0x38(%rbp), %rdi
callq 0x69b0
jmp 0x4845
leaq -0x39(%rbp), %rdi
callq 0x33b0
movq -0x990(%rbp), %rdi
movswl 0x4(%rdi), %esi
leaq -0x920(%rbp), %rdx
callq 0x3c20
jmp 0x4867
movl $0x0, -0x930(%rbp)
cmpl $0xa, -0x930(%rbp)
jae 0x4a9e
leaq -0x920(%rbp), %rax
addq $0x8, %rax
movl -0x930(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %eax
cmpl $0x0, %eax
jle 0x4a88
movq -0x990(%rbp), %rdi
movl -0x930(%rbp), %eax
leaq (%rax,%rax,2), %rax
movswl -0x90e(%rbp,%rax,4), %esi
callq 0x4470
movb %al, -0x991(%rbp)
jmp 0x48c9
movb -0x991(%rbp), %al
testb $0x1, %al
jne 0x48d8
jmp 0x49c6
movl -0x930(%rbp), %eax
leaq (%rax,%rax,2), %rax
leaq -0x918(%rbp,%rax,4), %rax
movq %rax, -0x9a8(%rbp)
leaq -0x951(%rbp), %rdi
movq %rdi, -0x9a0(%rbp)
callq 0x3570
movq -0x9a8(%rbp), %rsi
movq -0x9a0(%rbp), %rdx
leaq -0x950(%rbp), %rdi
callq 0x69b0
jmp 0x4920
leaq -0x951(%rbp), %rdi
callq 0x33b0
leaq -0x38(%rbp), %rdi
leaq -0x950(%rbp), %rsi
callq 0x3450
jmp 0x493e
leaq 0x7856(%rip), %rsi      # 0xc19b
leaq -0x38(%rbp), %rdi
callq 0x3330
jmp 0x4950
leaq -0x950(%rbp), %rdi
callq 0x3628
jmp 0x4a86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x33b0
jmp 0x4b58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4b4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x951(%rbp), %rdi
callq 0x33b0
jmp 0x4b4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x950(%rbp), %rdi
callq 0x3628
jmp 0x4b4f
movl -0x930(%rbp), %eax
leaq (%rax,%rax,2), %rax
leaq -0x918(%rbp,%rax,4), %rax
movq %rax, -0x9b8(%rbp)
leaq -0x979(%rbp), %rdi
movq %rdi, -0x9b0(%rbp)
callq 0x3570
movq -0x9b8(%rbp), %rsi
movq -0x9b0(%rbp), %rdx
leaq -0x978(%rbp), %rdi
callq 0x69b0
jmp 0x4a0e
leaq -0x979(%rbp), %rdi
callq 0x33b0
leaq -0x38(%rbp), %rdi
leaq -0x978(%rbp), %rsi
callq 0x3450
jmp 0x4a2c
leaq 0x7769(%rip), %rsi      # 0xc19c
leaq -0x38(%rbp), %rdi
callq 0x3330
jmp 0x4a3e
leaq -0x978(%rbp), %rdi
callq 0x3628
jmp 0x4a86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x979(%rbp), %rdi
callq 0x33b0
jmp 0x4b4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x978(%rbp), %rdi
callq 0x3628
jmp 0x4b4f
jmp 0x4a88
jmp 0x4a8a
movl -0x930(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x930(%rbp)
jmp 0x4871
leaq -0x850(%rbp), %rdi
movq %rdi, -0x9c8(%rbp)
leaq -0x18(%rbp), %rsi
callq 0x3320
leaq 0x76e2(%rip), %rsi      # 0xc19e
xorl %eax, %eax
movl %eax, -0x9bc(%rbp)
leaq -0x8a0(%rbp), %rdi
movq %rdi, -0x9d0(%rbp)
movl $0x8, %edx
callq 0x30b0
movq -0x9d0(%rbp), %rsi
movq -0x9c8(%rbp), %rdi
callq 0x3580
movq -0x9c8(%rbp), %rdi
leaq 0x75e9(%rip), %rsi      # 0xc0e6
callq 0x3580
leaq -0x38(%rbp), %rdi
callq 0x3190
movq -0x9c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3580
movq -0x9c8(%rbp), %rsi
movl -0x9bc(%rbp), %ecx
movq -0x990(%rbp), %rax
movl 0x8(%rax), %edi
movl $0x800, %edx            # imm = 0x800
callq 0x31d0
jmp 0x4b3d
leaq -0x38(%rbp), %rdi
callq 0x3628
addq $0x9d0, %rsp            # imm = 0x9D0
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x3628
movq -0x48(%rbp), %rdi
callq 0x3560
nopw %cs:(%rax,%rax)