// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1678, %rsp           # imm = 0x1678
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
movb (%rcx), %al
xorl %r14d, %r14d
movb %al, 0x13(%rsp)
testb %al, %al
movl $0x0, %r13d
movq %rcx, 0x38(%rsp)
cmovneq %rcx, %r13
leaq 0x646c(%rip), %rdx      # 0x12773
leaq 0x670(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rcx
xorl %eax, %eax
callq 0x5860
leaq 0x6455(%rip), %rsi      # 0x1277a
movq %rbx, %rdi
callq 0x55a0
testq %rax, %rax
je 0xc33a
movq %rax, %rdi
callq 0x5490
leaq 0x7642(%rip), %rsi      # 0x13983
movq %rbx, %rdi
callq 0x55a0
testq %rax, %rax
je 0xc504
movq %rax, %rbp
leaq 0x6421(%rip), %rsi      # 0x1277d
leaq 0x670(%rsp), %rdi
callq 0x55a0
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xc4fc
movq %r13, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x470(%rsp), %r15
leaq 0x63eb(%rip), %r14      # 0x12780
leaq 0x270(%rsp), %r13
leaq 0x70(%rsp), %r12
movl $0x0, 0x14(%rsp)
movq %r15, %rdi
movl $0x200, %esi            # imm = 0x200
movq %rbp, %rdx
callq 0x5840
testq %rax, %rax
je 0xc468
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5470
cmpl $0x2, %eax
jne 0xc3aa
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x56c0
testl %eax, %eax
jne 0xc3fd
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x56c0
testl %eax, %eax
je 0xc40c
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x5540
jmp 0xc3aa
incl 0x14(%rsp)
cmpb $0x0, 0x13(%rsp)
je 0xc3aa
andq $0x0, (%rsp)
leaq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
leaq 0x5ea9(%rip), %rdx      # 0x122d6
movq 0x18(%rsp), %rcx
movq %rdx, %r8
movq 0x38(%rsp), %r9
xorl %eax, %eax
callq 0xc04f
movq 0x28(%rsp), %rdi
leaq 0x6343(%rip), %rsi      # 0x12790
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0x5700
jmp 0xc3aa
cmpl $0x0, 0x14(%rsp)
sete %al
movq 0x30(%rsp), %r9
testq %r9, %r9
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xc4cc
andq $0x0, (%rsp)
leaq 0x5e48(%rip), %rdx      # 0x122d6
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rsi
movq 0x18(%rsp), %r15
movq %r15, %rcx
movq %rdx, %r8
xorl %eax, %eax
callq 0xc04f
leaq 0x62d9(%rip), %rsi      # 0x12790
movq 0x28(%rsp), %rdi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x5700
movq %rbp, %rdi
callq 0x5490
movq 0x28(%rsp), %rdi
callq 0x5490
movq %rbx, %rdi
callq 0x5930
leaq 0x670(%rsp), %rdi
movq %rbx, %rsi
callq 0x52b0
pushq $0x1
popq %r14
jmp 0xc504
movq %rbp, %rdi
callq 0x5490
movl %r14d, %eax
addq $0x1678, %rsp           # imm = 0x1678
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq