// File: /workspace/github_c_makefile_star2_O2/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 $0x448, %rsp            # imm = 0x448
movq %r9, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movl %esi, 0x34(%rsp)
movq %rdi, 0x10(%rsp)
cmpl $0x100, %edx            # imm = 0x100
jge 0x1a519
movl %r8d, %ebp
movq 0x490(%rsp), %r15
movq 0x488(%rsp), %r14
andl $0x0, 0x40(%rsp)
movq %rdx, %r13
movslq %edx, %rsi
pushq $0x1
popq %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
cmpq %rsi, %rax
jge 0x1a273
addl -0x4(%rdx,%rax,4), %ecx
movl %ecx, 0x40(%rsp,%rax,4)
incq %rax
jmp 0x1a261
movl 0x3c(%rsp,%rsi,4), %eax
movl %eax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movl -0x4(%rax,%rsi,4), %eax
movl %eax, 0xc(%rsp)
pushq $0x18
popq %rdx
movq 0x10(%rsp), %rdi
callq 0x10980
movq %rax, %r12
leal (,%rbp,8), %eax
cltq
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
testl %ebp, %ebp
cmovlel %ebx, %ebp
movq %r13, %rax
testl %eax, %eax
cmovlel %ebx, %eax
movq 0x20(%rsp), %rcx
leaq 0xc(%rcx), %r13
movq %rax, 0x28(%rsp)
cmpq %rax, %rbx
je 0x1a307
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x10afc
imulq $0x18, %rbx, %rcx
movq %rax, 0x8(%r12,%rcx)
movq %r13, %rcx
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x1a2fd
movl (%rcx), %esi
shlq $0x20, %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
addq $0x50, %rcx
jmp 0x1a2e5
incq %rbx
movq 0x28(%rsp), %rax
jmp 0x1a2c3
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rbp, %rcx
je 0x1a359
imulq $0x50, %rcx, %rsi
movq 0x20(%rsp), %rdi
leaq (%rdi,%rsi), %rdx
movl 0x4(%rdi,%rsi), %esi
testl %esi, %esi
cmovlel %eax, %esi
xorl %edi, %edi
cmpl %edi, %esi
je 0x1a354
movslq 0x28(%rdx), %r8
addq %rdi, %r8
shlq $0x4, %r8
movzbl 0xe(%r15,%r8), %r8d
imulq $0x18, %r8, %r8
movq 0x8(%r12,%r8), %r9
incq (%r9,%rcx,8)
incl 0x4(%r12,%r8)
incq %rdi
jmp 0x1a328
incq %rcx
jmp 0x1a30b
xorl %ebx, %ebx
cmpq 0x28(%rsp), %rbx
je 0x1a3b6
imulq $0x18, %rbx, %rax
andl $0x0, (%r12,%rax)
leaq (%r12,%rax), %r13
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rax, %rbp
je 0x1a396
movq 0x8(%r13), %rdx
movq (%rdx,%rax,8), %rsi
testl %esi, %esi
je 0x1a391
movslq %ecx, %rdi
incl %ecx
movl %ecx, (%r13)
movq %rsi, (%rdx,%rdi,8)
incq %rax
jmp 0x1a373
movslq 0x4(%r13), %rsi
shlq $0x4, %rsi
movq 0x10(%rsp), %rdi
callq 0x10afc
movq %rax, 0x10(%r13)
andl $0x0, 0x4(%r13)
incq %rbx
jmp 0x1a35b
movl 0x30(%rsp), %eax
addl %eax, 0xc(%rsp)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rbp, %rcx
je 0x1a467
imulq $0x50, %rcx, %rsi
movq 0x20(%rsp), %rdi
leaq (%rdi,%rsi), %rdx
movl 0x4(%rdi,%rsi), %esi
testl %esi, %esi
cmovlel %eax, %esi
xorl %edi, %edi
cmpl %edi, %esi
je 0x1a45f
movslq 0x28(%rdx), %r9
addq %rdi, %r9
shlq $0x4, %r9
movq (%r15,%r9), %r8
movq 0x8(%r15,%r9), %r9
movq %r9, %r10
shrq $0x30, %r10
movzbl %r10b, %r10d
testq %r8, %r8
js 0x1a412
movl 0x40(%rsp,%r10,4), %r11d
jmp 0x1a430
leal (,%r10,4), %r11d
movq 0x18(%rsp), %rbx
movl (%rbx,%r11), %ebx
addl 0x40(%rsp,%r11), %ebx
movl 0xc(%rsp), %r11d
subl %ebx, %r11d
movslq %r11d, %r11
subq %r11, %r9
imulq $0x18, %r10, %r10
movq 0x10(%r12,%r10), %r11
movslq 0x4(%r12,%r10), %rbx
leal 0x1(%rbx), %r13d
movl %r13d, 0x4(%r12,%r10)
shlq $0x4, %rbx
movq %r8, (%r11,%rbx)
movq %r9, 0x8(%r11,%rbx)
incq %rdi
jmp 0x1a3e3
incq %rcx
jmp 0x1a3c2
xorl %ebx, %ebx
pushq $0x3c
popq %r15
movl $0xff00ffff, %ebp       # imm = 0xFF00FFFF
cmpq 0x28(%rsp), %rbx
je 0x1a504
movq 0x18(%rsp), %rax
movl (%rax,%rbx,4), %edx
imulq $0x18, %rbx, %r13
movl (%r12,%r13), %ecx
movq 0x8(%r12,%r13), %r8
movq 0x10(%r12,%r13), %r9
andl $0x0, (%rsp)
movq 0x10(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x19230
movq %rax, (%r14,%rbx,8)
movl (%r12,%r13), %esi
movq 0x480(%rsp), %rax
movl %esi, (%rax,%rbx,4)
movzbl %bl, %eax
shll $0x10, %eax
movq %r15, %rcx
xorl %edx, %edx
movslq %esi, %rsi
cmpq %rsi, %rdx
jge 0x1a4fc
movq (%r14,%rbx,8), %rsi
orl $0x8000, (%rsi,%rcx)    # imm = 0x8000
movq (%r14,%rbx,8), %rsi
movl (%rsi,%rcx), %edi
andl %ebp, %edi
orl %eax, %edi
movl %edi, (%rsi,%rcx)
incq %rdx
movq 0x480(%rsp), %rsi
movl (%rsi,%rbx,4), %esi
addq $0x50, %rcx
jmp 0x1a4c7
incq %rbx
jmp 0x1a472
movq %r12, %rax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf4a0(%rip), %rdi      # 0x299c0
leaq 0xf45f(%rip), %rsi      # 0x29986
leaq 0xf4a7(%rip), %rcx      # 0x299d5
movl $0x150, %edx            # imm = 0x150
callq 0x21a0