// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %ecx, 0x24(%rsp)
movq %rsi, 0x30(%rsp)
testb $0x2, 0x79(%rdi)
jne 0x733b
leaq 0x50(%rdi), %r12
movq 0x50(%rdi), %r14
movq %r14, 0x26529(%rip)     # 0x2d5e8
shrq $0x20, %r14
movq %rdi, 0x10(%rsp)
movq 0x58(%rdi), %rax
movq %rax, 0x28(%rsp)
decl %r14d
xorl %eax, %eax
movl %edx, 0x1c(%rsp)
testb %dl, %dl
movl $0x7fff, %ecx           # imm = 0x7FFF
cmovnel %eax, %ecx
movl %ecx, 0x20(%rsp)
movq %rsp, %rbx
movl $0x0, 0xc(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movl (%r12), %r13d
movl $0x1, %r15d
subl %r13d, %r15d
addl $-0x2, %r13d
incl %r13d
movl %r14d, %edi
movl %r13d, %esi
callq 0x94ef
testb %al, %al
je 0x72fa
movl %r13d, (%rsp)
movl %r14d, 0x4(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x742a
testb %al, %al
je 0x72fa
movl %r14d, %edi
movl %r13d, %esi
callq 0x9492
movl %eax, %ebx
movsbl %al, %ebp
movsbq %bpl, %rdi
callq 0xb8d5
testb %al, %al
movl %ebp, 0x8(%rsp)
je 0x7219
movl %ebp, %edi
callq 0xe41e
testb %al, %al
je 0x71c3
movl %r14d, %edi
movl %r13d, %esi
xorl %eax, %eax
callq 0xf3d8
testq %rax, %rax
je 0x71c3
movq 0x10(%rsp), %rcx
testb $0x2, 0x79(%rcx)
jne 0x71c3
testb $0x8, 0x10(%rax)
je 0x71c3
cmpb $0x22, 0x14(%rax)
jne 0x71b0
movl $0x64, %edi
xorl %eax, %eax
callq 0xc674
cmpl $0x50, %eax
jl 0x7219
leaq 0x26aa9(%rip), %rax     # 0x2dc60
cmpl 0x54(%rax), %r14d
jne 0x7219
cmpl 0x50(%rax), %r13d
jne 0x7219
cmpb $0x3f, %bl
jne 0x7221
movl %r14d, %edi
movl %r13d, %esi
xorl %eax, %eax
callq 0xd104
testq %rax, %rax
je 0x7221
movq 0x10(%rax), %rax
cmpl $0x7, 0x28(%rax)
jne 0x7221
movl $0x3f, 0x8(%rsp)
leaq 0x26a6d(%rip), %rax     # 0x2dc60
cmpl 0x50(%rax), %r13d
movq %rsp, %rbx
jne 0x72fa
cmpl %r14d, 0x54(%rax)
movl 0xc(%rsp), %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movl %eax, 0xc(%rsp)
jmp 0x72fa
movq %rsp, %rbx
jmp 0x72fa
movq 0x10(%rsp), %rax
cmpb $0x56, 0x8d(%rax)
movq %rsp, %rbx
jne 0x7254
movq %rbx, %rdi
callq 0x701c
testq %rax, %rax
je 0x7254
movl $0x1, %esi
movq %rax, %rdi
callq 0x13d18
testl %eax, %eax
je 0x72fa
leaq 0x26a05(%rip), %rax     # 0x2dc60
cmpl 0x54(%rax), %r14d
jne 0x7277
cmpl 0x50(%rax), %r13d
jne 0x7277
cmpb $0x0, 0x24(%rsp)
je 0x72fa
jmp 0x7390
movq 0x30(%rsp), %rax
movl (%rax), %ecx
addl %r15d, %ecx
imull %ecx, %ecx
movl 0x4(%rax), %eax
subl %r14d, %eax
imull %eax, %eax
addl %ecx, %eax
je 0x73a2
incl 0x18(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
cmpl 0x20(%rsp), %eax
setl %cl
setg %dl
cmpb $0x0, 0x1c(%rsp)
cmovnel %edx, %ecx
cmpb $0x1, %cl
jne 0x72fa
movq 0x10(%rsp), %rcx
cmpl 0x58(%rcx), %r13d
jne 0x72ed
cmpl 0x5c(%rcx), %r14d
jne 0x72ed
cmpb $0x0, 0x1c(%rsp)
je 0x72fa
movq 0x10(%rsp), %rcx
movq 0x70(%rcx), %rcx
leaq 0x26984(%rip), %rdx     # 0x2dc60
cmpq %rcx, 0x70(%rdx)
setne %dl
testq %rcx, %rcx
sete %cl
orb %dl, %cl
jne 0x72fa
movq (%rsp), %rcx
movq %rcx, 0x28(%rsp)
movl %eax, 0x20(%rsp)
decl %r15d
cmpl (%r12), %r13d
jle 0x7114
movq 0x10(%rsp), %rax
movl 0x54(%rax), %eax
leal 0x1(%r14), %ecx
cmpl %eax, %r14d
movl %ecx, %r14d
jle 0x7103
cmpl $0x0, 0x18(%rsp)
jle 0x73b1
movq 0x28(%rsp), %rcx
movq %rcx, 0x262b2(%rip)     # 0x2d5e8
jmp 0x73f2
xorl %eax, %eax
movq %rdi, %r15
callq 0xf412
movq (%rax), %rbx
movq %rbx, 0x26299(%rip)     # 0x2d5e8
leaq 0x2690a(%rip), %rax     # 0x2dc60
movl 0x50(%rax), %ecx
movl %ebx, %r14d
subl %ecx, %r14d
imull %r14d, %r14d
shrq $0x20, %rbx
subl 0x54(%rax), %ebx
imull %ebx, %ebx
movl $0x3e8, %edi            # imm = 0x3E8
xorl %eax, %eax
callq 0xc674
cmpl $0x4, %eax
jg 0x7383
andb $-0x3, 0x79(%r15)
xorl %eax, %eax
orl %r14d, %ebx
sete %al
jmp 0x741b
movq (%rsp), %rax
movq %rax, 0x2624d(%rip)     # 0x2d5e8
movl $0x1, %eax
jmp 0x741b
movq (%rsp), %rax
movq %rax, 0x2623b(%rip)     # 0x2d5e8
xorl %eax, %eax
jmp 0x741b
leaq 0x268a8(%rip), %rdx     # 0x2dc60
movl 0x50(%rdx), %ecx
movl 0x54(%rdx), %edx
subl (%r12), %ecx
imull %ecx, %ecx
subl %eax, %edx
imull %edx, %edx
addl %ecx, %edx
cmpl $0x1, %edx
ja 0x73ec
cmpl $0x0, 0xc(%rsp)
jne 0x73ec
leaq 0x26881(%rip), %rax     # 0x2dc60
movq 0x50(%rax), %rcx
movq %rcx, 0x261fe(%rip)     # 0x2d5e8
jmp 0x73f9
movl 0x261f6(%rip), %ecx     # 0x2d5e8
leaq 0x26867(%rip), %rax     # 0x2dc60
cmpl %ecx, 0x50(%rax)
jne 0x7417
movl 0x54(%rax), %eax
cmpl 0x261e5(%rip), %eax     # 0x2d5ec
movl $0x1, %eax
movl 0x8(%rsp), %ecx
cmovel %eax, %ecx
movl %ecx, %eax
jmp 0x741b
movl 0x8(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq