// File: /workspace/github_c_makefile_star2_O2/debbiemarkslab[P]plmc/src/weights.c
// Function: ReadCustomWeightsFile
void ReadCustomWeightsFile(char *weightsFile, alignment_t *ali) {
    /* Note: Not using options->scale (or options->theta) for now (assuming this is done in original weights calc).*/
    /* Most of this is copied from MSAReweightSequences() */

    int validCode = ValidateCustomWeightsFile(weightsFile, ali);
    if (validCode != 0) {
        fprintf(stderr, "Error: weights file %s is invalid\n", weightsFile);
        exit(1);
    }

    /* Load weights (float array) into ali->weights and set ali->nEff */
    FILE *fp = fopen(weightsFile, "r");
    if (fp == NULL) {
        fprintf(stderr, "Error: could not open weights file %s\n", weightsFile);
        exit(1);
    }
    /* Reinitialize array just in case */
    for (int i = 0; i < ali->nSeqs; i++) ali->weights[i] = 1.0;

    /* Read weights, one float per line */
    int skippedIdx = 0, reducedIdx = 0, nWarnings = 0, maxWarnings = 64;
    int nSeqsTotal = ali->nSeqs + ali->nSkippedSeqs;
    for (int i = 0; i < nSeqsTotal; i++) {
        long double w;
        if (fscanf(fp, "%Lf", &w) != 1) {
            fprintf(stderr, "Error reading weights file %s at position %d\n", weightsFile, i);
            exit(1);
        }
        // Skip invalid sequence weights
        if ((skippedIdx < ali->nSkippedSeqs) && (i == ali->skippedSeqs[skippedIdx])) {
            if (w > 0 && nWarnings < maxWarnings) {
                fprintf(stderr, "Warning: Skipped nonzero weight in file %s at position %d\n", weightsFile, i);
                nWarnings++;
            }
            skippedIdx++;
            continue;
        } else {
            ali->weights[reducedIdx] = (numeric_t)w;
            reducedIdx++;
        }
    }
    fclose(fp);

    /* The effective number of sequences is then the sum of the weights */
    ali->nEff = 0;
    for (int i = 0; i < ali->nSeqs; i++) ali->nEff += ali->weights[i];

    fprintf(stderr,
            "Weights loaded successfully. Effective number of samples (to 1 decimal place): %.1f.\n",
            ali->nEff);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r12
callq 0xa9e8
testl %eax, %eax
jne 0xac59
leaq 0x2712(%rip), %rsi      # 0xd1f0
movq %r12, %rdi
callq 0x11c0
testq %rax, %rax
je 0xac6c
movq %rax, %r15
movl (%rbx), %r14d
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
cmpq %rax, %rcx
je 0xab1f
movq 0x50(%rbx), %rsi
movq %rdx, (%rsi,%rax,8)
incq %rax
jmp 0xab0d
movq %r12, 0x8(%rsp)
addl 0x28(%rbx), %r14d
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpl %ebp, %r14d
je 0xabd9
movq %r15, %rdi
leaq 0x1ea6(%rip), %rsi      # 0xc9fa
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1080
cmpl $0x1, %eax
jne 0xac32
cmpl 0x28(%rbx), %r13d
jge 0xab9e
movq 0x30(%rbx), %rax
movslq %r13d, %rcx
cmpl (%rax,%rcx,4), %ebp
jne 0xab9e
fldt 0x10(%rsp)
fldz
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
seta %al
cmpl $0x40, 0x4(%rsp)
setl %cl
andb %al, %cl
cmpb $0x1, %cl
je 0xabb4
incl %r13d
jmp 0xabb0
fldt 0x10(%rsp)
movq 0x50(%rbx), %rax
movslq %r12d, %r12
fstpl (%rax,%r12,8)
incl %r12d
incl %ebp
jmp 0xab41
movq 0x543d(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x1e67(%rip), %rsi      # 0xca2c
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1170
incl 0x4(%rsp)
jmp 0xab99
movq %r15, %rdi
callq 0x10b0
andq $0x0, 0x48(%rbx)
movl (%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
xorpd %xmm0, %xmm0
cmpq %rcx, %rax
je 0xac0b
movq 0x50(%rbx), %rdx
addsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rbx)
incq %rcx
jmp 0xabf3
movq 0x53e6(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x1e4b(%rip), %rsi      # 0xca67
movb $0x1, %al
callq 0x1170
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x53bf(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x1dbb(%rip), %rsi      # 0xc9fe
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1170
pushq $0x1
popq %rdi
callq 0x11f0
movq 0x5398(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x1d46(%rip), %rsi      # 0xc9b0
jmp 0xac7d
movq 0x5385(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0x1d56(%rip), %rsi      # 0xc9d3
movq %r12, %rdx
xorl %eax, %eax
callq 0x1170
jmp 0xac51