// File: /workspace/github_c_makefile_star2_O0/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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
leaq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1140
leaq -0x20(%rbp), %rdi
leaq 0x3514(%rip), %rsi      # 0x7f4b
callq 0x1200
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x10f0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4a6a
leaq 0x3652(%rip), %rdi      # 0x80ab
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x1170
movl -0x24(%rbp), %edi
callq 0x1220
leaq -0x20(%rbp), %rdi
callq 0x5e50
movq %rax, -0x30(%rbp)
movl $0x320, %edi            # imm = 0x320
callq 0x1180
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x64, -0x34(%rbp)
jge 0x4abe
movl $0x7c, %edi
callq 0x1180
movq %rax, %rdx
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4a94
movl $0x1, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
leaq 0x3671(%rip), %rsi      # 0x8149
leaq -0x58(%rbp), %rdx
callq 0x1210
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x4ba6
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1190
cmpl $0x0, %eax
jne 0x4b03
jmp 0x4acd
movl $0x0, -0x5c(%rbp)
movq -0x50(%rbp), %rdi
callq 0x10b0
movq %rax, %rdx
movq %rdx, %rax
movq %rsp, %rcx
movq %rcx, -0x68(%rbp)
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x148(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5e, %eax
je 0x4b71
movq -0x148(%rbp), %rax
movq -0x50(%rbp), %rcx
movslq -0x5c(%rbp), %rdx
movb (%rcx,%rdx), %dl
movslq -0x5c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4b3c
movq -0x148(%rbp), %rsi
movslq -0x5c(%rbp), %rax
movb $0x0, (%rsi,%rax)
movq -0x40(%rbp), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1140
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movq -0x68(%rbp), %rax
movq %rax, %rsp
jmp 0x4acd
movq -0x40(%rbp), %rax
movq (%rax), %rdi
leaq 0x2c9d(%rip), %rsi      # 0x7851
callq 0x1140
movq -0x40(%rbp), %rax
movq (%rax), %rdi
leaq -0x20(%rbp), %rsi
movq %rsi, -0x158(%rbp)
callq 0x1200
movq -0x158(%rbp), %rdi
callq 0x5e50
movq %rax, -0x78(%rbp)
movl -0x44(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x80(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x150(%rbp)
movq %rcx, %rsp
movq %rax, -0x88(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x4d01
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x41, %eax
je 0x4ca1
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x4d, %eax
je 0x4ca1
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x44, %eax
je 0x4ca1
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5e50
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq -0x150(%rbp), %rax
movl %ecx, %edx
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x90(%rbp), %rdi
callq 0x1160
jmp 0x4cf1
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x2e, %esi
callq 0x10a0
movq %rax, %rdi
callq 0x5e50
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq -0x150(%rbp), %rax
movl %ecx, %edx
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x98(%rbp), %rdi
callq 0x1160
jmp 0x4cf3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4c14
movl $0x0, -0x9c(%rbp)
leaq -0xab(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x3418(%rip), %rsi      # 0x8134
movb $0x0, %al
callq 0x1060
leaq -0xab(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movslq -0x9c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x9c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x4e4b
movq -0x150(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x9c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10b0
movq %rax, %rdx
movq -0x150(%rbp), %rax
movslq -0x9c(%rbp), %rcx
addq %rdx, %rcx
movl %ecx, -0x9c(%rbp)
leaq -0xab(%rbp), %rdi
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x34(%rbp), %ecx
leaq 0x337f(%rip), %rsi      # 0x8131
movb $0x0, %al
callq 0x1060
leaq -0xab(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movslq -0x9c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x9c(%rbp)
leaq -0xab(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10b0
movq -0x160(%rbp), %rdi
movq %rax, %rdx
leaq 0x332c(%rip), %rsi      # 0x8134
movb $0x0, %al
callq 0x1060
leaq -0xab(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movslq -0x9c(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
addl $0x5, %eax
movl %eax, -0x9c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4d49
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movl -0x9c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, %edx
movq %rdx, %rax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x168(%rbp)
movq %rdi, %rsp
movq %rdx, -0xb8(%rbp)
xorl %esi, %esi
callq 0x10d0
leaq -0xab(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x328d(%rip), %rsi      # 0x8134
movb $0x0, %al
callq 0x1060
movq -0x168(%rbp), %rdi
leaq 0x327b(%rip), %rsi      # 0x8137
callq 0x1140
movq -0x168(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x1200
movq -0x168(%rbp), %rdi
leaq 0x316c(%rip), %rsi      # 0x804b
callq 0x1200
movq -0x168(%rbp), %rdi
leaq -0xab(%rbp), %rsi
callq 0x1200
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x509f
movq -0x168(%rbp), %rdi
leaq 0x2acf(%rip), %rsi      # 0x79e7
callq 0x1200
leaq -0xab(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10b0
movq -0x170(%rbp), %rdi
movq %rax, %rdx
leaq 0x31e7(%rip), %rsi      # 0x8134
movb $0x0, %al
callq 0x1060
movq -0x168(%rbp), %rdi
leaq -0xab(%rbp), %rsi
callq 0x1200
movq -0x168(%rbp), %rdi
leaq 0x2a74(%rip), %rsi      # 0x79e9
callq 0x1200
movq -0x168(%rbp), %rdi
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1200
movq -0x168(%rbp), %rdi
leaq 0x2a49(%rip), %rsi      # 0x79e9
callq 0x1200
movq -0x150(%rbp), %rax
leaq -0xab(%rbp), %rdi
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x3173(%rip), %rsi      # 0x8134
movb $0x0, %al
callq 0x1060
movq -0x168(%rbp), %rdi
leaq -0xab(%rbp), %rsi
callq 0x1200
movq -0x168(%rbp), %rdi
leaq 0x2a00(%rip), %rsi      # 0x79e9
callq 0x1200
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x41, %eax
je 0x5057
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x4d, %eax
je 0x5057
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x44, %eax
je 0x5057
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x5e50
movq -0x168(%rbp), %rdi
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
callq 0x1200
jmp 0x508f
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl $0x2e, %esi
callq 0x10a0
movq %rax, %rdi
callq 0x5e50
movq -0x168(%rbp), %rdi
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rsi
callq 0x1200
jmp 0x5091
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x4efe
movq -0x168(%rbp), %rdi
leaq 0x323f(%rip), %rsi      # 0x82ec
callq 0x1200
movq -0x168(%rbp), %rdx
movl -0x9c(%rbp), %esi
leaq 0x3074(%rip), %rdi      # 0x813a
movb $0x0, %al
callq 0x1040
movq -0x168(%rbp), %rdi
callq 0x5be0
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rsi
leaq 0x2fb9(%rip), %rdi      # 0x80a7
movb $0x0, %al
callq 0x1040
leaq -0x140(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq 0x3044(%rip), %rsi      # 0x814b
movb $0x0, %al
callq 0x1060
leaq -0x140(%rbp), %rdi
callq 0x1120
movl $0x0, -0x34(%rbp)
cmpl $0x64, -0x34(%rbp)
jge 0x5143
movq -0x40(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5121
movq -0x40(%rbp), %rdi
callq 0x1160
movq -0x30(%rbp), %rdi
callq 0x1160
movq -0x80(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)