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

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

  bfs.read_block(curr_dir, (void*)&curr_block_ptr);
  if(!error){
    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){
        strcpy(buffer, "502: File exists\r\n");
        error = true;
      }
    }
  }

  if(!error){
    if(strlen(name) > MAX_FNAME_SIZE + 1){
      strcpy(buffer, "504: File name is too long\r\n");
      error = true;
    }
  }

  // make new free block
  short block_num = bfs.get_free_block();
  if(block_num == 0){
    block_num = bfs.get_free_block();
    if(block_num == 0){
      if(!error){
        strcpy(buffer, "505: Disk is full\r\n");
        error = true;
      }
    }
  }
  if(!error){
    if(curr_block_ptr.num_entries == MAX_DIR_ENTRIES){
      strcpy(buffer, "506: Directory is full\r\n");
      error = true;
    }
  }

  // fill new inode block_num to hold 0 to show blocks are unused
  if(!error){
    inode_t curr_dir_inode;
    curr_dir_inode.magic = INODE_MAGIC_NUM;
    curr_dir_inode.size = 0;
    for(int k = 0; k < MAX_DIR_ENTRIES; k++){
      curr_dir_inode.blocks[k] = 0;
    }
    bfs.write_block(block_num, (void*) &curr_dir_inode);
    strcpy(buffer, "200 OK\r\nLength: 0\r\n");
    strcat(buffer,file_name);
    strcpy(curr_block_ptr.dir_entries[curr_block_ptr.num_entries].name,
           file_name);
    curr_block_ptr.dir_entries[curr_block_ptr.num_entries].block_num =
      block_num;
    bfs.write_block(curr_dir, (void*)&curr_block_ptr);
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xc(%rsp), %r12
movq %r12, %rdi
callq 0x3270
movswl 0x4(%rbx), %esi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x3956
movl $0x8, %r14d
xorl %ebp, %ebp
leaq 0x16(%rsp), %r13
leaq (%rsp,%r14), %rsi
addq $0x20, %rsi
movq %r13, %rdi
callq 0x3270
movq %r13, %rdi
movq %r12, %rsi
callq 0x3370
testl %eax, %eax
jne 0x4390
movups 0x5d4a(%rip), %xmm0     # 0xa0c4
movaps %xmm0, 0xa0(%rsp)
movl $0xa0d73, 0xaf(%rsp) # imm = 0xA0D73
movb $0x1, %bpl
addq $0xc, %r14
cmpq $0x80, %r14
jne 0x4354
testb $0x1, %bpl
jne 0x43d2
movq %r15, %rdi
callq 0x3160
cmpq $0xb, %rax
jb 0x43d2
movups 0x5d2c(%rip), %xmm0     # 0xa0e4
movups %xmm0, 0xad(%rsp)
movups 0x5d10(%rip), %xmm0     # 0xa0d7
movaps %xmm0, 0xa0(%rsp)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x3880
testw %ax, %ax
jne 0x43f8
movq %rbx, %rdi
callq 0x3880
testw %ax, %ax
jne 0x43f8
testb $0x1, %bpl
je 0x44c5
xorl %eax, %eax
testb $0x1, %bpl
jne 0x4428
cmpl $0xa, 0x24(%rsp)
jne 0x4428
movups 0x5d05(%rip), %xmm0     # 0xa111
movups %xmm0, 0xa9(%rsp)
movups 0x5ced(%rip), %xmm0     # 0xa108
movaps %xmm0, 0xa0(%rsp)
jmp 0x44df
testb $0x1, %bpl
jne 0x44df
leaq 0x4a8(%rsp), %rdx
movl $0xfffffffe, (%rdx)     # imm = 0xFFFFFFFE
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rdx)
movq $0x0, 0x14(%rdx)
movswl %ax, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x395c
movups 0x5d89(%rip), %xmm0     # 0xa1ec
leaq 0xa0(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0xa0d30, 0x10(%rdi)    # imm = 0xA0D30
leaq 0xc(%rsp), %r15
movq %r15, %rsi
callq 0x3450
leaq 0x20(%rsp), %r12
movl 0x4(%r12), %eax
leaq (%rax,%rax,2), %rax
leaq 0x20(%rsp), %r14
leaq (%r14,%rax,4), %rdi
addq $0x8, %rdi
movq %r15, %rsi
callq 0x3270
movl 0x4(%r12), %eax
leaq (%rax,%rax,2), %rax
movw %bp, 0x12(%r14,%rax,4)
movswl 0x4(%rbx), %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x395c
jmp 0x44df
movups 0x5c28(%rip), %xmm0     # 0xa0f4
movaps %xmm0, 0xa0(%rsp)
movl $0xa0d6c, 0xb0(%rsp) # imm = 0xA0D6C
movl 0x8(%rbx), %edi
leaq 0xa0(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x3180
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq