// File: /workspace/github_c_makefile_star2_O1/evanwire[P]GitClone/client.c
// Function: push
void push(char* projectName){
  //Creating a path for the commit filec
  char commitPath[sizeof(projectName) + 8];
  strcpy(commitPath, projectName);
  strcat(commitPath, "/.Commit");

  //Checking for its existence
  int commitfd = open(commitPath,  O_RDONLY);
  if(commitfd < 0){
    printf("\nError: was unable to open .Commit file for the project you specified. Please make sure both the project and the .Commit file exist.\n");
    exit(1);
  }
  close(commitfd);

  //Read commit file to a char*
  char* commitContents = readFile(commitPath);

  //Will be storing all file paths in a char**
  int i;
  char** file_paths = malloc(100 * sizeof(char*));
  for(i = 0; i < 100; i++){
    file_paths[i] = malloc(124 * sizeof(char));
  }
  int file_paths_idx = 1;

  //tokenize the commit file by every line
  char* token;
  char* saveptr = commitContents;

  //loop through the tokens
  while(token = strtok_r(saveptr, "\n", &saveptr)){
    //store each file path in the commit
    if(strcmp(token, projectName) == 0){
      continue;
    }
    int idx = 0;
    char file_path[strlen(token)];
    while(token[idx] != '^'){
      file_path[idx] = token[idx];
      idx++;
    }
    file_path[idx] = '\0';
    strcpy(file_paths[file_paths_idx], file_path);
    file_paths_idx++; 
  }

  //Adding .Commit to the list of file paths
  strcpy(file_paths[0], "./");
  strcat(file_paths[0], commitPath);

  //Getting a copy of the .Commit to send to the client
  char* commit = readFile(commitPath);

  //Looping through all file paths, getting their sizes
  int file_sizes[file_paths_idx];
  
  for(i = 0; i < file_paths_idx; i++){
    if(file_paths[i][0] != 'A' && file_paths[i][0] != 'M' && file_paths[i][0] != 'D'){
      char* temp = readFile(file_paths[i]);
      file_sizes[i] = strlen(temp);
      free(temp);
    }else{
      char* temp = readFile(strchr(file_paths[i], '.'));
      
      file_sizes[i] = strlen(temp);
      free(temp);
    }
   
  }

  //Calculate bytes needed for message to client
  int bytes_needed = 0;
  char numStorage[15];
  sprintf(numStorage, "%d", file_paths_idx);
  bytes_needed += strlen(numStorage);
  for(i = 0; i< file_paths_idx; i++){
    //size of file
    bytes_needed += file_sizes[i];
    //length of file path
    bytes_needed += strlen(file_paths[i]);
    //the bytes it will take to write the size of the file's contents
    sprintf(numStorage, "%d %d", file_sizes[i], i);
    bytes_needed += strlen(numStorage);
    //the bytes it will take to write the size of the file's name
    sprintf(numStorage, "%d", strlen(file_paths[i]));
    bytes_needed += strlen(numStorage);
    bytes_needed += 5;
  }
  bytes_needed += strlen(projectName);

  //Set up buffer for responding to client
  char sendBuff[bytes_needed];
  memset(sendBuff, '\0', sizeof(sendBuff));

  //Copy the number of files to sendBuff
  sprintf(numStorage, "%d", file_paths_idx);
  strcpy(sendBuff, "p ");
  strcat(sendBuff, projectName);
  strcat(sendBuff, " ");
  strcat(sendBuff, numStorage);


  //for each file, copy the length of its path, its path, the length of its contents, and its contents
  for(i = 0; i < file_paths_idx; i++){
    strcat(sendBuff, "$");
    sprintf(numStorage, "%d", strlen(file_paths[i]));
    strcat(sendBuff, numStorage);
    strcat(sendBuff, "&");
    strcat(sendBuff, file_paths[i]);
    strcat(sendBuff, "&");
    sprintf(numStorage, "%d", file_sizes[i]);
    strcat(sendBuff, numStorage);
    strcat(sendBuff, "&");
    if(file_paths[i][0] != 'A' && file_paths[i][0] != 'M' && file_paths[i][0] != 'D'){
      char* contents = readFile(file_paths[i]);
      strcat(sendBuff, contents);
    }else{
      char* contents = readFile(strchr(file_paths[i], '.'));
      strcat(sendBuff, contents);
    }
    


  }
  strcat(sendBuff, "\0");

  printf("\nBytes: %d\n %s\n\n", bytes_needed, sendBuff);


  //Send this info to the server, receive its response and print it
  char* response = connect_to_WTFserver(sendBuff);
  printf("\n%s", response);
  char finished[100];
  sprintf(finished, "rm -rf %s", commitPath);
  system(finished);

  //free(response);
  for(i = 0; i < 100; i++){
    free(file_paths[i]);
  }
  free(file_paths);
  free(commitContents);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rsi
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %rsi, -0x38(%rbp)
callq 0x1140
movq %rbx, %rdi
callq 0x10a0
movabsq $0x74696d6d6f432e2f, %rcx # imm = 0x74696D6D6F432E2F
movq %rcx, -0x60(%rbp,%rax)
movb $0x0, -0x58(%rbp,%rax)
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
js 0x3a60
movl %eax, %edi
callq 0x1240
leaq -0x60(%rbp), %rdi
callq 0x43c0
movq %rax, %r14
movl $0x320, %edi            # imm = 0x320
callq 0x1180
movq %rax, %r13
xorl %ebx, %ebx
movl $0x7c, %edi
callq 0x1180
movq %rax, (%r13,%rbx,8)
incq %rbx
cmpq $0x64, %rbx
jne 0x365f
leaq -0x98(%rbp), %rbx
movq %r14, -0x80(%rbp)
movq %r14, (%rbx)
movl $0x1, %eax
movq %rax, -0x30(%rbp)
leaq 0x2029(%rip), %r12      # 0x56be
movq -0x38(%rbp), %r14
movq -0x98(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x3713
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x11a0
testl %eax, %eax
je 0x3699
movq %r15, %rdi
callq 0x10a0
movq %rsp, %r14
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
movb (%r15), %cl
xorl %eax, %eax
cmpb $0x5e, %cl
je 0x36f5
movb %cl, (%rsi,%rax)
movb 0x1(%r15,%rax), %cl
incq %rax
jmp 0x36e3
movb $0x0, (%rsi,%rax)
movq -0x30(%rbp), %r15
movq (%r13,%r15,8), %rdi
callq 0x1140
incq %r15
movq %r15, -0x30(%rbp)
movq %r14, %rsp
jmp 0x3695
movq (%r13), %rdi
movb $0x0, 0x2(%rdi)
movw $0x2f2e, (%rdi)         # imm = 0x2F2E
leaq -0x60(%rbp), %rbx
movq %rbx, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x43c0
movq %rsp, -0x78(%rbp)
movq -0x30(%rbp), %r12
movl %r12d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
xorl %r14d, %r14d
movl $0x1009, %r15d          # imm = 0x1009
movq (%r13,%r14,8), %rdi
movzbl (%rdi), %eax
addl $-0x41, %eax
cmpl $0xc, %eax
ja 0x3784
btl %eax, %r15d
jae 0x3784
movl $0x2e, %esi
callq 0x1090
movq %rax, %rdi
callq 0x43c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x10a0
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
movq %rbx, %rdi
callq 0x1160
incq %r14
cmpq %r14, %r12
jne 0x3761
leaq 0x1ef6(%rip), %rsi      # 0x56a9
leaq -0x6f(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
callq 0x10a0
movq %r13, -0x40(%rbp)
movq %rax, %r13
xorl %ebx, %ebx
movq %r12, -0x30(%rbp)
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %r15d
movq -0x40(%rbp), %rax
movq (%rax,%rbx,8), %r12
movq %r12, %rdi
callq 0x10a0
movq %rax, -0x90(%rbp)
leaq -0x6f(%rbp), %r14
movq %r14, %rdi
leaq 0x1ea0(%rip), %rsi      # 0x56a6
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1050
movq %r14, %rdi
callq 0x10a0
movq %rax, -0x88(%rbp)
movq %r12, %rdi
callq 0x10a0
movq %r14, %rdi
leaq 0x1e76(%rip), %rsi      # 0x56a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x1050
movq %r14, %rdi
callq 0x10a0
addl %r15d, %r13d
addl -0x90(%rbp), %r13d
addl -0x88(%rbp), %r13d
addl %eax, %r13d
addl $0x5, %r13d
incq %rbx
cmpq %rbx, -0x30(%rbp)
jne 0x37d9
movq -0x38(%rbp), %rdi
callq 0x10a0
addl %eax, %r13d
leaq 0xf(%r13), %r14
andq $-0x10, %r14
movq %rsp, %r15
movq %r15, %rbx
subq %r14, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x10c0
leaq 0x1e0b(%rip), %rsi      # 0x56a9
leaq -0x6f(%rbp), %r12
movq %r12, %rdi
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x1050
negq %r14
movb $0x0, 0x2(%r15,%r14)
movw $0x2070, (%rbx)         # imm = 0x2070
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw $0x20, (%rax,%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x10a0
movw $0x24, (%rbx,%rax)
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %r15
movq %r15, %rdi
callq 0x10a0
leaq -0x6f(%rbp), %rdi
leaq 0x1d9a(%rip), %rsi      # 0x56a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
leaq -0x6f(%rbp), %r12
movq %r12, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw $0x26, %cx
movw %cx, (%rbx,%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw $0x26, %cx
movw %cx, (%rbx,%rax)
movq -0x48(%rbp), %rax
movl (%rax,%r14,4), %edx
movq %r12, %rdi
leaq 0x1d44(%rip), %rsi      # 0x56a9
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw $0x26, %cx
movw %cx, (%rbx,%rax)
movzbl (%r15), %eax
addl $-0x41, %eax
cmpl $0xc, %eax
ja 0x39af
movl $0x1009, %ecx           # imm = 0x1009
btl %eax, %ecx
jae 0x39af
movq %r15, %rdi
movl $0x2e, %esi
callq 0x1090
movq %rax, %rdi
jmp 0x39b2
movq %r15, %rdi
callq 0x43c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1220
incq %r14
cmpq %r14, -0x30(%rbp)
jne 0x38e6
leaq 0x1cd9(%rip), %rdi      # 0x56af
xorl %r14d, %r14d
movl %r13d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x4202
leaq 0x1cad(%rip), %rdi      # 0x56a2
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
leaq 0x1cba(%rip), %rsi      # 0x56c0
leaq -0x100(%rbp), %rbx
leaq -0x60(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
callq 0x1120
movq -0x80(%rbp), %rbx
movq -0x40(%rbp), %r15
movq (%r15,%r14,8), %rdi
callq 0x1160
incq %r14
cmpq $0x64, %r14
jne 0x3a2b
movq %r15, %rdi
callq 0x1160
movq %rbx, %rdi
callq 0x1160
movq -0x78(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x229c(%rip), %rdi      # 0x5d03
callq 0x11d0
movl $0x1, %edi
callq 0x1170