// File: /workspace/github_c_makefile_star2_O3/lh3[P]fermi-lite/mrope.c
// Function: mr_insert_multi
void mr_insert_multi(mrope_t *mr, int64_t len, const uint8_t *s, int is_thr)
{
	int64_t k, m, n0;
	int b, is_srt = (mr->so != MR_SO_IO), is_comp = (mr->so == MR_SO_RCLO), stop_thr = 0;
	volatile int n_fin_workers = 0;
	triple64_t *a[2], *curr, *prev, *swap;
	pthread_t *tid = 0;
	worker_t *w = 0;

	if (mr->thr_min < 0) mr->thr_min = 0;
	assert(len > 0 && s[len-1] == 0);
	{ // split into short strings
		cstr_t p, q, end = s + len;
		for (p = s, m = 0; p != end; ++p) // count #sentinels
			if (*p == 0) ++m;
		curr = a[0] = malloc(m * sizeof(triple64_t));
		prev = a[1] = malloc(m * sizeof(triple64_t));
		for (p = q = s, k = 0; p != end; ++p) // find the start of each string
			if (*p == 0) prev[k++].p = q, q = p + 1;
	}

	for (k = n0 = 0; k < 6; ++k) n0 += mr->r[k]->c[0];
	for (k = 0; k != m; ++k) {
		if (is_srt) prev[k].l = 0, prev[k].u = n0;
		else prev[k].l = prev[k].u = n0 + k;
		prev[k].c = 0;
	}
	mr_insert_multi_aux(mr->r[0], m, prev, is_comp); // insert the first (actually the last) column

	if (is_thr) {
		tid = alloca(4 * sizeof(pthread_t));
		w = alloca(4 * sizeof(worker_t));
		memset(w, 0, 4 * sizeof(worker_t));
		for (b = 0; b < 4; ++b) {
			w[b].mr = mr, w[b].b = b + 1, w[b].is_comp = is_comp;
			w[b].n_fin_workers = &n_fin_workers;
		}
		for (b = 0; b < 4; ++b) pthread_create(&tid[b], 0, worker, &w[b]);
	}

	n0 = 0; // the number of inserted strings
	while (m) {
		int64_t c[6], ac[6];
		triple64_t *q[6];

		memset(c, 0, 48);
		for (k = n0; k != m; ++k) ++c[prev[k].c]; // counting
		for (q[0] = curr + n0, b = 1; b < 6; ++b) q[b] = q[b-1] + c[b-1];
		if (n0 + c[0] < m) {
			for (k = n0; k != m; ++k) *q[prev[k].c]++ = prev[k]; // sort
			for (b = 0; b < 6; ++b) q[b] -= c[b];
		}
		n0 += c[0];

		if (is_thr && !stop_thr) {
			struct timespec req, rem;
			req.tv_sec = 0; req.tv_nsec = 1000000;
			stop_thr = (m - n0 <= mr->thr_min);
			for (b = 0; b < 4; ++b) {
				w[b].a = q[b+1], w[b].m = c[b+1];
				if (stop_thr) w[b].to_exit = 1; // signal the workers to exit
				while (!__sync_bool_compare_and_swap(&w[b].to_run, 0, 1)); // signal the workers to start
			}
			if (c[5]) mr_insert_multi_aux(mr->r[5], c[5], q[5], is_comp); // the master thread processes the "N" bucket
			while (!__sync_bool_compare_and_swap(&n_fin_workers, 4, 0)) // wait until all 4 workers finish
				nanosleep(&req, &rem);
			if (stop_thr && n0 < m)
				fprintf(stderr, "[M::%s] Turn off parallelization for this batch as too few strings are left.\n", __func__);
		} else {
			for (b = 1; b < 6; ++b)
				if (c[b]) mr_insert_multi_aux(mr->r[b], c[b], q[b], is_comp);
		}
		if (n0 == m) break;

		memset(ac, 0, 48);
		for (b = 1; b < 6; ++b) { // update the intervals to account for buckets ahead
			int a;
			for (a = 0; a < 6; ++a) ac[a] += mr->r[b-1]->c[a];
			for (k = 0; k < c[b]; ++k) {
				triple64_t *p = &q[b][k];
				p->l += ac[p->c]; p->u += ac[p->c];
			}
		}
		swap = curr, curr = prev, prev = swap;
	}
	if (is_thr) for (b = 0; b < 4; ++b) pthread_join(tid[b], 0);
	free(a[0]); free(a[1]);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %ecx, -0x38(%rbp)
movq %rdx, %r13
movq %rsi, %rbx
movb (%rdi), %dl
xorl %ecx, %ecx
cmpb $0x2, %dl
sete %al
movl $0x0, -0x3c(%rbp)
cmpl $0x0, 0x4(%rdi)
jns 0x7eab
movl $0x0, 0x4(%rdi)
testq %rbx, %rbx
jle 0x8457
cmpb $0x0, -0x1(%r13,%rbx)
jne 0x8457
movb %dl, -0x58(%rbp)
movq %rdi, -0x48(%rbp)
movb %al, %cl
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
xorl %r14d, %r14d
movq %r14, %r15
movb (%r13,%rax), %r12b
cmpb $0x1, %r12b
adcq $0x0, %r14
incq %rax
cmpq %rax, %rbx
jne 0x7ed1
leaq (,%r14,8), %rax
leaq (%rax,%rax,2), %rdi
movq %rdi, -0x50(%rbp)
callq 0x21a0
movq %rax, -0x88(%rbp)
movq -0x50(%rbp), %rdi
callq 0x21a0
leaq 0x1(%r13), %rsi
xorl %ecx, %ecx
movq -0x48(%rbp), %r8
movb -0x58(%rbp), %r9b
cmpb $0x0, -0x1(%rsi)
jne 0x7f31
leaq (%rcx,%rcx,2), %rdx
incq %rcx
movq %r13, 0x10(%rax,%rdx,8)
movq %rsi, %r13
incq %rsi
decq %rbx
jne 0x7f1c
movq %r15, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movl $0x1, %ecx
xorl %eax, %eax
movq (%r8,%rcx,8), %rdx
addq 0x8(%rdx), %rax
incq %rcx
cmpq $0x7, %rcx
jne 0x7f48
movabsq $0x1fffffffffffffff, %r15 # imm = 0x1FFFFFFFFFFFFFFF
testq %r14, %r14
je 0x7fa8
movq %rax, %rcx
andq %r15, %rcx
cmpb $0x1, %r12b
movq -0x68(%rbp), %rdx
adcq $0x0, %rdx
movq -0x60(%rbp), %rsi
testb %r9b, %r9b
je 0x7f8f
movq $0x0, (%rsi)
movq %rcx, %rdi
jmp 0x7f98
movq %rax, %rdi
andq %r15, %rdi
movq %rdi, (%rsi)
movq %rdi, 0x8(%rsi)
addq $0x18, %rsi
incq %rax
decq %rdx
jne 0x7f7e
movq 0x8(%r8), %rdi
movq %r14, %rsi
movq -0x60(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0x8476
cmpl $0x0, -0x38(%rbp)
movq %r14, -0x78(%rbp)
je 0x805a
movq %rsp, %rcx
addq $-0x20, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rsp, %rbx
addq $-0xc0, %rbx
movq %rbx, %rsp
movl $0xc0, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2100
movl $0x1c, %eax
movl $0x1, %ecx
leaq -0x3c(%rbp), %rdx
movq -0x48(%rbp), %rsi
movl -0x34(%rbp), %edi
movq %rsi, -0xc(%rbx,%rax)
movl %ecx, -0x4(%rbx,%rax)
movl %edi, (%rbx,%rax)
movq %rdx, -0x1c(%rbx,%rax)
addq $0x30, %rax
incl %ecx
cmpq $0xdc, %rax
jne 0x8008
xorl %r14d, %r14d
movq %rbx, %r13
movq -0x70(%rbp), %rax
leaq (%rax,%r14), %rdi
xorl %esi, %esi
leaq 0x7c3(%rip), %rdx       # 0x8801
movq %r13, %rcx
callq 0x21f0
addq $0x30, %r13
addq $0x8, %r14
cmpq $0x20, %r14
jne 0x802d
movq -0x78(%rbp), %r14
jmp 0x8064
movq $0x0, -0x70(%rbp)
xorl %ebx, %ebx
testq %r14, %r14
je 0x8413
cmpb $0x1, %r12b
adcq $0x0, -0x68(%rbp)
movdqa 0xf562(%rip), %xmm2     # 0x175e0
movabsq $-0x2000000000000000, %r13 # imm = 0xE000000000000000
movl $0x1, %r12d
xorl %eax, %eax
movq -0x60(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq %rdx, -0x50(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x48(%rbp), %r8
movq %rbx, -0x90(%rbp)
movq %rcx, -0x80(%rbp)
pxor %xmm0, %xmm0
movdqa %xmm0, -0xd0(%rbp)
movdqa %xmm0, -0xc0(%rbp)
movdqa %xmm0, -0xb0(%rbp)
cmpq %r14, %rax
je 0x8105
movq -0x68(%rbp), %rcx
subq %rax, %rcx
leaq (%rax,%rax,2), %rdx
movq -0x80(%rbp), %rsi
leaq (%rsi,%rdx,8), %rdx
addq $0x8, %rdx
movq (%rdx), %rsi
shrq $0x3d, %rsi
incq -0xd0(%rbp,%rsi,8)
addq $0x18, %rdx
decq %rcx
jne 0x80ed
leaq (%rax,%rax,2), %rcx
movq -0x50(%rbp), %rdx
leaq (%rdx,%rcx,8), %rdx
movq %rdx, -0x100(%rbp)
movl $0x1, %esi
movq -0xd8(%rbp,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%rdx,%rdi,8), %rdx
movq %rdx, -0x100(%rbp,%rsi,8)
incq %rsi
cmpq $0x6, %rsi
jne 0x811d
movq -0xd0(%rbp), %rdx
addq %rax, %rdx
movq %r14, %r9
movq %rdx, -0x58(%rbp)
subq %rdx, %r9
jle 0x81ea
cmpq %r14, %rax
je 0x81a5
movq -0x68(%rbp), %rdx
subq %rax, %rdx
movq -0x80(%rbp), %rax
leaq (%rax,%rcx,8), %rax
addq $0x8, %rax
movq (%rax), %rcx
shrq $0x3d, %rcx
movq -0x100(%rbp,%rcx,8), %rsi
leaq 0x18(%rsi), %rdi
movq %rdi, -0x100(%rbp,%rcx,8)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsi)
movdqu -0x8(%rax), %xmm0
movdqu %xmm0, (%rsi)
addq $0x18, %rax
decq %rdx
jne 0x8170
xorl %eax, %eax
pxor %xmm0, %xmm0
psubq -0xd0(%rbp,%rax,8), %xmm0
movdqa %xmm0, %xmm1
pmuludq %xmm2, %xmm1
psrlq $0x20, %xmm0
pmuludq %xmm2, %xmm0
psllq $0x20, %xmm0
paddq %xmm1, %xmm0
paddq -0x100(%rbp,%rax,8), %xmm0
movdqa %xmm0, -0x100(%rbp,%rax,8)
addq $0x2, %rax
cmpq $0x6, %rax
jne 0x81a7
cmpl $0x0, -0x38(%rbp)
je 0x8313
cmpl $0x0, -0x30(%rbp)
jne 0x8313
movq $0x0, -0x110(%rbp)
movq $0xf4240, -0x108(%rbp) # imm = 0xF4240
movslq 0x4(%r8), %r8
xorl %ecx, %ecx
xorl %edi, %edi
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
leaq (%rbx,%rax), %rdx
movq -0xf8(%rbp,%rcx,8), %rsi
movq %rsi, 0x28(%rbx,%rax)
movq -0xc8(%rbp,%rcx,8), %rsi
movq %rsi, 0x20(%rbx,%rax)
cmpq %r8, %r9
jg 0x824e
movl $0x1, 0xc(%rdx)
incq %rcx
xorl %eax, %eax
lock
cmpxchgl %r12d, 0x8(%rdx)
jne 0x8251
cmpq $0x4, %rcx
jne 0x821c
movl $0x0, -0x30(%rbp)
movq %r9, -0xa0(%rbp)
movq %r8, -0x98(%rbp)
cmpq %r8, %r9
setle -0x29(%rbp)
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x82a2
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0xd8(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0x8476
xorl %edi, %edi
movl $0x4, %eax
lock
cmpxchgl %edi, -0x3c(%rbp)
leaq -0x110(%rbp), %rbx
leaq -0x150(%rbp), %r14
je 0x82d5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20d0
xorl %ecx, %ecx
movl $0x4, %eax
lock
cmpxchgl %ecx, -0x3c(%rbp)
jne 0x82bc
movq -0x58(%rbp), %rax
cmpq %rax, -0x78(%rbp)
jle 0x82f3
movq -0x98(%rbp), %rax
cmpq %rax, -0xa0(%rbp)
jle 0x8409
movl -0x30(%rbp), %eax
movb -0x29(%rbp), %cl
movb %cl, %al
movl %eax, -0x30(%rbp)
movq -0x48(%rbp), %r8
movdqa 0xf2d6(%rip), %xmm2     # 0x175e0
movq -0x90(%rbp), %rbx
jmp 0x834d
xorl %r14d, %r14d
movq -0xc8(%rbp,%r14,8), %rsi
testq %rsi, %rsi
je 0x8344
movq 0x10(%r8,%r14,8), %rdi
movq -0xf8(%rbp,%r14,8), %rdx
movl -0x34(%rbp), %ecx
callq 0x8476
movdqa 0xf2a0(%rip), %xmm2     # 0x175e0
movq -0x48(%rbp), %r8
incq %r14
cmpq $0x5, %r14
jne 0x8316
movq -0x78(%rbp), %r14
cmpq -0x58(%rbp), %r14
je 0x8413
pxor %xmm0, %xmm0
movdqa %xmm0, -0x120(%rbp)
movdqa %xmm0, -0x130(%rbp)
movdqa %xmm0, -0x140(%rbp)
movl $0x1, %eax
movq (%r8,%rax,8), %rcx
xorl %edx, %edx
movdqu 0x8(%rcx,%rdx,8), %xmm0
paddq -0x140(%rbp,%rdx,8), %xmm0
movdqa %xmm0, -0x140(%rbp,%rdx,8)
addq $0x2, %rdx
cmpq $0x6, %rdx
jne 0x8382
movq -0xd0(%rbp,%rax,8), %rcx
testq %rcx, %rcx
jle 0x83eb
movq -0x100(%rbp,%rax,8), %rdx
addq $0x8, %rdx
movq (%rdx), %rsi
movq %rsi, %rdi
shrq $0x3d, %rdi
movq -0x140(%rbp,%rdi,8), %rdi
addq %rdi, -0x8(%rdx)
addq %rsi, %rdi
andq %r15, %rdi
andq %r13, %rsi
orq %rdi, %rsi
movq %rsi, (%rdx)
addq $0x18, %rdx
decq %rcx
jne 0x83bd
incq %rax
cmpq $0x6, %rax
jne 0x837c
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq %rdx, -0x50(%rbp)
jmp 0x80b1
callq 0x23c4
jmp 0x82f3
cmpl $0x0, -0x38(%rbp)
movq -0x70(%rbp), %r14
je 0x8433
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2270
incq %rbx
cmpq $0x4, %rbx
jne 0x841f
movq -0x88(%rbp), %rdi
callq 0x2040
movq -0x60(%rbp), %rdi
callq 0x2040
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf232(%rip), %rdi      # 0x17690
leaq 0xf1b4(%rip), %rsi      # 0x17619
leaq 0xf23d(%rip), %rcx      # 0x176a9
movl $0xe6, %edx
callq 0x20e0