// File: /workspace/github_c_makefile_star2_O0/evanwire[P]GitClone/client.c
// Function: add
void add(char* project_name, char* file_name){
  //Verify the project exists on the client side
  DIR* dir = opendir(project_name);
  if(!dir){
    printf("\nError: the project you specified does not seem to exist on the client side. Please create the project before running again.\n");
    exit(1);
  }
  closedir(dir);

  char manifest_path[sizeof(project_name) + 10];
  strcpy(manifest_path, project_name);
  strcat(manifest_path, "/.Manifest");

  char file_path[sizeof(project_name) + sizeof(file_name) + 2];
  memset(file_path, '\0', sizeof(project_name) + sizeof(file_name) + 2);
  strcpy(file_path, project_name);
  strcat(file_path, "/");
  strcat(file_path, file_name);

  //Open file to copy and manifest to copy to
  int manifest_fd = open(manifest_path, O_WRONLY | O_APPEND);
  if(manifest_fd < 0){
    printf("\nError: could not open .Manifest. Please try again.\n");
    exit(1);
  }

  int file_fd = open(file_path, O_RDONLY);
  if(file_fd < 0){
    printf("\nError: could not find %s in your project. Make sure it is in the project, and provide a path through any subdirectories when you specify the file's name. Please  Please try again.\n");
    exit(1);
  }

  //Read file contents to a single char*
  char* file_contents = readFile(file_path);
  printf("\n%s\n", file_contents);

  //Hash the file
  char* hashed_file = hashIt(file_contents);

  //Create an entry in the manifest for the new file
  int size_of_write = strlen(project_name) + strlen(hashed_file) + strlen(file_name) + 12;
  char to_be_written[size_of_write];
  strcpy(to_be_written, "A./");
  strcat(to_be_written, file_path);
  strcat(to_be_written, "~V:0~H:");
  strcat(to_be_written, hashed_file);
  strcat(to_be_written, "\n");


  int bytes_written = write(manifest_fd, to_be_written, size_of_write);
  if(bytes_written < 0){
    printf("\nError: was unable to write your file data to the .Manifest file.\n");
    exit(1);
  }

  free(hashed_file);
  free(file_contents);
  close(manifest_fd);
  close(file_fd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0x11b0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x55ef
leaq 0x2109(%rip), %rdi      # 0x76e7
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movq -0x18(%rbp), %rdi
callq 0x11e0
leaq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1140
leaq -0x30(%rbp), %rdi
leaq 0x2091(%rip), %rsi      # 0x76a1
callq 0x1200
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x12, %edx
callq 0x10d0
leaq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1140
leaq -0x50(%rbp), %rdi
leaq 0x2215(%rip), %rsi      # 0x7852
callq 0x1200
leaq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1200
leaq -0x30(%rbp), %rdi
movl $0x401, %esi            # imm = 0x401
movb $0x0, %al
callq 0x10f0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jge 0x5680
leaq 0x20f6(%rip), %rdi      # 0x7765
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jge 0x56ae
leaq 0x20fd(%rip), %rdi      # 0x779a
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
leaq -0x50(%rbp), %rdi
callq 0x5e50
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
leaq 0x1fe6(%rip), %rdi      # 0x76ac
xorl %eax, %eax
callq 0x1040
movq -0x60(%rbp), %rdi
callq 0x5f60
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
addl %ecx, %eax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movl -0x94(%rbp), %eax
movl %ecx, %edx
movl %eax, %ecx
movl %edx, %eax
leal 0xc(%rax,%rcx), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x90(%rbp)
movq %rdi, %rsp
movq %rax, -0x80(%rbp)
leaq 0x20fc(%rip), %rsi      # 0x7850
callq 0x1140
movq -0x90(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1200
movq -0x90(%rbp), %rdi
leaq 0x20dd(%rip), %rsi      # 0x7854
callq 0x1200
movq -0x90(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x1200
movq -0x90(%rbp), %rdi
leaq 0x29af(%rip), %rsi      # 0x8149
callq 0x1200
movq -0x90(%rbp), %rsi
movl -0x54(%rbp), %edi
movslq -0x6c(%rbp), %rdx
callq 0x1090
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jge 0x57d9
leaq 0x2094(%rip), %rdi      # 0x785c
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movq -0x68(%rbp), %rdi
callq 0x1160
movq -0x60(%rbp), %rdi
callq 0x1160
movl -0x54(%rbp), %edi
callq 0x1220
movl -0x58(%rbp), %edi
callq 0x1220
movq -0x78(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)