// File: /workspace/github_c_makefile_star2_O2/jalalium[P]graph_algorithms_task/src_scc_para/main_scc_para.c
// Function: kernel
void	*kernel(void *res)
{
	pthread_t tr[3];
	t_resource  *all = (t_resource*)res;
	t_vect list = *(all->list);
	t_vect dir_reach;
	t_vect ind_reach;
	t_stack	stk;
	int mark;

	pthread_mutex_lock(all->mtx);
	//inits the forward and backward reach vectors
	init_vector(&dir_reach);
	init_vector(&ind_reach);
	//In order to avoid redoing the same vertex twice
	if (G->scc[list.tab[0]] != -1)
	{
		pthread_mutex_unlock(all->mtx);
		return NULL;
	}
	//Case of single vertex SCC
	if (list.used < 2)
	{
		pthread_mutex_unlock(all->mtx);
		puts("New connected component");
		printf("%d\n", list.tab[0] + 1);
		G->scc[list.tab[0]] = list.tab[0];
		return (NULL);
	}
	qsort(list.tab, list.used,sizeof(int), cmp_int);
	push(&stk, list.tab[0]);
	G->vis[list.tab[0]] = 1;
	//forward reach of the pivot
	while (stk.size > 0)
	{
		int top = stk.top->val;
		int tag = 0;
		for (int i = 0; i < G->adj[top].used; i++)
		{
			if (find(&list, G->adj[top].tab[i]) && G->vis[G->adj[top].tab[i]] == 0 && G->scc[G->adj[top].tab[i]] == -1)
			{
				tag = 1;
				G->vis[G->adj[top].tab[i]] = 1;
				push_back(&dir_reach, G->adj[top].tab[i]);
				push(&stk, G->adj[top].tab[i]);
			}
		}
		if (!tag)
			pop(&stk);
	}
	//to avoid recreating a visited array
	for (int i = 0; i < dir_reach.used; i++)
		G->vis[dir_reach.tab[i]] = 0;
	push(&stk, list.tab[0]);
	//Backward reach of the pivot
	while (stk.size > 0)
	{
		int top = stk.top->val;
		int tag = 0;
		for (int i = 0; i < G->rev_adj[top].used; i++)
			if (find(&list, G->rev_adj[top].tab[i]) && G->vis[G->rev_adj[top].tab[i]] == 0 && G->scc[G->rev_adj[top].tab[i]] == -1)
			{
				tag =1;
				G->vis[G->rev_adj[top].tab[i]] = 1;
				push_back(&ind_reach, G->rev_adj[top].tab[i]);
				push(&stk, G->rev_adj[top].tab[i]);
			}
		if (!tag)
		pop(&stk);
	}
	//to avoid recreating a visited array
	G->vis[list.tab[0]] = 0;
	for (int i = 0; i < ind_reach.used; i++)
		G->vis[ind_reach.tab[i]] = 0;
	mark = list.tab[0];
	G->scc[list.tab[0]] = mark;
	t_vect *child = (t_vect*)malloc(sizeof(t_vect)*3);
	for (int i = 0; i < 3; i++)
		init_vector(&child[i]);
	//sort of forward and backward reach in order to search in log(n) with binary search
	qsort(dir_reach.tab, dir_reach.used,sizeof(int), cmp_int);
	qsort(ind_reach.tab, ind_reach.used,sizeof(int), cmp_int);
	puts("New connected component");
	printf("%d ", list.tab[0] + 1);
	for (int i = 1; i < list.used; i++)
	{
		//checks if vertex is in forward reach
		int dir = find(&dir_reach, list.tab[i]);
		//checks if vertex is in backward reach
		int ind = find(&ind_reach, list.tab[i]);
		//Case 1: in Scc so printed
		if (dir&ind)
		{
			printf("%d ", list.tab[i] + 1);
			G->scc[list.tab[i]] = mark;
		}
		//Case 2: Only if forward reach -> group 1
		else if (dir)
			push_back(&child[0], list.tab[i]);
		//Case 3: Only if backward reach -> group 2
		else if (ind)
			push_back(&child[1], list.tab[i]);
		//Case 4: not reachable -> group 3
		else
			push_back(&child[2], list.tab[i]);	
	}
	puts("");
	pthread_mutex_unlock(all->mtx);
	//recursive parallel call to the 3 subproblems
	t_resource **out = (t_resource**)malloc(sizeof(t_resource*)*3);
	for (int i = 0; i < 3; i++)
		out[i] = init_resource(all->mtx, &child[i]);
	for (int i = 0; i < 3; i++)
		pthread_create(&tr[i], NULL, kernel, out[i]);
	for (int i = 0; i < 3; i++)
		pthread_join(tr[i], NULL);
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x60(%rbp)
movups (%rax), %xmm0
movaps %xmm0, -0x70(%rbp)
movq (%rdi), %rdi
callq 0x1110
leaq -0xb0(%rbp), %rdi
callq 0x1ba8
leaq -0x98(%rbp), %rdi
callq 0x1ba8
movq 0x2d50(%rip), %rax      # 0x40a8
movq 0x28(%rax), %rax
movq -0x70(%rbp), %r14
movslq (%r14), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1373
movq (%rbx), %rdi
callq 0x10a0
jmp 0x13b5
movq -0x60(%rbp), %rsi
cmpq $0x1, %rsi
ja 0x13c9
movq (%rbx), %rdi
callq 0x10a0
leaq 0xc78(%rip), %rdi       # 0x2004
callq 0x1050
movl (%r14), %esi
incl %esi
leaq 0xc7f(%rip), %rdi       # 0x201c
xorl %eax, %eax
callq 0x1070
movslq (%r14), %rax
movq 0x2cfa(%rip), %rcx      # 0x40a8
movq 0x28(%rcx), %rcx
movl %eax, (%rcx,%rax,4)
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x49c(%rip), %rcx       # 0x186c
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rsi, -0x58(%rbp)
callq 0x1060
movl (%r14), %esi
leaq -0x48(%rbp), %rdi
callq 0x1b67
movq 0x2cb6(%rip), %rax      # 0x40a8
movq 0x20(%rax), %rax
movslq (%r14), %rcx
movl $0x1, (%rax,%rcx,4)
movq %rbx, -0x38(%rbp)
movq %r14, -0x50(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x14cb
movq -0x48(%rbp), %rax
movslq 0x10(%rax), %rax
xorl %r12d, %r12d
movq 0x2c83(%rip), %r13      # 0x40a8
imulq $0x18, %rax, %r14
movl $0x0, -0x30(%rbp)
movq (%r13), %rax
cmpq %r12, 0x10(%rax,%r14)
jbe 0x14af
addq %r14, %rax
movq (%rax), %r15
movl (%r15,%r12,4), %ebx
leaq -0x70(%rbp), %rdi
movl %ebx, %esi
callq 0x12c8
testl %eax, %eax
je 0x14aa
movslq %ebx, %rax
movq 0x20(%r13), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x14aa
movq 0x28(%r13), %rdx
cmpl $-0x1, (%rdx,%rax,4)
jne 0x14aa
movl $0x1, (%rcx,%rax,4)
movl (%r15,%r12,4), %esi
leaq -0xb0(%rbp), %rdi
callq 0x1c4c
movq 0x2c1f(%rip), %rax      # 0x40a8
movq (%rax), %rax
movq (%rax,%r14), %rax
movl (%rax,%r12,4), %esi
leaq -0x48(%rbp), %rdi
callq 0x1b67
movq 0x2c04(%rip), %r13      # 0x40a8
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
incq %r12
jmp 0x1430
cmpl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %r14
jne 0x1408
leaq -0x48(%rbp), %rdi
callq 0x1b28
jmp 0x1408
movq -0xb0(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq 0x2bc8(%rip), %rdx      # 0x40a8
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x14f9
movq 0x20(%rdx), %rdi
movslq (%rax,%rsi,4), %r8
andl $0x0, (%rdi,%r8,4)
incq %rsi
jmp 0x14e2
movl (%r14), %esi
leaq -0x48(%rbp), %rdi
callq 0x1b67
cmpq $0x0, -0x40(%rbp)
je 0x15c9
movq -0x48(%rbp), %rax
movslq 0x10(%rax), %rax
xorl %r12d, %r12d
movq 0x2b86(%rip), %r13      # 0x40a8
imulq $0x18, %rax, %r14
movl $0x0, -0x30(%rbp)
movq 0x8(%r13), %rax
cmpq %r12, 0x10(%rax,%r14)
jbe 0x15ad
addq %r14, %rax
movq (%rax), %r15
movl (%r15,%r12,4), %ebx
leaq -0x70(%rbp), %rdi
movl %ebx, %esi
callq 0x12c8
testl %eax, %eax
je 0x15a8
movslq %ebx, %rax
movq 0x20(%r13), %rcx
cmpl $0x0, (%rcx,%rax,4)
jne 0x15a8
movq 0x28(%r13), %rdx
cmpl $-0x1, (%rdx,%rax,4)
jne 0x15a8
movl $0x1, (%rcx,%rax,4)
movl (%r15,%r12,4), %esi
leaq -0x98(%rbp), %rdi
callq 0x1c4c
movq 0x2b22(%rip), %rax      # 0x40a8
movq 0x8(%rax), %rax
movq (%rax,%r14), %rax
movl (%rax,%r12,4), %esi
leaq -0x48(%rbp), %rdi
callq 0x1b67
movq 0x2b06(%rip), %r13      # 0x40a8
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
incq %r12
jmp 0x152d
cmpl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %r14
jne 0x1505
leaq -0x48(%rbp), %rdi
callq 0x1b28
jmp 0x1505
movq 0x2ad8(%rip), %rax      # 0x40a8
movq 0x20(%rax), %rcx
movslq (%r14), %rdx
andl $0x0, (%rcx,%rdx,4)
movq -0x98(%rbp), %rdx
movq -0x88(%rbp), %rsi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x15fe
movslq (%rdx,%rdi,4), %r8
andl $0x0, (%rcx,%r8,4)
incq %rdi
jmp 0x15eb
movslq (%r14), %rcx
movq 0x28(%rax), %rax
movq %rcx, -0x30(%rbp)
movl %ecx, (%rax,%rcx,4)
pushq $0x48
popq %rdi
callq 0x10b0
movq %rax, %r12
xorl %ebx, %ebx
cmpq $0x48, %rbx
je 0x162e
leaq (%r12,%rbx), %rdi
callq 0x1ba8
addq $0x18, %rbx
jmp 0x1619
leaq -0xb0(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rax), %rsi
leaq 0x229(%rip), %rbx       # 0x186c
pushq $0x4
popq %r13
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1060
leaq -0x98(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rax), %rsi
movq %r13, %rdx
movq -0x38(%rbp), %r13
movq %rbx, %rcx
callq 0x1060
leaq 0x98e(%rip), %rdi       # 0x2004
callq 0x1050
movl (%r14), %esi
incl %esi
leaq 0x999(%rip), %rdi       # 0x2020
xorl %eax, %eax
callq 0x1070
leaq 0x18(%r12), %rax
movq %rax, -0x80(%rbp)
movq %r12, %rax
addq $0x30, %rax
movq %rax, -0x78(%rbp)
pushq $0x1
popq %r14
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %r15
cmpq %r14, %rax
je 0x1740
movl (%r15,%r14,4), %ebx
leaq -0xb0(%rbp), %rdi
movl %ebx, %esi
callq 0x12c8
movl %eax, %r13d
leaq -0x98(%rbp), %rdi
movl %ebx, %esi
callq 0x12c8
testl %r13d, %eax
je 0x1709
incl %ebx
leaq 0x938(%rip), %rdi       # 0x2020
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
movq 0x29b0(%rip), %rax      # 0x40a8
movq 0x28(%rax), %rax
movslq (%r15,%r14,4), %rcx
movq -0x30(%rbp), %rdx
movl %edx, (%rax,%rcx,4)
jmp 0x1718
testl %r13d, %r13d
je 0x1725
movq %r12, %rdi
movl %ebx, %esi
callq 0x1c4c
movq -0x38(%rbp), %r13
incq %r14
movq -0x58(%rbp), %rax
jmp 0x16ae
testl %eax, %eax
movq -0x38(%rbp), %r13
je 0x1733
movq -0x80(%rbp), %rdi
jmp 0x1737
movq -0x78(%rbp), %rdi
movl %ebx, %esi
callq 0x1c4c
jmp 0x171c
pushq $0xa
popq %rdi
callq 0x1040
movq (%r13), %rdi
callq 0x10a0
pushq $0x18
popq %rdi
callq 0x10b0
movq %rax, %r14
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x177d
movq (%r13), %rdi
movq %r12, %rsi
callq 0x12a7
movq %rax, (%r14,%rbx,8)
incq %rbx
addq $0x18, %r12
jmp 0x175e
xorl %r15d, %r15d
leaq -0x480(%rip), %rbx      # 0x1307
cmpq $0x18, %r15
je 0x17ac
leaq (%r15,%rbp), %rdi
addq $-0xd0, %rdi
movq (%r14,%r15), %rcx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10c0
addq $0x8, %r15
jmp 0x1787
xorl %ebx, %ebx
cmpq $0x3, %rbx
je 0x13b5
movq -0xd0(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x10f0
incq %rbx
jmp 0x17ae