// File: /workspace/github_c_makefile_star2_O3/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
movq %rsi, -0x68(%rbp)
cmpq $0x0, (%rdi)
je 0x1ab14
movl %ecx, %ebx
movq %rdx, %r15
movq 0x8(%rdx), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0x1ab14
movl %r8d, %r12d
movq %rdi, %r14
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 0x1aaf9
testq %rdx, %rdx
jne 0x1ab23
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %ebx
movl %ebx, %r13d
adcl $0x0, %r13d
leaq 0xb7ac(%rip), %rdi      # 0x262e0
xorl %esi, %esi
callq 0x24f0
testl %eax, %eax
jne 0x1ac1d
movl %r13d, -0x2c(%rbp)
movl %ebx, -0x54(%rbp)
movq (%r14), %rdi
movl $0x8, %esi
movq %rdi, -0x38(%rbp)
callq 0x2250
movq %rax, %r13
testl %r12d, %r12d
movq -0x68(%rbp), %r12
je 0x1ab74
movq (%r12), %rbx
testq %rbx, %rbx
jne 0x1ac27
cmpq $0x0, -0x38(%rbp)
je 0x1acb9
movq -0x38(%rbp), %rcx
leaq 0x1(%rcx), %rax
andq $-0x2, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x4c30(%rip), %xmm1     # 0x1f7d0
xorl %ecx, %ecx
movdqa 0x4c36(%rip), %xmm2     # 0x1f7e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x4c36(%rip), %xmm4     # 0x1f7f0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x1abf4
movq $0x1, (%r13,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x1ac0b
movq $0x1, 0x8(%r13,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x1abba
jmp 0x1acb9
callq 0x3530
jmp 0x1ab14
movq %r15, -0x50(%rbp)
movq %r14, -0x48(%rbp)
movq 0x10(%r12), %r15
leaq -0x78(%rbp), %r12
cmpl $0x3, 0x8(%r15)
jb 0x1aca4
movq (%r15), %r14
testb $0x1, (%r13,%r14,8)
jne 0x1aca4
movsd 0x18(%r15), %xmm0
movq %r12, %rdi
callq 0x21a0
movsd 0x4be0(%rip), %xmm1     # 0x1f840
ucomisd %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
jbe 0x1ac78
addsd 0x620d(%rip), %xmm0     # 0x20e80
movsd %xmm0, -0x78(%rbp)
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x61fc(%rip), %xmm0     # 0x20e88
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
leaq 0x1(,%rdx,2), %rax
movq %rax, (%r13,%r14,8)
addq $0x20, %r15
decq %rbx
jne 0x1ac38
movq -0x48(%rbp), %r14
movq -0x68(%rbp), %r12
movq -0x50(%rbp), %r15
movslq -0x2c(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq -0x38(%rbp), %rax
xorl %edx, %edx
divq %rdi
movq %rax, -0x70(%rbp)
movl $0x50, %esi
callq 0x2250
movq %rax, %rbx
cmpl $0x0, -0x40(%rbp)
jle 0x1ad93
movq %r15, -0x50(%rbp)
movq -0x70(%rbp), %rdx
leaq 0x1(%rdx), %rcx
movq %r14, -0x48(%rbp)
movq 0x10(%r14), %r14
movl -0x2c(%rbp), %eax
movq %rax, -0x40(%rbp)
leaq 0x8(,%rdx,8), %rax
movq %rax, -0x88(%rbp)
imulq $0x58, %rdx, %rax
addq $0x58, %rax
movq %rax, -0x70(%rbp)
movq %rbx, %rax
movq %r13, %rbx
movq %rax, -0x60(%rbp)
leaq 0x28(%rax), %r13
xorl %r15d, %r15d
movq %rbx, -0x80(%rbp)
xorl %r12d, %r12d
movq %rcx, -0x90(%rbp)
movl %r12d, -0x28(%r13)
movq %r14, -0x20(%r13)
movq %rbx, -0x8(%r13)
movl $0x1, %edi
movl $0x18, %esi
callq 0x2250
movq -0x90(%rbp), %rcx
movq %rax, -0x18(%r13)
movq -0x50(%rbp), %rax
movq %rax, -0x10(%r13)
leaq (%r15,%rcx), %rax
cmpq %rax, -0x38(%rbp)
jb 0x1adbb
incq %r12
addq -0x88(%rbp), %rbx
addq -0x70(%rbp), %r14
movq %rcx, (%r13)
addq $0x50, %r13
movq %rax, %r15
cmpq %r12, -0x40(%rbp)
jne 0x1ad3a
jmp 0x1adc6
movq %rsp, %r15
movl -0x2c(%rbp), %eax
movq %rax, -0x40(%rbp)
movq %rbx, %rdi
callq 0x1affb
movq %r12, %rdi
callq 0x1aa78
movq $0x0, 0x8(%r12)
jmp 0x1af40
movq -0x38(%rbp), %rax
subq %r15, %rax
movq %rax, (%r13)
movq %rsp, %r15
movq %rsp, %rdx
movq -0x40(%rbp), %rcx
leaq 0xf(,%rcx,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
cmpl $0x2, -0x54(%rbp)
jae 0x1adf3
movq -0x60(%rbp), %rdi
callq 0x1affb
jmp 0x1ae6b
movq %r15, -0x38(%rbp)
movq %rdx, -0x50(%rbp)
leaq 0x8(%rdx), %r14
movq %rcx, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rbx
movl $0x50, %r12d
leaq 0x1e4(%rip), %r13       # 0x1affb
movq -0x60(%rbp), %r15
leaq (%r15,%r12), %rcx
movq %r14, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2360
addq $0x50, %r12
addq $0x8, %r14
cmpq %r12, %rbx
jne 0x1ae1b
movq %r15, %rdi
callq 0x1affb
cmpl $0x2, -0x54(%rbp)
movq -0x38(%rbp), %r15
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r12
jb 0x1ae6b
movl $0x1, %ebx
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x24a0
incq %rbx
cmpq %rbx, %r14
jne 0x1ae58
movq -0x68(%rbp), %r12
movq %r12, %rdi
callq 0x1aa78
movq $0x0, 0x8(%r12)
cmpl $0x0, -0x2c(%rbp)
movq -0x80(%rbp), %r13
jle 0x1af38
movq -0x40(%rbp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %r14
xorl %eax, %eax
xorl %edi, %edi
movq -0x60(%rbp), %rbx
movq 0x10(%rbx,%rax), %rcx
addq (%rcx), %rdi
addq $0x50, %rax
cmpq %rax, %r14
jne 0x1aea2
movq %rdi, 0x8(%r12)
shlq $0x5, %rdi
movq $0x0, (%r12)
callq 0x2310
movq %rax, 0x10(%r12)
cmpl $0x0, -0x2c(%rbp)
jle 0x1aff2
movq %r15, -0x38(%rbp)
movq %rbx, %r15
xorl %ebx, %ebx
movq (%r12), %rdi
shlq $0x5, %rdi
addq 0x10(%r12), %rdi
movq 0x10(%r15,%rbx), %rax
movq (%rax), %rdx
movq 0x10(%rax), %rsi
shlq $0x5, %rdx
callq 0x22b0
movq 0x10(%r15,%rbx), %rax
movq (%rax), %rcx
addq %rcx, (%r12)
movq 0x10(%rax), %rdi
callq 0x2060
movq 0x10(%r15,%rbx), %rdi
callq 0x2060
addq $0x50, %rbx
cmpq %rbx, %r14
jne 0x1aee1
movq -0x48(%rbp), %r14
movq %r15, %rbx
movq -0x38(%rbp), %r15
jmp 0x1af54
movq -0x48(%rbp), %r14
movq -0x60(%rbp), %rbx
movq $0x0, (%r12)
xorl %edi, %edi
callq 0x2310
movq %rax, 0x10(%r12)
movq (%r14), %rax
testq %rax, %rax
jne 0x1af60
xorl %ecx, %ecx
jmp 0x1af7a
movq 0x10(%r14), %rdx
addq $0x38, %rdx
xorl %ecx, %ecx
cmpl $0x1, (%rdx)
sbbq $-0x1, %rcx
addq $0x58, %rdx
decq %rax
jne 0x1af6a
cmpl $0x0, -0x2c(%rbp)
jle 0x1afa9
movq -0x40(%rbp), %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rax
xorl %edx, %edx
xorl %r9d, %r9d
xorl %r8d, %r8d
addq 0x30(%rbx,%rdx), %r8
addq 0x38(%rbx,%rdx), %r9
addq $0x50, %rdx
cmpq %rdx, %rax
jne 0x1af94
jmp 0x1afaf
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0xb03a(%rip), %rax      # 0x25ff0
movq (%rax), %rdi
leaq 0x5f1e(%rip), %rsi      # 0x20ede
leaq 0x5f04(%rip), %rdx      # 0x20ecb
xorl %eax, %eax
callq 0x2280
leaq 0xb30b(%rip), %rdi      # 0x262e0
callq 0x2180
movq %r13, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2060
movq %r15, %rsp
jmp 0x1ab14
movq -0x48(%rbp), %r14
jmp 0x1af54