// File: /workspace/github_c_makefile_star2_O3/j4r0g[P]Projet_Labyrinthe/fonctions/deplacement.c
// Function: pluscourte_dist
int pluscourte_dist(t_lab labyrinthe[X][Y], int xdep, int ydep, int xarr, int yarr, int* xdir, int *ydir)  {
	int matr_dist[X][Y];
	int i, j;
	int xcur, ycur;
	int dist = 0;
	int parcouru=0;
	for(i=0; i<X; i++){				/*initialisation de la matrice*/
		for(j=0; j<Y; j++){
			matr_dist[i][j]=-2;
		}
	}

	matr_dist[xdep][ydep]=0;

	while (parcouru==0){
		//printf("dist : %i\n", dist);
		dist++;
		parcouru=1;
		for (i=0; i<X; i++){
			for(j=0; j<Y; j++){

				if (labyrinthe[i][j].etat==mur){
					matr_dist[i][j]=-1;
				}
				if (i==xdep && j==ydep && dist==1) {		/*si on est au début, on fixe la distance à 0*/
					if (matr_dist[i-1][j]==-2){
						matr_dist[i-1][j]=dist;
					}if (matr_dist[i][j-1]==-2){
						matr_dist[i][j-1]=dist;
					}if (matr_dist[i][j+1]==-2){
						matr_dist[i][j+1]=dist;
					}if (matr_dist[i+1][j]==-2) {
						matr_dist[i+1][j]=dist;
					}
				}
				else if (matr_dist[i][j]==dist-1 && dist>1){		/*sinon on indique la distance en cercles concentriques*/
					if (matr_dist[i-1][j]==-2 && i-1>=0){
						matr_dist[i-1][j]=dist;
					}if (matr_dist[i][j-1]==-2 && j-1>=0){
						matr_dist[i][j-1]=dist;
					}if (matr_dist[i][j+1]==-2 && j+1<Y){
						matr_dist[i][j+1]=dist;
					}if (matr_dist[i+1][j]==-2 && i+1<X) {
						matr_dist[i+1][j]=dist;
					}
				}
				if (matr_dist[i][j]==-2){							//si on a parcouru toute la matrice, on sort de la boucle
					parcouru=0;
				}
			}
		}
	}

	xcur = xarr; ycur=yarr;
	i = 0;
	while(matr_dist[xcur][ycur] != 1 && i<100){								/*On parcourt le chemin en sens inverse pour trouver le prochain déplacement*/
		if(coord_correctes(xcur-1,ycur) && (matr_dist[xcur-1][ycur] == matr_dist[xcur][ycur]-1)){
			xcur--;
		}else if(coord_correctes(xcur+1,ycur) && (matr_dist[xcur+1][ycur] == matr_dist[xcur][ycur]-1)){
			xcur++;
		}else if(coord_correctes(xcur,ycur-1) && (matr_dist[xcur][ycur-1] == matr_dist[xcur][ycur]-1)){
			ycur--;
		}else if(coord_correctes(xcur,ycur+1) && (matr_dist[xcur][ycur+1] == matr_dist[xcur][ycur]-1)){
			ycur++;
		}
		i++;
		/*if(i>20){
			break;
		}*/
	}
	*xdir = xcur; *ydir=ycur;
	return matr_dist[xarr][yarr];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c78, %rsp           # imm = 0x1C78
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
leaq 0x50(%rsp), %rax
xorl %ecx, %ecx
movaps 0x3b98(%rip), %xmm0     # 0x6740
xorl %edi, %edi
movaps %xmm0, (%rax,%rdi,4)
addq $0x4, %rdi
cmpq $0x3c, %rdi
jne 0x2baa
incq %rcx
addq $0xf0, %rax
cmpq $0x1e, %rcx
jne 0x2ba8
movslq %esi, %rax
movslq %edx, %rcx
imulq $0xf0, %rax, %rdx
addq %rsp, %rdx
addq $0x50, %rdx
movl $0x0, (%rdx,%rcx,4)
movl %ecx, %ecx
movl %eax, %ebp
imulq $0xf0, %rbp, %rax
addq %rsp, %rax
addq $0x50, %rax
leaq (%rax,%rcx,4), %rdx
addq $-0x4, %rdx
movq %rcx, 0x38(%rsp)
leaq (,%rcx,4), %r11
addq $0x4, 0x20(%rsp)
xorl %r10d, %r10d
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, %r12d
leal 0x1(%r12), %eax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %r9
xorl %r15d, %r15d
movl $0x1, %r13d
leaq 0x1(%r15), %rax
movq %rax, 0x40(%rsp)
imulq $0xf0, %r15, %rax
leaq 0x50(%rsp), %rcx
addq %rcx, %rax
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,4), %rsi
addq $-0xf0, %rsi
movq %rsi, 0x18(%rsp)
leaq (%rax,%rcx,4), %rbx
movl $0xf0, %eax
addq %rax, %rbx
movq %rdi, 0x48(%rsp)
xorl %r14d, %r14d
cmpl $0x23, (%rdi)
jne 0x2c8b
movl $0xffffffff, (%r9,%r14) # imm = 0xFFFFFFFF
movq %r15, %rax
xorq %rbp, %rax
testl %r12d, %r12d
sete %r8b
movq %r11, %rsi
xorq %r14, %rsi
orq %rax, %rsi
sete %al
andb %r8b, %al
cmpb $0x1, %al
jne 0x2ce7
movq 0x18(%rsp), %rsi
movl (%rsi), %eax
cmpl $-0x2, %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movl %eax, (%rsi)
movl (%rdx), %eax
cmpl $-0x2, %eax
cmovel %ecx, %eax
movl %eax, (%rdx)
movl 0x8(%rdx), %eax
cmpl $-0x2, %eax
cmovel %ecx, %eax
movl %eax, 0x8(%rdx)
movl (%rbx), %eax
cmpl $-0x2, %eax
cmovel %ecx, %eax
movl %eax, (%rbx)
movl 0x4(%rdx), %eax
jmp 0x2d93
testl %r12d, %r12d
sete %sil
movl (%r9,%r14), %eax
cmpl %r12d, %eax
setne %r8b
orb %sil, %r8b
jne 0x2d93
movl -0xf0(%r9,%r14), %eax
cmpl $-0x2, %eax
movl 0x14(%rsp), %ecx
movl %ecx, %esi
cmovnel %eax, %esi
movl -0x4(%r9,%r14), %r8d
testq %r15, %r15
cmovel %eax, %esi
cmpl $-0x2, %r8d
movl %ecx, %eax
cmovnel %r8d, %eax
testq %r14, %r14
cmovel %r8d, %eax
movl 0x4(%r9,%r14), %r8d
cmpl $-0x2, %r8d
movq %r11, %r10
movq %rbp, %r11
movl %ecx, %ebp
cmovnel %r8d, %ebp
cmpq $0xec, %r14
cmovel %r8d, %ebp
movl 0xf0(%r9,%r14), %r8d
cmpl $-0x2, %r8d
cmovnel %r8d, %ecx
cmpq $0x1d, %r15
movl %esi, -0xf0(%r9,%r14)
movl %eax, -0x4(%r9,%r14)
movl %ebp, 0x4(%r9,%r14)
movq %r11, %rbp
movq %r10, %r11
movl $0x0, %r10d
cmovel %r8d, %ecx
movl %ecx, 0xf0(%r9,%r14)
movl %r12d, %eax
cmpl $-0x2, %eax
cmovel %r10d, %r13d
addq $0x4, %r14
addq $0xc, %rdi
cmpq $0xf0, %r14
jne 0x2c7e
addq $0xf0, %r9
movq 0x48(%rsp), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movq 0x40(%rsp), %rax
movq %rax, %r15
cmpq $0x1e, %rax
jne 0x2c3d
testl %r13d, %r13d
je 0x2c1a
movq 0x8(%rsp), %rcx
movslq %ecx, %r14
movq 0x28(%rsp), %rsi
movslq %esi, %r13
imulq $0xf0, %r14, %rax
addq %rsp, %rax
addq $0x50, %rax
movl (%rax,%r13,4), %eax
cmpl $0x1, %eax
movl %eax, 0x18(%rsp)
jne 0x2e2f
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movq 0x1cb0(%rsp), %rax
movl %esi, (%rax)
movl 0x18(%rsp), %eax
addq $0x1c78, %rsp           # imm = 0x1C78
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
movl %eax, %r15d
movq %rcx, 0x8(%rsp)
leal -0x1(%rcx), %r12d
movl %r12d, %edi
movq %rsi, %rbp
callq 0x1343
testl %eax, %eax
je 0x2e67
movslq %r12d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x50, %rax
leal -0x1(%r15), %ecx
cmpl %ecx, (%rax,%r13,4)
je 0x2e99
movq 0x8(%rsp), %rax
leal 0x1(%rax), %r12d
movl %r12d, %edi
movl %ebp, %esi
callq 0x1343
testl %eax, %eax
je 0x2ea1
movslq %r12d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x50, %rax
leal -0x1(%r15), %ecx
cmpl %ecx, (%rax,%r13,4)
jne 0x2ea1
movl %r12d, %ecx
movq %rbp, %rsi
jmp 0x2f0a
leal -0x1(%rbp), %r12d
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq 0x1343
testl %eax, %eax
je 0x2ed0
movslq %r12d, %rax
imulq $0xf0, %r14, %rcx
addq %rsp, %rcx
addq $0x50, %rcx
leal -0x1(%r15), %edx
cmpl %edx, (%rcx,%rax,4)
je 0x2f02
leal 0x1(%rbp), %r12d
movq 0x8(%rsp), %rdi
movl %r12d, %esi
callq 0x1343
testl %eax, %eax
je 0x2f3f
movslq %r12d, %rax
imulq $0xf0, %r14, %rcx
addq %rsp, %rcx
addq $0x50, %rcx
decl %r15d
cmpl %r15d, (%rcx,%rax,4)
je 0x2f02
movl %ebp, %r12d
movl %r12d, %esi
movq 0x8(%rsp), %rcx
movslq %ecx, %r14
movslq %esi, %r13
imulq $0xf0, %r14, %rax
addq %rsp, %rax
addq $0x50, %rax
movl (%rax,%r13,4), %r15d
cmpl $0x1, %r15d
je 0x2e08
leal 0x1(%rbx), %eax
cmpl $0x63, %ebx
movl %eax, %ebx
jb 0x2e34
jmp 0x2e08
movq 0x8(%rsp), %rcx
jmp 0x2e9c