// File: /workspace/github_cpp_makefile_star2_O3/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::cat(char const*)
void FileSys::cat(const char *name)
{
  bool error = false;
  bool found = false;

  // may need to increase buffer size to account for terminal messages
  char buffer [MAX_FILE_SIZE + 256];

  datablock_t* file_contents = new datablock_t;
  dirblock_t dir_block;
  bfs.read_block(curr_dir, (void *) &dir_block);
  for(int i = 0; i < MAX_DIR_ENTRIES; i++)
    {
      //target file found in current element in dir_entries
      if(strcmp(name, dir_block.dir_entries[i].name)==0)
        {
          found = true;
          if(is_directory(dir_block.dir_entries[i].block_num))
            {
              strcpy(buffer, "501: File is a directory\r\n");
              error = true;
              delete file_contents;
              break;
            }
          else if(!is_directory(dir_block.dir_entries[i].block_num))
            {
              inode_t file_inode;
              bfs.read_block(dir_block.dir_entries[i].block_num, (void *) &file_inode);

              string file_size_str = to_string(file_inode.size);
              int byte_count = (file_size_str.length() + 1);
              char* file_size = new char[byte_count];
              strcpy(file_size, file_size_str.c_str());

              strcat(buffer, "200 OK\r\n Length:");
              strcat(buffer, file_size);
              strcat(buffer, "\r\n");
              strcat(buffer, "\r\n");
              delete[] file_size;

              //append each data block pointed to by inode_t.blocks[] to our buffer
              for(int j = 0; j < sizeof(file_inode.blocks); j++)
                {
                  bfs.read_block(file_inode.blocks[j], (void *) &file_contents);
                  strcat(buffer, file_contents->data);
                }
              delete file_contents;
              // send buffer to socket here
            }
        }
    }

    // if point reached, file not found.
    // ERROR 503 File does not exist
    if(!found && !error)
    {
      strcpy(buffer, "503: File does not exist\r\n");
    }

  delete file_contents;
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2048, %rsp           # imm = 0x2048
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movl $0x80, %edi
callq 0x32a0
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
movswl 0x4(%rbx), %esi
leaq 0xc0(%rsp), %rdx
movq %rbx, %rdi
callq 0x3956
leaq 0x140(%rsp), %rbp
xorl %r12d, %r12d
movq $0x0, 0x30(%rsp)
leaq (%r12,%r12,2), %rax
leaq 0xc0(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
addq $0x8, %r13
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x3370
leaq 0x40(%rsp), %r14
testl %eax, %eax
je 0x55e9
incq %r12
cmpq $0xa, %r12
jne 0x55b1
jmp 0x579c
movswl 0xa(%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3956
cmpl $-0x1, 0x40(%rsp)
je 0x57c3
movswl 0xa(%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3956
cmpl $-0x1, 0x40(%rsp)
movb $0x1, %al
movq %rax, 0x30(%rsp)
je 0x55db
movswl 0xa(%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3956
movl 0x44(%rsp), %r13d
movl $0x1, %eax
cmpl $0xa, %r13d
jb 0x5687
movl $0x4, %eax
movl %r13d, %ecx
movl $0xd1b71759, %esi       # imm = 0xD1B71759
cmpl $0x63, %ecx
jbe 0x5680
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x5685
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x5687
movl %ecx, %edx
imulq %rsi, %rdx
shrq $0x2d, %rdx
addl $0x4, %eax
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x564f
addl $-0x3, %eax
jmp 0x5687
addl $-0x2, %eax
jmp 0x5687
decl %eax
movl %eax, %esi
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x3320
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %esi
movl %r13d, %edx
callq 0x5c7b
movl 0x18(%rsp), %eax
incl %eax
movslq %eax, %rdi
callq 0x3030
movq %rax, %r13
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x3270
movq %rbp, %rdi
callq 0x3160
xorl %ecx, %ecx
movb %cl, 0x150(%rsp,%rax)
movups 0x4c5a(%rip), %xmm0     # 0xa340
movups %xmm0, 0x140(%rsp,%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3450
movq %rbp, %rdi
callq 0x3160
movw $0xa0d, %r14w           # imm = 0xA0D
movw %r14w, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %rbp, %rdi
callq 0x3160
movw %r14w, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %r13, %rdi
callq 0x3350
movl $0x4, %r14d
movswl 0x40(%rsp,%r14,2), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3956
movq 0x8(%rsp), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3450
incq %r14
cmpq $0x7c, %r14
jne 0x5740
movl $0x80, %esi
movq %r13, %rdi
callq 0x32c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x55db
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32c0
jmp 0x55db
testb $0x1, 0x30(%rsp)
jne 0x57f0
movups 0x4bb2(%rip), %xmm0     # 0xa35c
movups %xmm0, 0x14b(%rsp)
movups 0x4b98(%rip), %xmm0     # 0xa351
movaps %xmm0, 0x140(%rsp)
jmp 0x57f0
movups 0x4a41(%rip), %xmm0     # 0xa20b
movups %xmm0, 0x14b(%rsp)
movups 0x4a27(%rip), %xmm0     # 0xa200
movaps %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32c0
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32c0
movl 0x8(%rbx), %edi
leaq 0x140(%rsp), %rsi
movl $0x1f00, %edx           # imm = 0x1F00
xorl %ecx, %ecx
callq 0x3180
addq $0x2048, %rsp           # imm = 0x2048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x582a
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5849
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0x3440
nop