// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x10, %edi
callq 0x3420
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x8(%rbp), %rdi
callq 0xcf90
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jl 0xd5d6
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movl $0x58, %edi
callq 0x3420
movq -0x28(%rbp), %rcx
movq %rax, (%rcx)
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x38(%rbp), %rax
movb $0x0, 0xc(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x0, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x48(%rax)
movslq -0x14(%rbp), %rdi
shlq $0x2, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x3620
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xd590
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x3100
movb %al, %dl
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x18(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x18(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xd4ec
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl $0x10, %edi
callq 0x3420
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0xd3c0
movl -0x20(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rdi
shlq $0x3, %rdi
callq 0x3420
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa0(%rax)
movl -0x3c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x88(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x18(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xd747
movq -0x28(%rbp), %rax
cmpq $0x0, (%rax)
je 0xd728
movq -0x28(%rbp), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x18(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3050
jmp 0xd6f3
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)