// File: /workspace/github_cpp_makefile_star2_O1/nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: mg_modify_passwords_file
int mg_modify_passwords_file(const char *fname, const char *domain,
                             const char *user, const char *pass) {
  int found;
  char line[512], u[512], d[512], ha1[33], tmp[PATH_MAX];
  FILE *fp, *fp2;

  found = 0;
  fp = fp2 = NULL;

  // Regard empty password as no password - remove user record.
  if (pass[0] == '\0') {
    pass = NULL;
  }

  (void) snprintf(tmp, sizeof(tmp), "%s.tmp", fname);

  // Create the file if does not exist
  if ((fp = mg_fopen(fname, "a+")) != NULL) {
    (void) fclose(fp);
  }

  // Open the given file and temporary file
  if ((fp = mg_fopen(fname, "r")) == NULL) {
    return 0;
  } else if ((fp2 = mg_fopen(tmp, "w+")) == NULL) {
    fclose(fp);
    return 0;
  }

  // Copy the stuff to temporary file
  while (fgets(line, sizeof(line), fp) != NULL) {
    if (sscanf(line, "%[^:]:%[^:]:%*s", u, d) != 2) {
      continue;
    }

    if (!strcmp(u, user) && !strcmp(d, domain)) {
      found++;
      if (pass != NULL) {
        mg_md5(ha1, user, ":", domain, ":", pass, NULL);
        fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
      }
    } else {
      (void) fprintf(fp2, "%s", line);
    }
  }

  // If new user, just add it
  if (!found && pass != NULL) {
    mg_md5(ha1, user, ":", domain, ":", pass, NULL);
    (void) fprintf(fp2, "%s:%s:%s\n", user, domain, ha1);
  }

  // Close files
  (void) fclose(fp);
  (void) fclose(fp2);

  // Put the temp file in place of real file
  (void) mg_remove(fname);
  (void) mg_rename(tmp, fname);

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1668, %rsp           # imm = 0x1668
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
movb (%rcx), %al
xorl %r14d, %r14d
movb %al, 0xb(%rsp)
testb %al, %al
movl $0x0, %ebp
cmovneq %rcx, %rbp
leaq 0x6681(%rip), %rdx      # 0x15773
leaq 0x660(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rcx
xorl %eax, %eax
callq 0x5850
leaq 0x666a(%rip), %rsi      # 0x1577a
movq %rbx, %rdi
callq 0x55c0
testq %rax, %rax
je 0xf125
movq %rax, %rdi
callq 0x54a0
leaq 0x785a(%rip), %rsi      # 0x16986
movq %rbx, %rdi
callq 0x55c0
testq %rax, %rax
je 0xf2f7
movq %rax, %r13
leaq 0x6636(%rip), %rsi      # 0x1577d
leaq 0x660(%rsp), %rdi
callq 0x55c0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xf2ef
movq %rbp, 0x28(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
leaq 0x460(%rsp), %r14
leaq 0x6600(%rip), %r15      # 0x15780
leaq 0x260(%rsp), %rbp
leaq 0x60(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
callq 0x5840
testq %rax, %rax
je 0xf256
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5490
cmpl $0x2, %eax
jne 0xf195
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x56b0
testl %eax, %eax
jne 0xf1e8
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x56b0
testl %eax, %eax
je 0xf1f7
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x5550
jmp 0xf195
incl 0xc(%rsp)
cmpb $0x0, 0xb(%rsp)
je 0xf195
movq $0x0, (%rsp)
leaq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x60bb(%rip), %rdx      # 0x152d6
movq 0x10(%rsp), %rcx
movq %rdx, %r8
movq 0x28(%rsp), %r9
xorl %eax, %eax
callq 0xedf1
movq 0x20(%rsp), %rdi
leaq 0x6555(%rip), %rsi      # 0x15790
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
leaq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x56f0
jmp 0xf195
cmpl $0x0, 0xc(%rsp)
sete %al
movq 0x28(%rsp), %r9
testq %r9, %r9
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xf2bd
movq $0x0, (%rsp)
leaq 0x6057(%rip), %rdx      # 0x152d6
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %r15
movq %r15, %rcx
movq %rdx, %r8
xorl %eax, %eax
callq 0xedf1
leaq 0x64e8(%rip), %rsi      # 0x15790
movq 0x20(%rsp), %rdi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x56f0
movq %r13, %rdi
callq 0x54a0
movq 0x20(%rsp), %rdi
callq 0x54a0
movq %rbx, %rdi
callq 0x5930
leaq 0x660(%rsp), %rdi
movq %rbx, %rsi
callq 0x52b0
movl $0x1, %r14d
jmp 0xf2f7
movq %r13, %rdi
callq 0x54a0
movl %r14d, %eax
addq $0x1668, %rsp           # imm = 0x1668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq