// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, -0x48(%rbp)
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movb (%rdi), %dl
xorl %ecx, %ecx
cmpb $0x2, %dl
sete %al
movl $0x0, -0x4c(%rbp)
cmpl $0x0, 0x4(%rdi)
jns 0x7aed
movl $0x0, 0x4(%r14)
testq %rbx, %rbx
jle 0x8082
cmpb $0x0, -0x1(%r13,%rbx)
jne 0x8082
movb %dl, -0x58(%rbp)
movb %al, %cl
movl %ecx, -0x2c(%rbp)
xorl %eax, %eax
xorl %ecx, %ecx
movq %rcx, %r15
movb (%r13,%rax), %r12b
cmpb $0x1, %r12b
adcq $0x0, %rcx
incq %rax
cmpq %rax, %rbx
jne 0x7b0e
movq %rcx, -0x38(%rbp)
leaq (,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
movq %rdi, -0x40(%rbp)
callq 0x21b0
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
callq 0x21b0
leaq 0x1(%r13), %rsi
xorl %ecx, %ecx
movb -0x58(%rbp), %r9b
cmpb $0x0, -0x1(%rsi)
jne 0x7b6e
leaq (%rcx,%rcx,2), %rdx
incq %rcx
movq %r13, 0x10(%rax,%rdx,8)
movq %rsi, %r13
incq %rsi
decq %rbx
jne 0x7b59
movb %r12b, -0x40(%rbp)
movq %r15, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movl $0x1, %ecx
xorl %eax, %eax
movq (%r14,%rcx,8), %rdx
addq 0x8(%rdx), %rax
incq %rcx
cmpq $0x7, %rcx
jne 0x7b89
movabsq $-0x2000000000000000, %r15 # imm = 0xE000000000000000
movabsq $0x1fffffffffffffff, %r12 # imm = 0x1FFFFFFFFFFFFFFF
cmpq $0x0, -0x38(%rbp)
je 0x7c0f
movq %rax, %rcx
andq %r12, %rcx
movq -0x60(%rbp), %rdx
addq $0x8, %rdx
cmpb $0x1, -0x40(%rbp)
movq -0x68(%rbp), %rsi
adcq $0x0, %rsi
testb %r9b, %r9b
je 0x7bea
movq $0x0, -0x8(%rdx)
movq (%rdx), %rdi
andq %r15, %rdi
orq %rcx, %rdi
movq %rdi, (%rdx)
jmp 0x7c00
movq %rax, %rdi
andq %r12, %rdi
movq (%rdx), %r8
andq %r15, %r8
orq %rdi, %r8
movq %r8, (%rdx)
movq %rdi, -0x8(%rdx)
andq %r12, (%rdx)
addq $0x18, %rdx
incq %rax
decq %rsi
jne 0x7bcf
movq 0x8(%r14), %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x80a1
cmpl $0x0, -0x48(%rbp)
je 0x7cbd
movq %rsp, %rcx
addq $-0x20, %rcx
movq %rcx, -0x80(%rbp)
movq %rcx, %rsp
movq %rsp, %r13
addq $-0xc0, %r13
movq %r13, %rsp
movl $0xc0, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x2100
movl $0x1c, %eax
movl $0x1, %ecx
leaq -0x4c(%rbp), %rdx
movl -0x2c(%rbp), %esi
movq %r14, -0xc(%r13,%rax)
movl %ecx, -0x4(%r13,%rax)
movl %esi, (%r13,%rax)
movq %rdx, -0x1c(%r13,%rax)
addq $0x30, %rax
incl %ecx
cmpq $0xdc, %rax
jne 0x7c68
xorl %ebx, %ebx
movq %r13, -0x58(%rbp)
movq -0x80(%rbp), %rax
leaq (%rax,%rbx), %rdi
xorl %esi, %esi
leaq 0x784(%rip), %rdx       # 0x8425
movq %r13, %rcx
callq 0x2200
addq $0x30, %r13
addq $0x8, %rbx
cmpq $0x20, %rbx
jne 0x7c90
movq -0x58(%rbp), %r8
jmp 0x7cc8
movq $0x0, -0x80(%rbp)
xorl %r8d, %r8d
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x803e
cmpb $0x1, -0x40(%rbp)
adcq $0x0, -0x68(%rbp)
movl $0x1, %r13d
xorl %r10d, %r10d
movq -0x88(%rbp), %r9
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
xorl %eax, %eax
movq %r8, -0x58(%rbp)
movq %r14, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
cmpq %rcx, %rax
je 0x7d4c
movq -0x68(%rbp), %rcx
subq %rax, %rcx
leaq (%rax,%rax,2), %rdx
movq -0x70(%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 0x7d34
leaq (%rax,%rax,2), %rcx
leaq (%r9,%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 0x7d60
movq -0xd0(%rbp), %r11
addq %rax, %r11
movq -0x38(%rbp), %rdx
movq %rdx, %rbx
subq %r11, %rbx
jle 0x7e0a
cmpq %rdx, %rax
je 0x7de2
movq -0x68(%rbp), %rdx
subq %rax, %rdx
movq -0x70(%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)
movups -0x8(%rax), %xmm0
movups %xmm0, (%rsi)
addq $0x18, %rax
decq %rdx
jne 0x7daf
xorl %eax, %eax
movq -0xd0(%rbp,%rax,8), %rcx
xorl %edx, %edx
subq %rcx, %rdx
leaq (%rdx,%rdx,2), %rcx
shlq $0x3, %rcx
addq %rcx, -0x100(%rbp,%rax,8)
incq %rax
cmpq $0x6, %rax
jne 0x7de4
cmpl $0x0, -0x48(%rbp)
movq %r9, -0x40(%rbp)
movq %r11, -0x90(%rbp)
je 0x7f35
testl %r10d, %r10d
jne 0x7f35
movq $0x0, -0x130(%rbp)
movq $0xf4240, -0x128(%rbp) # imm = 0xF4240
movq -0x78(%rbp), %rax
movslq 0x4(%rax), %rdi
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rax
shlq $0x4, %rax
leaq (%r8,%rax), %rdx
movq -0xf8(%rbp,%rcx,8), %rsi
movq %rsi, 0x28(%r8,%rax)
movq -0xc8(%rbp,%rcx,8), %rsi
movq %rsi, 0x20(%r8,%rax)
cmpq %rdi, %rbx
jg 0x7e7a
movl $0x1, 0xc(%rdx)
incq %rcx
xorl %eax, %eax
lock
cmpxchgl %r13d, 0x8(%rdx)
jne 0x7e7d
cmpq $0x4, %rcx
jne 0x7e48
movq %rbx, -0xa0(%rbp)
movq %rdi, -0x98(%rbp)
cmpq %rdi, %rbx
setle -0x44(%rbp)
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x7ec5
movq -0x78(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0xd8(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x80a1
movl $0x4, %eax
xorl %ecx, %ecx
lock
cmpxchgl %ecx, -0x4c(%rbp)
leaq -0x130(%rbp), %r14
leaq -0x140(%rbp), %rbx
je 0x7efa
movq %r14, %rdi
movq %rbx, %rsi
callq 0x20d0
xorl %ecx, %ecx
movl $0x4, %eax
lock
cmpxchgl %ecx, -0x4c(%rbp)
jne 0x7ee1
movq -0x38(%rbp), %rcx
movq -0x90(%rbp), %rbx
cmpq %rbx, %rcx
jle 0x7f1e
movq -0x98(%rbp), %rax
cmpq %rax, -0xa0(%rbp)
jle 0x8030
xorl %r10d, %r10d
movb -0x44(%rbp), %al
movb %al, %r10b
movq -0x78(%rbp), %r14
movq -0x58(%rbp), %r8
movq -0x40(%rbp), %r9
jmp 0x7f81
movl %r10d, -0x44(%rbp)
xorl %ebx, %ebx
movq -0x78(%rbp), %r14
movq -0xc8(%rbp,%rbx,8), %rsi
testq %rsi, %rsi
je 0x7f65
movq 0x10(%r14,%rbx,8), %rdi
movq -0xf8(%rbp,%rbx,8), %rdx
movl -0x2c(%rbp), %ecx
callq 0x80a1
movq -0x58(%rbp), %r8
incq %rbx
cmpq $0x5, %rbx
jne 0x7f3f
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r9
movl -0x44(%rbp), %r10d
movq -0x90(%rbp), %rbx
cmpq %rbx, %rcx
je 0x8022
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movl $0x1, %eax
movq (%r14,%rax,8), %rcx
xorl %edx, %edx
movq 0x8(%rcx,%rdx,8), %rsi
addq %rsi, -0x130(%rbp,%rdx,8)
incq %rdx
cmpq $0x6, %rdx
jne 0x7fad
movq -0xd0(%rbp,%rax,8), %rcx
testq %rcx, %rcx
jle 0x800a
movq -0x100(%rbp,%rax,8), %rdx
addq $0x8, %rdx
movq (%rdx), %rsi
movq %rsi, %rdi
shrq $0x3d, %rdi
movq -0x130(%rbp,%rdi,8), %rdi
addq %rdi, -0x8(%rdx)
addq %rsi, %rdi
andq %r12, %rdi
andq %r15, %rsi
orq %rdi, %rsi
movq %rsi, (%rdx)
addq $0x18, %rdx
decq %rcx
jne 0x7fdc
incq %rax
cmpq $0x6, %rax
jne 0x7fa7
movq -0x70(%rbp), %rax
movq %r9, -0x70(%rbp)
movq %rax, %r9
movq -0x38(%rbp), %rcx
movq %rbx, %rax
cmpq %rbx, %rcx
jne 0x7d00
jmp 0x803e
callq 0x23d4
movq -0x38(%rbp), %rcx
jmp 0x7f1e
cmpl $0x0, -0x48(%rbp)
movq -0x80(%rbp), %r14
je 0x805e
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
xorl %esi, %esi
callq 0x2280
incq %rbx
cmpq $0x4, %rbx
jne 0x804a
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 0xf5ec(%rip), %rdi      # 0x17675
leaq 0xf56e(%rip), %rsi      # 0x175fe
leaq 0xf5f7(%rip), %rcx      # 0x1768e
movl $0xe6, %edx
callq 0x20e0