// File: /workspace/github_cpp_makefile_star2_O3/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 0x676c(%rip), %rdx      # 0x15773
leaq 0x670(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rcx
xorl %eax, %eax
callq 0x5850
leaq 0x6755(%rip), %rsi      # 0x1577a
movq %rbx, %rdi
callq 0x55c0
testq %rax, %rax
je 0xf03a
movq %rax, %rdi
callq 0x54a0
leaq 0x7942(%rip), %rsi      # 0x16983
movq %rbx, %rdi
callq 0x55c0
testq %rax, %rax
je 0xf20c
movq %rax, %rbp
leaq 0x6721(%rip), %rsi      # 0x1577d
leaq 0x670(%rsp), %rdi
callq 0x55c0
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xf204
movq %r13, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x18(%rsp)
leaq 0x470(%rsp), %r15
leaq 0x66eb(%rip), %r14      # 0x15780
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 0xf16b
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5490
cmpl $0x2, %eax
jne 0xf0aa
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x56b0
testl %eax, %eax
jne 0xf0fd
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x56b0
testl %eax, %eax
je 0xf10c
movq %r15, %rdi
movq 0x28(%rsp), %rsi
callq 0x5550
jmp 0xf0aa
incl 0x14(%rsp)
cmpb $0x0, 0x13(%rsp)
je 0xf0aa
movq $0x0, (%rsp)
leaq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
leaq 0x61a6(%rip), %rdx      # 0x152d6
movq 0x18(%rsp), %rcx
movq %rdx, %r8
movq 0x38(%rsp), %r9
xorl %eax, %eax
callq 0xed54
movq 0x28(%rsp), %rdi
leaq 0x6640(%rip), %rsi      # 0x15790
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0x56f0
jmp 0xf0aa
cmpl $0x0, 0x14(%rsp)
sete %al
movq 0x30(%rsp), %r9
testq %r9, %r9
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xf1d2
movq $0x0, (%rsp)
leaq 0x6142(%rip), %rdx      # 0x152d6
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 0xed54
leaq 0x65d3(%rip), %rsi      # 0x15790
movq 0x28(%rsp), %rdi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x56f0
movq %rbp, %rdi
callq 0x54a0
movq 0x28(%rsp), %rdi
callq 0x54a0
movq %rbx, %rdi
callq 0x5930
leaq 0x670(%rsp), %rdi
movq %rbx, %rsi
callq 0x52b0
movl $0x1, %r14d
jmp 0xf20c
movq %rbp, %rdi
callq 0x54a0
movl %r14d, %eax
addq $0x1678, %rsp           # imm = 0x1678
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq