// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdi, %r12
callq 0xc85f
testl %eax, %eax
jne 0xcac1
leaq 0x2966(%rip), %rsi      # 0xf2a7
movq %r12, %rdi
callq 0x11c0
testq %rax, %rax
je 0xcac9
movq %rax, %r15
movslq (%r14), %rbx
testq %rbx, %rbx
jle 0xc9e9
movq 0x50(%r14), %rax
leal 0x1(%rbx), %ecx
leaq -0x1(%rbx), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %ecx
shlq $0x4, %rcx
pmovsxbq 0x1faf(%rip), %xmm1     # 0xe934
xorl %edx, %edx
movdqa 0x1721(%rip), %xmm2     # 0xe0b0
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
pmovsxbq 0x1f8c(%rip), %xmm4     # 0xe936
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm0, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0xc9c7
movq %rsi, (%rax,%rdx)
pxor %xmm3, %xmm5
pextrb $0x8, %xmm5, %edi
testb $0x1, %dil
je 0xc9dc
movq %rsi, 0x8(%rax,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xc9aa
movq %r12, 0x8(%rsp)
addl 0x28(%r14), %ebx
jle 0xca9b
xorl %ebp, %ebp
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
movq %r15, %rdi
leaq 0x2090(%rip), %rsi      # 0xeaa2
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1080
cmpl $0x1, %eax
jne 0xcab5
cmpl 0x28(%r14), %r12d
jge 0xca5c
movq 0x30(%r14), %rax
movslq %r12d, %rcx
cmpl (%rax,%rcx,4), %ebp
jne 0xca5c
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 0xca76
incl %r12d
jmp 0xca6e
fldt 0x10(%rsp)
movq 0x50(%r14), %rax
movslq %r13d, %r13
fstpl (%rax,%r13,8)
incl %r13d
incl %ebp
cmpl %ebp, %ebx
jne 0xca08
jmp 0xca9b
movq 0x557b(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x204d(%rip), %rsi      # 0xead4
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1170
incl 0x4(%rsp)
jmp 0xca57
movq %r15, %rdi
movq %r14, %rsi
callq 0x2ce2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x2cbc
movq %r12, %rdi
callq 0x2c98
movq %r12, %rdi
callq 0x2d34