// File: /workspace/github_c_makefile_star2_O1/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:
cmpq $0x0, (%rdi)
je 0x18d7e
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r15d
movq %rdx, %r14
movq 0x8(%rdx), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0x18d70
movl %r8d, %r13d
movq %rsi, %r12
movq %rdi, %rbx
movq 0x10(%rcx), %rcx
addq $0x20, %rcx
movq %rax, %rdx
movl (%rcx), %esi
addl %esi, %esi
sarl $0x1f, %esi
movslq %esi, %rsi
addq %rsi, %rdx
addq $0x28, %rcx
decq %rax
jne 0x18d55
testq %rdx, %rdx
jne 0x18d7f
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %r15d
adcl $0x0, %r15d
leaq 0xb55a(%rip), %rdi      # 0x242e8
xorl %esi, %esi
callq 0x2500
testl %eax, %eax
jne 0x18de4
movq %r14, -0x80(%rbp)
movq %rbx, -0x58(%rbp)
movq (%rbx), %rdi
movl $0x8, %esi
movq %rdi, -0x38(%rbp)
callq 0x2250
movq %rax, %r14
testl %r13d, %r13d
movq %r12, -0x60(%rbp)
je 0x18dc7
movq (%r12), %rbx
testq %rbx, %rbx
jne 0x18deb
movq -0x38(%rbp), %rax
testq %rax, %rax
je 0x18e26
xorl %ecx, %ecx
movq $0x1, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x18dd2
jmp 0x18e26
callq 0x355c
jmp 0x18d70
movq 0x10(%r12), %r13
cmpl $0x3, 0x8(%r13)
jb 0x18e02
movq (%r13), %rax
testb $0x1, (%r14,%rax,8)
je 0x18e0d
addq $0x20, %r13
decq %rbx
jne 0x18df0
jmp 0x18e22
leaq (%r14,%rax,8), %rdx
movq %r13, %rdi
leaq -0x88(%rbp), %rsi
callq 0x357b
jmp 0x18e02
movq -0x38(%rbp), %rax
movq %r14, -0x48(%rbp)
movslq %r15d, %r14
xorl %edx, %edx
divq %r14
movq %rax, %rbx
movl $0x50, %esi
movq %r14, %rdi
callq 0x2250
movq %rax, -0x50(%rbp)
movl %r15d, -0x3c(%rbp)
movl %r15d, %eax
movq %rax, -0x30(%rbp)
testl %r14d, %r14d
jle 0x18ef0
leaq 0x1(%rbx), %rcx
movq -0x58(%rbp), %rax
movq 0x10(%rax), %r12
leaq 0x8(,%rbx,8), %rax
movq %rax, -0x70(%rbp)
imulq $0x58, %rbx, %rax
addq $0x58, %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rax
leaq 0x28(%rax), %rbx
xorl %r13d, %r13d
movq -0x48(%rbp), %r15
xorl %r14d, %r14d
movq %rcx, -0x78(%rbp)
movl %r14d, -0x28(%rbx)
movq %r12, -0x20(%rbx)
movq %r15, -0x8(%rbx)
movl $0x1, %edi
movl $0x18, %esi
callq 0x2250
movq -0x78(%rbp), %rcx
movq %rax, -0x18(%rbx)
movq -0x80(%rbp), %rax
movq %rax, -0x10(%rbx)
leaq (%rcx,%r13), %rax
cmpq %rax, -0x38(%rbp)
jb 0x18ee6
incq %r14
addq -0x70(%rbp), %r15
addq -0x68(%rbp), %r12
movq %rcx, (%rbx)
addq $0x50, %rbx
movq %rax, %r13
cmpq %r14, -0x30(%rbp)
jne 0x18e94
jmp 0x18ef0
movq -0x38(%rbp), %rax
subq %r13, %rax
movq %rax, (%rbx)
movq %rsp, -0x38(%rbp)
movq %rsp, %r12
movq -0x30(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
cmpl $0x2, -0x3c(%rbp)
jl 0x18f4c
movq -0x50(%rbp), %rbx
addq $0x50, %rbx
leaq 0x8(%r12), %r15
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %r13
leaq 0x1b8(%rip), %r14       # 0x190e7
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x2370
addq $0x50, %rbx
addq $0x8, %r15
decq %r13
jne 0x18f2f
movq -0x50(%rbp), %r13
movq %r13, %rdi
callq 0x190e7
movl -0x3c(%rbp), %r15d
cmpl $0x2, %r15d
movq -0x30(%rbp), %r14
jl 0x18f7e
movl $0x1, %ebx
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x24b0
incq %rbx
cmpq %rbx, %r14
jne 0x18f6b
movq -0x60(%rbp), %r12
movq %r12, %rdi
callq 0x18cd4
movq $0x0, 0x8(%r12)
testl %r15d, %r15d
jle 0x18fc0
movq 0x8(%r12), %rax
movq %r14, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movq 0x10(%r13,%rdx), %rsi
addq (%rsi), %rax
addq $0x50, %rdx
cmpq %rdx, %rcx
jne 0x18faa
movq %rax, 0x8(%r12)
movq $0x0, (%r12)
movq 0x8(%r12), %rdi
shlq $0x5, %rdi
callq 0x2320
movq %rax, 0x10(%r12)
testl %r15d, %r15d
jle 0x19039
movq -0x30(%rbp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rbx
xorl %r14d, %r14d
movq (%r12), %rdi
shlq $0x5, %rdi
addq 0x10(%r12), %rdi
movq 0x10(%r13,%r14), %rax
movq (%rax), %rdx
movq 0x10(%rax), %rsi
shlq $0x5, %rdx
callq 0x22c0
movq 0x10(%r13,%r14), %rax
movq (%rax), %rcx
addq %rcx, (%r12)
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x10(%r13,%r14), %rdi
callq 0x2060
addq $0x50, %r14
cmpq %r14, %rbx
jne 0x18fef
movq -0x58(%rbp), %rcx
movq (%rcx), %rax
testq %rax, %rax
jne 0x19051
xorl %ecx, %ecx
movq -0x48(%rbp), %rbx
movq -0x30(%rbp), %rsi
jmp 0x19073
movq 0x10(%rcx), %rdx
addq $0x38, %rdx
xorl %ecx, %ecx
movq -0x48(%rbp), %rbx
movq -0x30(%rbp), %rsi
cmpl $0x1, (%rdx)
sbbq $-0x1, %rcx
addq $0x58, %rdx
decq %rax
jne 0x19063
testl %r15d, %r15d
jle 0x1909d
shlq $0x4, %rsi
leaq (%rsi,%rsi,4), %rax
xorl %edx, %edx
xorl %r9d, %r9d
xorl %r8d, %r8d
addq 0x30(%r13,%rdx), %r8
addq 0x38(%r13,%rdx), %r9
addq $0x50, %rdx
cmpq %rdx, %rax
jne 0x19088
jmp 0x190a3
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0xaf46(%rip), %rax      # 0x23ff0
movq (%rax), %rdi
leaq 0x5d6a(%rip), %rsi      # 0x1ee1e
leaq 0x5d50(%rip), %rdx      # 0x1ee0b
xorl %eax, %eax
callq 0x2290
leaq 0xb21f(%rip), %rdi      # 0x242e8
callq 0x2180
movq %rbx, %rdi
callq 0x2060
movq %r13, %rdi
callq 0x2060
movq -0x38(%rbp), %rsp
jmp 0x18d70