// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x3310
movq %rax, %r14
movq %rbx, 0x8(%rsp)
andq $0x0, 0x8(%rbx)
movq %rax, %rbx
xorl %ebp, %ebp
movq %r15, %rdi
callq 0x921e
testl %eax, %eax
js 0x955b
movl %ebp, 0x14(%rsp)
movq 0x30(%r15), %r12
movq 0x8(%rsp), %rax
addq %r12, 0x8(%rax)
pushq $0x58
popq %rdi
callq 0x3420
movq %rax, %rbp
movq %rax, (%rbx)
movl %r12d, 0x8(%rax)
movb $0x0, 0xc(%rax)
movslq %r12d, %r12
movq %r12, %rdi
callq 0x3420
movq %rax, 0x10(%rbp)
andq $0x0, 0x18(%rbp)
andl $0x0, 0x20(%rbp)
andq $0x0, 0x28(%rbp)
leaq (,%r12,4), %r13
movq %r13, %rdi
callq 0x3420
movq %rax, 0x30(%rbp)
movq %r13, %rdi
callq 0x3420
movq %rax, 0x38(%rbp)
movq %r13, %rdi
callq 0x3420
movq %rax, 0x40(%rbp)
movq %r13, %rdi
callq 0x3420
movq %rax, 0x48(%rbp)
movq %r13, %rdi
callq 0x3420
movq %rax, 0x50(%rbp)
movq 0x10(%r15), %rdi
callq 0x3630
movq %rax, (%rbp)
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x953b
callq 0x3030
movq (%rax), %rax
movq 0x40(%r15), %rcx
movsbq (%rcx,%r13), %rcx
movb (%rax,%rcx,4), %al
movq 0x10(%rbp), %rcx
movb %al, (%rcx,%r13)
movq 0x30(%rbp), %rax
andl $0x0, (%rax,%r13,4)
movq 0x38(%rbp), %rax
andl $0x0, (%rax,%r13,4)
movq 0x40(%rbp), %rax
andl $0x0, (%rax,%r13,4)
movq 0x48(%rbp), %rax
andl $0x0, (%rax,%r13,4)
movq 0x50(%rbp), %rax
andl $0x0, (%rax,%r13,4)
incq %r13
jmp 0x94e8
movl 0x14(%rsp), %ebp
incl %ebp
pushq $0x10
popq %rdi
callq 0x3420
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, %rbx
jmp 0x9434
movl %ebp, %edi
shlq $0x3, %rdi
callq 0x3420
movq 0x8(%rsp), %rcx
movq %rax, 0xa0(%rcx)
movl %ebp, (%rcx)
leaq 0x10(%rcx), %rdi
xorl %ebx, %ebx
movl $0x90, %edx
xorl %esi, %esi
callq 0x3260
testq %r14, %r14
je 0x95b9
movq (%r14), %rax
testq %rax, %rax
je 0x95a8
movq 0x8(%rsp), %rcx
movq 0xa0(%rcx), %rcx
movslq %ebx, %rdx
incl %ebx
movq %rax, (%rcx,%rdx,8)
movq 0x8(%r14), %r15
movq %r14, %rdi
callq 0x3070
movq %r15, %r14
jmp 0x9586
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq