// File: /workspace/github_c_makefile_star2_O2/ajpaulson[P]srogue11/chase.c
// Function: chase
int chase(struct thing *tp,struct coord *ee,bool runaway,bool dofight)
{
	int x, y, ch;
	int dist, thisdist, closest;
	struct coord *er = &tp->t_pos;
	struct coord try, closecoord;
	int numsteps, onscare;

	/*
	 * If the thing is confused, let it move randomly.
	 */
	ch = CHASE;
	onscare = FALSE;
	if (on(*tp, ISHUH)) {
		ch_ret = *rndmove(tp);
		dist = DISTANCE(hero.y, hero.x, ch_ret.y, ch_ret.x);
		if (rnd(1000) < 5)
			tp->t_flags &= ~ISHUH;
		if (dist == 0)
			ch = FIGHT;
	}
	else {
		/*
		 * Otherwise, find the the best spot to run to
		 * in order to get to your goal.
		 */
		numsteps = 0;
		if (runaway)
			closest = 0;
		else
			closest = FARAWAY;
		ch_ret = *er;
		closecoord = tp->t_oldpos;
		for (y = er->y - 1; y <= er->y + 1; y += 1) {
			for (x = er->x - 1; x <= er->x + 1; x += 1) {
				if (!cordok(y, x))
					continue;
				try.x = x;
				try.y = y;
				if (!diag_ok(er, &try))
					continue;
				ch = winat(y, x);
				if (step_ok(ch)) {
					struct trap *trp;

					if (isatrap(ch)) {
						trp = trap_at(y, x);
						if (trp != NULL && off(*tp, ISHUH)) {
							/*
							 * Dont run over found traps unless
							 * the hero is standing on it. If confused,
							 * then he can run into them.
							 */
							if (trp->tr_flags & ISFOUND) {
								if (trp->tr_type == POOL && rnd(100) < 80)
									continue;
								else if (y != hero.y || x != hero.x)
									continue;
							}
						}
					}
					/*
					 * Check for scare monster scrolls.
					 */
					if (ch == SCROLL) {
						struct linked_list *item;

						item = find_obj(y, x);
						if (item != NULL)
							if ((OBJPTR(item))->o_which == S_SCARE) {
								if (ce(hero, try))
									onscare = TRUE;
								continue;
							}
					}
					/*
					 * Vampires will not run into a lit room.
					 */
					if (tp->t_type == 'V') {
						struct room *lr;

						lr = roomin(&try);
						if (lr != NULL && !rf_on(lr, ISDARK))
							continue;
					}
					/*
					 * This is a valid place to step
					 */
					if (y == hero.y && x == hero.x) {
						if (dofight) {
							ch_ret = try;	/* if fighting */
							return FIGHT;	/* hit hero */
						}
						else
							continue;
					}
					thisdist = DISTANCE(y, x, ee->y, ee->x);
					if (thisdist <= 0) {
						ch_ret = try;	/* got here but */
						return CHASE;	/* dont fight */
					}
					numsteps += 1;
					if ((!runaway && thisdist < closest) ||
						(runaway && thisdist > closest)) {
						/*
						 * dont count the monsters last position as
						 * the closest spot, unless running away and
						 * in the same room.
						 */
						if (!ce(try, tp->t_oldpos) || (runaway
						  && player.t_room == tp->t_room
						  && tp->t_room != NULL)) {
							closest = thisdist;
							closecoord = try;
						}
					}
				}
			}
		}
		/*
		 * If dead end, then go back from whence you came.
		 * Otherwise, pick the closest of the remaining spots.
		 */
		if (numsteps > 0)			/* move to best spot */
			ch_ret = closecoord;
		else {						/* nowhere to go */
			if (DISTANCE(tp->t_pos.y, tp->t_pos.x, hero.y, hero.x) < 2)
				if (!onscare)
					ch_ret = hero;
		}
		if (ce(hero, ch_ret))
			ch = FIGHT;
	}
	return ch;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, 0x34(%rsp)
movq %rsi, 0x40(%rsp)
testb $0x2, 0x79(%rdi)
jne 0x7291
xorl %eax, %eax
movl %edx, 0x30(%rsp)
testb %dl, %dl
movl $0x7fff, %r13d          # imm = 0x7FFF
cmovnel %eax, %r13d
leaq 0x50(%rdi), %rbx
movq 0x50(%rdi), %rcx
movq %rcx, 0x265e6(%rip)     # 0x2d5d8
movq %rdi, 0x18(%rsp)
movq 0x58(%rdi), %rax
movq %rax, 0x38(%rsp)
movq %rcx, %rax
shrq $0x20, %rax
leal -0x1(%rax), %r14d
pushq $0x1
popq %r12
leaq 0x20(%rsp), %rbp
movl $0x0, 0x8(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0xc(%rsp)
movq %rbx, 0x10(%rsp)
leal 0x1(%rax), %edx
cmpl %edx, %r14d
jg 0x7309
movl (%rbx), %ecx
leal -0x1(%rcx), %r15d
subl %ecx, %r12d
leal 0x1(%rcx), %eax
cmpl %eax, %r15d
jg 0x727d
movl %r14d, %edi
movl %r15d, %esi
callq 0x9308
testb %al, %al
je 0x7251
movl %r15d, 0x20(%rsp)
movl %r14d, 0x24(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7398
testb %al, %al
je 0x7251
movl %r12d, 0x4(%rsp)
movl %r14d, %edi
movq %rbp, %r12
movl %r15d, %esi
callq 0x92ab
movl %eax, %ebx
movsbl %al, %ebp
movsbq %bpl, %rdi
callq 0xb3d0
testb %al, %al
movl %ebp, 0x8(%rsp)
je 0x716d
movl %ebp, %edi
callq 0xdbbb
testb %al, %al
movq %r12, %rbp
je 0x7114
movl %r14d, %edi
movl %r15d, %esi
xorl %eax, %eax
callq 0xea05
testq %rax, %rax
je 0x7114
movq 0x18(%rsp), %rcx
testb $0x2, 0x79(%rcx)
jne 0x7114
testb $0x8, 0x10(%rax)
je 0x7114
cmpb $0x22, 0x14(%rax)
jne 0x70f9
pushq $0x64
popq %rdi
xorl %eax, %eax
callq 0xc004
cmpl $0x50, %eax
jl 0x7270
leaq 0x26b50(%rip), %rax     # 0x2dc50
cmpl 0x54(%rax), %r14d
jne 0x7270
cmpl 0x50(%rax), %r15d
jne 0x7270
cmpb $0x3f, %bl
jne 0x7182
movl %r14d, %edi
movl %r15d, %esi
xorl %eax, %eax
callq 0xc996
testq %rax, %rax
je 0x7182
movq 0x10(%rax), %rax
cmpl $0x7, 0x28(%rax)
jne 0x7182
leaq 0x26b14(%rip), %rax     # 0x2dc50
cmpl 0x50(%rax), %r15d
movq 0x10(%rsp), %rbx
movl 0x4(%rsp), %r12d
jne 0x715e
cmpl %r14d, 0x54(%rax)
pushq $0x1
popq %rax
movl 0xc(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, 0xc(%rsp)
movl %r13d, %eax
pushq $0x3f
popq %rcx
movl %ecx, 0x8(%rsp)
jmp 0x7254
movl %r13d, %eax
movq 0x10(%rsp), %rbx
movq %r12, %rbp
movl 0x4(%rsp), %r12d
jmp 0x7254
movq 0x18(%rsp), %rax
cmpb $0x56, 0x8d(%rax)
movq 0x10(%rsp), %rbx
movl 0x4(%rsp), %r12d
jne 0x71ba
movq %rbp, %rdi
callq 0x6f44
testq %rax, %rax
je 0x71ba
movq %rax, %rdi
pushq $0x1
popq %rsi
callq 0x12f50
testl %eax, %eax
je 0x7251
leaq 0x26a8f(%rip), %rax     # 0x2dc50
cmpl 0x54(%rax), %r14d
jne 0x71dc
cmpl 0x50(%rax), %r15d
jne 0x71dc
movl %r13d, %eax
cmpb $0x0, 0x34(%rsp)
je 0x7254
jmp 0x72e2
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl %r12d, %ecx
imull %ecx, %ecx
movl 0x4(%rax), %eax
subl %r14d, %eax
imull %eax, %eax
addl %ecx, %eax
je 0x72f6
incl 0x2c(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
cmpl %r13d, %eax
setl %cl
setg %dl
cmpb $0x0, 0x30(%rsp)
cmovnel %edx, %ecx
cmpb $0x1, %cl
jne 0x7251
movq 0x18(%rsp), %rcx
cmpl 0x58(%rcx), %r15d
jne 0x7264
cmpl 0x5c(%rcx), %r14d
jne 0x7264
cmpb $0x0, 0x30(%rsp)
je 0x7251
movq 0x18(%rsp), %rcx
movq 0x70(%rcx), %rcx
leaq 0x26a10(%rip), %rdx     # 0x2dc50
cmpq %rcx, 0x70(%rdx)
setne %dl
testq %rcx, %rcx
sete %cl
orb %dl, %cl
je 0x7264
movl %r13d, %eax
incl %r15d
movl (%rbx), %ecx
decl %r12d
movl %eax, %r13d
jmp 0x7046
movq 0x20(%rsp), %rcx
movq %rcx, 0x38(%rsp)
jmp 0x7254
movl %r13d, %eax
movq 0x10(%rsp), %rbx
jmp 0x7178
incl %r14d
movq 0x18(%rsp), %rax
movl 0x54(%rax), %eax
pushq $0x1
popq %r12
jmp 0x7031
xorl %eax, %eax
movq %rdi, %r14
callq 0xea34
movq (%rax), %rbx
movq %rbx, 0x26333(%rip)     # 0x2d5d8
leaq 0x269a4(%rip), %rax     # 0x2dc50
movl 0x50(%rax), %ecx
movl %ebx, %ebp
subl %ecx, %ebp
imull %ebp, %ebp
shrq $0x20, %rbx
subl 0x54(%rax), %ebx
imull %ebx, %ebx
movl $0x3e8, %edi            # imm = 0x3E8
xorl %eax, %eax
callq 0xc004
cmpl $0x4, %eax
jg 0x72d6
andb $-0x3, 0x79(%r14)
xorl %eax, %eax
orl %ebp, %ebx
sete %al
jmp 0x7389
movq 0x20(%rsp), %rax
movq %rax, 0x262ea(%rip)     # 0x2d5d8
pushq $0x1
popq %rax
jmp 0x7389
movq 0x20(%rsp), %rax
movq %rax, 0x262d6(%rip)     # 0x2d5d8
xorl %eax, %eax
jmp 0x7389
cmpl $0x0, 0x2c(%rsp)
jle 0x7329
movq 0x38(%rsp), %rcx
movq %rcx, 0x262bc(%rip)     # 0x2d5d8
leaq 0x2692d(%rip), %rax     # 0x2dc50
movl 0x8(%rsp), %edx
jmp 0x7373
leaq 0x26920(%rip), %rsi     # 0x2dc50
movl 0x50(%rsi), %edx
subl %ecx, %edx
movl 0x54(%rsi), %ecx
imull %edx, %edx
subl %eax, %ecx
imull %ecx, %ecx
addl %edx, %ecx
cmpl $0x1, %ecx
movl 0x8(%rsp), %edx
ja 0x7366
cmpl $0x0, 0xc(%rsp)
jne 0x7366
leaq 0x268f7(%rip), %rax     # 0x2dc50
movq 0x50(%rax), %rcx
movq %rcx, 0x26274(%rip)     # 0x2d5d8
jmp 0x7373
movl 0x2626c(%rip), %ecx     # 0x2d5d8
leaq 0x268dd(%rip), %rax     # 0x2dc50
cmpl %ecx, 0x50(%rax)
jne 0x7387
movl 0x54(%rax), %eax
cmpl 0x2625b(%rip), %eax     # 0x2d5dc
pushq $0x1
popq %rax
cmovel %eax, %edx
movl %edx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq