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

  }

  //Tokenize to identify project name and .Commit contents
  char* token;
  char* saveptr = message;
  token = strtok_r(saveptr, " ", &saveptr);
  char* projectName = token;
  token = strtok_r(saveptr, " ", &saveptr);
  char commitFile[strlen(token) + 2];
  memset(commitFile, 0, strlen(token) + 2);
  strcpy(commitFile, token);


  //Add commit contents to a new file in the commits folder within the project, or create commits folder if it was not already created
  char commits_path[strlen(projectName) + 10];
  memset(commits_path, 0, strlen(projectName) + 8);
  strcpy(commits_path, "./");
  strcat(commits_path, projectName);
  strcat(commits_path, "/Commits");
  DIR* dir = opendir(commits_path);

  //If Commits folder does not exist in the project, make it and create the first commit file
  if(dir == NULL){
    int test = mkdir(commits_path, 0777);
    if(test < 0){
      char* msg = "\nError: Server was unable to create a commits folder for this project. Please try again.";
      write(connfd, msg, strlen(msg));
      return;
    }
    char commitFile_path[strlen(projectName) + 11];
    strcpy(commitFile_path, commits_path);
    strcat(commitFile_path, "/.1");
    int commitfd = open(commitFile_path, O_WRONLY | O_CREAT, 0777);
    if(commitfd < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the directory here (maybe?)
      return;
    }
    int bytes_written = write(commitfd, commitFile , strlen(commitFile));
    if(bytes_written < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the file here
      return;
    }
    char* msg = "\nStatus: Successful commit";
    write(connfd, msg, strlen(msg));
    close(commitfd);
    closedir(dir);

    //Commits folder exists, must create a new commit file with a unique name
  }else{

    struct dirent* ent;
    int newCommitName = 0;
    while((ent = readdir(dir)) != NULL){
      char* fname = ent->d_name;
      if(strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0){
        continue;
      }
      char* tkn;
      char* svptr = fname;
      tkn = strtok_r(svptr, ".", &svptr);
      newCommitName = atoi(tkn);
    }

    newCommitName++;
    char nameBuffer[5];
    sprintf(nameBuffer, "%d\0", newCommitName);
    char newCommitFullName[strlen(nameBuffer) + 1];
    strcpy(newCommitFullName, ".");
    strcat(newCommitFullName, nameBuffer);


    char commitFile_path[strlen(projectName) + 8 + strlen(newCommitFullName)];
    strcpy(commitFile_path, projectName);
    strcat(commitFile_path, "/Commits/");
    strcat(commitFile_path, newCommitFullName);

    int commitfd = open(commitFile_path, O_WRONLY | O_CREAT, 0777);
    if(commitfd < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the directory here (maybe?)
      return;
    }
    int bytes_written = write(commitfd, commitFile , strlen(commitFile));
    if(bytes_written < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the file here
      return;
    }
    char* msg = "\nStatus: Successful commit";
    write(connfd, msg, strlen(msg));
    close(commitfd);
    closedir(dir);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, -0x2c(%rbp)
movq %rdi, %rbx
callq 0x10b0
movq %rax, %r14
leaq -0x1(%rax), %rdx
movq %rsp, %r15
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x3, %r14
jb 0x2fb5
addq $-0x2, %r14
xorl %eax, %eax
movb 0x2(%rbx,%rax), %cl
testb %cl, %cl
je 0x2fad
movb %cl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2fa1
leaq -0x50(%rbp), %rbx
movq %r15, (%rbx)
leaq 0x12d3(%rip), %r14      # 0x4296
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1290
movq %rax, %r12
movq (%rbx), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1290
movq %rax, %rbx
movq %rax, %rdi
callq 0x10b0
leaq 0x2(%rax), %rdx
addq $0x11, %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r15, -0x38(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1150
movq %r12, %rdi
callq 0x10b0
leaq 0x19(%rax), %rbx
andq $-0x10, %rbx
movq %rsp, %r15
movq %r15, %r13
subq %rbx, %r13
movq %r13, %rsp
movq %rax, %rcx
andq $-0x4, %rcx
addq $0x5, %rax
cmpq $-0x8, %rcx
movl $0x0, %edx
cmovneq %rax, %rdx
leaq 0x3(%r13), %rdi
xorl %esi, %esi
callq 0x10d0
negq %rbx
movb %r14b, 0x2(%r15,%rbx)
movw $0x2f2e, (%r13)         # imm = 0x2F2E
movq %r13, %rdi
movq %r12, -0x40(%rbp)
movq %r12, %rsi
callq 0x1270
movq %r13, %rdi
callq 0x10b0
movabsq $0x7374696d6d6f432f, %rcx # imm = 0x7374696D6D6F432F
movq %rcx, (%rax,%r13)
movb %r14b, 0x8(%rax,%r13)
movq %r13, %rdi
callq 0x1210
testq %rax, %rax
je 0x3127
movq %rax, %r12
movq %rax, %rdi
callq 0x1220
testq %rax, %rax
je 0x31d9
movq %rax, %r14
xorl %r13d, %r13d
leaq 0x131d(%rip), %rbx      # 0x43e5
leaq 0x1315(%rip), %r15      # 0x43e4
addq $0x13, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x11e0
testl %eax, %eax
je 0x310f
movq %r14, %rdi
movq %r15, %rsi
callq 0x11e0
testl %eax, %eax
je 0x310f
movq %r14, -0x48(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
leaq -0x48(%rbp), %rdx
callq 0x1290
movq %rax, %rdi
callq 0x1250
movl %eax, %r13d
movq %r12, %rdi
callq 0x1220
movq %rax, %r14
testq %rax, %rax
jne 0x30cf
incl %r13d
jmp 0x31df
movq %r13, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10c0
testl %eax, %eax
js 0x3317
movq -0x40(%rbp), %rdi
callq 0x10b0
movq %rsp, %r14
movq %rsp, %rbx
addq $0x1a, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1150
movq %rbx, %rdi
callq 0x10b0
movl $0x312e2f, (%rax,%rbx)  # imm = 0x312E2F
movq %rbx, %rdi
movl $0x41, %esi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x32fe
movl %eax, %ebx
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r15, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x32fe
leaq 0x1210(%rip), %rsi      # 0x43c9
movl $0x1a, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12b0
xorl %edi, %edi
callq 0x1260
jmp 0x3312
movl $0x1, %r13d
leaq 0x154e(%rip), %rsi      # 0x4734
leaq -0x48(%rbp), %r14
movq %r14, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
callq 0x10b0
movq %rsp, %r15
movq %rsp, %rcx
addq $0x10, %rax
andq $-0x10, %rax
movq %rcx, %rbx
subq %rax, %rbx
movq %rbx, %rsp
negq %rax
movw $0x2e, (%rcx,%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1270
movq -0x40(%rbp), %r13
movq %r13, %rdi
callq 0x10b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x10b0
addq %r14, %rax
addq $0x17, %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
movq %r13, %rsi
callq 0x1150
movq %r14, %rdi
callq 0x10b0
movabsq $0x7374696d6d6f432f, %rcx # imm = 0x7374696D6D6F432F
movq %rcx, (%rax,%r14)
movw $0x2f, 0x8(%rax,%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1270
movq %r14, %rdi
movl $0x41, %esi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x32e5
movl %eax, %ebx
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x32e5
leaq 0x1102(%rip), %rsi      # 0x43c9
movl $0x1a, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12b0
movq %r12, %rdi
callq 0x1260
jmp 0x32f9
leaq 0x1088(%rip), %rsi      # 0x4374
movl $0x54, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq %r15, %rsp
jmp 0x332b
leaq 0x106f(%rip), %rsi      # 0x4374
movl $0x54, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq %r14, %rsp
jmp 0x332b
leaq 0xffd(%rip), %rsi       # 0x431b
movl $0x58, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq