// File: 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:
subq $0x1688, %rsp           # imm = 0x1688
movq %rdi, 0x1678(%rsp)
movq %rsi, 0x1670(%rsp)
movq %rdx, 0x1668(%rsp)
movq %rcx, 0x1660(%rsp)
movl $0x0, 0x165c(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x1660(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x17e20
movq $0x0, 0x1660(%rsp)
leaq 0x20(%rsp), %rdi
movq 0x1678(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0xa942(%rip), %rdx      # 0x2277b
movb $0x0, %al
callq 0x59a0
movq 0x1678(%rsp), %rdi
leaq 0xa933(%rip), %rsi      # 0x22782
callq 0x56b0
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x17e69
movq 0x18(%rsp), %rdi
callq 0x5560
movq 0x1678(%rsp), %rdi
leaq 0xbb16(%rip), %rsi      # 0x2398e
callq 0x56b0
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
jne 0x17e98
movl $0x0, 0x1684(%rsp)
jmp 0x180b9
leaq 0x20(%rsp), %rdi
leaq 0xa8e1(%rip), %rsi      # 0x22785
callq 0x56b0
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
jne 0x17ece
movq 0x18(%rsp), %rdi
callq 0x5560
movl $0x0, 0x1684(%rsp)
jmp 0x180b9
jmp 0x17ed0
jmp 0x17ed2
leaq 0x1450(%rsp), %rdi
movq 0x18(%rsp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x5980
cmpq $0x0, %rax
je 0x18000
leaq 0x1450(%rsp), %rdi
leaq 0x1250(%rsp), %rdx
leaq 0x1050(%rsp), %rcx
leaq 0xa876(%rip), %rsi      # 0x22788
movb $0x0, %al
callq 0x5540
cmpl $0x2, %eax
je 0x17f20
jmp 0x17ed2
leaq 0x1250(%rsp), %rdi
movq 0x1668(%rsp), %rsi
callq 0x5810
cmpl $0x0, %eax
jne 0x17fe0
leaq 0x1050(%rsp), %rdi
movq 0x1670(%rsp), %rsi
callq 0x5810
cmpl $0x0, %eax
jne 0x17fe0
movl 0x165c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x165c(%rsp)
cmpq $0x0, 0x1660(%rsp)
je 0x17fde
leaq 0x1020(%rsp), %rdi
movq 0x1668(%rsp), %rsi
movq 0x1670(%rsp), %rcx
movq 0x1660(%rsp), %r9
leaq 0xa31b(%rip), %r8       # 0x222ba
xorl %eax, %eax
movq %r8, %rdx
movq $0x0, (%rsp)
movb $0x0, %al
callq 0x178c0
movq 0x10(%rsp), %rdi
movq 0x1668(%rsp), %rdx
movq 0x1670(%rsp), %rcx
leaq 0x1020(%rsp), %r8
leaq 0xa7c1(%rip), %rsi      # 0x22798
movb $0x0, %al
callq 0x5850
jmp 0x17ffb
movq 0x10(%rsp), %rdi
leaq 0x1450(%rsp), %rdx
leaq 0xb784(%rip), %rsi      # 0x23778
movb $0x0, %al
callq 0x5850
jmp 0x17ed2
cmpl $0x0, 0x165c(%rsp)
jne 0x1807b
cmpq $0x0, 0x1660(%rsp)
je 0x1807b
leaq 0x1020(%rsp), %rdi
movq 0x1668(%rsp), %rsi
movq 0x1670(%rsp), %rcx
movq 0x1660(%rsp), %r9
leaq 0xa27e(%rip), %r8       # 0x222ba
xorl %eax, %eax
movq %r8, %rdx
movq $0x0, (%rsp)
movb $0x0, %al
callq 0x178c0
movq 0x10(%rsp), %rdi
movq 0x1668(%rsp), %rdx
movq 0x1670(%rsp), %rcx
leaq 0x1020(%rsp), %r8
leaq 0xa724(%rip), %rsi      # 0x22798
movb $0x0, %al
callq 0x5850
movq 0x18(%rsp), %rdi
callq 0x5560
movq 0x10(%rsp), %rdi
callq 0x5560
movq 0x1678(%rsp), %rdi
callq 0x5aa0
leaq 0x20(%rsp), %rdi
movq 0x1678(%rsp), %rsi
callq 0x5310
movl $0x1, 0x1684(%rsp)
movl 0x1684(%rsp), %eax
addq $0x1688, %rsp           # imm = 0x1688
retq
nopl (%rax,%rax)