// File: /workspace/github_c_makefile_star2_O2/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 $0x118, %rsp            # imm = 0x118
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %rdx
movb (%rdi), %r12b
movl $0x0, -0x30(%rbp)
cmpb $0x2, %r12b
sete %al
movl $0x0, -0x48(%rbp)
cmpl $0x0, 0x4(%rdi)
jns 0x6a43
andl $0x0, 0x4(%rdx)
testq %r15, %r15
jle 0x6f62
cmpb $0x0, -0x1(%rbx,%r15)
jne 0x6f62
movl -0x30(%rbp), %esi
movb %al, %sil
movl %esi, -0x30(%rbp)
xorl %eax, %eax
xorl %r14d, %r14d
cmpq %rax, %r15
je 0x6a78
cmpb $0x1, (%rbx,%rax)
adcq $0x0, %r14
incq %rax
jmp 0x6a66
movl %ecx, -0x44(%rbp)
imulq $0x18, %r14, %r13
movq %r13, %rdi
movq %rdx, -0x40(%rbp)
callq 0x21a0
movq %rax, -0x78(%rbp)
movq %r13, %rdi
callq 0x21a0
movq -0x40(%rbp), %r8
leaq 0x1(%rbx), %rsi
xorl %ecx, %ecx
testq %r15, %r15
je 0x6ac3
cmpb $0x0, -0x1(%rsi)
jne 0x6abb
imulq $0x18, %rcx, %rdx
incq %rcx
movq %rbx, 0x10(%rax,%rdx)
movq %rsi, %rbx
incq %rsi
decq %r15
jmp 0x6aa1
pushq $0x1
popq %rcx
xorl %r9d, %r9d
cmpq $0x7, %rcx
je 0x6adc
movq (%r8,%rcx,8), %rdx
addq 0x8(%rdx), %r9
incq %rcx
jmp 0x6ac9
movabsq $0x1fffffffffffffff, %rbx # imm = 0x1FFFFFFFFFFFFFFF
movq %r9, %rcx
andq %rbx, %rcx
xorl %edx, %edx
movq %rax, %rsi
cmpq %rdx, %r14
je 0x6b1b
testb %r12b, %r12b
je 0x6b04
andq $0x0, (%rsi)
movq %rcx, %rdi
jmp 0x6b0e
leaq (%r9,%rdx), %rdi
andq %rbx, %rdi
movq %rdi, (%rsi)
movq %rdi, 0x8(%rsi)
incq %rdx
addq $0x18, %rsi
jmp 0x6af1
movq 0x8(%r8), %rdi
movq %r14, %rsi
movq %rax, -0x70(%rbp)
movq %rax, %rdx
movl -0x30(%rbp), %ecx
callq 0x6f81
cmpl $0x0, -0x44(%rbp)
je 0x6bde
movq %r14, %r12
pushq $0x1
popq %r14
movq %rsp, %rcx
addq $-0x20, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rdi
addq $-0xc0, %rdi
movq %rdi, %rsp
movl $0xc0, %edx
movq %rdi, -0x50(%rbp)
xorl %esi, %esi
callq 0x2100
pushq $0x1c
popq %rax
leaq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r9
movq -0x50(%rbp), %rsi
movl -0x30(%rbp), %edx
cmpq $0xdc, %rax
je 0x6ba2
movq %r9, -0xc(%rsi,%rax)
movl %r14d, -0x4(%rsi,%rax)
movl %edx, (%rsi,%rax)
movq %rcx, -0x1c(%rsi,%rax)
addq $0x30, %rax
incl %r14d
jmp 0x6b7f
xorl %r14d, %r14d
leaq 0x73e(%rip), %r13       # 0x72ea
movq -0x50(%rbp), %r15
cmpq $0x20, %r14
je 0x6bd9
movq -0x58(%rbp), %rax
leaq (%rax,%r14), %rdi
xorl %esi, %esi
movq %r13, %rdx
movq %r15, %rcx
callq 0x21f0
movq -0x40(%rbp), %r9
addq $0x8, %r14
addq $0x30, %r15
jmp 0x6bb0
movq %r12, %r14
jmp 0x6bf2
movq $0x0, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %r9
testq %r14, %r14
je 0x6f1f
pushq $0x8
popq %r10
movabsq $-0x2000000000000000, %r15 # imm = 0xE000000000000000
pushq $0x1
popq %r12
movl $0x0, -0x34(%rbp)
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %r11
xorl %ecx, %ecx
movq %r14, -0x60(%rbp)
movq %rax, -0x80(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
imulq $0x18, %rcx, %rdx
leaq (%r11,%rdx), %rax
addq $0x8, %rax
movq %rax, %rsi
movq %rcx, %rdi
cmpq %rdi, %r14
je 0x6c6d
movq (%rsi), %r8
shrq $0x3d, %r8
incq -0xc0(%rbp,%r8,8)
incq %rdi
addq $0x18, %rsi
jmp 0x6c50
addq -0x80(%rbp), %rdx
movq %rdx, -0xf0(%rbp)
pushq $0x1
popq %rsi
cmpq $0x6, %rsi
je 0x6c9a
imulq $0x18, -0xc8(%rbp,%rsi,8), %rdi
addq %rdi, %rdx
movq %rdx, -0xf0(%rbp,%rsi,8)
incq %rsi
jmp 0x6c7b
movq -0xc0(%rbp), %rdx
addq %rcx, %rdx
movq %rdx, -0x68(%rbp)
subq %rdx, %r14
jle 0x6d10
movq -0x60(%rbp), %rdx
subq %rcx, %rdx
subq $0x1, %rdx
jb 0x6cea
movq (%rax), %rcx
shrq $0x3d, %rcx
movq -0xf0(%rbp,%rcx,8), %rsi
leaq 0x18(%rsi), %rdi
movq %rdi, -0xf0(%rbp,%rcx,8)
movq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsi)
movups -0x8(%rax), %xmm0
movups %xmm0, (%rsi)
addq $0x18, %rax
jmp 0x6cb4
xorl %eax, %eax
cmpq $0x6, %rax
je 0x6d10
movq -0xc0(%rbp,%rax,8), %rcx
xorl %edx, %edx
subq %rcx, %rdx
imulq $0x18, %rdx, %rcx
addq %rcx, -0xf0(%rbp,%rax,8)
incq %rax
jmp 0x6cec
cmpl $0x0, -0x44(%rbp)
je 0x6d7c
cmpl $0x0, -0x34(%rbp)
jne 0x6d7c
andq $0x0, -0x100(%rbp)
movq $0xf4240, -0xf8(%rbp) # imm = 0xF4240
movslq 0x4(%r9), %r13
xorl %ecx, %ecx
movq -0x50(%rbp), %rdi
cmpq $0x4, %rcx
je 0x6dbe
imulq $0x30, %rcx, %rax
leaq (%rdi,%rax), %rdx
movq -0xe8(%rbp,%rcx,8), %rsi
movq %rsi, 0x28(%rdi,%rax)
movq -0xb8(%rbp,%rcx,8), %rsi
movq %rsi, 0x20(%rdi,%rax)
cmpq %r13, %r14
jg 0x6d6d
movl $0x1, 0xc(%rdx)
incq %rcx
xorl %eax, %eax
lock
cmpxchgl %r12d, 0x8(%rdx)
jne 0x6d70
jmp 0x6d39
xorl %r14d, %r14d
cmpq $0x5, %r14
je 0x6e46
movq -0xb8(%rbp,%r14,8), %rsi
testq %rsi, %rsi
je 0x6db9
movq 0x10(%r9,%r14,8), %rdi
movq -0xe8(%rbp,%r14,8), %rdx
movl -0x30(%rbp), %ecx
movq %r11, %r13
callq 0x6f81
movq %r13, %r11
pushq $0x8
popq %r10
movq -0x40(%rbp), %r9
incq %r14
jmp 0x6d7f
movq %r11, -0x88(%rbp)
movl $0x0, -0x34(%rbp)
cmpq %r13, %r14
setle %cl
movq -0x98(%rbp), %rsi
testq %rsi, %rsi
je 0x6dfb
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0xc8(%rbp), %rdx
movb %cl, -0x29(%rbp)
movl -0x30(%rbp), %ecx
callq 0x6f81
movb -0x29(%rbp), %cl
movl -0x34(%rbp), %eax
movb %cl, %al
movl %eax, -0x34(%rbp)
pushq $0x4
popq %rax
xorl %ecx, %ecx
lock
cmpxchgl %ecx, -0x48(%rbp)
je 0x6e24
leaq -0x100(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x20d0
jmp 0x6e03
movq -0x68(%rbp), %rax
cmpq %rax, -0x60(%rbp)
jle 0x6e37
cmpq %r13, %r14
jle 0x6efb
movq -0x40(%rbp), %r9
pushq $0x8
popq %r10
movq -0x88(%rbp), %r11
movq -0x60(%rbp), %r14
cmpq -0x68(%rbp), %r14
je 0x6f1f
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
pushq $0x1
popq %rax
cmpq $0x6, %rax
je 0x6eeb
movq (%r9,%rax,8), %rcx
xorl %edx, %edx
cmpq $0x6, %rdx
je 0x6e93
movq 0x8(%rcx,%rdx,8), %rsi
addq %rsi, -0x130(%rbp,%rdx,8)
incq %rdx
jmp 0x6e7b
movq -0xc0(%rbp,%rax,8), %rcx
testq %rcx, %rcx
movl $0x0, %edx
cmovleq %rdx, %rcx
movq %r10, %rdx
subq $0x1, %rcx
jb 0x6ee6
movq -0xf0(%rbp,%rax,8), %rsi
movq (%rsi,%rdx), %rdi
movq %rdi, %r8
shrq $0x3d, %r8
movq -0x130(%rbp,%r8,8), %r8
addq %r8, -0x8(%rsi,%rdx)
addq %rdi, %r8
andq %rbx, %r8
andq %r15, %rdi
orq %r8, %rdi
movq %rdi, (%rsi,%rdx)
addq $0x18, %rdx
jmp 0x6eaa
incq %rax
jmp 0x6e6f
movq %r11, %rax
movq -0x80(%rbp), %r11
movq -0x68(%rbp), %rcx
jmp 0x6c22
movq 0x140f6(%rip), %rax     # 0x1aff8
movq (%rax), %rdi
leaq 0xd7bd(%rip), %rsi      # 0x146c9
leaq 0xd804(%rip), %rdx      # 0x14717
xorl %eax, %eax
callq 0x2150
jmp 0x6e37
cmpl $0x0, -0x44(%rbp)
je 0x6f41
xorl %ebx, %ebx
cmpq $0x4, %rbx
je 0x6f41
movq -0x58(%rbp), %rax
movq (%rax,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2270
incq %rbx
jmp 0x6f27
movq -0x78(%rbp), %rdi
callq 0x2040
movq -0x70(%rbp), %rdi
callq 0x2040
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd708(%rip), %rdi      # 0x14671
leaq 0xd68a(%rip), %rsi      # 0x145fa
leaq 0xd713(%rip), %rcx      # 0x1468a
movl $0xe6, %edx
callq 0x20e0