// File: /workspace/github_c_makefile_star2_O2/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 $0x1c68, %rsp           # imm = 0x1C68
movq %r9, 0x20(%rsp)
movl %r8d, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdi, 0x18(%rsp)
leaq 0x40(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x1e, %rcx
je 0x2a24
xorl %edi, %edi
cmpq $0x3c, %rdi
je 0x2a19
movl $0xfffffffe, (%rax,%rdi,4) # imm = 0xFFFFFFFE
incq %rdi
jmp 0x2a07
incq %rcx
addq $0xf0, %rax
jmp 0x29ff
movslq %esi, %rax
movslq %edx, %rcx
imulq $0xf0, %rax, %rdx
addq %rsp, %rdx
addq $0x40, %rdx
andl $0x0, (%rdx,%rcx,4)
movl %ecx, %ecx
movl %eax, %r13d
imulq $0xf0, %r13, %rax
addq %rsp, %rax
addq $0x40, %rax
leaq (%rax,%rcx,4), %rdx
addq $-0x4, %rdx
movq %rcx, 0x28(%rsp)
leaq (,%rcx,4), %r9
addq $0x4, 0x18(%rsp)
xorl %r11d, %r11d
xorl %r15d, %r15d
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x2c38
leal 0x1(%r15), %eax
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %r10
xorl %ebp, %ebp
pushq $0x1
popq %r12
cmpq $0x1e, %rbp
je 0x2c2c
leaq 0x1(%rbp), %rax
movq %rax, 0x30(%rsp)
imulq $0xf0, %rbp, %rax
leaq 0x40(%rsp), %rcx
addq %rcx, %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx,4), %rsi
addq $-0xf0, %rsi
movq %rsi, (%rsp)
leaq (%rax,%rcx,4), %rbx
movl $0xf0, %eax
addq %rax, %rbx
movq %rdi, 0x38(%rsp)
xorl %r14d, %r14d
cmpq $0xf0, %r14
je 0x2c0f
cmpl $0x23, (%rdi)
jne 0x2af5
orl $-0x1, (%r10,%r14)
movq %rbp, %rax
xorq %r13, %rax
testl %r15d, %r15d
sete %r8b
movq %r9, %rsi
xorq %r14, %rsi
orq %rax, %rsi
sete %al
andb %r8b, %al
cmpb $0x1, %al
jne 0x2b4e
movq (%rsp), %rsi
movl (%rsi), %eax
cmpl $-0x2, %eax
pushq $0x1
popq %rcx
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 0x2bfb
testl %r15d, %r15d
sete %sil
movl (%r10,%r14), %eax
cmpl %r15d, %eax
setne %r8b
orb %sil, %r8b
jne 0x2bfb
movl -0xf0(%r10,%r14), %eax
cmpl $-0x2, %eax
movl 0x14(%rsp), %ecx
movl %ecx, %esi
cmovnel %eax, %esi
movl -0x4(%r10,%r14), %r8d
testq %rbp, %rbp
cmovel %eax, %esi
cmpl $-0x2, %r8d
movl %ecx, %eax
cmovnel %r8d, %eax
testq %r14, %r14
cmovel %r8d, %eax
movl 0x4(%r10,%r14), %r8d
cmpl $-0x2, %r8d
movq %r9, %r11
movq %r13, %r9
movl %ecx, %r13d
cmovnel %r8d, %r13d
cmpq $0xec, %r14
cmovel %r8d, %r13d
movl 0xf0(%r10,%r14), %r8d
cmpl $-0x2, %r8d
cmovnel %r8d, %ecx
cmpq $0x1d, %rbp
movl %esi, -0xf0(%r10,%r14)
movl %eax, -0x4(%r10,%r14)
movl %r13d, 0x4(%r10,%r14)
movq %r9, %r13
movq %r11, %r9
movl $0x0, %r11d
cmovel %r8d, %ecx
movl %ecx, 0xf0(%r10,%r14)
movl %r15d, %eax
cmpl $-0x2, %eax
cmovel %r11d, %r12d
addq $0x4, %r14
addq $0xc, %rdi
jmp 0x2ade
addq $0xf0, %r10
movq 0x38(%rsp), %rdi
addq $0x2d0, %rdi            # imm = 0x2D0
movq 0x30(%rsp), %rbp
jmp 0x2a94
movl 0x14(%rsp), %eax
movl %eax, %r15d
jmp 0x2a73
xorl %ecx, %ecx
movl 0xc(%rsp), %eax
movl %eax, %r15d
movl 0x10(%rsp), %eax
movl %eax, %r12d
movslq %r15d, %rax
movslq %r12d, %rbx
imulq $0xf0, %rax, %rax
leaq (%rsp,%rax), %r14
addq $0x40, %r14
movl (%r14,%rbx,4), %ebp
cmpl $0x1, %ebp
je 0x2d28
cmpl $0x63, %ecx
ja 0x2d28
movl %ecx, (%rsp)
leal -0x1(%r15), %r13d
movl %r13d, %edi
movl %r12d, %esi
callq 0x1349
testl %eax, %eax
je 0x2ca2
movslq %r13d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x40, %rax
leal -0x1(%rbp), %ecx
cmpl %ecx, (%rax,%rbx,4)
je 0x2cce
leal 0x1(%r15), %r13d
movl %r13d, %edi
movl %r12d, %esi
callq 0x1349
testl %eax, %eax
je 0x2ce1
movslq %r13d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x40, %rax
leal -0x1(%rbp), %ecx
cmpl %ecx, (%rax,%rbx,4)
jne 0x2ce1
movl %r13d, %r15d
movl %r12d, %r13d
movl (%rsp), %ecx
incl %ecx
movl %r13d, %r12d
jmp 0x2c48
leal -0x1(%r12), %r13d
movl %r15d, %edi
movl %r13d, %esi
callq 0x1349
testl %eax, %eax
je 0x2d01
movslq %r13d, %rax
leal -0x1(%rbp), %ecx
cmpl %ecx, (%r14,%rax,4)
je 0x2cd4
leal 0x1(%r12), %r13d
movl %r15d, %edi
movl %r13d, %esi
callq 0x1349
testl %eax, %eax
je 0x2cd1
movslq %r13d, %rax
decl %ebp
cmpl %ebp, (%r14,%rax,4)
movl (%rsp), %ecx
je 0x2cd7
movl %r12d, %r13d
jmp 0x2cd7
movq 0x20(%rsp), %rax
movl %r15d, (%rax)
movq 0x1ca0(%rsp), %rax
movl %r12d, (%rax)
movslq 0xc(%rsp), %rax
movslq 0x10(%rsp), %rcx
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x40, %rax
movl (%rax,%rcx,4), %eax
addq $0x1c68, %rsp           # imm = 0x1C68
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq