// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/alignment.c
// Function: scg_read_alignment
void scg_read_alignment(sr_db_t *sr_db, scg_ra_v *ra_v, scg_t *g, int n_threads, int for_unzip)
{
    if (sr_db->n == 0 || !asmg_vtx_n1(g->utg_asmg)) return;

    int i;
    uint64_t j, b;

    if (n_threads == 0)
        n_threads = 1;

    if (pthread_mutex_init(&lock, NULL) != 0) {
        fprintf(stderr, "[E::%s] mutex init failed\n", __func__);
        return;
    }

    int64_t *old_ra;
    scg_ra_t *ra;
    MYCALLOC(old_ra, sr_db->n);
    if (for_unzip && ra_v->n > 0) {
        // for unzipping purpose
        // only map reads previously mapped to at least three unitigs
        // only map previouly mapped reads
        // only keep mapping results without mapping score decrease
        double fractpart, intpart;
        uint64_t sid;
        for (j = 0; j < ra_v->n; ++j) {
            ra = &ra_v->a[j];
            sid = ra->sid;
            if (ra->n > 2 && (old_ra[sid] & 1) == 0) {
                fractpart = modf(ra->s, &intpart);
                if (fractpart < DBL_EPSILON)
                    intpart -= 1;
                old_ra[sid] = (uint64_t) intpart << 1 | 1;
            }
        }
    } else {
        // map every reads
        for (j = 0; j < sr_db->n; ++j) old_ra[j] = 1;
    }

    b = sr_db->n / n_threads + 1;
    p_data_t *dat;
    MYCALLOC(dat, n_threads);
    for (i = 0; i < n_threads; ++i) {
        dat[i].tid = i;
        dat[i].sr = sr_db->a + i * b;
        dat[i].old_ra = old_ra + i * b;
        MYCALLOC(dat[i].ra_v, 1);
        dat[i].g = g;
        if (sr_db->n >= (i + 1) * b) {
            dat[i].n = b;
        } else {
            dat[i].n = sr_db->n - i * b;
            break;
        }
    }

    pthread_t threads[n_threads];
    for (i = 1; i < n_threads; ++i)
        pthread_create(threads + i, NULL, scg_ra_analysis_thread, dat + i);

    scg_ra_analysis_thread(dat);

    for (i = 1; i < n_threads; ++i)
        pthread_join(threads[i], NULL);

    // clean old results in ra_v
    scg_ra_v_clean(ra_v);
    // collect results
    ra_v->m = 0;
    for (i = 0; i < n_threads; ++i)
        ra_v->m += dat[i].ra_v->n;
    ra_v->n = 0;
    MYMALLOC(ra_v->a, ra_v->m);
    for (i = 0; i < n_threads; ++i) {
        // copy data
        memcpy(ra_v->a + ra_v->n, dat[i].ra_v->a, sizeof(scg_ra_t) * dat[i].ra_v->n);
        ra_v->n += dat[i].ra_v->n;
        free(dat[i].ra_v->a);
        free(dat[i].ra_v);
    }

    uint64_t n_r, n_m, n_u, n;
    n_r = n_m = n_u = 0;
    for (n = 0; n < sr_db->n; ++n)
        n_r += sr_db->a[n].n > 0;
    for (i = 0; i < n_threads; ++i) {
        n_m += dat[i].m_stats[0];
        n_u += dat[i].m_stats[1];
    }
    fprintf(stderr ,"[M::%s] %lu mappable reads, %lu mapped (%lu unique mapping)\n", __func__, n_r, n_m, n_u);

    pthread_mutex_destroy(&lock);
    free(old_ra);
    free(dat);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
cmpq $0x0, (%rdi)
je 0x15284
movl %r8d, %ebx
movl %ecx, %r12d
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
movq 0x8(%rdx), %rax
movq (%rax), %rcx
pushq $0x20
popq %rdx
movq %rcx, %rsi
subq $0x1, %rcx
jb 0x1527f
movq 0x10(%rax), %rdi
movl (%rdi,%rdx), %edi
addl %edi, %edi
sarl $0x1f, %edi
movslq %edi, %rdi
addq %rdi, %rsi
addq $0x28, %rdx
jmp 0x15261
testq %rsi, %rsi
jne 0x15293
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %r12d
adcl $0x0, %r12d
leaq 0xc03e(%rip), %rdi      # 0x212e0
xorl %esi, %esi
callq 0x24f0
testl %eax, %eax
jne 0x152fe
movq %r13, -0x90(%rbp)
movl %r12d, -0x2c(%rbp)
movq %r15, -0x40(%rbp)
movq %r14, -0x50(%rbp)
movq (%r14), %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2250
movq %rax, %r13
testl %ebx, %ebx
movq %r15, -0x38(%rbp)
je 0x152e5
movq -0x40(%rbp), %rax
movq (%rax), %r14
testq %r14, %r14
jne 0x1532b
xorl %eax, %eax
cmpq %rax, %r15
je 0x153b3
movq $0x1, (%r13,%rax,8)
incq %rax
jmp 0x152e7
movq 0xbceb(%rip), %rax      # 0x20ff0
movq (%rax), %rdi
leaq 0x5b49(%rip), %rsi      # 0x1ae58
leaq 0x5b5d(%rip), %rdx      # 0x1ae73
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2280
movq -0x40(%rbp), %rax
movq 0x10(%rax), %r15
leaq -0x68(%rbp), %rbx
subq $0x1, %r14
jb 0x153af
cmpl $0x3, 0x8(%r15)
jb 0x153a9
movq (%r15), %r12
testb $0x1, (%r13,%r12,8)
jne 0x153a9
movsd 0x18(%r15), %xmm0
movq %rbx, %rdi
callq 0x21a0
movsd 0x4463(%rip), %xmm1     # 0x197c8
ucomisd %xmm0, %xmm1
movsd -0x68(%rbp), %xmm0
jbe 0x1537d
addsd 0x5ac8(%rip), %xmm0     # 0x1ae40
movsd %xmm0, -0x68(%rbp)
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x5ab7(%rip), %xmm0     # 0x1ae48
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
leaq 0x1(,%rdx,2), %rax
movq %rax, (%r13,%r12,8)
addq $0x20, %r15
jmp 0x15337
movq -0x38(%rbp), %r15
movslq -0x2c(%rbp), %r14
movq %r15, %rax
xorl %edx, %edx
divq %r14
movq %rax, %rbx
incq %rax
movq %rax, -0x88(%rbp)
pushq $0x50
popq %rsi
movq %r14, %rdi
callq 0x2250
xorl %ecx, %ecx
movq %rax, %r12
movq %r14, -0x60(%rbp)
testl %r14d, %r14d
movl $0x0, %eax
cmovgl -0x2c(%rbp), %eax
movq %rax, -0x58(%rbp)
leaq 0x8(,%rbx,8), %rax
movq %rax, -0x80(%rbp)
imulq $0x58, %rbx, %rax
addq $0x58, %rax
movq %rax, -0x78(%rbp)
movq %r13, %rbx
leaq 0x28(%r12), %r13
movq %rbx, -0x70(%rbp)
xorl %r15d, %r15d
xorl %esi, %esi
movq %r12, -0x48(%rbp)
cmpq %rsi, -0x58(%rbp)
je 0x1549c
movq %rsi, %r14
movl %esi, -0x28(%r13)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
addq %rcx, %rax
movq %rax, -0x20(%r13)
movq %rbx, -0x8(%r13)
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
movq %rcx, %r12
callq 0x2250
movq %rax, -0x18(%r13)
movq -0x90(%rbp), %rax
movq %rax, -0x10(%r13)
movq -0x88(%rbp), %rcx
leaq (%rcx,%r15), %rax
cmpq %rax, -0x38(%rbp)
jb 0x1548d
movq %rcx, (%r13)
addq -0x80(%rbp), %rbx
addq -0x78(%rbp), %r12
addq $0x50, %r13
movq %r14, %rsi
incq %rsi
movq %rax, %r15
movq %r12, %rcx
movq -0x48(%rbp), %r12
jmp 0x1541d
movq -0x38(%rbp), %rax
subq %r15, %rax
movq %rax, (%r13)
movq -0x48(%rbp), %r12
movq %rsp, -0x38(%rbp)
movl -0x2c(%rbp), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rcx, %r13
subq %rax, %r13
movq %r13, %rsp
movq %r12, %r14
addq $0x50, %r14
negq %rax
leaq (%rcx,%rax), %r15
addq $0x8, %r15
pushq $0x1
popq %rbx
cmpq -0x60(%rbp), %rbx
jge 0x154f7
movq %r15, %rdi
xorl %esi, %esi
leaq 0x151(%rip), %rdx       # 0x15633
movq %r14, %rcx
callq 0x2360
incq %rbx
addq $0x50, %r14
addq $0x8, %r15
jmp 0x154d0
movq %r12, %rdi
callq 0x15633
pushq $0x1
popq %rbx
cmpq -0x60(%rbp), %rbx
jge 0x15519
movq (%r13,%rbx,8), %rdi
xorl %esi, %esi
callq 0x24a0
incq %rbx
jmp 0x15502
movq -0x40(%rbp), %r13
movq %r13, %rdi
callq 0x151f4
andq $0x0, 0x8(%r13)
imulq $0x50, -0x58(%rbp), %rbx
xorl %eax, %eax
xorl %edi, %edi
movq -0x70(%rbp), %r15
cmpq %rax, %rbx
je 0x1554e
movq 0x10(%r12,%rax), %rcx
addq (%rcx), %rdi
movq %rdi, 0x8(%r13)
addq $0x50, %rax
jmp 0x15537
andq $0x0, (%r13)
shlq $0x5, %rdi
callq 0x2310
movq %rax, 0x10(%r13)
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x155ae
movq (%r13), %rdi
shlq $0x5, %rdi
addq 0x10(%r13), %rdi
movq 0x10(%r12,%r14), %rax
movq (%rax), %rdx
movq 0x10(%rax), %rsi
shlq $0x5, %rdx
callq 0x22b0
movq 0x10(%r12,%r14), %rax
movq (%rax), %rcx
addq %rcx, (%r13)
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x10(%r12,%r14), %rdi
callq 0x2060
addq $0x50, %r14
jmp 0x15563
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
pushq $0x38
popq %rdx
xorl %ecx, %ecx
subq $0x1, %rax
jb 0x155d2
movq 0x10(%rdi), %rsi
cmpl $0x1, (%rsi,%rdx)
sbbq $-0x1, %rcx
addq $0x58, %rdx
jmp 0x155ba
xorl %eax, %eax
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %rax, %rbx
je 0x155ef
addq 0x30(%r12,%rax), %r8
addq 0x38(%r12,%rax), %r9
addq $0x50, %rax
jmp 0x155da
movq 0xb9fa(%rip), %rax      # 0x20ff0
movq (%rax), %rdi
leaq 0x5886(%rip), %rsi      # 0x1ae86
leaq 0x586c(%rip), %rdx      # 0x1ae73
xorl %eax, %eax
callq 0x2280
leaq 0xbccb(%rip), %rdi      # 0x212e0
callq 0x2180
movq %r15, %rdi
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq -0x38(%rbp), %rsp
jmp 0x15284