// File: /workspace/github_c_makefile_star2_O2/j4r0g[P]Projet_Labyrinthe/fonctions/deplacement.c
// Function: deplacement
void deplacement (t_lab lab[X][Y], int pos_x, int pos_y, t_fourmi fourmi[], int bouffe, int dureevie) {
	int i, j, xdir, ydir, dist;
	typedef struct {int dist; int x; int y;} nourr;
	int ins_adj = insecte_adjacent(pos_x, pos_y, lab, bouffe, dureevie, fourmi);	//ins_adj vaut 1 si il y a un insecte a coté, 0 sinon.
	if(nourriture_pres(lab)){
		int nb_nourr = 0;
		nourr plusproche;
		nourr interet[50];
		for (i=0; i<X; i++) {														//on parcourt la matrice pour trouver toutes les cases de nourriture ou les insectes proches
			for (j=0; j<Y; j++) {
				if (coord_correctes(i, j) && lab[i][j].etat==food){

					dist=pluscourte_dist(lab, pos_x, pos_y, i, j, &xdir, &ydir);

						interet[nb_nourr].dist=dist;
						interet[nb_nourr].x=xdir;
						interet[nb_nourr].y=ydir;
						nb_nourr++;

				}
			}
		}

		plusproche.dist=interet[0].dist;
		plusproche.x=interet[0].x;
		plusproche.y=interet[0].y;
		for (i=1; i<nb_nourr; i++){									/*on parcourt le tableau pour trouver la nourriture la plus proche*/
			if (interet[i].dist<plusproche.dist){
				plusproche.dist=interet[i].dist;
				plusproche.x=interet[i].x;
				plusproche.y=interet[i].y;

			}
		}
		dist=pluscourte_dist(lab, pos_x, pos_y, plusproche.x, plusproche.y, &xdir, &ydir);
		if (nourriture_pres(lab) && dist<20) {
			modif_pos(lab, fourmi, pos_x, pos_y, plusproche.x, plusproche.y, bouffe, dureevie);
		}
		else {
			prochain_deplacement(pos_x, pos_y, lab, bouffe, dureevie, fourmi);
		}
	}
	else {
		prochain_deplacement(pos_x, pos_y, lab, bouffe, dureevie, fourmi);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movl %r9d, %ebp
movl %r8d, %r14d
movq %rcx, %rbx
movl %edx, %r13d
movl %esi, %r12d
movq %rdi, %r15
movl %esi, %edi
movl %edx, %esi
movq %r15, %rdx
movl %r8d, %ecx
movl %r9d, %r8d
movq %rbx, %r9
callq 0x2547
movq %r15, %rdi
callq 0x27c4
testl %eax, %eax
je 0x2992
movl %r13d, 0x1c(%rsp)
movl %r14d, 0x20(%rsp)
movl %ebp, 0x24(%rsp)
movq %rbx, 0x30(%rsp)
movq %r15, %r13
addq $0x4, %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq $0x1e, %rbx
je 0x28e9
movq %r15, 0x38(%rsp)
xorl %r14d, %r14d
cmpq $0x3c, %r14
je 0x28d5
movl %ebx, %edi
movl %r14d, %esi
callq 0x1349
testl %eax, %eax
je 0x28cc
cmpl $0x2a, (%r15)
jne 0x28cc
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movl %r12d, %esi
movl 0x1c(%rsp), %edx
movl %ebx, %ecx
movl %r14d, %r8d
leaq 0x2c(%rsp), %r9
callq 0x29d4
movslq %ebp, %rbp
imulq $0xc, %rbp, %rcx
movl %eax, 0x40(%rsp,%rcx)
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rsp,%rcx)
movl 0x28(%rsp), %eax
movl %eax, 0x48(%rsp,%rcx)
incl %ebp
incq %r14
addq $0xc, %r15
jmp 0x2873
incq %rbx
movq 0x38(%rsp), %r15
addq $0x2d0, %r15            # imm = 0x2D0
jmp 0x2865
leaq 0x54(%rsp), %rax
movl -0x14(%rax), %r9d
movl -0x10(%rax), %ebx
movl -0xc(%rax), %r14d
movslq %ebp, %rcx
pushq $0x1
popq %r8
movl 0x1c(%rsp), %edx
movl %r12d, %esi
movq %r13, %r15
cmpq %rcx, %r8
jge 0x2929
movl -0x8(%rax), %edi
cmpl %r9d, %edi
jge 0x2920
movl -0x4(%rax), %ebx
movl (%rax), %r14d
movl %edi, %r9d
incq %r8
addq $0xc, %rax
jmp 0x290a
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
leaq 0x2c(%rsp), %r9
movq %r15, %rdi
movl %ebx, %ecx
movl %r14d, %r8d
callq 0x29d4
movl %eax, %ebp
movq %r15, %rdi
callq 0x27c4
testl %eax, %eax
je 0x29a6
cmpl $0x13, %ebp
jg 0x29a6
movl 0x24(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movl %r12d, %edx
movl 0x1c(%rsp), %ecx
movl %ebx, %r8d
movl %r14d, %r9d
callq 0x31ea
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %edi
movl %r13d, %esi
movq %r15, %rdx
movl %r14d, %ecx
movl %ebp, %r8d
movq %rbx, %r9
jmp 0x29be
movl %r12d, %edi
movl 0x1c(%rsp), %esi
movq %r13, %rdx
movl 0x20(%rsp), %ecx
movl 0x24(%rsp), %r8d
movq 0x30(%rsp), %r9
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25d2