// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x17e00
movl %eax, 0x54(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x17f7f
movq 0x4099(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
movq 0x60(%rsp), %rdx
leaq 0x1969(%rip), %rsi      # 0x198d7
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x11c0
movq 0x60(%rsp), %rdi
leaq 0x219e(%rip), %rsi      # 0x1a129
callq 0x1190
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x17fc4
movq 0x4054(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
movq 0x60(%rsp), %rdx
leaq 0x1947(%rip), %rsi      # 0x198fa
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x11c0
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x18001
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x44(%rsp), %rcx
movsd 0x1061(%rip), %xmm0     # 0x19050
movsd %xmm0, (%rax,%rcx,8)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x17fcc
movl $0x0, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x40, 0x34(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %eax
movq 0x58(%rsp), %rcx
addl 0x28(%rcx), %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x30(%rsp), %eax
jge 0x18146
movq 0x48(%rsp), %rdi
leaq 0x18cb(%rip), %rsi      # 0x19921
leaq 0x10(%rsp), %rdx
movb $0x0, %al
callq 0x1060
cmpl $0x1, %eax
je 0x18092
movq 0x3f8a(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
movq 0x60(%rsp), %rdx
movl 0x2c(%rsp), %ecx
leaq 0x18a4(%rip), %rsi      # 0x19925
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x11c0
movl 0x40(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x1810a
movl 0x2c(%rsp), %eax
movq 0x58(%rsp), %rcx
movq 0x30(%rcx), %rcx
movslq 0x40(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0x1810a
fldt 0x10(%rsp)
fldz
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x180fd
jmp 0x180c7
movl 0x38(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x180fd
movq 0x3f20(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
movq 0x60(%rsp), %rdx
movl 0x2c(%rsp), %ecx
leaq 0x1868(%rip), %rsi      # 0x19953
movb $0x0, %al
callq 0x1140
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x18136
fldt 0x10(%rsp)
fstpl (%rsp)
movsd (%rsp), %xmm0
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x3c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x18136
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1803c
movq 0x48(%rsp), %rdi
callq 0x10a0
movq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rax)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x181a1
movq 0x58(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
addsd 0x48(%rax), %xmm0
movsd %xmm0, 0x48(%rax)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x18165
movq 0x3e50(%rip), %rax      # 0x1bff8
movq (%rax), %rdi
movq 0x58(%rsp), %rax
movsd 0x48(%rax), %xmm0
leaq 0x17d2(%rip), %rsi      # 0x1998e
movb $0x1, %al
callq 0x1140
addq $0x68, %rsp
retq
nopl (%rax,%rax)