// File: /workspace/github_c_makefile_star2_O3/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 0x2679
xorl %eax, %eax
movq %r15, 0x20(%rsp)
movl $0x4, %edx
cmpl $0x2a, (%r15,%rdx)
je 0x29cc
addq $0xc, %rdx
cmpq $0x2d4, %rdx            # imm = 0x2D4
jne 0x298d
incq %rax
addq $0x2d0, %r15            # imm = 0x2D0
cmpq $0x1e, %rax
jne 0x2988
movl 0x18(%rsp), %edi
movl 0x1c(%rsp), %esi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
movl %ebp, %r8d
movq %rbx, %r9
jmp 0x2b5f
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 0x2a49
cmpl $0x2a, (%r13)
jne 0x2a49
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 0x2b75
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 0x29f3
incq %rbx
addq $0x2d0, %r12            # imm = 0x2D0
cmpq $0x1e, %rbx
jne 0x29ed
movl 0x44(%rsp), %ebx
movl 0x48(%rsp), %ebp
cmpl $0x2, %r15d
jl 0x2aab
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 0x2aa0
movl 0x50(%rsp,%rcx), %ebx
movl 0x54(%rsp,%rcx), %ebp
movl %esi, %edx
addq $0xc, %rcx
cmpq %rcx, %rax
jne 0x2a8e
jmp 0x2ab0
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 0x2b75
xorl %ecx, %ecx
movl $0x4, %esi
cmpl $0x2a, (%r15,%rsi)
je 0x2b05
addq $0xc, %rsi
cmpq $0x2d4, %rsi            # imm = 0x2D4
jne 0x2adf
incq %rcx
addq $0x2d0, %r15            # imm = 0x2D0
cmpq $0x1e, %rcx
jne 0x2ada
jmp 0x2b45
cmpl $0x13, %eax
jg 0x2b45
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 0x349e
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 0x2708