// File: /workspace/github_c_makefile_star2_O3/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 0xc001
testl %eax, %eax
jne 0xc263
leaq 0x21a1(%rip), %rsi      # 0xe284
movq %r12, %rdi
callq 0x11d0
testq %rax, %rax
je 0xc26b
movq %rax, %r15
movslq (%r14), %rbx
testq %rbx, %rbx
jle 0xc18b
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 0x1801(%rip), %xmm1     # 0xd928
xorl %edx, %edx
movdqa 0xf7f(%rip), %xmm2      # 0xd0b0
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
pmovsxbq 0x17de(%rip), %xmm4     # 0xd92a
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm0, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0xc169
movq %rsi, (%rax,%rdx)
pxor %xmm3, %xmm5
pextrb $0x8, %xmm5, %edi
testb $0x1, %dil
je 0xc17e
movq %rsi, 0x8(%rax,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xc14c
movq %r12, 0x8(%rsp)
addl 0x28(%r14), %ebx
jle 0xc23d
xorl %ebp, %ebp
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
xorl %r13d, %r13d
movq %r15, %rdi
leaq 0x18da(%rip), %rsi      # 0xda8e
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x1080
cmpl $0x1, %eax
jne 0xc257
cmpl 0x28(%r14), %r12d
jge 0xc1fe
movq 0x30(%r14), %rax
movslq %r12d, %rcx
cmpl (%rax,%rcx,4), %ebp
jne 0xc1fe
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 0xc218
incl %r12d
jmp 0xc210
fldt 0x10(%rsp)
movq 0x50(%r14), %rax
movslq %r13d, %r13
fstpl (%rax,%r13,8)
incl %r13d
incl %ebp
cmpl %ebp, %ebx
jne 0xc1aa
jmp 0xc23d
movq 0x4dd9(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x1897(%rip), %rsi      # 0xdac0
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1180
incl 0x4(%rsp)
jmp 0xc1f9
movq %r15, %rdi
movq %r14, %rsi
callq 0x22bd
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
movl %ebp, %esi
callq 0x2297
movq %r12, %rdi
callq 0x2273
movq %r12, %rdi
callq 0x2309