// File: /workspace/github_cpp_makefile_star2_O3/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 0x10(%rsp), %rdi
callq 0x2ea0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3d0c
movq %rbx, %rdi
callq 0x61d8
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movaps %xmm0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x10(%rax)
movups %xmm0, 0x28(%rax)
movq %r14, 0x38(%rax)
movaps %xmm0, 0x50(%rax)
movq %r14, 0x60(%rax)
movups %xmm0, 0x78(%rax)
movq %r14, 0x88(%rax)
movl %ebp, %r15d
leal -0x3(%r15), %r13d
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %r12d, %r12d
leaq (%rsp,%r12), %rdi
addq $0x30, %rdi
movq %rbx, %rsi
callq 0x6c0a
movl %ebp, 0x48(%rsp,%r12)
movl %r14d, 0x4c(%rsp,%r12)
cmpl $0x3, %ebp
jne 0x6969
cmpl %r13d, %r14d
jne 0x6969
movl $0x0, 0xc(%rsp)
movl $0x3, %ebp
movl %r13d, %r14d
jmp 0x697d
movl %r15d, %eax
subl %ebp, %eax
cmpl %eax, %r14d
jne 0x697a
decl %ebp
xorl %r14d, %r14d
jmp 0x697d
incl %r14d
addq $0x28, %r12
cmpq $0xa0, %r12
jne 0x6933
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
callq 0x21b0
movq %r12, %rdi
xorl %esi, %esi
callq 0x21d0
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
leaq (%rsp,%r12), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
leaq -0x206(%rip), %rdx      # 0x67ba
movq %r13, %rcx
callq 0x2290
testl %eax, %eax
jne 0x6a97
addq $0x28, %r13
addq $0x8, %r12
cmpq $0x20, %r12
jne 0x69ac
xorl %r12d, %r12d
movq 0xd0(%rsp,%r12,8), %rdi
leaq 0x28(%rsp), %rsi
callq 0x2130
movl %eax, %r13d
testl %eax, %eax
jne 0x6abd
incq %r12
cmpq $0x4, %r12
jne 0x69e1
xorl %r12d, %r12d
cmpl %r15d, 0x50(%rsp,%r12)
jge 0x6a2c
leaq (%rsp,%r12), %rsi
addq $0x30, %rsi
movq %rbx, %rdi
callq 0x6c0a
movl 0x50(%rsp,%r12), %r15d
xorl %r14d, %r14d
movl %r15d, %ebp
addq $0x28, %r12
cmpq $0xa0, %r12
jne 0x6a0a
testb $0x1, 0xc(%rsp)
jne 0x6926
movl $0x78, %r14d
movq 0x30(%rsp,%r14), %rdi
testq %rdi, %rdi
je 0x6a61
movq 0x40(%rsp,%r14), %rsi
subq %rdi, %rsi
callq 0x2170
addq $-0x28, %r14
cmpq $-0x28, %r14
jne 0x6a4a
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6a82
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x353a(%rip), %rdi      # 0x9fd8
leaq 0x897(%rip), %rsi       # 0x733c
movl $0x1f, %edx
callq 0x2180
movq 0x3522(%rip), %rdi      # 0x9fd8
callq 0x2090
jmp 0x6aec
movq 0x3514(%rip), %rdi      # 0x9fd8
leaq 0x891(%rip), %rsi       # 0x735c
movl $0x17, %edx
callq 0x2180
movq 0x34fc(%rip), %rdi      # 0x9fd8
movl %r13d, %esi
callq 0x2260
movq %rax, %rdi
callq 0x2090
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21a0
movq %rax, %r14
jmp 0x6b32
movq %rax, %r14
jmp 0x6b46
jmp 0x6b08
jmp 0x6b08
jmp 0x6b08
jmp 0x6b08
movq %rax, %r14
movl $0x78, %r15d
movq 0x30(%rsp,%r15), %rdi
testq %rdi, %rdi
je 0x6b28
movq 0x40(%rsp,%r15), %rsi
subq %rdi, %rsi
callq 0x2170
addq $-0x28, %r15
cmpq $-0x28, %r15
jne 0x6b11
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x6b46
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6b5d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %r14, %rdi
callq 0x2270
nop