// File: /workspace/github_cpp_makefile_star2_O2/xiangzejun[P]Optimizing_Implementations_of_Linear_Layers/reduce.cpp
// Function: reduce(std::vector<std::bitset<32ul>, std::allocator<std::bitset<32ul>>>)
vector<xpair> reduce(vector<ROW> m)
{

	vector<ROW> tmp_m(m);

	vector<xpair> seq = strgy3(m);
	int l = reduce_step(seq);


	int gap = l;
	int start = 0;

	thread_data data[THREAD_NUM];
	bool flag = true;
	while(flag)
	{
		for(int j = 0; j < THREAD_NUM; j++)
		{
			data[j].seq = seq;
			data[j].gap = gap;
			data[j].start = start;
			if((gap != 3) || (start != l - gap))
			{
				if(start == l - gap)
				{
					gap--;
					start = 0;
				}
				else
				{
					start++;
				}
			}
			else
			{
				flag = false;
			}
		}
		int rc;
		pthread_t threads[THREAD_NUM];
		pthread_attr_t attr;
		void* status;
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
		for(int j = 0; j < THREAD_NUM; j++)
		{

			rc = pthread_create(&threads[j], NULL, reduce_thread, (void *)(data + j));
			if(rc)
			{
				cout << "Error: unable to create thread!" << endl;
				exit(-1);
			}
		}
		// pthread_attr_destroy(&attr);
		for(int j = 0; j < THREAD_NUM; j++)
		{
			rc = pthread_join(threads[j], &status);
			if(rc)
			{
				cout << "Error: unable to join, " << rc << endl;
				exit(-1);
			}
		}
		for(int j = 0; j < THREAD_NUM; j++)
		{
			if(data[j].len < l)
			{
				seq = data[j].seq;
				l = data[j].len;
				gap = l;
				start = 0;
			}
		}
		// cout << "gap = " << gap << endl;
	}
	return seq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsp), %rdi
callq 0x29e4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x328b
movq %rbx, %rdi
callq 0x4ff4
movl %eax, %r14d
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
andq $0x0, 0x38(%rax)
movups %xmm0, 0x28(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
andq $0x0, 0x88(%rax)
movups %xmm0, 0x78(%rax)
movb $0x1, %al
xorl %r12d, %r12d
movl %r14d, %r13d
testb $0x1, %al
je 0x5691
leal -0x3(%r14), %ebp
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r15d, %r15d
cmpq $0xa0, %r15
je 0x55da
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
movq %rbx, %rsi
callq 0x5816
movl %r13d, 0x68(%rsp,%r15)
movl %r12d, 0x6c(%rsp,%r15)
cmpl $0x3, %r13d
jne 0x55be
cmpl %ebp, %r12d
jne 0x55be
movl $0x0, 0xc(%rsp)
pushq $0x3
popq %r13
movl %ebp, %r12d
jmp 0x55d4
movl %r14d, %eax
subl %r13d, %eax
cmpl %eax, %r12d
jne 0x55d1
decl %r13d
xorl %r12d, %r12d
jmp 0x55d4
incl %r12d
addq $0x28, %r15
jmp 0x557f
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x21a0
movq %r15, %rdi
xorl %esi, %esi
callq 0x21c0
leaq 0x50(%rsp), %rcx
xorl %edx, %edx
leaq 0x8(%rdx), %r15
cmpq $0x28, %r15
je 0x562e
leaq 0x28(%rcx), %rbp
leaq (%rsp,%rdx), %rdi
addq $0x30, %rdi
xorl %esi, %esi
leaq -0x1ee(%rip), %rdx      # 0x542c
callq 0x2290
movq %rbp, %rcx
movq %r15, %rdx
testl %eax, %eax
je 0x55fb
jmp 0x56cb
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x5654
movq 0x30(%rsp,%r15,8), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2130
movl %eax, %ebp
incq %r15
testl %eax, %eax
je 0x5631
jmp 0x56e8
xorl %r15d, %r15d
cmpq $0xa0, %r15
je 0x5688
cmpl %r14d, 0x70(%rsp,%r15)
jge 0x5682
leaq (%rsp,%r15), %rsi
addq $0x50, %rsi
movq %rbx, %rdi
callq 0x5816
movl 0x70(%rsp,%r15), %r14d
xorl %r12d, %r12d
movl %r14d, %r13d
addq $0x28, %r15
jmp 0x5657
movl 0xc(%rsp), %eax
jmp 0x556a
pushq $0x78
popq %r14
leaq (%rsp,%r14), %rdi
addq $0x50, %rdi
callq 0x2c54
addq $-0x28, %r14
cmpq $-0x28, %r14
jne 0x5695
leaq 0x18(%rsp), %rdi
callq 0x2bbe
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3906(%rip), %rdi      # 0x8fd8
leaq 0xb93(%rip), %rsi       # 0x626c
callq 0x2150
movq %rax, %rdi
callq 0x20a0
jmp 0x570d
movq 0x38e9(%rip), %rdi      # 0x8fd8
leaq 0xb96(%rip), %rsi       # 0x628c
callq 0x2150
movq %rax, %rdi
movl %ebp, %esi
callq 0x2250
movq %rax, %rdi
callq 0x20a0
pushq $-0x1
popq %rdi
callq 0x2190
movq %rax, %r14
jmp 0x5745
movq %rax, %r14
jmp 0x574d
jmp 0x5727
jmp 0x5727
jmp 0x5727
jmp 0x5727
movq %rax, %r14
pushq $0x78
popq %r15
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
callq 0x2c54
addq $-0x28, %r15
cmpq $-0x28, %r15
jne 0x572e
movq %rbx, %rdi
callq 0x2c54
leaq 0x18(%rsp), %rdi
callq 0x2bbe
movq %r14, %rdi
callq 0x2270
nop