// File: /workspace/github_cpp_makefile_star2_O1/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::stat(char const*)
void FileSys::stat(const char *name)
{
  dirblock_t curr_block_ptr;
  bfs.read_block(curr_dir, (void*)&curr_block_ptr);
  bool found = false;
  int found_index;

  char file_name[MAX_FNAME_SIZE+1];
  char curr_file_name[MAX_FNAME_SIZE+1];
  strcpy(file_name, name);

  dirblock_t found_dir_ptr;
  bool dir_check;
  char bufferStart[] = "200 OK\r\n";
  char msgLength[80];
  char message[2048];
  char buffer[2048];

  //arrays to hold inode stats and holders for inode values
  bool check_first_block = false;
  int counter = 0;
  short first_block;
  char directory_block[MAX_DATA_BLOCKS];
  char inode_block[MAX_DATA_BLOCKS];
  char bytes_in_file[MAX_FILE_SIZE];
  char number_of_blocks[MAX_DATA_BLOCKS];
  char first_block_num[MAX_DATA_BLOCKS];

  for(unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    strcpy(curr_file_name, curr_block_ptr.dir_entries[i].name);
    if(strcmp(curr_file_name, file_name) == 0){
      found = true;
      found_index = i;
      bfs.read_block(curr_block_ptr.dir_entries[i].block_num,
                     (void*)&found_dir_ptr);
    }
  }

  if(found){
    if(is_directory(curr_block_ptr.dir_entries[found_index].block_num)){
      strcat(buffer, "\nDirectory name: ");
      strcat(buffer, file_name);
      strcat(buffer, "\nDirectory block: ");
      sprintf(directory_block, "%d",
              curr_block_ptr.dir_entries[found_index].block_num);
      strcat(buffer, directory_block);
    }
    else{
      inode_t new_inode;
      bfs.read_block(curr_block_ptr.dir_entries[found_index].block_num,
                     (void*)&new_inode);
      for(int k = 0; k < MAX_DATA_BLOCKS; k++){
        if(new_inode.blocks[k] != 0){
          counter = counter + 1;
          // determine first block
          if(!check_first_block){
            first_block = k;
            check_first_block = true;
          }
        }
      }
      strcat(buffer, "\nInode block: ");
      sprintf(inode_block, "%d",
              curr_block_ptr.dir_entries[found_index].block_num);
      strcat(buffer, inode_block);
      strcat(buffer, "\nBytes in file: ");
      sprintf(bytes_in_file, "%d", new_inode.size);
      strcat(buffer, bytes_in_file);
      strcat(buffer, "\nNumber of blocks: ");
      sprintf(number_of_blocks, "%d", counter);
      strcat(buffer, number_of_blocks);
      strcat(buffer, "\nFirst block: ");
      sprintf(first_block_num, "%d", first_block);
      strcat(buffer, first_block_num);
    }
    strcpy(message, bufferStart);
    sprintf(msgLength, "Length: %d \r\n", sizeof(buffer));
    strcat(message, msgLength);
    //strcat(message, buffer);
    strcat(buffer, message);
  }
  else{
    strcpy(message, "503 File does not exist\r\n");
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3038, %rsp           # imm = 0x3038
movq %rsi, %r15
movswl 0x4(%rdi), %esi
leaq 0x20(%rsp), %rdx
movq %rdi, (%rsp)
callq 0x3924
leaq 0xc(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x3270
leaq 0x32(%rsp), %r13
xorl %ebp, %ebp
leaq 0x16(%rsp), %r15
xorl %r12d, %r12d
leaq -0xa(%r13), %rsi
movq %r15, %rdi
callq 0x3270
movq %r15, %rdi
movq %r14, %rsi
callq 0x3370
testl %eax, %eax
jne 0x46ad
movswl (%r13), %esi
movq (%rsp), %rdi
leaq 0x9b0(%rsp), %rdx
callq 0x3924
movb $0x1, %r12b
movl %ebp, %ebx
incq %rbp
addq $0xc, %r13
cmpq $0xa, %rbp
jne 0x4677
testb $0x1, %r12b
je 0x487f
movslq %ebx, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,4), %r15
addq $0x32, %r15
movswl (%r15), %esi
leaq 0x1230(%rsp), %r14
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3924
cmpl $-0x1, (%r14)
je 0x48a6
movswl (%r15), %esi
leaq 0xa30(%rsp), %rdx
movq %rbx, %rdi
callq 0x3924
xorl %eax, %eax
movl $0x1, %ecx
xorl %ebp, %ebp
xorl %edx, %edx
xorl %esi, %esi
cmpw $0x0, 0xa38(%rsp,%rax,2)
setne %sil
sete %dil
orb %dl, %dil
movzbl %dl, %edx
addl %esi, %ebp
testb $0x1, %dil
cmovel %ecx, %edx
cmovel %eax, %r13d
incq %rax
cmpq $0x3c, %rax
jne 0x4717
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
callq 0x3160
movabsq $0x622065646f6e490a, %rcx # imm = 0x622065646F6E490A
movq %rcx, 0x1b0(%rsp,%rax)
movabsq $0x203a6b636f6c62, %rcx # imm = 0x203A6B636F6C62
movq %rcx, 0x1b7(%rsp,%rax)
movswl (%r15), %edx
leaq 0x5a18(%rip), %r15      # 0xa19d
leaq 0x120(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r12, %rsi
callq 0x3450
movq %r14, %rdi
callq 0x3160
movups 0x5a58(%rip), %xmm0     # 0xa20c
movups %xmm0, 0x1b0(%rsp,%rax)
movb $0x0, 0x1c0(%rsp,%rax)
movl 0xa34(%rsp), %edx
leaq 0x1230(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r12, %rsi
callq 0x3450
movq %r14, %rdi
callq 0x3160
movups 0x5a23(%rip), %xmm0     # 0xa21d
movups %xmm0, 0x1b0(%rsp,%rax)
movl $0x203a73, 0x1c0(%rsp,%rax) # imm = 0x203A73
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r12, %rsi
callq 0x3450
movq %r14, %rdi
callq 0x3160
movabsq $0x622074737269460a, %rcx # imm = 0x622074737269460A
movq %rcx, 0x1b0(%rsp,%rax)
movabsq $0x203a6b636f6c62, %rcx # imm = 0x203A6B636F6C62
movq %rcx, 0x1b7(%rsp,%rax)
movswl %r13w, %edx
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r12, %rsi
jmp 0x4921
movups 0x588d(%rip), %xmm0     # 0xa113
movups %xmm0, 0xa3a(%rsp)
movups 0x5874(%rip), %xmm0     # 0xa109
movaps %xmm0, 0xa30(%rsp)
movq (%rsp), %rbx
jmp 0x4979
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
callq 0x3160
movups 0x591b(%rip), %xmm0     # 0xa1d8
movups %xmm0, 0x1b0(%rsp,%rax)
movw $0x20, 0x1c0(%rsp,%rax)
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x3450
movq %r14, %rdi
callq 0x3160
movups 0x58ff(%rip), %xmm0     # 0xa1ea
movups %xmm0, 0x1b0(%rsp,%rax)
movl $0x203a6b, 0x1bf(%rsp,%rax) # imm = 0x203A6B
movswl (%r15), %edx
leaq 0x5894(%rip), %rsi      # 0xa19d
leaq 0x1230(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r15, %rsi
callq 0x3450
movabsq $0xa0d4b4f20303032, %rax # imm = 0xA0D4B4F20303032
leaq 0xa30(%rsp), %r14
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
leaq 0x58f9(%rip), %rsi      # 0xa240
leaq 0x160(%rsp), %r15
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %eax, %eax
callq 0x3080
movq %r14, %rdi
movq %r15, %rsi
callq 0x3450
leaq 0x1b0(%rsp), %rdi
movq %r14, %rsi
callq 0x3450
movl 0x8(%rbx), %edi
leaq 0x1b0(%rsp), %rsi
movl $0x800, %edx            # imm = 0x800
xorl %ecx, %ecx
callq 0x3180
addq $0x3038, %rsp           # imm = 0x3038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq