// File: /workspace/github_c_makefile_star2_O3/S01den[P]Lost_CavernRL/ennemy.c
// Function: moveRand
void moveRand(Ennemy ennemies[], Map map[LONGUEUR][LARGEUR], int indice) 
{
	if(ennemies[indice].type == 1 || ennemies[indice].type == 3)
	{
		int movX = 2;
		int movY = 2;

		while((movX == 2 || movY == 2) && (ennemies[indice].x+movX <= 0 || ennemies[indice].x+movX >= LONGUEUR) && (ennemies[indice].y+movY <= 0 || ennemies[indice].y+movY >= LARGEUR))
		{
			movX = rand()%5-2;
			movY = rand()%5-2;		
		}
		
		if(ennemies[indice].x+movX >= 0 && ennemies[indice].x+movX <= LONGUEUR && ennemies[indice].y+movY >= 0 && ennemies[indice].y+movY <= LARGEUR)
		{
			if(map[ennemies[indice].x+movX][ennemies[indice].y+movY].content == WALL)
			{
				if(ennemies[indice].x-1 > 0 && ennemies[indice].x+1 < LONGUEUR && ennemies[indice].y+1 > 0 && ennemies[indice].y+1 < LARGEUR)
				{	
					if(map[ennemies[indice].x+1][ennemies[indice].y+movY].content != WALL)
					{
						movX = 1;
						ennemies[indice].x += movX;
						ennemies[indice].y += movY;
					}
					else if(map[ennemies[indice].x-1][ennemies[indice].y+movY].content != WALL)
					{
						if(ennemies[indice].x-1 > 0 && ennemies[indice].x-1 < LONGUEUR && ennemies[indice].y+movY > 0 && ennemies[indice].y+movY < LARGEUR)
						{
							movX = -1;
							ennemies[indice].x += movX;
							ennemies[indice].y += movY;
						}		
					}
					else if(map[ennemies[indice].x][ennemies[indice].y+1].content != WALL)
					{
						if(ennemies[indice].x > 0 && ennemies[indice].x < LONGUEUR && ennemies[indice].y+1 > 0 && ennemies[indice].y+1 < LARGEUR)
						{
							movY = 1;
							ennemies[indice].x += movX;
							ennemies[indice].y += movY;
						}		
					}
					else if(map[ennemies[indice].x][ennemies[indice].y-1].content != WALL)
					{
						if(ennemies[indice].x > 0 && ennemies[indice].x < LONGUEUR && ennemies[indice].y-1 > 0 && ennemies[indice].y-1 < LARGEUR)
						{
							movY = -1;
							ennemies[indice].x += movX;
							ennemies[indice].y += movY;
						}		
					}
				}
			}
			else
			{
				ennemies[indice].x += movX;
				ennemies[indice].y += movY;	
			}
		}

		if(map[ennemies[indice].x][ennemies[indice].y].content == MINES)
		{
			map[ennemies[indice].x][ennemies[indice].y].content = FLOOR;
			ennemies[indice].life -= 20;
		}
	}
}

// Assembly:
movslq %edx, %rax
imulq $0x38, %rax, %rax
movl 0x20(%rdi,%rax), %ecx
orl $0x2, %ecx
cmpl $0x3, %ecx
jne 0x580e
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
addq %rax, %rbx
movl $0x2, %ecx
movl $0xffffff7e, %r12d      # imm = 0xFFFFFF7E
movl $0x2, %r15d
movl (%rbx), %eax
leal (%r15,%rax), %edx
addl $-0x27, %edx
cmpl $-0x27, %edx
ja 0x56e9
movl 0x4(%rbx), %edx
leal (%rcx,%r12), %esi
addl %edx, %esi
cmpl %r12d, %esi
ja 0x56ec
callq 0x11d0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
negl %ecx
leal (%rax,%rcx), %r15d
addl $-0x2, %r15d
callq 0x11d0
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,4), %ecx
negl %ecx
addl %eax, %ecx
addl $-0x2, %ecx
cmpl $0x2, %r15d
sete %al
cmpl $0x2, %ecx
sete %dl
orb %al, %dl
jne 0x566d
movl (%rbx), %eax
movl 0x4(%rbx), %edx
addl %eax, %r15d
cmpl $0x27, %r15d
ja 0x57d4
addl %edx, %ecx
cmpl $0x82, %ecx
ja 0x57d4
movl %r15d, %esi
movl %ecx, %r8d
imulq $0xe38, %rsi, %rsi      # imm = 0xE38
addq %r14, %rsi
leaq (%r8,%r8,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r8, %rdi
cmpl $0x2, (%rsi,%rdi)
jne 0x57c8
cmpl $0x2, %eax
jl 0x57d4
cmpl $0x25, %eax
ja 0x57d4
cmpl $0x80, %edx
ja 0x57d4
leal 0x1(%rax), %esi
movl %esi, %r8d
imulq $0xe38, %r8, %r8        # imm = 0xE38
addq %r14, %r8
cmpl $0x2, (%r8,%rdi)
jne 0x57cb
leal -0x1(%rax), %esi
movl %esi, %r8d
imulq $0xe38, %r8, %r8        # imm = 0xE38
addq %r14, %r8
cmpl $0x2, (%r8,%rdi)
jne 0x580f
leal 0x1(%rdx), %ecx
movl %eax, %esi
movl %ecx, %edi
imulq $0xe38, %rsi, %rsi      # imm = 0xE38
addq %r14, %rsi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
cmpl $0x2, (%rsi,%r8)
jne 0x57c8
cmpl $0x2, %edx
jb 0x57d4
movslq %edx, %rcx
leaq (%rcx,%rcx,8), %rdi
addq $-0x9, %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rdi, %rcx
decq %rcx
cmpl $0x2, (%rsi,%rcx)
je 0x57d4
decl %edx
movl %r15d, %esi
movl %edx, %ecx
jmp 0x57cb
movl %r15d, %esi
movl %esi, (%rbx)
movl %ecx, 0x4(%rbx)
movl %ecx, %edx
movl %esi, %eax
cltq
movslq %edx, %rcx
imulq $0xe38, %rax, %rax      # imm = 0xE38
addq %rax, %r14
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
cmpl $0x4, (%r14,%rax)
jne 0x5803
addq %rax, %r14
movl $0x1, (%r14)
addl $-0x14, 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
testb %cl, %cl
je 0x57d4
movzbl %cl, %edi
cmpl $0x82, %edi
je 0x57d4
jmp 0x57cb