// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: gen_scaffold_id(void*, long, int)
static void gen_scaffold_id(void *data, long i, int tid) // callback for kt_for()
{
    scaf_mul_t *ss = (scaf_mul_t *)data;
    ma_ug_t *ug = ss->ug;
    usc_t *z = &(ss->a[i]);
    uint32_t v = z->raw_sid, w = z->raw_eid, sv, sw;
    uint64_t *a, a_n, k; int64_t ql, mmql, max_ql; 
    uc_block_t *p, *n, *m0, *m1;
    sv = (v&1?((ug->u.a[v>>1].a[0]>>32)^1):(ug->u.a[v>>1].a[ug->u.a[v>>1].n-1]>>32));
    sw = (w&1?((ug->u.a[w>>1].a[ug->u.a[w>>1].n-1]>>32)^1):(ug->u.a[w>>1].a[0]>>32));
    // if((((v>>1) == 15016) && ((w>>1) == 2778)) || (((v>>1) == 2778) && ((w>>1) == 15016))) {
    //     fprintf(stderr, "[M::%s::]\t%.*s(%c)(sv>>1::%u)(utg%.6ul(%c))\t%.*s(%c)(sw>>1::%u)(utg%.6ul(%c))\n", __func__, 
    //         (int)Get_NAME_LENGTH(R_INF, (sv>>1)), Get_NAME(R_INF, (sv>>1)), "+-"[sv&1], sv>>1, (v>>1)+1, "+-"[v&1],
    //         (int)Get_NAME_LENGTH(R_INF, (sw>>1)), Get_NAME(R_INF, (sw>>1)), "+-"[sw&1], sw>>1, (w>>1)+1, "+-"[w&1]);
    // }
    sv = Get_READ_LENGTH(R_INF, (sv>>1)); sw = Get_READ_LENGTH(R_INF, (sw>>1));
    max_ql = MIN(sv, sw); max_ql = -max_ql;
    // if((((v>>1) == 235) && ((w>>1) == 153)) || (((v>>1) == 153) && ((w>>1) == 235))) {
    //     fprintf(stderr, "[M::%s] sv::%u, sw::%u, max_ql::%ld, v>>1::%u(%c), w>>1::%u(%c)\n", 
    //     __func__, sv, sw, max_ql, v>>1, "+-"[v&1], w>>1, "+-"[w&1]);
    // }
    // if((((v>>1) == 235) && ((w>>1) == 153)) || (((v>>1) == 153) && ((w>>1) == 235))) {
    //     print_ul_alignment(ug, &UL_INF, 5625, "+++");
    // }
    uint32_t uv, uw, rev = 0, id = (uint32_t)-1;
    p = n = m0 = m1 = NULL;
    a = UL_INF.ridx.occ.a + UL_INF.ridx.idx.a[v>>1];
	a_n = UL_INF.ridx.idx.a[(v>>1)+1] - UL_INF.ridx.idx.a[v>>1]; 
	for (k = 0, mmql = INT32_MAX; k < a_n; k++) {
        p = &(UL_INF.a[a[k]>>32].bb.a[(uint32_t)(a[k])]); assert(p->hid == (v>>1));
        if(UL_INF.a[a[k]>>32].dd != 3) continue;///dd = 3 is saved
		if(p->base || (!p->el) || (!p->pchain)) continue;
        uv = (((uint32_t)(p->hid))<<1)|((uint32_t)(p->rev));

        // if((uv == v) && (p->aidx != (uint32_t)-1)) {
        if((uv == v) && (p->aidx == (uint32_t)-1) && (((uint32_t)(a[k]))+1 < UL_INF.a[a[k]>>32].bb.n)) {
			n = &(UL_INF.a[a[k]>>32].bb.a[((uint32_t)(a[k]))+1]);
            if(n->base || (!n->el) || (!n->pchain) || (n->pidx != (uint32_t)-1)) continue;
			uw = (((uint32_t)(n->hid))<<1)|((uint32_t)(n->rev));
			if(uw == w) {
                // if((((v>>1) == 15016) && ((w>>1) == 2778)) || (((v>>1) == 2778) && ((w>>1) == 15016))) {
                //     fprintf(stderr, "+[M::%s] ul_id::%lu, qs::%u, qe::%u\n", __func__, a[k]>>32, n->qs, n->qe);
                //     // print_ul_alignment(ug, &UL_INF, a[k]>>32, "+++");
                // }
                ql = ((int64_t)n->qs) - ((int64_t)p->qe);
                if(ql > max_ql && n->qe > p->qe && n->qs > p->qs) {
                    if(ql < mmql) {
                        mmql = ql; rev = 0; m0 = p; m1 = n; id = a[k]>>32;
                    }
                }
            }
		}

		// if(((uv^1) == v) && (p->pidx != (uint32_t)-1)) {
        if(((uv^1) == v) && (p->pidx == (uint32_t)-1) && (((uint32_t)(a[k])) > 0)) {
			n = &(UL_INF.a[a[k]>>32].bb.a[((uint32_t)(a[k]))-1]);
            if(n->base || (!n->el) || (!n->pchain) || (n->aidx != (uint32_t)-1)) continue;
			uw = (((uint32_t)(n->hid))<<1)|((uint32_t)(n->rev)); uw ^= 1;
			if(uw == w) {
                // if((((v>>1) == 15016) && ((w>>1) == 2778)) || (((v>>1) == 2778) && ((w>>1) == 15016))) {
                //     fprintf(stderr, "-[M::%s] ul_id::%lu, qs::%u, qe::%u\n", __func__, a[k]>>32, n->qs, n->qe);
                //     // print_ul_alignment(ug, &UL_INF, a[k]>>32, "+++");
                // }
                ql = ((int64_t)p->qs) - ((int64_t)n->qe);
                if(ql > max_ql && n->qe < p->qe && n->qs < p->qs) {
                    if(ql < mmql) {
                        mmql = ql; rev = 1; m0 = n; m1 = p; id = a[k]>>32;
                    }
                }
            }
		}
    }

    if(m0 && m1 && id != ((uint32_t)-1)) {
        z->ulid = (id<<1)|rev;
        z->raw_sof = m0->qe;
        z->raw_eof = m1->qs;
        // if((((v>>1) == 15016) && ((w>>1) == 2778)) || (((v>>1) == 2778) && ((w>>1) == 15016))) {
        //     fprintf(stderr, "[M::%s] id::%u, v>>1::%u(%c), w>>1::%u(%c), raw_sof::%u, raw_eof::%u\n", 
        //     __func__, id, v>>1, "+-"[v&1], w>>1, "+-"[w&1], z->raw_sof, z->raw_eof);
        // }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
imulq $0x18, -0x10(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x36736a
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
shrq $0x20, %rax
xorq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x3673b1
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x34(%rbp), %edx
shrl %edx
movl %edx, %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x367413
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x38(%rbp), %edx
shrl %edx
movl %edx, %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
xorq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x36743b
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
shrq $0x20, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movl %eax, -0x40(%rbp)
leaq 0x5de84(%rip), %rax     # 0x3c52d0
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x3c(%rbp)
leaq 0x5de6b(%rip), %rax     # 0x3c52d0
movq 0x18(%rax), %rax
movl -0x40(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
ja 0x36748a
movl -0x3c(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0x367493
movl -0x40(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
xorl %eax, %eax
subq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x9c(%rbp)
movl $0xffffffff, -0xa0(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
leaq 0x5dece(%rip), %rax     # 0x3c53b8
movq 0x40(%rax), %rax
leaq 0x5dec3(%rip), %rcx     # 0x3c53b8
movq 0x28(%rcx), %rcx
movl -0x34(%rbp), %edx
shrl %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
leaq 0x5dea3(%rip), %rax     # 0x3c53b8
movq 0x28(%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
leaq 0x5de8b(%rip), %rcx     # 0x3c53b8
movq 0x28(%rcx), %rcx
movl -0x34(%rbp), %edx
shrl %edx
movl %edx, %edx
subl (%rcx,%rdx,4), %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x7fffffff, -0x68(%rbp) # imm = 0x7FFFFFFF
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x36796f
leaq 0x5de52(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl (%rax), %eax
movl -0x34(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x3675af
jmp 0x3675ce
leaq 0x33d96(%rip), %rdi     # 0x39b34c
leaq 0x2ebfc(%rip), %rsi     # 0x3961b9
movl $0x3a12, %edx           # imm = 0x3A12
leaq 0x33d94(%rip), %rcx     # 0x39b35d
callq 0x2130
leaq 0x5dde3(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movzbl 0x50(%rax), %eax
cmpl $0x3, %eax
je 0x3675fe
jmp 0x36795e
movq -0x78(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0x367629
movq -0x78(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x367629
movq -0x78(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1f, %al
cmpb $0x0, %al
jne 0x36762e
jmp 0x36795e
movq -0x78(%rbp), %rax
movl (%rax), %eax
shll %eax
movq -0x78(%rbp), %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0x3677e0
movq -0x78(%rbp), %rax
cmpl $-0x1, 0x1c(%rax)
jne 0x3677e0
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addl $0x1, %eax
movl %eax, %eax
leaq 0x5dd35(%rip), %rcx     # 0x3c53b8
movq 0x48(%rcx), %rcx
movq -0x48(%rbp), %rdx
movq -0x58(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
shrq $0x20, %rdx
imulq $0x58, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x20(%rcx), %rax
jae 0x3677e0
leaq 0x5dd09(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
addl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0x36771f
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x36771f
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1f, %al
cmpb $0x0, %al
je 0x36771f
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x14(%rax)
je 0x367724
jmp 0x36795e
movq -0x80(%rbp), %rax
movl (%rax), %eax
shll %eax
movq -0x80(%rbp), %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x3677de
movq -0x80(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x78(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x3677dc
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jbe 0x3677dc
movq -0x80(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x3677dc
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x3677da
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x9c(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xa0(%rbp)
jmp 0x3677dc
jmp 0x3677de
jmp 0x3677e0
movl -0x94(%rbp), %eax
xorl $0x1, %eax
cmpl -0x34(%rbp), %eax
jne 0x36795c
movq -0x78(%rbp), %rax
cmpl $-0x1, 0x14(%rax)
jne 0x36795c
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jbe 0x36795c
leaq 0x5db9c(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rax
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
jne 0x36788c
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
shrb $0x7, %al
cmpb $0x0, %al
je 0x36788c
movq -0x80(%rbp), %rax
movb 0x20(%rax), %al
andb $0x1f, %al
cmpb $0x0, %al
je 0x36788c
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x1c(%rax)
je 0x367891
jmp 0x36795e
movq -0x80(%rbp), %rax
movl (%rax), %eax
shll %eax
movq -0x80(%rbp), %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x36795a
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x80(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x367958
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x367958
movq -0x80(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x367958
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x367956
movq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x1, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xa0(%rbp)
jmp 0x367958
jmp 0x36795a
jmp 0x36795c
jmp 0x36795e
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x367551
cmpq $0x0, -0x88(%rbp)
je 0x3679c3
cmpq $0x0, -0x90(%rbp)
je 0x3679c3
cmpl $-0x1, -0xa0(%rbp)
je 0x3679c3
movl -0xa0(%rbp), %ecx
shll %ecx
orl -0x9c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x88(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x90(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x14(%rax)
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)