// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x10f0
leaq -0x68(%rbp), %rdi
callq 0x22a0
leaq -0x80(%rbp), %rdi
callq 0x22a0
movq 0x3c7d(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x1445
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x1080
movq $0x0, -0x8(%rbp)
jmp 0x1c74
cmpq $0x2, -0x40(%rbp)
jae 0x14a3
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x1080
leaq 0x1ba5(%rip), %rdi      # 0x3004
callq 0x1040
movq -0x50(%rbp), %rax
movl (%rax), %esi
addl $0x1, %esi
leaq 0x1ba8(%rip), %rdi      # 0x301c
movb $0x0, %al
callq 0x1060
movq -0x50(%rbp), %rax
movl (%rax), %edx
movq 0x3c10(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movq $0x0, -0x8(%rbp)
jmp 0x1c74
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x4, %edx
leaq 0x899(%rip), %rcx       # 0x1d50
callq 0x1050
movq -0x50(%rbp), %rax
movl (%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0x2210
movq 0x3bc3(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movl $0x1, (%rax,%rcx,4)
cmpq $0x0, -0x88(%rbp)
jbe 0x16c0
movq -0x90(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movslq -0xa0(%rbp), %rax
movq 0x3b71(%rip), %rcx      # 0x5098
movq (%rcx), %rcx
movslq -0x98(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x10(%rcx), %rax
jae 0x16a6
movq 0x3b4f(%rip), %rax      # 0x5098
movq (%rax), %rax
movslq -0x98(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x50(%rbp), %rdi
callq 0x1330
cmpl $0x0, %eax
je 0x1690
movq 0x3b18(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq 0x3b0d(%rip), %rcx      # 0x5098
movq (%rcx), %rcx
movslq -0x98(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xa0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x1690
movq 0x3add(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq 0x3ad2(%rip), %rcx      # 0x5098
movq (%rcx), %rcx
movslq -0x98(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xa0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x1690
movl $0x1, -0x9c(%rbp)
movq 0x3a98(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq 0x3a8d(%rip), %rcx      # 0x5098
movq (%rcx), %rcx
movslq -0x98(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xa0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x1, (%rax,%rcx,4)
movq 0x3a60(%rip), %rax      # 0x5098
movq (%rax), %rax
movslq -0x98(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x68(%rbp), %rdi
callq 0x23d0
movq 0x3a32(%rip), %rax      # 0x5098
movq (%rax), %rax
movslq -0x98(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x90(%rbp), %rdi
callq 0x2210
jmp 0x1692
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x1519
cmpl $0x0, -0x9c(%rbp)
jne 0x16bb
leaq -0x90(%rbp), %rdi
callq 0x2180
jmp 0x14e7
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x1709
movq 0x39ba(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq -0x68(%rbp), %rcx
movslq -0xa4(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x16ca
movq -0x50(%rbp), %rax
movl (%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0x88(%rbp)
jbe 0x18fb
movq -0x90(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movslq -0xb0(%rbp), %rax
movq 0x393d(%rip), %rcx      # 0x5098
movq 0x8(%rcx), %rcx
movslq -0xa8(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x10(%rcx), %rax
jae 0x18e1
movq 0x391a(%rip), %rax      # 0x5098
movq 0x8(%rax), %rax
movslq -0xa8(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xb0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x50(%rbp), %rdi
callq 0x1330
cmpl $0x0, %eax
je 0x18cb
movq 0x38e2(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq 0x38d7(%rip), %rcx      # 0x5098
movq 0x8(%rcx), %rcx
movslq -0xa8(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xb0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x18cb
movq 0x38a6(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq 0x389b(%rip), %rcx      # 0x5098
movq 0x8(%rcx), %rcx
movslq -0xa8(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xb0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x18cb
movl $0x1, -0xac(%rbp)
movq 0x3860(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq 0x3855(%rip), %rcx      # 0x5098
movq 0x8(%rcx), %rcx
movslq -0xa8(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0xb0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x1, (%rax,%rcx,4)
movq 0x3827(%rip), %rax      # 0x5098
movq 0x8(%rax), %rax
movslq -0xa8(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xb0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x80(%rbp), %rdi
callq 0x23d0
movq 0x37f8(%rip), %rax      # 0x5098
movq 0x8(%rax), %rax
movslq -0xa8(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0xb0(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x90(%rbp), %rdi
callq 0x2210
jmp 0x18cd
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x174d
cmpl $0x0, -0xac(%rbp)
jne 0x18f6
leaq -0x90(%rbp), %rdi
callq 0x2180
jmp 0x171b
movq 0x3796(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0x0, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x195d
movq 0x3766(%rip), %rax      # 0x5098
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movslq -0xb4(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x191e
movq -0x50(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %edx
movq 0x3722(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movslq (%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x48, %edi
callq 0x1090
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
cmpl $0x3, -0xc4(%rbp)
jge 0x19d3
movq -0xc0(%rbp), %rdi
movslq -0xc4(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x22a0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x199f
movq -0x68(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x4, %edx
leaq 0x369(%rip), %rcx       # 0x1d50
callq 0x1050
movq -0x80(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl $0x4, %edx
leaq 0x350(%rip), %rcx       # 0x1d50
callq 0x1050
leaq 0x15f8(%rip), %rdi      # 0x3004
callq 0x1040
movq -0x50(%rbp), %rax
movl (%rax), %esi
addl $0x1, %esi
leaq 0x15ff(%rip), %rdi      # 0x3020
movb $0x0, %al
callq 0x1060
movl $0x1, -0xc8(%rbp)
movslq -0xc8(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0x1b58
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x68(%rbp), %rdi
callq 0x1330
movl %eax, -0xcc(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq -0x80(%rbp), %rdi
callq 0x1330
movl %eax, -0xd0(%rbp)
movl -0xcc(%rbp), %eax
andl -0xd0(%rbp), %eax
cmpl $0x0, %eax
je 0x1ad2
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
addl $0x1, %esi
leaq 0x157a(%rip), %rdi      # 0x3020
movb $0x0, %al
callq 0x1060
movl -0x94(%rbp), %edx
movq 0x35de(%rip), %rax      # 0x5098
movq 0x28(%rax), %rax
movq -0x50(%rbp), %rcx
movslq -0xc8(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1b42
cmpl $0x0, -0xcc(%rbp)
je 0x1af7
movq -0xc0(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x23d0
jmp 0x1b40
cmpl $0x0, -0xd0(%rbp)
je 0x1b20
movq -0xc0(%rbp), %rdi
addq $0x18, %rdi
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x23d0
jmp 0x1b3e
movq -0xc0(%rbp), %rdi
addq $0x30, %rdi
movq -0x50(%rbp), %rax
movslq -0xc8(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x23d0
jmp 0x1b40
jmp 0x1b42
jmp 0x1b44
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x1a32
leaq 0x14dd(%rip), %rdi      # 0x303c
callq 0x1040
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x1080
movl $0x18, %edi
callq 0x1090
movq %rax, -0xd8(%rbp)
movl $0x0, -0xdc(%rbp)
cmpl $0x3, -0xdc(%rbp)
jge 0x1bdb
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0xc0(%rbp), %rsi
movslq -0xdc(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
callq 0x12f0
movq %rax, %rdx
movq -0xd8(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x1b8b
movl $0x0, -0xe0(%rbp)
cmpl $0x3, -0xe0(%rbp)
jge 0x1c33
movslq -0xe0(%rbp), %rax
leaq -0x30(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq -0xd8(%rbp), %rax
movslq -0xe0(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x85d(%rip), %rdx      # 0x13c0
callq 0x10a0
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0x1be5
movl $0x0, -0xe4(%rbp)
cmpl $0x3, -0xe4(%rbp)
jge 0x1c6c
movslq -0xe4(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10d0
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x1c3d
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)