// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq (%rdi), %r9
movq 0x10(%rdi), %rax
imulq $0x18, %rsi, %rdx
movl 0x8(%r9,%rdx), %ecx
movl 0xc(%r9,%rdx), %r8d
movl %ecx, %esi
shrl %esi
movq 0x10(%rax), %rax
testb $0x1, %cl
jne 0x1d3551
imulq $0x28, %rsi, %rdi
movl 0x10(%rax,%rdi), %esi
decl %esi
shlq $0x3, %rsi
addq 0x18(%rax,%rdi), %rsi
jmp 0x1d355a
imulq $0x28, %rsi, %rsi
movq 0x18(%rax,%rsi), %rsi
movq (%rsi), %rsi
shrq $0x21, %rsi
movl %r8d, %edi
shrl %edi
testb $0x1, %r8b
movq %r8, 0x38(%rsp)
jne 0x1d357c
imulq $0x28, %rdi, %rdi
movq 0x18(%rax,%rdi), %rdi
jmp 0x1d3590
imulq $0x28, %rdi, %r8
movl 0x10(%rax,%r8), %edi
decl %edi
shlq $0x3, %rdi
addq 0x18(%rax,%r8), %rdi
addq %rdx, %r9
movq %r9, 0x50(%rsp)
movq (%rdi), %rax
leaq 0x80c8e(%rip), %rdx     # 0x254230
movq 0x18(%rdx), %rdx
movl (%rdx,%rsi,8), %esi
shrq $0x21, %rax
movl (%rdx,%rax,8), %eax
cmpl %eax, %esi
cmovbl %esi, %eax
negq %rax
movq %rax, 0x30(%rsp)
leaq 0x80d54(%rip), %rax     # 0x254318
movl %ecx, %edi
shrl %edi
movq 0x28(%rax), %rdx
movq 0x48(%rax), %r8
movl (%rdx,%rdi,4), %r10d
movl 0x4(%rdx,%rdi,4), %r9d
subl %r10d, %r9d
shlq $0x3, %r10
addq 0x40(%rax), %r10
movl %ecx, %eax
andl $-0x2, %eax
movl %eax, 0x2c(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x4(%rsp)
xorl %r15d, %r15d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x8(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, (%rsp)
cmpq %r15, %r9
je 0x1d37fb
movq (%r10,%r15,8), %r12
movq %r12, %r13
shrq $0x20, %r13
imulq $0x58, %r13, %rax
movq 0x30(%r8,%rax), %rbx
movl %r12d, %edx
imulq $0x24, %rdx, %r14
cmpl %edi, (%rbx,%r14)
jne 0x1d3840
addq %r8, %rax
cmpb $0x3, 0x50(%rax)
jne 0x1d37f3
addq %rbx, %r14
movzbl 0x20(%r14), %r11d
movl %r11d, %esi
andb $-0x40, %sil
negb %sil
setno %sil
testb $0x1f, %r11b
sete %bpl
orb %sil, %bpl
jne 0x1d37f3
shrl $0x5, %r11d
andl $0x1, %r11d
orl 0x2c(%rsp), %r11d
cmpl %ecx, %r11d
jne 0x1d3751
cmpl $-0x1, 0x1c(%r14)
jne 0x1d3751
leal 0x1(%r12), %esi
cmpq 0x20(%rax), %rsi
jae 0x1d3751
movq %rcx, 0x10(%rsp)
imulq $0x24, %rsi, %rcx
movzbl 0x20(%rbx,%rcx), %esi
movl %esi, %ebp
andb $-0x40, %bpl
negb %bpl
setno %bpl
testb $0x1f, %sil
sete %al
orb %bpl, %al
jne 0x1d36dc
addq %rbx, %rcx
cmpl $-0x1, 0x14(%rcx)
je 0x1d36e6
movq 0x10(%rsp), %rcx
jmp 0x1d37f3
movq %rcx, %rbp
movl (%rcx), %eax
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rax,2), %eax
cmpl 0x38(%rsp), %eax
movq 0x10(%rsp), %rcx
jne 0x1d3751
movl 0x4(%rbp), %esi
movl 0x8(%r14), %eax
movq %rsi, 0x40(%rsp)
movq %rax, 0x48(%rsp)
subq %rax, %rsi
cmpq 0x30(%rsp), %rsi
jle 0x1d3751
movq 0x48(%rsp), %rax
cmpl %eax, 0x8(%rbp)
jbe 0x1d3751
movq 0x40(%rsp), %rax
cmpl 0x4(%r14), %eax
jbe 0x1d3751
cmpq 0x8(%rsp), %rsi
jge 0x1d3751
movl $0x0, (%rsp)
movq %rsi, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl %r13d, 0x4(%rsp)
xorl %ecx, %r11d
cmpl $0x1, %r11d
jne 0x1d37f3
testq %rdx, %rdx
je 0x1d37f3
cmpl $-0x1, 0x14(%r14)
jne 0x1d37f3
decl %r12d
imulq $0x24, %r12, %rdx
movzbl 0x20(%rbx,%rdx), %eax
movl %eax, %esi
andb $-0x40, %sil
negb %sil
setno %sil
testb $0x1f, %al
sete %r11b
orb %sil, %r11b
jne 0x1d37f3
addq %rdx, %rbx
cmpl $-0x1, 0x1c(%rbx)
jne 0x1d37f3
movl (%rbx), %edx
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rdx,2), %eax
xorl 0x38(%rsp), %eax
cmpl $0x1, %eax
jne 0x1d37f3
movl 0x4(%r14), %edx
movl 0x8(%rbx), %esi
movq %rdx, %rax
subq %rsi, %rax
cmpq 0x30(%rsp), %rax
jle 0x1d37f3
cmpl 0x8(%r14), %esi
jae 0x1d37f3
cmpl %edx, 0x4(%rbx)
jae 0x1d37f3
cmpq 0x8(%rsp), %rax
jge 0x1d37f3
movq %rax, 0x8(%rsp)
movq %rbx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
pushq $0x1
popq %rax
movl %eax, (%rsp)
movl %r13d, 0x4(%rsp)
incq %r15
jmp 0x1d361a
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
movl 0x4(%rsp), %eax
movq 0x18(%rsp), %rsi
je 0x1d3831
testq %rsi, %rsi
je 0x1d3831
cmpl $-0x1, %eax
je 0x1d3831
addl %eax, %eax
orl (%rsp), %eax
movq 0x50(%rsp), %rcx
movl %eax, 0x4(%rcx)
movl 0x8(%rdx), %eax
movl %eax, 0x10(%rcx)
movl 0x4(%rsi), %eax
movl %eax, 0x14(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28ddc(%rip), %rdi     # 0x1fc623
leaq 0x23c8b(%rip), %rsi     # 0x1f74d9
leaq 0x28ddf(%rip), %rcx     # 0x1fc634
movl $0x3a12, %edx           # imm = 0x3A12
callq 0x2110