// File: /workspace/github_c_makefile_star2_O1/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 $0x1c58, %rsp           # imm = 0x1C58
movq %r9, 0x18(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x30(%rsp), %rax
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0xfffffffe, (%rax,%r8,4) # imm = 0xFFFFFFFE
incq %r8
cmpq $0x3c, %r8
jne 0x2a99
incq %rcx
addq $0xf0, %rax
cmpq $0x1e, %rcx
jne 0x2a96
movslq %esi, %rcx
movslq %edx, %rax
imulq $0xf0, %rcx, %rdx
addq %rsp, %rdx
addq $0x30, %rdx
movl $0x0, (%rdx,%rax,4)
movl %eax, %edi
movl %ecx, %eax
movq %rax, 0x20(%rsp)
shlq $0x2, %rdi
addq $0x4, 0x10(%rsp)
xorl %r9d, %r9d
movl $0x1, %r8d
xorl %edx, %edx
movl %edx, %r10d
leal 0x1(%r10), %edx
movq 0x10(%rsp), %rbp
leaq 0x30(%rsp), %r14
xorl %r12d, %r12d
movl $0x1, %r15d
leaq 0x1(%r12), %rax
movq %rax, 0x28(%rsp)
movq %r12, %r13
xorq 0x20(%rsp), %r13
movq %rbp, (%rsp)
xorl %esi, %esi
cmpl $0x23, (%rbp)
jne 0x2b32
movl $0xffffffff, (%r14,%rsi) # imm = 0xFFFFFFFF
testl %r10d, %r10d
sete %cl
movq %rdi, %rax
xorq %rsi, %rax
orq %r13, %rax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x2b9f
movl -0xf0(%r14,%rsi), %eax
cmpl $-0x2, %eax
cmovel %r8d, %eax
movl -0x4(%r14,%rsi), %ecx
cmpl $-0x2, %ecx
cmovel %r8d, %ecx
movl %eax, -0xf0(%r14,%rsi)
movl %ecx, -0x4(%r14,%rsi)
movl 0x4(%r14,%rsi), %eax
cmpl $-0x2, %eax
cmovel %r8d, %eax
movl %eax, 0x4(%r14,%rsi)
movl 0xf0(%r14,%rsi), %eax
cmpl $-0x2, %eax
cmovel %r8d, %eax
movl %eax, 0xf0(%r14,%rsi)
jmp 0x2c2b
testl %r10d, %r10d
je 0x2c2b
cmpl %r10d, (%r14,%rsi)
jne 0x2c2b
movl -0xf0(%r14,%rsi), %eax
cmpl $-0x2, %eax
movl %edx, %ecx
cmovnel %eax, %ecx
movl -0x4(%r14,%rsi), %ebx
testq %r12, %r12
cmovel %eax, %ecx
cmpl $-0x2, %ebx
movl %edx, %eax
cmovnel %ebx, %eax
testq %rsi, %rsi
cmovel %ebx, %eax
movl 0x4(%r14,%rsi), %ebx
cmpl $-0x2, %ebx
movl %edx, %r9d
cmovnel %ebx, %r9d
cmpq $0xec, %rsi
cmovel %ebx, %r9d
movl 0xf0(%r14,%rsi), %ebx
cmpl $-0x2, %ebx
movl %edx, %r11d
cmovnel %ebx, %r11d
cmpq $0x1d, %r12
movl %ecx, -0xf0(%r14,%rsi)
movl %eax, -0x4(%r14,%rsi)
movl %r9d, 0x4(%r14,%rsi)
movl $0x0, %r9d
cmovel %ebx, %r11d
movl %r11d, 0xf0(%r14,%rsi)
cmpl $-0x2, (%r14,%rsi)
cmovel %r9d, %r15d
addq $0x4, %rsi
addq $0xc, %rbp
cmpq $0xf0, %rsi
jne 0x2b24
addq $0xf0, %r14
movq (%rsp), %rbp
addq $0x2d0, %rbp            # imm = 0x2D0
movq 0x28(%rsp), %rax
movq %rax, %r12
cmpq $0x1e, %rax
jne 0x2b0c
testl %r15d, %r15d
je 0x2af2
movl 0x8(%rsp), %edx
movslq %edx, %rbx
movl 0xc(%rsp), %ecx
movslq %ecx, %r12
imulq $0xf0, %rbx, %rax
addq %rsp, %rax
addq $0x30, %rax
movl (%rax,%r12,4), %ebp
movl %ecx, %esi
cmpl $0x1, %ebp
je 0x2db0
xorl %r14d, %r14d
movl 0xc(%rsp), %eax
movl %eax, %esi
movl 0x8(%rsp), %eax
movl %eax, %edx
movq %rdx, (%rsp)
leal -0x1(%rdx), %r13d
movl %r13d, %edi
movq %rsi, %r15
callq 0x1343
testl %eax, %eax
je 0x2ce1
movslq %r13d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x30, %rax
leal -0x1(%rbp), %ecx
cmpl %ecx, (%rax,%r12,4)
je 0x2d12
movq (%rsp), %rax
leal 0x1(%rax), %r13d
movl %r13d, %edi
movl %r15d, %esi
callq 0x1343
testl %eax, %eax
je 0x2d1a
movslq %r13d, %rax
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x30, %rax
leal -0x1(%rbp), %ecx
cmpl %ecx, (%rax,%r12,4)
jne 0x2d1a
movl %r13d, %edx
movq %r15, %rsi
jmp 0x2d82
leal -0x1(%r15), %r13d
movq (%rsp), %rdi
movl %r13d, %esi
callq 0x1343
testl %eax, %eax
je 0x2d47
movslq %r13d, %rax
imulq $0xf0, %rbx, %rcx
addq %rsp, %rcx
addq $0x30, %rcx
leal -0x1(%rbp), %edx
cmpl %edx, (%rcx,%rax,4)
je 0x2d76
leal 0x1(%r15), %r13d
movq (%rsp), %rdi
movl %r13d, %esi
callq 0x1343
testl %eax, %eax
je 0x2d7b
movslq %r13d, %rax
imulq $0xf0, %rbx, %rcx
addq %rsp, %rcx
addq $0x30, %rcx
decl %ebp
cmpl %ebp, (%rcx,%rax,4)
je 0x2d76
movl %r15d, %r13d
movl %r13d, %esi
jmp 0x2d7e
movq %r15, %rsi
movq (%rsp), %rdx
movslq %edx, %rbx
movslq %esi, %r12
imulq $0xf0, %rbx, %rax
addq %rsp, %rax
addq $0x30, %rax
movl (%rax,%r12,4), %ebp
cmpl $0x1, %ebp
je 0x2db0
leal 0x1(%r14), %eax
cmpl $0x63, %r14d
movl %eax, %r14d
jb 0x2cb0
movq 0x18(%rsp), %rax
movl %edx, (%rax)
movq 0x1c90(%rsp), %rax
movl %esi, (%rax)
movslq 0x8(%rsp), %rax
movslq 0xc(%rsp), %rcx
imulq $0xf0, %rax, %rax
addq %rsp, %rax
addq $0x30, %rax
movl (%rax,%rcx,4), %eax
addq $0x1c58, %rsp           # imm = 0x1C58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq