// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq (%rax), %rbx
movq 0x10(%rax), %r15
movq (%rdi), %rdi
callq 0x1110
leaq -0x70(%rbp), %rdi
callq 0x1ccc
leaq -0x58(%rbp), %rdi
callq 0x1ccc
movq 0x2d59(%rip), %rax      # 0x40a8
movq 0x28(%rax), %rax
movslq (%rbx), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1369
movq (%r14), %rdi
callq 0x10a0
jmp 0x1886
cmpq $0x1, %r15
ja 0x13ab
movq (%r14), %rdi
callq 0x10a0
leaq 0xc86(%rip), %rdi       # 0x2004
callq 0x1050
movl (%rbx), %esi
incl %esi
leaq 0xc8e(%rip), %rdi       # 0x201c
xorl %eax, %eax
callq 0x1070
movslq (%rbx), %rax
movq 0x2d09(%rip), %rcx      # 0x40a8
movq 0x28(%rcx), %rcx
movl %eax, (%rcx,%rax,4)
jmp 0x1886
movq %r14, -0x78(%rbp)
leaq 0x592(%rip), %rcx       # 0x1948
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1060
movl (%rbx), %esi
leaq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x1c86
movq 0x2ccd(%rip), %rax      # 0x40a8
movq 0x20(%rax), %rcx
movslq (%rbx), %rdx
movl $0x1, (%rcx,%rdx,4)
cmpq $0x0, 0x8(%r12)
je 0x14fe
movq -0x40(%rbp), %rcx
movslq 0x10(%rcx), %r14
movq (%rax), %rcx
leaq (%r14,%r14,2), %r13
cmpq $0x0, 0x10(%rcx,%r13,8)
je 0x14e3
leaq (%rcx,%r13,8), %rdx
movb $0x1, %cl
xorl %r12d, %r12d
testl %r15d, %r15d
js 0x1469
movq (%rdx), %rdx
movslq (%rdx,%r12,4), %rsi
xorl %edi, %edi
movl %r15d, %r8d
leal (%rdi,%r8), %r9d
shrl %r9d
movl (%rbx,%r9,4), %r10d
cmpl %esi, %r10d
je 0x1454
cmpl %esi, %r10d
jg 0x1447
incl %r9d
movl %r9d, %edi
jmp 0x144d
decl %r9d
movl %r9d, %r8d
cmpl %r8d, %edi
jle 0x142a
jmp 0x1469
movq 0x20(%rax), %rdi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x1469
movq 0x28(%rax), %r8
cmpl $-0x1, (%r8,%rsi,4)
je 0x147b
incq %r12
movq (%rax), %rdx
leaq (%rdx,%r13,8), %rdx
cmpq %r12, 0x10(%rdx)
ja 0x1419
jmp 0x14de
movl $0x1, (%rdi,%rsi,4)
movl (%rdx,%r12,4), %esi
leaq -0x70(%rbp), %rdi
callq 0x1d72
movq 0x2c12(%rip), %rax      # 0x40a8
movq (%rax), %rax
leaq (,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, -0x30(%rbp)
movq (%rax,%rcx), %rax
movl (%rax,%r12,4), %esi
leaq -0x40(%rbp), %rdi
callq 0x1c86
movq 0x2be7(%rip), %rax      # 0x40a8
incq %r12
movq (%rax), %rsi
movq -0x30(%rbp), %rdi
leaq (%rsi,%rdi), %rdx
xorl %ecx, %ecx
cmpq %r12, 0x10(%rsi,%rdi)
ja 0x1419
jmp 0x14f3
testb $0x1, %cl
je 0x14f3
leaq -0x40(%rbp), %rdi
callq 0x1c44
movq 0x2bb5(%rip), %rax      # 0x40a8
cmpq $0x0, -0x38(%rbp)
jne 0x13f5
movq -0x60(%rbp), %rcx
testq %rcx, %rcx
je 0x1524
movq 0x20(%rax), %rax
movq -0x70(%rbp), %rdx
xorl %esi, %esi
movslq (%rdx,%rsi,4), %rdi
movl $0x0, (%rax,%rdi,4)
incq %rsi
cmpq %rsi, %rcx
jne 0x1511
movl (%rbx), %esi
leaq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x1c86
cmpq $0x0, 0x8(%r12)
movq 0x2b69(%rip), %rax      # 0x40a8
je 0x1652
movq -0x40(%rbp), %rcx
movslq 0x10(%rcx), %r14
movq 0x8(%rax), %rcx
leaq (%r14,%r14,2), %r13
cmpq $0x0, 0x10(%rcx,%r13,8)
je 0x1637
leaq (%rcx,%r13,8), %rdx
movb $0x1, %cl
xorl %r12d, %r12d
testl %r15d, %r15d
js 0x15ba
movq (%rdx), %rdx
movslq (%rdx,%r12,4), %rsi
xorl %edi, %edi
movl %r15d, %r8d
leal (%rdi,%r8), %r9d
shrl %r9d
movl (%rbx,%r9,4), %r10d
cmpl %esi, %r10d
je 0x15a5
cmpl %esi, %r10d
jg 0x1598
incl %r9d
movl %r9d, %edi
jmp 0x159e
decl %r9d
movl %r9d, %r8d
cmpl %r8d, %edi
jle 0x157b
jmp 0x15ba
movq 0x20(%rax), %rdi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x15ba
movq 0x28(%rax), %r8
cmpl $-0x1, (%r8,%rsi,4)
je 0x15cd
incq %r12
movq 0x8(%rax), %rdx
leaq (%rdx,%r13,8), %rdx
cmpq %r12, 0x10(%rdx)
ja 0x156a
jmp 0x1632
movl $0x1, (%rdi,%rsi,4)
movl (%rdx,%r12,4), %esi
leaq -0x58(%rbp), %rdi
callq 0x1d72
movq 0x2ac0(%rip), %rax      # 0x40a8
movq 0x8(%rax), %rax
leaq (,%r14,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, -0x30(%rbp)
movq (%rax,%rcx), %rax
movl (%rax,%r12,4), %esi
leaq -0x40(%rbp), %rdi
callq 0x1c86
movq 0x2a94(%rip), %rax      # 0x40a8
incq %r12
movq 0x8(%rax), %rsi
movq -0x30(%rbp), %rdi
leaq (%rsi,%rdi), %rdx
xorl %ecx, %ecx
cmpq %r12, 0x10(%rsi,%rdi)
ja 0x156a
jmp 0x1647
testb $0x1, %cl
je 0x1647
leaq -0x40(%rbp), %rdi
callq 0x1c44
movq 0x2a61(%rip), %rax      # 0x40a8
cmpq $0x0, -0x38(%rbp)
jne 0x1545
movq 0x20(%rax), %rcx
movslq (%rbx), %rdx
movl $0x0, (%rcx,%rdx,4)
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x1683
movq -0x58(%rbp), %rsi
xorl %edi, %edi
movslq (%rsi,%rdi,4), %r8
movl $0x0, (%rcx,%r8,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x166f
movslq (%rbx), %r14
movq 0x28(%rax), %rax
movl %r14d, (%rax,%r14,4)
movl $0x48, %edi
callq 0x10b0
movq %rax, %r12
xorl %r13d, %r13d
leaq (%r12,%r13), %rdi
callq 0x1ccc
addq $0x18, %r13
cmpq $0x48, %r13
jne 0x169e
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
leaq 0x288(%rip), %r13       # 0x1948
movl $0x4, %edx
movq %r13, %rcx
callq 0x1060
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x4, %edx
movq %r13, %rcx
callq 0x1060
leaq 0x91b(%rip), %rdi       # 0x2004
callq 0x1050
movl (%rbx), %esi
incl %esi
leaq 0x927(%rip), %rdi       # 0x2020
xorl %eax, %eax
callq 0x1070
leaq 0x18(%r12), %rax
movq %rax, -0x30(%rbp)
movq %r12, %rax
addq $0x30, %rax
movq %rax, -0x80(%rbp)
movl $0x1, %r13d
movl (%rbx,%r13,4), %esi
movl -0x60(%rbp), %edi
movb $0x1, %al
movb $0x1, %cl
movl $0x0, %edx
testl %edi, %edi
js 0x1765
xorl %r8d, %r8d
movq -0x70(%rbp), %rdx
leal (%r8,%rdi), %r9d
shrl %r9d
movl (%rdx,%r9,4), %ecx
cmpl %esi, %ecx
setne %cl
je 0x1760
jg 0x1751
incl %r9d
movl %r9d, %r8d
jmp 0x1757
decl %r9d
movl %r9d, %edi
cmpl %edi, %r8d
jle 0x1735
xorl %edx, %edx
jmp 0x1765
movl $0x1, %edx
movl -0x48(%rbp), %edi
testl %edi, %edi
js 0x179a
xorl %r9d, %r9d
movq -0x58(%rbp), %r8
leal (%r9,%rdi), %r10d
shrl %r10d
movl (%r8,%r10,4), %eax
cmpl %esi, %eax
setne %al
je 0x179e
jg 0x178f
incl %r10d
movl %r10d, %r9d
jmp 0x1795
decl %r10d
movl %r10d, %edi
cmpl %edi, %r9d
jle 0x1773
xorl %edi, %edi
jmp 0x17a3
movl $0x1, %edi
testl %edx, %edi
je 0x17cc
incl %esi
leaq 0x870(%rip), %rdi       # 0x2020
xorl %eax, %eax
callq 0x1070
movq 0x28ea(%rip), %rax      # 0x40a8
movq 0x28(%rax), %rax
movslq (%rbx,%r13,4), %rcx
movl %r14d, (%rax,%rcx,4)
jmp 0x17e8
testb %cl, %cl
je 0x17da
testb %al, %al
je 0x17df
movq -0x80(%rbp), %rdi
jmp 0x17e3
movq %r12, %rdi
jmp 0x17e3
movq -0x30(%rbp), %rdi
callq 0x1d72
incq %r13
cmpq %r15, %r13
jne 0x171a
movl $0xa, %edi
callq 0x1040
movq -0x78(%rbp), %r14
movq (%r14), %rdi
callq 0x10a0
movl $0x18, %edi
callq 0x10b0
movq %rax, %rbx
movq (%r14), %r14
xorl %r15d, %r15d
movl $0x10, %edi
callq 0x10b0
movq %r14, (%rax)
movq %r12, 0x8(%rax)
movq %rax, (%rbx,%r15,8)
incq %r15
addq $0x18, %r12
cmpq $0x3, %r15
jne 0x181d
xorl %r15d, %r15d
leaq -0x53a(%rip), %r14      # 0x130f
leaq (%r15,%rbp), %rdi
addq $-0xa0, %rdi
movq (%rbx,%r15), %rcx
xorl %esi, %esi
movq %r14, %rdx
callq 0x10c0
addq $0x8, %r15
cmpq $0x18, %r15
jne 0x1849
xorl %ebx, %ebx
movq -0xa0(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x10f0
incq %rbx
cmpq $0x3, %rbx
jne 0x186e
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq