// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, %r15
movl %esi, 0x18(%rsp)
movl %esi, %edi
movl %edx, 0x1c(%rsp)
movl %edx, %esi
movq %r15, %rdx
movl %r8d, %ecx
movl %r9d, %r8d
movq %rbx, %r9
callq 0x2569
xorl %eax, %eax
movq %r15, 0x20(%rsp)
movl $0x4, %esi
movl (%r15,%rsi), %edx
cmpl $0x2a, %edx
je 0x28a5
addq $0xc, %rsi
cmpq $0x2d4, %rsi            # imm = 0x2D4
jne 0x2876
incq %rax
addq $0x2d0, %r15            # imm = 0x2D0
cmpq $0x1e, %rax
jne 0x2871
cmpl $0x2a, %edx
jne 0x2a53
movl %r14d, 0x28(%rsp)
movl %ebp, 0x2c(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x20(%rsp), %rax
leaq 0x4(%rax), %r12
xorl %ebx, %ebx
leaq 0x34(%rsp), %rbp
xorl %r15d, %r15d
movq %r12, %r13
xorl %r14d, %r14d
movl %ebx, %edi
movl %r14d, %esi
callq 0x1343
testl %eax, %eax
je 0x2922
cmpl $0x2a, (%r13)
jne 0x2922
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movl 0x18(%rsp), %esi
movl 0x1c(%rsp), %edx
movl %ebx, %ecx
movl %r14d, %r8d
movq %rbp, %r9
callq 0x2a6b
movslq %r15d, %r15
leaq (%r15,%r15,2), %rcx
movl %eax, 0x40(%rsp,%rcx,4)
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp,%rcx,4)
movl 0x30(%rsp), %eax
movl %eax, 0x48(%rsp,%rcx,4)
incl %r15d
incq %r14
addq $0xc, %r13
cmpq $0x3c, %r14
jne 0x28cc
incq %rbx
addq $0x2d0, %r12            # imm = 0x2D0
cmpq $0x1e, %rbx
jne 0x28c6
movl 0x44(%rsp), %ebx
movl 0x48(%rsp), %ebp
cmpl $0x2, %r15d
jl 0x2984
movl 0x40(%rsp), %edx
movl %r15d, %eax
leaq (%rax,%rax,2), %rax
leaq -0xc(,%rax,4), %rax
xorl %ecx, %ecx
movl 0x28(%rsp), %r14d
movl 0x4c(%rsp,%rcx), %esi
cmpl %edx, %esi
jge 0x2979
movl 0x50(%rsp,%rcx), %ebx
movl 0x54(%rsp,%rcx), %ebp
movl %esi, %edx
addq $0xc, %rcx
cmpq %rcx, %rax
jne 0x2967
jmp 0x2989
movl 0x28(%rsp), %r14d
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
leaq 0x34(%rsp), %r9
movq 0x20(%rsp), %r15
movq %r15, %rdi
movl 0x18(%rsp), %esi
movl 0x1c(%rsp), %edx
movl %ebx, %ecx
movl %ebp, %r8d
callq 0x2a6b
xorl %ecx, %ecx
movl $0x4, %edi
movl (%r15,%rdi), %esi
cmpl $0x2a, %esi
je 0x29de
addq $0xc, %rdi
cmpq $0x2d4, %rdi            # imm = 0x2D4
jne 0x29b8
incq %rcx
addq $0x2d0, %r15            # imm = 0x2D0
cmpq $0x1e, %rcx
jne 0x29b3
cmpl $0x2a, %esi
jne 0x2a23
cmpl $0x13, %eax
jg 0x2a23
movl 0x2c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %r14d, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl 0x18(%rsp), %edx
movl 0x1c(%rsp), %ecx
movl %ebx, %r8d
movl %ebp, %r9d
callq 0x3345
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rsp), %edi
movl 0x1c(%rsp), %esi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
movl 0x2c(%rsp), %r8d
movq 0x38(%rsp), %r9
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25f8
movl 0x18(%rsp), %edi
movl 0x1c(%rsp), %esi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
movl %ebp, %r8d
movq %rbx, %r9
jmp 0x2a3d