// File: /workspace/github_c_makefile_star2_O0/evanwire[P]GitClone/server.c
// Function: currentVersion
void currentVersion(char* command, int connfd){
  //Parse project name from the message received from the client
  char project_name[strlen(command) - 1];
  memset(project_name, 0, sizeof(project_name));
  int i;
  for(i = 2; i < strlen(command); i++){
      if(command[i]){
        project_name[i - 2] = command[i];
      }

  }

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

  //Attempt to open directory with the name specified by the client
  DIR* dir = opendir(project_name);

  //Report an error assuming the project does not exist
  if(ENOENT == errno){
    closedir(dir);
    strcpy(sendBuff, "x");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;

  //project does exist, so continue
  }else if (dir){
    //do nothing and continue

  //opendir() failed
  }else{
    printf("\nERROR: opendir() failed.\n");
    strcpy(sendBuff, "e");
    printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
    write(connfd, sendBuff, strlen(sendBuff));
    return;
  }

  //Open the needed Manifest file
  char* manifestPath = (char*)malloc(sizeof(project_name)+11);
  strcpy(manifestPath,project_name);
  strcat(manifestPath,"/.Manifest");

  int manifest_fd = open(manifestPath, O_RDONLY);

    /*
    TODO: Communicate errors like these to clients in an efficient way
        -Make a method for only communicating errors
    */
  if(manifest_fd < 0){
    printf("\nError: Was unable to open/create the .Manifest file. Please try again.\n");
    exit(1);
  }
  char* fileContents=readFile(manifestPath);
  int j=0;
  //read current project version
  char* currentChar=(char*)malloc(sizeof(char)*2);
  memset(currentChar,'\0',sizeof(currentChar));
  currentChar[0]=fileContents[j];
  strcpy(sendBuff,currentChar);
  j++;
  currentChar[0]=fileContents[j];
  strcat(sendBuff,currentChar);
  j++;
  while (fileContents[j]!='\n'){
      currentChar[0]=fileContents[j];
      strcat(sendBuff,currentChar);
      j++;
  }
  strcat(sendBuff,"\n");
  j++;

  //now we read all filenames and their version numbers. each iteration is one line
  // of .Manifest

  while(fileContents[j]!='\0'){
      //read and store in sendBuff until first ~
        while(fileContents[j]!='~'){
            currentChar[0]=fileContents[j];
            strcat(sendBuff,currentChar);
            j++;
        }
        j++;
        strcat(sendBuff,"~");

      //read and store in sendBuff until second ~
      while(fileContents[j]!='~'){
            currentChar[0]=fileContents[j];
            strcat(sendBuff,currentChar);
            j++;
        }


      //read and DON'T store until end of line except \n
      while(fileContents[j]!='\n'){
            j++;
        }
        strcat(sendBuff,"\n");
        j++;

  }


  printf("Message: -%s- size: %d\n", sendBuff, strlen(sendBuff));
  write(connfd, sendBuff, strlen(sendBuff));

  //cleanup
  closedir(dir);
  close(manifest_fd);
  free(manifestPath);
  free(currentChar);
  free(fileContents);


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4d0, %rsp            # imm = 0x4D0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
decq %rdx
movq %rdx, -0x478(%rbp)
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
addq $0xe, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x470(%rbp)
movq %rdi, %rsp
movq %rdx, -0x20(%rbp)
xorl %esi, %esi
callq 0x10e0
movl $0x2, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x480(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movq -0x480(%rbp), %rax
cmpq %rcx, %rax
jae 0x4015
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x4008
movq -0x470(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movb (%rcx,%rdx), %dl
movl -0x24(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x400a
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3fb9
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x10e0
movq -0x470(%rbp), %rdi
callq 0x1220
movq %rax, -0x438(%rbp)
callq 0x1030
movq %rax, %rcx
movl $0x2, %eax
cmpl (%rcx), %eax
jne 0x40e8
movq -0x438(%rbp), %rdi
callq 0x1260
leaq -0x430(%rbp), %rdi
leaq 0x146f(%rip), %rsi      # 0x54d9
callq 0x1160
leaq -0x430(%rbp), %rax
movq %rax, -0x498(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movq -0x498(%rbp), %rsi
movq %rax, %rdx
leaq 0x140b(%rip), %rdi      # 0x54a5
movb $0x0, %al
callq 0x1040
movl -0xc(%rbp), %eax
movl %eax, -0x48c(%rbp)
leaq -0x430(%rbp), %rax
movq %rax, -0x488(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movl -0x48c(%rbp), %edi
movq -0x488(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x43c(%rbp)
jmp 0x4520
cmpq $0x0, -0x438(%rbp)
je 0x40f7
jmp 0x4191
leaq 0x12b2(%rip), %rdi      # 0x53b0
movb $0x0, %al
callq 0x1040
leaq -0x430(%rbp), %rdi
leaq 0x12b8(%rip), %rsi      # 0x53cb
callq 0x1160
leaq -0x430(%rbp), %rax
movq %rax, -0x4b0(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movq -0x4b0(%rbp), %rsi
movq %rax, %rdx
leaq 0x1362(%rip), %rdi      # 0x54a5
movb $0x0, %al
callq 0x1040
movl -0xc(%rbp), %eax
movl %eax, -0x4a4(%rbp)
leaq -0x430(%rbp), %rax
movq %rax, -0x4a0(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movl -0x4a4(%rbp), %edi
movq -0x4a0(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x43c(%rbp)
jmp 0x4520
jmp 0x4193
movq -0x478(%rbp), %rdi
addq $0xb, %rdi
callq 0x11e0
movq -0x470(%rbp), %rsi
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rdi
callq 0x1160
movq -0x448(%rbp), %rdi
leaq 0x1296(%rip), %rsi      # 0x5461
callq 0x1270
movq -0x448(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1100
movl %eax, -0x44c(%rbp)
cmpl $0x0, -0x44c(%rbp)
jge 0x4207
leaq 0x11d7(%rip), %rdi      # 0x53cd
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x11b0
movq -0x448(%rbp), %rdi
callq 0x4cb0
movq %rax, -0x458(%rbp)
movl $0x0, -0x45c(%rbp)
movl $0x2, %edi
callq 0x11e0
movq %rax, -0x468(%rbp)
movq -0x468(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x10e0
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x468(%rbp), %rax
movb %cl, (%rax)
leaq -0x430(%rbp), %rdi
movq -0x468(%rbp), %rsi
callq 0x1160
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x468(%rbp), %rax
movb %cl, (%rax)
leaq -0x430(%rbp), %rdi
movq -0x468(%rbp), %rsi
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x4315
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x468(%rbp), %rax
movb %cl, (%rax)
leaq -0x430(%rbp), %rdi
movq -0x468(%rbp), %rsi
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x42c0
leaq -0x430(%rbp), %rdi
leaq 0x10a6(%rip), %rsi      # 0x53c9
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x4471
jmp 0x4354
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7e, %eax
je 0x43a9
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x468(%rbp), %rax
movb %cl, (%rax)
leaq -0x430(%rbp), %rdi
movq -0x468(%rbp), %rsi
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x4354
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
leaq -0x430(%rbp), %rdi
leaq 0x110e(%rip), %rsi      # 0x54d4
callq 0x1270
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7e, %eax
je 0x4420
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x468(%rbp), %rax
movb %cl, (%rax)
leaq -0x430(%rbp), %rdi
movq -0x468(%rbp), %rsi
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x43cb
jmp 0x4422
movq -0x458(%rbp), %rax
movslq -0x45c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
je 0x444a
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x4422
leaq -0x430(%rbp), %rdi
leaq 0xf71(%rip), %rsi       # 0x53c9
callq 0x1270
movl -0x45c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x45c(%rbp)
jmp 0x4337
leaq -0x430(%rbp), %rax
movq %rax, -0x4c8(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movq -0x4c8(%rbp), %rsi
movq %rax, %rdx
leaq 0x1009(%rip), %rdi      # 0x54a5
movb $0x0, %al
callq 0x1040
movl -0xc(%rbp), %eax
movl %eax, -0x4bc(%rbp)
leaq -0x430(%rbp), %rax
movq %rax, -0x4b8(%rbp)
leaq -0x430(%rbp), %rdi
callq 0x10c0
movl -0x4bc(%rbp), %edi
movq -0x4b8(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movq -0x438(%rbp), %rdi
callq 0x1260
movl -0x44c(%rbp), %edi
callq 0x12b0
movq -0x448(%rbp), %rdi
callq 0x11a0
movq -0x468(%rbp), %rdi
callq 0x11a0
movq -0x458(%rbp), %rdi
callq 0x11a0
movl $0x0, -0x43c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)