// File: /workspace/github_c_makefile_star2_O1/sc932[P]ALE/src/ALEhelpers.c
// Function: readAssembly
void readAssembly(kseq_t *ins, assemblyT *theAssembly){
    int contigLen, i, l, j = 0;

    // read contigs into a linked list.  Consolidate to an array later
    contig_ll *tmp, *head = malloc(sizeof(contig_ll));
    head->contig = NULL;
    head->next = NULL;
    tmp = head;
    theAssembly->totalAssemLen = 0;

    while ((l = kseq_read(ins)) >= 0) {
        contigLen = (int)(ins->seq.l);
        theAssembly->totalAssemLen += (long)(ins->seq.l);
        //printf("Found contig %d, contigLen = %i, name=%s\n", j, contigLen, ins->name.s);

        contig_t *contig = tmp->contig = (contig_t*) malloc(sizeof(contig_t));
        contig->seqLen = contigLen;
        contig->isCircular = 0; // assume nothing is circular until there reasonable evidence
        contig->seq = malloc(contigLen*sizeof(char));
        contig->seqNum = NULL; // only use if realigning sequences
        contig->ambiguousBaseCount = 0;
        contig->ambiguousBasePositions = NULL;
        contig->depth = malloc(contigLen*sizeof(float));
        contig->matchLogLikelihood = malloc(contigLen*sizeof(float));
        contig->insertLogLikelihood = malloc(contigLen*sizeof(float));
        contig->depthLogLikelihood = malloc(contigLen*sizeof(float));
        contig->kmerLogLikelihood = malloc(contigLen*sizeof(float));
        contig->name = strdup(ins->name.s);
        for(i = 0; i < contigLen; i++){
            contig->seq[i] = toupper(ins->seq.s[i]);
            contig->depth[i] = 0.0;
            contig->matchLogLikelihood[i] = 0.0;
            contig->insertLogLikelihood[i] = 0.0;
            contig->depthLogLikelihood[i] = 0.0;
            contig->kmerLogLikelihood[i] = 0.0;
        }
        j++;
        tmp->next = malloc(sizeof(contig_ll));
        tmp = tmp->next;
        tmp->contig = NULL;
        tmp->next = NULL;

    }
    int numberAssemblyPieces = j;
    //printf("Found %d contigs\n", numberAssemblyPieces);

    theAssembly->contigs = (contig_t**)malloc((numberAssemblyPieces)*sizeof(contig_t*));
    theAssembly->numContigs = numberAssemblyPieces;
    
    theAssembly->totalScore = 0.0;
    theAssembly->kmerAvgSum = 0.0;
    theAssembly->kmerAvgNorm = 0.0;
    theAssembly->placeAvgSum = 0.0;
    theAssembly->placeAvgNorm = 0.0;
    theAssembly->insertAvgSum = 0.0;
    theAssembly->insertAvgNorm = 0.0;
    theAssembly->depthScoreAvgSum = 0.0;
    theAssembly->depthScoreAvgNorm = 0.0;
    theAssembly->depthAvgSum = 0.0;
    theAssembly->depthAvgNorm = 0.0;
    theAssembly->overlapAvgSum = 0.0;
    theAssembly->overlapAvgNorm = 0.0;
    theAssembly->totalReads = 0;
    theAssembly->totalUnmappedReads = 0;
    theAssembly->totalMappedReads = 0;
    theAssembly->totalPlacedReads = 0;
    theAssembly->avgReadSize = 0.0;

    // consolidate linked list into array, free linked list
    tmp = head;
    i = 0;
    while (tmp != NULL) {
        if (tmp->contig != NULL)
            theAssembly->contigs[i++] = tmp->contig;
        head = tmp;
        tmp = head->next;
        free(head);
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %r15
movl $0x10, %edi
callq 0x3420
xorps %xmm0, %xmm0
movq %rax, 0x10(%rsp)
movups %xmm0, (%rax)
movq $0x0, 0x8(%r12)
movq %r15, %rdi
callq 0xb0c1
movl $0x0, %ebp
testl %eax, %eax
js 0xb57b
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
movq %r12, 0x18(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x30(%r15), %r14
addq %r14, 0x8(%r12)
movl $0x58, %edi
callq 0x3420
movq %rax, %r12
movq %r13, 0x20(%rsp)
movq %rax, (%r13)
movl %r14d, 0x8(%rax)
movb $0x0, 0xc(%rax)
movslq %r14d, %r13
movq %r13, %rdi
callq 0x3420
movq %rax, 0x10(%r12)
movq %rbx, 0x18(%r12)
movl $0x0, 0x20(%r12)
movq %rbx, 0x28(%r12)
leaq (,%r13,4), %rbp
movq %rbp, %rdi
callq 0x3420
movq %rax, 0x30(%r12)
movq %rbp, %rdi
callq 0x3420
movq %rax, 0x38(%r12)
movq %rbp, %rdi
callq 0x3420
movq %rax, 0x40(%r12)
movq %rbp, %rdi
callq 0x3420
movq %rax, 0x48(%r12)
movq %rbp, %rdi
callq 0x3420
movq %rax, 0x50(%r12)
movq 0x10(%r15), %rdi
callq 0x3630
movq %rax, (%r12)
testl %r13d, %r13d
jle 0xb53f
callq 0x3030
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movq (%rax), %rdx
movq 0x40(%r15), %rsi
movsbq (%rsi,%rcx), %rsi
movb (%rdx,%rsi,4), %dl
movq 0x10(%r12), %rsi
movb %dl, (%rsi,%rcx)
movq 0x30(%r12), %rdx
movl %ebx, (%rdx,%rcx,4)
movq 0x38(%r12), %rdx
movl %ebx, (%rdx,%rcx,4)
movq 0x40(%r12), %rdx
movl %ebx, (%rdx,%rcx,4)
movq 0x48(%r12), %rdx
movl %ebx, (%rdx,%rcx,4)
movq 0x50(%r12), %rdx
movl %ebx, (%rdx,%rcx,4)
incq %rcx
cmpq %rcx, %r14
jne 0xb4f8
movl 0xc(%rsp), %ebp
incl %ebp
movl $0x10, %edi
callq 0x3420
movq %rax, %r12
movq 0x20(%rsp), %rax
movq %r12, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movq %r15, %rdi
callq 0xb0c1
movq %r12, %r13
testl %eax, %eax
movq 0x18(%rsp), %r12
jns 0xb441
movl %ebp, %edi
shlq $0x3, %rdi
callq 0x3420
movq %rax, 0xa0(%r12)
movl %ebp, (%r12)
leaq 0x10(%r12), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x3260
movq 0x10(%rsp), %rdi
xorl %ebp, %ebp
movq (%rdi), %rax
testq %rax, %rax
je 0xb5c3
movq 0xa0(%r12), %rcx
movslq %ebp, %rdx
incl %ebp
movq %rax, (%rcx,%rdx,8)
movq 0x8(%rdi), %rbx
callq 0x3070
movq %rbx, %rdi
testq %rbx, %rbx
jne 0xb5aa
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq