// File: /workspace/github_c_makefile_star2_O3/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 0x2fcc
addq $-0x2, %r14
xorl %eax, %eax
movb 0x2(%rbx,%rax), %cl
testb %cl, %cl
je 0x2fc4
movb %cl, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x2fb8
leaq -0x50(%rbp), %rbx
movq %r15, (%rbx)
leaq 0x12bc(%rip), %r14      # 0x4296
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12a0
movq %rax, %r15
movq (%rbx), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x12a0
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
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1160
movq %r15, %rdi
callq 0x10b0
leaq 0x19(%rax), %rbx
andq $-0x10, %rbx
movq %rsp, %r12
movq %r12, %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
xorl %eax, %eax
movb %al, 0x2(%r12,%rbx)
xorl %ebx, %ebx
movw $0x2f2e, (%r13)         # imm = 0x2F2E
movq %r13, %rdi
movq %r15, -0x40(%rbp)
movq %r15, %rsi
callq 0x1280
movq %r13, %rdi
callq 0x10b0
movabsq $0x7374696d6d6f432f, %rcx # imm = 0x7374696D6D6F432F
movq %rcx, (%rax,%r13)
movb %bl, 0x8(%rax,%r13)
movq %r13, %rdi
callq 0x1220
testq %rax, %rax
je 0x3127
movq %rax, %r12
movq %rax, %rdi
callq 0x1230
testq %rax, %rax
je 0x31d0
xorl %r13d, %r13d
leaq 0x15dc(%rip), %rbx      # 0x46b1
leaq -0x48(%rbp), %r15
cmpb $0x2e, 0x13(%rax)
jne 0x30f1
cmpb $0x0, 0x14(%rax)
je 0x3112
cmpb $0x2e, 0x14(%rax)
jne 0x30f1
cmpb $0x0, 0x15(%rax)
je 0x3112
addq $0x13, %rax
movq %rax, -0x48(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x12a0
movq %rax, %rdi
callq 0x1260
movl %eax, %r13d
movq %r12, %rdi
callq 0x1230
testq %rax, %rax
jne 0x30d9
incl %r13d
jmp 0x31d6
movq %r13, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10c0
testl %eax, %eax
js 0x3310
movq -0x40(%rbp), %rdi
callq 0x10b0
movq %rsp, %r15
movq %rsp, %rbx
addq $0x1a, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1160
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 0x1100
testl %eax, %eax
js 0x32dd
movl %eax, %ebx
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x32dd
leaq 0x1214(%rip), %rsi      # 0x43c9
movl $0x1a, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12c0
xorl %edi, %edi
jmp 0x32d6
movl $0x1, %r13d
leaq 0x1554(%rip), %rsi      # 0x4731
leaq -0x48(%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 0x1280
movq -0x40(%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 0x1160
movq %r15, %rdi
callq 0x10b0
movabsq $0x7374696d6d6f432f, %rcx # imm = 0x7374696D6D6F432F
movq %rcx, (%rax,%r15)
movw $0x2f, 0x8(%rax,%r15)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1280
movq %r15, %rdi
movl $0x41, %esi
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x1100
testl %eax, %eax
js 0x32f6
movl %eax, %ebx
movq -0x38(%rbp), %r15
movq %r14, %rdi
callq 0x10b0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
testl %eax, %eax
js 0x32dd
leaq 0x110a(%rip), %rsi      # 0x43c9
movl $0x1a, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movl %ebx, %edi
callq 0x12c0
movq %r12, %rdi
callq 0x1270
jmp 0x32f1
leaq 0x1090(%rip), %rsi      # 0x4374
movl $0x54, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq %r15, %rsp
jmp 0x3324
leaq 0x1077(%rip), %rsi      # 0x4374
movl $0x54, %edx
movl -0x2c(%rbp), %edi
callq 0x10a0
movq -0x38(%rbp), %rsp
jmp 0x3324
leaq 0x1004(%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