// File: /workspace/github_c_makefile_star2_O2/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, -0x68(%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 0x10f0
testl %eax, %eax
js 0x38bd
movl %eax, %edi
callq 0x1240
leaq -0x60(%rbp), %rdi
callq 0x41b4
movq %rax, %r15
movl $0x320, %edi            # imm = 0x320
callq 0x1180
movq %rax, %r13
xorl %r14d, %r14d
pushq $0x7c
popq %rbx
cmpq $0x64, %r14
je 0x34cd
movq %rbx, %rdi
callq 0x1180
movq %rax, (%r13,%r14,8)
incq %r14
jmp 0x34b5
leaq -0x98(%rbp), %rbx
movq %r15, -0x88(%rbp)
movq %r15, (%rbx)
pushq $0x1
popq %rax
movq %rax, -0x30(%rbp)
leaq 0x21d2(%rip), %r12      # 0x56be
movq -0x98(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x3563
movq %rax, %r15
movq %rax, %rdi
movq -0x68(%rbp), %rsi
callq 0x11a0
testl %eax, %eax
je 0x34ec
movq %r15, %rdi
callq 0x10a0
movq %rsp, %r14
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
xorl %eax, %eax
movb (%r15,%rax), %cl
cmpb $0x5e, %cl
je 0x3545
movb %cl, (%rsi,%rax)
incq %rax
jmp 0x3534
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 0x34ec
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 0x41b4
movq %rsp, -0x80(%rbp)
movl -0x30(%rbp), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
xorl %r15d, %r15d
movl $0x1009, %r12d          # imm = 0x1009
pushq $0x2e
popq %rbx
cmpq %r15, -0x30(%rbp)
je 0x35fb
movq (%r13,%r15,8), %rdi
movzbl (%rdi), %eax
addl $-0x41, %eax
cmpl $0xc, %eax
ja 0x35d6
btl %eax, %r12d
jae 0x35d6
movl %ebx, %esi
callq 0x1090
movq %rax, %rdi
callq 0x41b4
movq %rax, %r14
movq %rax, %rdi
callq 0x10a0
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%r15,4)
movq %r14, %rdi
callq 0x1160
incq %r15
jmp 0x35b0
leaq 0x20a7(%rip), %rsi      # 0x56a9
leaq -0x77(%rbp), %rbx
movq %rbx, %rdi
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
callq 0x10a0
movq %rax, %r12
xorl %ebx, %ebx
movq %r13, -0x40(%rbp)
cmpq %rbx, -0x30(%rbp)
je 0x36bc
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %r15d
movq %r12, %r14
movq (%r13,%rbx,8), %r12
movq %r12, %rdi
callq 0x10a0
movq %rax, -0x38(%rbp)
leaq -0x77(%rbp), %r13
movq %r13, %rdi
leaq 0x204d(%rip), %rsi      # 0x56a6
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1050
movq %r13, %rdi
callq 0x10a0
movq %rax, -0x90(%rbp)
movq %r12, %rdi
movq %r14, %r12
callq 0x10a0
movq %r13, %rdi
leaq 0x2020(%rip), %rsi      # 0x56a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x1050
movq %r13, %rdi
callq 0x10a0
addl %r15d, %r12d
addl -0x38(%rbp), %r12d
addl -0x90(%rbp), %r12d
movq -0x40(%rbp), %r13
addl %eax, %r12d
addl $0x5, %r12d
incq %rbx
jmp 0x3625
movq -0x68(%rbp), %r13
movq %r13, %rdi
callq 0x10a0
addl %eax, %r12d
leaq 0xf(%r12), %r14
andq $-0x10, %r14
movq %rsp, %r15
movq %r15, %rbx
subq %r14, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, -0x38(%rbp)
movq %r12, %rdx
callq 0x10c0
leaq 0x1fb1(%rip), %rsi      # 0x56a9
leaq -0x77(%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 %r13, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw $0x20, (%rax,%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
xorl %r13d, %r13d
movw $0x26, %r14w
cmpq %r13, -0x30(%rbp)
je 0x3826
movq %rbx, %rdi
callq 0x10a0
movw $0x24, (%rbx,%rax)
movq -0x40(%rbp), %rax
movq (%rax,%r13,8), %r15
movq %r15, %rdi
callq 0x10a0
leaq -0x77(%rbp), %r12
movq %r12, %rdi
leaq 0x1f2f(%rip), %rsi      # 0x56a9
movq %rax, %rdx
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw %r14w, (%rbx,%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw %r14w, (%rbx,%rax)
movq -0x48(%rbp), %rax
movl (%rax,%r13,4), %edx
movq %r12, %rdi
leaq 0x1ee3(%rip), %rsi      # 0x56a9
xorl %eax, %eax
callq 0x1050
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1220
movq %rbx, %rdi
callq 0x10a0
movw %r14w, (%rbx,%rax)
movzbl (%r15), %eax
addl $-0x41, %eax
cmpl $0xc, %eax
ja 0x380b
movl $0x1009, %ecx           # imm = 0x1009
btl %eax, %ecx
jae 0x380b
movq %r15, %rdi
pushq $0x2e
popq %rsi
callq 0x1090
movq %rax, %rdi
jmp 0x380e
movq %r15, %rdi
callq 0x41b4
movq %rax, %rsi
movq %rbx, %rdi
callq 0x1220
incq %r13
jmp 0x3744
leaq 0x1e82(%rip), %rdi      # 0x56af
xorl %r14d, %r14d
movq -0x38(%rbp), %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x401a
leaq 0x1e55(%rip), %rdi      # 0x56a2
movq %rax, %rsi
xorl %eax, %eax
callq 0x1040
leaq 0x1e62(%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 -0x88(%rbp), %rbx
movq -0x40(%rbp), %r15
cmpq $0x64, %r14
je 0x389a
movq (%r15,%r14,8), %rdi
callq 0x1160
incq %r14
jmp 0x3886
movq %r15, %rdi
callq 0x1160
movq %rbx, %rdi
callq 0x1160
movq -0x80(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x243f(%rip), %rdi      # 0x5d03
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x1170