// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/./Overlaps.h
// Function: get_unitig(asg_t*, ma_ug_t*, unsigned int, unsigned int*, long long*, long long*, long long*, long long*, unsigned int, buf_t*)
inline uint32_t get_unitig(asg_t *sg, ma_ug_t *ug, uint32_t begNode, uint32_t* endNode, 
long long* nodeLen, long long* baseLen, long long* max_stop_nodeLen, long long* max_stop_baseLen, 
uint32_t stops_threshold, buf_t* b)
{
	ma_utg_v* u = NULL; 
    uint32_t v = begNode, w, k;
    uint32_t kv, return_flag, n_stops = 0;
	long long pre_baseLen = 0, pre_nodeLen = 0;
	long long cur_baseLen = 0, cur_nodeLen = 0;
	(*max_stop_nodeLen) = (*max_stop_baseLen) = (*nodeLen) = (*baseLen) = 0;
    (*endNode) = (uint32_t)-1;
	if(ug!=NULL) u = &(ug->u);

    while (1)
    {
        kv = get_real_length(sg, v, NULL);
        (*endNode) = v;
		if(u == NULL) 
		{
			(*nodeLen)++;
		}
		else
		{
			(*nodeLen) += EvaluateLen((*u), v>>1);
		}
        if(b) kv_push(uint32_t, b->b, v);
		///means reach the end of a unitig
		if(kv!=1) (*baseLen) += sg->seq[v>>1].len;
		if(kv==0)
		{
			return_flag = END_TIPS;
			break;
			///return END_TIPS;
		} 
		if(kv>1)
		{
			return_flag = MUL_OUTPUT;
			break;
			///return MUL_OUTPUT;
		}
        ///kv must be 1 here
        kv = get_real_length(sg, v, &w);
		///means reach the end of a unitig
        if(get_real_length(sg, w^1, NULL)!=1)
		{

			n_stops++;
			if(n_stops >= stops_threshold)
			{
				(*baseLen) += sg->seq[v>>1].len;
				return_flag = MUL_INPUT;
				break;
				///return MUL_INPUT;
			}
			else
			{
				for (k = 0; k < asg_arc_n(sg, v); k++)
				{
					if(asg_arc_a(sg, v)[k].del) continue;
					///here is just one undeleted edge
					(*baseLen) += asg_arc_len(asg_arc_a(sg, v)[k]);
					break;
				}
			}

			cur_baseLen = (*baseLen) - pre_baseLen;
			pre_baseLen = (*baseLen);
			if(cur_baseLen > (*max_stop_baseLen))
			{
				(*max_stop_baseLen) = cur_baseLen;
			}


			cur_nodeLen = (*nodeLen) - pre_nodeLen;
			pre_nodeLen = (*nodeLen);
			if(cur_nodeLen > (*max_stop_nodeLen))
			{
				(*max_stop_nodeLen) = cur_nodeLen;
			}
		}
		else
		{
			for (k = 0; k < asg_arc_n(sg, v); k++)
			{
				if(asg_arc_a(sg, v)[k].del) continue;
				///here is just one undeleted edge
				(*baseLen) += asg_arc_len(asg_arc_a(sg, v)[k]);
				break;
			}
		}
		

        v = w;
        if(v == begNode)
		{
			return_flag = LOOP;
			break;
			///return LOOP;
		} 
    }




	cur_baseLen = (*baseLen) - pre_baseLen;
	pre_baseLen = (*baseLen);
	if(cur_baseLen > (*max_stop_baseLen))
	{
		(*max_stop_baseLen) = cur_baseLen;
	}


	cur_nodeLen = (*nodeLen) - pre_nodeLen;
	pre_nodeLen = (*nodeLen);
	if(cur_nodeLen > (*max_stop_nodeLen))
	{
		(*max_stop_nodeLen) = cur_nodeLen;
	}

	return return_flag;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movq %r9, (%rsp)
andq $0x0, (%r9)
movq %rdi, %r13
movq %r8, 0x20(%rsp)
andq $0x0, (%r8)
movq 0x78(%rsp), %rax
andq $0x0, (%rax)
movq 0x70(%rsp), %rax
andq $0x0, (%rax)
movq 0x88(%rsp), %r15
movq %rcx, 0x30(%rsp)
orl $-0x1, (%rcx)
xorl %ebx, %ebx
movl %edx, 0x2c(%rsp)
movl %edx, %r14d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %r13, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xc2e7a
movl %eax, %ebp
movq 0x30(%rsp), %rax
movl %r14d, (%rax)
pushq $0x1
popq %rax
testq %r12, %r12
je 0xc2f52
movq 0x10(%r12), %rax
movl %r14d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movl 0x4(%rax,%rcx), %eax
movq 0x20(%rsp), %rcx
addq %rax, (%rcx)
testq %r15, %r15
je 0xc2f9f
movq 0x38(%r15), %rcx
movq 0x48(%r15), %rax
cmpq 0x40(%r15), %rcx
jne 0xc2f93
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x40(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x48(%r15)
movq 0x38(%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%r15)
movl %r14d, (%rax,%rcx,4)
cmpl $0x1, %ebp
jne 0xc30ba
movq %r13, %rdi
movl %r14d, %esi
leaq 0xc(%rsp), %rdx
callq 0xc2e7a
movl 0xc(%rsp), %esi
xorl $0x1, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0xc2e7a
movl %r14d, %ecx
cmpl $0x1, %eax
jne 0xc3011
movq 0x28(%r13), %rax
movq (%rax,%rcx,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
incq %rax
imulq $0x18, %rcx, %rcx
decq %rax
je 0xc309f
leaq 0x18(%rcx), %rdx
movq 0x8(%r13), %rsi
cmpl $0x0, 0xc(%rsi,%rcx)
movq %rdx, %rcx
js 0xc2fe6
movl -0x18(%rsi,%rdx), %eax
movq (%rsp), %rcx
addq %rax, (%rcx)
jmp 0xc309f
incl %ebx
cmpl 0x80(%rsp), %ebx
jae 0xc30df
movq 0x28(%r13), %rax
movq (%rax,%rcx,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
incq %rax
imulq $0x18, %rcx, %rcx
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %r8
decq %rax
je 0xc3066
leaq 0x18(%rcx), %rdx
movq 0x8(%r13), %rsi
cmpl $0x0, 0xc(%rsi,%rcx)
movq %rdx, %rcx
js 0xc303f
movl -0x18(%rsi,%rdx), %eax
movq (%rsp), %rcx
addq (%rcx), %rax
movq %rax, (%rcx)
jmp 0xc306d
movq (%rsp), %rax
movq (%rax), %rax
movq %rax, %rcx
subq 0x10(%rsp), %rcx
cmpq (%r8), %rcx
jle 0xc307d
movq %rcx, (%r8)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, %rdx
subq 0x18(%rsp), %rdx
cmpq (%rdi), %rdx
jle 0xc3095
movq %rdx, (%rdi)
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movl 0xc(%rsp), %r14d
cmpl 0x2c(%rsp), %r14d
jne 0xc2f21
movq (%rsp), %rax
movq (%rax), %rcx
pushq $0x7
jmp 0xc30fc
movq 0x20(%r13), %rax
shrl %r14d
movl (%rax,%r14,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq (%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
cmpl $0x1, %ebp
adcl $0x4, %eax
jmp 0xc30fd
movq 0x20(%r13), %rax
shrl %r14d
movl (%rax,%r14,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq (%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
pushq $0x3
popq %rax
subq 0x10(%rsp), %rcx
movq 0x78(%rsp), %rsi
cmpq (%rsi), %rcx
movq 0x70(%rsp), %rdx
jle 0xc3114
movq %rcx, (%rsi)
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
subq 0x18(%rsp), %rcx
cmpq (%rdx), %rcx
jle 0xc3129
movq %rcx, (%rdx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq