// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movswl 0x4(%rdi), %esi
leaq 0x20(%rsp), %r14
movq %rdi, (%rsp)
movq %r14, %rdx
callq 0x3956
leaq 0xc(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3270
xorl %eax, %eax
leaq 0x16(%rsp), %r12
xorl %r13d, %r13d
xorl %ebx, %ebx
leaq (%rax,%rax,2), %rcx
leaq (%r14,%rcx,4), %r14
addq $0x12, %r14
movq %rax, %rbp
leaq -0xa(%r14), %rsi
movq %r12, %rdi
callq 0x3270
movq %r12, %rdi
movq %r15, %rsi
callq 0x3370
testl %eax, %eax
je 0x470a
incq %rbp
addq $0xc, %r14
cmpq $0xa, %rbp
jne 0x46e0
jmp 0x473b
movswl (%r14), %esi
movq (%rsp), %rdi
leaq 0x9b0(%rsp), %rdx
callq 0x3956
leaq 0x1(%rbp), %rax
movb $0x1, %r13b
movq %rbp, %rbx
cmpq $0x9, %rbp
leaq 0x20(%rsp), %r14
jne 0x46d1
movl $0x9, %ebx
jmp 0x4745
testb $0x1, %r13b
je 0x4902
movslq %ebx, %rax
leaq (%rax,%rax,2), %rax
leaq 0x20(%rsp), %rcx
leaq (%rcx,%rax,4), %r15
addq $0x12, %r15
movswl (%r15), %esi
leaq 0x1230(%rsp), %r14
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3956
cmpl $-0x1, (%r14)
je 0x4929
movswl (%r15), %esi
leaq 0xa30(%rsp), %rdx
movq %rbx, %rdi
callq 0x3956
xorl %eax, %eax
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %edx, %edx
cmpw $0x0, 0xa38(%rsp,%rax,2)
setne %dl
sete %sil
orb %cl, %sil
movl %esi, %edi
xorb $0x1, %dil
orb %dil, %cl
addl %edx, %ebp
testb $0x1, %sil
cmovel %eax, %r13d
incq %rax
cmpq $0x3c, %rax
jne 0x4798
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 0x59e1(%rip), %r15      # 0xa1e9
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 0x5a21(%rip), %xmm0     # 0xa258
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 0x59ec(%rip), %xmm0     # 0xa269
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 0x49a4
movups 0x5856(%rip), %xmm0     # 0xa15f
movups %xmm0, 0xa3a(%rsp)
movups 0x583d(%rip), %xmm0     # 0xa155
movaps %xmm0, 0xa30(%rsp)
movq (%rsp), %rbx
jmp 0x49fc
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
callq 0x3160
movups 0x58e4(%rip), %xmm0     # 0xa224
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 0x58c8(%rip), %xmm0     # 0xa236
movups %xmm0, 0x1b0(%rsp,%rax)
movl $0x203a6b, 0x1bf(%rsp,%rax) # imm = 0x203A6B
movswl (%r15), %edx
leaq 0x585d(%rip), %rsi      # 0xa1e9
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 0x58c2(%rip), %rsi      # 0xa28c
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
nop