// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %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
pushq $0x2
popq %rax
cmpq %rax, %r14
jbe 0x2ea8
movb (%rbx,%rax), %cl
testb %cl, %cl
je 0x2ea3
movb %cl, -0x2(%r15,%rax)
incq %rax
jmp 0x2e92
leaq -0x58(%rbp), %rbx
movq %r15, (%rbx)
leaq 0x13e0(%rip), %r14      # 0x4296
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1290
movq %rax, %r15
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, %r14
subq %rax, %r14
movq %r14, %rsp
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1150
movq %r15, %rdi
movq %r15, %r14
callq 0x10b0
leaq 0x19(%rax), %r15
andq $-0x10, %r15
movq %rsp, %r12
movq %r12, %rbx
subq %r15, %rbx
movq %rbx, %rsp
movq %rax, %rcx
andq $-0x4, %rcx
addq $0x5, %rax
cmpq $-0x8, %rcx
movl $0x0, %edx
cmovneq %rax, %rdx
leaq 0x3(%rbx), %rdi
xorl %esi, %esi
callq 0x10d0
negq %r15
movb %r13b, 0x2(%r12,%r15)
movw $0x2f2e, (%rbx)         # imm = 0x2F2E
movq %rbx, %rdi
movq %r14, -0x48(%rbp)
movq %r14, %rsi
callq 0x1270
movq %rbx, %rdi
callq 0x10b0
movabsq $0x7374696d6d6f432f, %r15 # imm = 0x7374696D6D6F432F
movq %r15, (%rax,%rbx)
movb %r13b, 0x8(%rax,%rbx)
movq %rbx, %rdi
callq 0x1210
testq %rax, %rax
je 0x30ff
movq %rax, %r12
leaq 0x170c(%rip), %r14      # 0x46b1
leaq -0x50(%rbp), %rbx
movq %r12, %rdi
callq 0x1220
testq %rax, %rax
je 0x2ff1
cmpb $0x2e, 0x13(%rax)
jne 0x2fce
cmpb $0x0, 0x14(%rax)
je 0x2fa9
cmpb $0x2e, 0x14(%rax)
jne 0x2fce
cmpb $0x0, 0x15(%rax)
je 0x2fa9
addq $0x13, %rax
movq %rax, -0x50(%rbp)
movq %rax, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1290
movq %rax, %rdi
callq 0x1250
movl %eax, %r13d
jmp 0x2fa9
incl %r13d
leaq 0x1736(%rip), %rsi      # 0x4731
movq %r15, %r14
leaq -0x50(%rbp), %r15
movq %r15, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
movq %r15, %rdi
callq 0x10b0
movq %rsp, -0x38(%rbp)
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 %r15, %rsi
callq 0x1270
movq -0x48(%rbp), %r13
movq %r13, %rdi
callq 0x10b0
movq %rax, %r15
movq %rbx, %rdi
callq 0x10b0
addq %r15, %rax
addq $0x17, %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
movq %r13, %rsi
callq 0x1150
movq %r15, %rdi
callq 0x10b0
movq %r14, (%rax,%r15)
movw $0x2f, 0x8(%rax,%r15)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1270
pushq $0x41
popq %rsi
movq %r15, %rdi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x31a3
movl %eax, %ebx
movq -0x38(%rbp), %r15
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x31bb
leaq 0x12e9(%rip), %rsi      # 0x43c9
pushq $0x1a
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12b0
movq %r12, %rdi
callq 0x1260
jmp 0x31cd
movq %rbx, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10c0
testl %eax, %eax
js 0x31e9
movq -0x48(%rbp), %rdi
callq 0x10b0
movq %rsp, %r12
movq %rsp, %r15
addq $0x1a, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1150
movq %r15, %rdi
callq 0x10b0
movl $0x312e2f, (%rax,%r15)  # imm = 0x312E2F
pushq $0x41
popq %rsi
movq %r15, %rdi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x31d2
movl %eax, %ebx
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x31d2
leaq 0x1241(%rip), %rsi      # 0x43c9
pushq $0x1a
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12b0
xorl %edi, %edi
callq 0x1260
jmp 0x31e4
leaq 0x11ca(%rip), %rsi      # 0x4374
pushq $0x54
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq -0x38(%rbp), %rsp
jmp 0x31fb
leaq 0x11b2(%rip), %rsi      # 0x4374
pushq $0x54
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq %r15, %rsp
jmp 0x31fb
leaq 0x119b(%rip), %rsi      # 0x4374
pushq $0x54
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq %r12, %rsp
jmp 0x31fb
leaq 0x112b(%rip), %rsi      # 0x431b
pushq $0x58
popq %rdx
movl -0x2c(%rbp), %edi
callq 0x10a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq