// File: /workspace/github_c_makefile_star2_O1/lh3[P]minimap2/hit.c
// Function: mm_seg_gen
mm_seg_t *mm_seg_gen(void *km, uint32_t hash, int n_segs, const int *qlens, int n_regs0, const mm_reg1_t *regs0, int *n_regs, mm_reg1_t **regs, const mm128_t *a)
{
	int s, i, j, acc_qlen[MM_MAX_SEG+1], qlen_sum = 0;
	mm_seg_t *seg;

	assert(n_segs <= MM_MAX_SEG);
	for (s = 1, acc_qlen[0] = 0; s < n_segs; ++s)
		acc_qlen[s] = acc_qlen[s-1] + qlens[s-1];
	qlen_sum = acc_qlen[n_segs - 1] + qlens[n_segs - 1];

	seg = (mm_seg_t*)kcalloc(km, n_segs, sizeof(mm_seg_t));
	for (s = 0; s < n_segs; ++s) {
		seg[s].u = (uint64_t*)kmalloc(km, n_regs0 * 8);
		for (i = 0; i < n_regs0; ++i)
			seg[s].u[i] = (uint64_t)regs0[i].score << 32;
	}
	for (i = 0; i < n_regs0; ++i) {
		const mm_reg1_t *r = &regs0[i];
		for (j = 0; j < r->cnt; ++j) {
			int sid = (a[r->as + j].y&MM_SEED_SEG_MASK)>>MM_SEED_SEG_SHIFT;
			++seg[sid].u[i];
			++seg[sid].n_a;
		}
	}
	for (s = 0; s < n_segs; ++s) {
		mm_seg_t *sr = &seg[s];
		for (i = 0, sr->n_u = 0; i < n_regs0; ++i) // squeeze out zero-length per-segment chains
			if ((int32_t)sr->u[i] != 0)
				sr->u[sr->n_u++] = sr->u[i];
		sr->a = (mm128_t*)kmalloc(km, sr->n_a * sizeof(mm128_t));
		sr->n_a = 0;
	}

	for (i = 0; i < n_regs0; ++i) {
		const mm_reg1_t *r = &regs0[i];
		for (j = 0; j < r->cnt; ++j) {
			int sid = (a[r->as + j].y&MM_SEED_SEG_MASK)>>MM_SEED_SEG_SHIFT;
			mm128_t a1 = a[r->as + j];
			// on reverse strand, the segment position is:
			//   x_for_cat = qlen_sum - 1 - (int32_t)a1.y - 1 + q_span
			//   (int32_t)new_a1.y = qlens[sid] - (x_for_cat - acc_qlen[sid] + 1 - q_span) - 1 = (int32_t)a1.y - (qlen_sum - (qlens[sid] + acc_qlen[sid]))
			a1.y -= a1.x>>63? qlen_sum - (qlens[sid] + acc_qlen[sid]) : acc_qlen[sid];
			seg[sid].a[seg[sid].n_a++] = a1;
		}
	}
	for (s = 0; s < n_segs; ++s) {
		regs[s] = mm_gen_regs(km, hash, qlens[s], seg[s].n_u, seg[s].u, seg[s].a, 0);
		n_regs[s] = seg[s].n_u;
		for (i = 0; i < n_regs[s]; ++i) {
			regs[s][i].seg_split = 1;
			regs[s][i].seg_id = s;
		}
	}
	return seg;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %r8, 0x10(%rsp)
movl %esi, 0x44(%rsp)
movq %rdi, 0x30(%rsp)
cmpl $0x100, %edx            # imm = 0x100
jge 0x1cffc
movq %rcx, %rbx
movl %edx, %ebp
movq %r9, 0x38(%rsp)
movl $0x0, 0x50(%rsp)
movl %edx, %eax
movq %rax, 0x28(%rsp)
cmpl $0x2, %edx
jl 0x1ccf8
movl 0x50(%rsp), %eax
movq 0x28(%rsp), %rcx
decq %rcx
xorl %edx, %edx
addl (%rbx,%rdx,4), %eax
movl %eax, 0x54(%rsp,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x1cce9
movslq %ebp, %rsi
movl 0x4c(%rsp,%rsi,4), %eax
movl %eax, 0x40(%rsp)
movl -0x4(%rbx,%rsi,4), %r14d
movl $0x18, %edx
movq 0x30(%rsp), %rdi
callq 0x1257c
movq %rax, %r15
movl 0x10(%rsp), %r13d
movl %ebp, 0x1c(%rsp)
testl %ebp, %ebp
jle 0x1cd8b
movq 0x10(%rsp), %rax
leal (,%rax,8), %eax
movslq %eax, %rbp
movq 0x38(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x126e9
leaq (%r12,%r12,2), %rcx
movq %rax, 0x8(%r15,%rcx,8)
cmpl $0x0, 0x10(%rsp)
jle 0x1cd81
movq 0x20(%rsp), %rcx
xorl %edx, %edx
movl (%rcx), %esi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x50, %rcx
cmpq %rdx, %r13
jne 0x1cd6b
incq %r12
cmpq 0x28(%rsp), %r12
jne 0x1cd47
movq 0x4a0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x10(%rsp)
movq 0x38(%rsp), %r10
movl 0x1c(%rsp), %r11d
jle 0x1cdfe
addq $0x8, %rax
xorl %ecx, %ecx
leaq (%rcx,%rcx,4), %rsi
shlq $0x4, %rsi
movslq 0x4(%r10,%rsi), %rdx
testq %rdx, %rdx
jle 0x1cdf6
addq %r10, %rsi
movslq 0x28(%rsi), %rsi
shlq $0x4, %rsi
addq %rax, %rsi
shlq $0x4, %rdx
xorl %edi, %edi
movzbl 0x6(%rsi,%rdi), %r8d
leaq (%r8,%r8,2), %r8
movq 0x8(%r15,%r8,8), %r9
incq (%r9,%rcx,8)
incl 0x4(%r15,%r8,8)
addq $0x10, %rdi
cmpq %rdi, %rdx
jne 0x1cdd5
incq %rcx
cmpq %r13, %rcx
jne 0x1cdaf
testl %r11d, %r11d
jle 0x1ce74
xorl %ebp, %ebp
leaq (,%rbp,2), %rax
addq %rbp, %rax
leaq (%r15,%rax,8), %r12
movl $0x0, (%r15,%rax,8)
cmpl $0x0, 0x10(%rsp)
jle 0x1ce49
movq 0x8(%r12), %rax
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdx
testl %edx, %edx
je 0x1ce41
movslq (%r12), %rsi
leal 0x1(%rsi), %edi
movl %edi, (%r12)
movq %rdx, (%rax,%rsi,8)
incq %rcx
cmpq %rcx, %r13
jne 0x1ce2a
movslq 0x4(%r12), %rsi
shlq $0x4, %rsi
movq 0x30(%rsp), %rdi
callq 0x126e9
movq %rax, 0x10(%r12)
movl $0x0, 0x4(%r12)
incq %rbp
cmpq 0x28(%rsp), %rbp
jne 0x1ce05
cmpl $0x0, 0x10(%rsp)
movq 0x38(%rsp), %r12
jle 0x1cf33
addl 0x40(%rsp), %r14d
addq $0x8, 0x20(%rsp)
xorl %eax, %eax
leaq (%rax,%rax,4), %rdx
shlq $0x4, %rdx
movslq 0x4(%r12,%rdx), %rcx
testq %rcx, %rcx
jle 0x1cf27
addq %r12, %rdx
movslq 0x28(%rdx), %rdx
shlq $0x4, %rdx
addq 0x20(%rsp), %rdx
shlq $0x4, %rcx
xorl %esi, %esi
movq -0x8(%rdx,%rsi), %rdi
movq (%rdx,%rsi), %r8
movq %r8, %r9
shrq $0x30, %r9
movzbl %r9b, %r9d
testq %rdi, %rdi
js 0x1cedd
movl 0x50(%rsp,%r9,4), %r10d
jmp 0x1cef4
leal (,%r9,4), %r10d
movl (%rbx,%r10), %r11d
addl 0x50(%rsp,%r10), %r11d
movl %r14d, %r10d
subl %r11d, %r10d
movslq %r10d, %r10
subq %r10, %r8
leaq (%r9,%r9,2), %r9
movq 0x10(%r15,%r9,8), %r10
movslq 0x4(%r15,%r9,8), %r11
leal 0x1(%r11), %ebp
movl %ebp, 0x4(%r15,%r9,8)
shlq $0x4, %r11
movq %rdi, (%r10,%r11)
movq %r8, 0x8(%r10,%r11)
addq $0x10, %rsi
cmpq %rsi, %rcx
jne 0x1cebd
incq %rax
cmpq %r13, %rax
jne 0x1ce91
cmpl $0x0, 0x1c(%rsp)
jle 0x1cfe7
movq 0x498(%rsp), %r14
movq 0x490(%rsp), %r12
xorl %r13d, %r13d
movl $0xff00ffff, %ebp       # imm = 0xFF00FFFF
movq %rbx, 0x48(%rsp)
movl (%rbx,%r13,4), %edx
leaq (,%r13,2), %rbx
addq %r13, %rbx
movl (%r15,%rbx,8), %ecx
movq 0x8(%r15,%rbx,8), %r8
movq 0x10(%r15,%rbx,8), %r9
movl $0x0, (%rsp)
movq 0x30(%rsp), %rdi
movl 0x44(%rsp), %esi
callq 0x1bb24
movq %rax, (%r14,%r13,8)
movl (%r15,%rbx,8), %eax
movl %eax, (%r12,%r13,4)
testl %eax, %eax
jle 0x1cfd4
movzbl %r13b, %eax
shll $0x10, %eax
movl $0x3c, %ecx
xorl %edx, %edx
movq (%r14,%r13,8), %rsi
orl $0x8000, (%rsi,%rcx)    # imm = 0x8000
movq (%r14,%r13,8), %rsi
movl (%rsi,%rcx), %edi
andl %ebp, %edi
orl %eax, %edi
movl %edi, (%rsi,%rcx)
incq %rdx
movslq (%r12,%r13,4), %rsi
addq $0x50, %rcx
cmpq %rsi, %rdx
jl 0x1cfab
incq %r13
cmpq 0x28(%rsp), %r13
movq 0x48(%rsp), %rbx
jne 0x1cf5b
movq %r15, %rax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1097d(%rip), %rdi     # 0x2d980
leaq 0x1093c(%rip), %rsi     # 0x2d946
leaq 0x10984(%rip), %rcx     # 0x2d995
movl $0x150, %edx            # imm = 0x150
callq 0x21a0