// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq (%rax), %rbx
movq 0x10(%rax), %r15
movq (%rdi), %rdi
callq 0x1100
leaq -0x68(%rbp), %rdi
callq 0x1d48
leaq -0x50(%rbp), %rdi
callq 0x1d48
movq 0x2d52(%rip), %rax      # 0x40a0
movq 0x28(%rax), %rax
movslq (%rbx), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1368
movq (%r14), %rdi
callq 0x1090
jmp 0x18a2
cmpq $0x1, %r15
ja 0x13aa
movq (%r14), %rdi
callq 0x1090
leaq 0xc87(%rip), %rdi       # 0x2004
callq 0x1050
movl (%rbx), %esi
incl %esi
leaq 0xc8f(%rip), %rdi       # 0x201c
xorl %eax, %eax
callq 0x1070
movslq (%rbx), %rax
movq 0x2d02(%rip), %rcx      # 0x40a0
movq 0x28(%rcx), %rcx
movl %eax, (%rcx,%rax,4)
jmp 0x18a2
movq %r14, -0x70(%rbp)
leaq 0x5af(%rip), %rcx       # 0x1964
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1060
movl (%rbx), %esi
leaq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x1d02
movq 0x2cc6(%rip), %rax      # 0x40a0
movq 0x20(%rax), %rax
movslq (%rbx), %rcx
movl $0x1, (%rax,%rcx,4)
cmpq $0x0, 0x8(%r12)
je 0x14e9
movq -0x38(%rbp), %rax
movslq 0x10(%rax), %rdx
movq 0x2c9d(%rip), %rcx      # 0x40a0
movq (%rcx), %rax
leaq (%rdx,%rdx,2), %r14
cmpq $0x0, 0x10(%rax,%r14,8)
je 0x14d5
leaq (%rax,%r14,8), %rdx
xorl %r13d, %r13d
xorl %eax, %eax
movq (%rdx), %rdx
movslq (%rdx,%r13,4), %rsi
xorl %r10d, %r10d
movl %r15d, %r11d
cmpl %r11d, %r10d
jg 0x14b6
movl %r11d, %edi
movl %r10d, %r8d
leal (%rdi,%r8), %r12d
shrl %r12d
movl (%rbx,%r12,4), %r9d
cmpl %esi, %r9d
je 0x1464
cmpl %esi, %r9d
jg 0x145b
incl %r12d
movl %r12d, %r10d
movl %edi, %r11d
jmp 0x1464
decl %r12d
movl %r8d, %r10d
movl %r12d, %r11d
cmpl %esi, %r9d
jne 0x142c
cmpl %edi, %r8d
jg 0x14b6
movq 0x20(%rcx), %rdi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x14b6
movq 0x28(%rcx), %rcx
cmpl $-0x1, (%rcx,%rsi,4)
jne 0x14b6
movl $0x1, (%rdi,%rsi,4)
movl (%rdx,%r13,4), %esi
leaq -0x68(%rbp), %rdi
callq 0x1dee
movq 0x2c03(%rip), %rax      # 0x40a0
movq (%rax), %rax
movq (%rax,%r14,8), %rax
movl (%rax,%r13,4), %esi
leaq -0x38(%rbp), %rdi
callq 0x1d02
movl $0x1, %eax
incq %r13
movq 0x2be0(%rip), %rcx      # 0x40a0
movq (%rcx), %rdx
leaq (%rdx,%r14,8), %rdx
cmpq %r13, 0x10(%rdx)
ja 0x141f
testl %eax, %eax
jne 0x14de
leaq -0x38(%rbp), %rdi
callq 0x1cc0
cmpq $0x0, -0x30(%rbp)
jne 0x13f4
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x1516
movq 0x2ba7(%rip), %rcx      # 0x40a0
movq 0x20(%rcx), %rcx
movq -0x68(%rbp), %rdx
xorl %esi, %esi
movslq (%rdx,%rsi,4), %rdi
movl $0x0, (%rcx,%rdi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x1503
movl (%rbx), %esi
leaq -0x38(%rbp), %r12
movq %r12, %rdi
callq 0x1d02
cmpq $0x0, 0x8(%r12)
je 0x1628
movq -0x38(%rbp), %rax
movslq 0x10(%rax), %rdx
movq 0x2b61(%rip), %rcx      # 0x40a0
movq 0x8(%rcx), %rax
leaq (%rdx,%rdx,2), %r14
cmpq $0x0, 0x10(%rax,%r14,8)
je 0x1614
leaq (%rax,%r14,8), %rdx
xorl %r13d, %r13d
xorl %eax, %eax
movq (%rdx), %rdx
movslq (%rdx,%r13,4), %rsi
xorl %r10d, %r10d
movl %r15d, %r11d
cmpl %r11d, %r10d
jg 0x15f4
movl %r11d, %edi
movl %r10d, %r8d
leal (%rdi,%r8), %r12d
shrl %r12d
movl (%rbx,%r12,4), %r9d
cmpl %esi, %r9d
je 0x15a1
cmpl %esi, %r9d
jg 0x1598
incl %r12d
movl %r12d, %r10d
movl %edi, %r11d
jmp 0x15a1
decl %r12d
movl %r8d, %r10d
movl %r12d, %r11d
cmpl %esi, %r9d
jne 0x1569
cmpl %edi, %r8d
jg 0x15f4
movq 0x20(%rcx), %rdi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x15f4
movq 0x28(%rcx), %rcx
cmpl $-0x1, (%rcx,%rsi,4)
jne 0x15f4
movl $0x1, (%rdi,%rsi,4)
movl (%rdx,%r13,4), %esi
leaq -0x50(%rbp), %rdi
callq 0x1dee
movq 0x2ac6(%rip), %rax      # 0x40a0
movq 0x8(%rax), %rax
movq (%rax,%r14,8), %rax
movl (%rax,%r13,4), %esi
leaq -0x38(%rbp), %rdi
callq 0x1d02
movl $0x1, %eax
incq %r13
movq 0x2aa2(%rip), %rcx      # 0x40a0
movq 0x8(%rcx), %rdx
leaq (%rdx,%r14,8), %rdx
cmpq %r13, 0x10(%rdx)
ja 0x155c
testl %eax, %eax
jne 0x161d
leaq -0x38(%rbp), %rdi
callq 0x1cc0
cmpq $0x0, -0x30(%rbp)
jne 0x1530
movq 0x2a71(%rip), %rax      # 0x40a0
movq 0x20(%rax), %rcx
movslq (%rbx), %rdx
movl $0x0, (%rcx,%rdx,4)
movq -0x40(%rbp), %rdx
testq %rdx, %rdx
je 0x1660
movq -0x50(%rbp), %rsi
xorl %edi, %edi
movslq (%rsi,%rdi,4), %r8
movl $0x0, (%rcx,%r8,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x164c
movslq (%rbx), %rcx
movq 0x28(%rax), %rax
movq %rcx, -0x88(%rbp)
movl %ecx, (%rax,%rcx,4)
movl $0x48, %edi
callq 0x10a0
movq %rax, %r12
xorl %r14d, %r14d
leaq (%r12,%r14), %rdi
callq 0x1d48
addq $0x18, %r14
cmpq $0x48, %r14
jne 0x1681
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq 0x2c1(%rip), %r13       # 0x1964
movl $0x4, %edx
movq %r13, %rcx
callq 0x1060
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x4, %edx
movq %r13, %rcx
callq 0x1060
leaq 0x938(%rip), %rdi       # 0x2004
callq 0x1050
movl (%rbx), %esi
incl %esi
leaq 0x944(%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)
cmpq $0x3, %r15
movl $0x2, %eax
cmovaeq %r15, %rax
movq %rax, -0x90(%rbp)
movl $0x1, %r15d
movl (%rbx,%r15,4), %esi
movl -0x58(%rbp), %r10d
movq -0x68(%rbp), %rdi
xorl %r9d, %r9d
movl %r10d, %eax
movl %r9d, %ecx
xorl %edx, %edx
cmpl %r10d, %r9d
jg 0x1765
leal (%rax,%rcx), %r11d
shrl %r11d
movl (%rdi,%r11,4), %r8d
movl %ecx, %r9d
movl %eax, %r10d
cmpl %esi, %r8d
je 0x1759
jg 0x1750
incl %r11d
movl %r11d, %r9d
movl %eax, %r10d
jmp 0x1759
decl %r11d
movl %ecx, %r9d
movl %r11d, %r10d
cmpl %esi, %r8d
jne 0x1720
movl $0x1, %edi
jmp 0x1767
xorl %edi, %edi
movl -0x40(%rbp), %r14d
movq -0x50(%rbp), %r10
movl %r14d, %r8d
movl %edx, %r9d
cmpl %r14d, %edx
jg 0x17b2
leal (%r8,%r9), %r13d
shrl %r13d
movl (%r10,%r13,4), %r11d
movl %r9d, %edx
movl %r8d, %r14d
cmpl %esi, %r11d
je 0x17a6
jg 0x179d
incl %r13d
movl %r13d, %edx
movl %r8d, %r14d
jmp 0x17a6
decl %r13d
movl %r9d, %edx
movl %r13d, %r14d
cmpl %esi, %r11d
jne 0x176f
movl $0x1, %edx
jmp 0x17b4
xorl %edx, %edx
testl %edi, %edx
je 0x17e3
incl %esi
leaq 0x85f(%rip), %rdi       # 0x2020
xorl %eax, %eax
callq 0x1070
movq 0x28d1(%rip), %rax      # 0x40a0
movq 0x28(%rax), %rax
movslq (%rbx,%r15,4), %rcx
movq -0x88(%rbp), %rdx
movl %edx, (%rax,%rcx,4)
jmp 0x1800
cmpl %eax, %ecx
jle 0x17f2
cmpl %r8d, %r9d
jle 0x17f7
movq -0x78(%rbp), %rdi
jmp 0x17fb
movq %r12, %rdi
jmp 0x17fb
movq -0x80(%rbp), %rdi
callq 0x1dee
incq %r15
cmpq -0x90(%rbp), %r15
jne 0x1711
movl $0xa, %edi
callq 0x1040
movq -0x70(%rbp), %r14
movq (%r14), %rdi
callq 0x1090
movl $0x18, %edi
callq 0x10a0
movq %rax, %rbx
movq (%r14), %r14
xorl %r15d, %r15d
movl $0x10, %edi
callq 0x10a0
movq %r14, (%rax)
movq %r12, 0x8(%rax)
movq %rax, (%rbx,%r15,8)
incq %r15
addq $0x18, %r12
cmpq $0x3, %r15
jne 0x1839
xorl %r15d, %r15d
leaq -0x55a(%rip), %r14      # 0x130b
leaq (%r15,%rbp), %rdi
addq $-0xb0, %rdi
movq (%rbx,%r15), %rcx
xorl %esi, %esi
movq %r14, %rdx
callq 0x10b0
addq $0x8, %r15
cmpq $0x18, %r15
jne 0x1865
xorl %ebx, %ebx
movq -0xb0(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x10e0
incq %rbx
cmpq $0x3, %rbx
jne 0x188a
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq