// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movl %ecx, 0x4c(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, %r14
testb $0x2, 0x79(%rdi)
jne 0x7483
leaq 0x50(%r14), %r15
movq 0x50(%r14), %rax
movq %rax, 0x264f0(%rip)     # 0x2d5e8
movl 0x54(%r14), %r12d
movq 0x58(%r14), %rax
movq %rax, 0x40(%rsp)
decl %r12d
xorl %eax, %eax
movl %edx, 0x34(%rsp)
testb %dl, %dl
movl $0x7fff, %ecx           # imm = 0x7FFF
cmovnel %eax, %ecx
movl %ecx, 0xc(%rsp)
leaq 0x28(%rsp), %rbx
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x30(%rsp)
xorl %ebp, %ebp
movq %r14, 0x20(%rsp)
movq %r15, 0x10(%rsp)
movl (%r15), %r13d
movl $0x1, %r14d
subl %r13d, %r14d
addl $-0x2, %r13d
incl %r13d
movl %r12d, %edi
movl %r13d, %esi
callq 0x9571
testb %al, %al
je 0x73d9
movl %r13d, 0x28(%rsp)
movl %r12d, 0x2c(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x74e2
testb %al, %al
je 0x73d9
movq %rbx, %r15
movl %r12d, %edi
movl %r13d, %esi
callq 0x9514
movl %eax, %ebx
movsbl %al, %ebp
movsbq %bpl, %rdi
callq 0xb995
testb %al, %al
je 0x726d
movl %ebp, 0x3c(%rsp)
movl %ebp, %edi
callq 0xe4fd
testb %al, %al
je 0x7216
movl %r12d, %edi
movl %r13d, %esi
xorl %eax, %eax
callq 0xf4ea
testq %rax, %rax
je 0x7216
movq 0x20(%rsp), %rcx
testb $0x2, 0x79(%rcx)
jne 0x7216
testb $0x8, 0x10(%rax)
je 0x7216
cmpb $0x22, 0x14(%rax)
jne 0x71f6
movl $0x64, %edi
xorl %eax, %eax
callq 0xc72d
movl $0x7, %ebp
cmpl $0x50, %eax
jl 0x73eb
movl $0x7, %ebp
leaq 0x26a5e(%rip), %rax     # 0x2dc60
cmpl 0x54(%rax), %r12d
jne 0x73eb
cmpl 0x50(%rax), %r13d
jne 0x73eb
cmpb $0x3f, %bl
jne 0x727a
xorl %ebp, %ebp
movl %r12d, %edi
movl %r13d, %esi
xorl %eax, %eax
callq 0xd15e
movb $0x1, %cl
testq %rax, %rax
movq %r15, %rbx
je 0x7284
movq 0x10(%rax), %rax
cmpl $0x7, 0x28(%rax)
movq 0x10(%rsp), %r15
jne 0x7289
movl $0x7, %ebp
leaq 0x26a11(%rip), %rax     # 0x2dc60
cmpl 0x50(%rax), %r13d
jne 0x7269
cmpl %r12d, 0x54(%rax)
movl 0x1c(%rsp), %eax
movl $0x1, %ecx
cmovel %ecx, %eax
movl %eax, 0x1c(%rsp)
xorl %ecx, %ecx
jmp 0x7289
movq %r15, %rbx
movq 0x10(%rsp), %r15
jmp 0x73d9
movq %r15, %rbx
movq 0x10(%rsp), %r15
jmp 0x7291
movq 0x10(%rsp), %r15
testb %cl, %cl
je 0x73c0
movq 0x20(%rsp), %rax
cmpb $0x56, 0x8d(%rax)
movl 0xc(%rsp), %ecx
jne 0x72d6
movq %rbx, %rdi
callq 0x7052
testq %rax, %rax
je 0x72c1
movl $0x1, %esi
movq %rax, %rdi
callq 0x13e74
testl %eax, %eax
je 0x72c7
xorl %ebp, %ebp
movb $0x1, %al
jmp 0x72ce
movl $0x7, %ebp
xorl %eax, %eax
testb %al, %al
movl 0xc(%rsp), %ecx
je 0x7315
leaq 0x26983(%rip), %rax     # 0x2dc60
cmpl 0x54(%rax), %r12d
jne 0x731c
cmpl 0x50(%rax), %r13d
jne 0x731c
movl $0x7, %ebp
cmpb $0x0, 0x4c(%rsp)
je 0x7315
movq 0x28(%rsp), %rax
movq %rax, 0x262e7(%rip)     # 0x2d5e8
movl $0x1, %ebp
movl %ecx, %eax
movl $0x1, 0x38(%rsp)
jmp 0x73c4
movl %ecx, %eax
jmp 0x73c4
movq 0x50(%rsp), %rax
movl (%rax), %ecx
addl %r14d, %ecx
imull %ecx, %ecx
movl 0x4(%rax), %eax
subl %r12d, %eax
imull %eax, %eax
addl %ecx, %eax
je 0x73a7
incl 0x30(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%rsp), %esi
cmpl %esi, %eax
setl %cl
setg %dl
cmpb $0x0, 0x34(%rsp)
cmovnel %edx, %ecx
xorl %ebp, %ebp
cmpb $0x1, %cl
jne 0x73e7
movq 0x20(%rsp), %rcx
cmpl 0x58(%rcx), %r13d
jne 0x739b
movq 0x20(%rsp), %rcx
cmpl 0x5c(%rcx), %r12d
jne 0x739b
cmpb $0x0, 0x34(%rsp)
je 0x73c0
movq 0x20(%rsp), %rcx
movq 0x70(%rcx), %rcx
leaq 0x268d6(%rip), %rdx     # 0x2dc60
cmpq %rcx, 0x70(%rdx)
setne %dl
testq %rcx, %rcx
sete %cl
orb %dl, %cl
jne 0x73c0
movq 0x28(%rsp), %rcx
movq %rcx, 0x40(%rsp)
jmp 0x73c4
movq 0x28(%rsp), %rax
movq %rax, 0x26235(%rip)     # 0x2d5e8
movl $0x0, 0x38(%rsp)
movl $0x1, %ebp
movl 0xc(%rsp), %eax
cmpl $0x7, %ebp
je 0x73d1
testl %ebp, %ebp
jne 0x74cf
movl %eax, 0xc(%rsp)
movl 0x3c(%rsp), %ebp
decl %r14d
cmpl (%r15), %r13d
jle 0x714d
jmp 0x73f9
movl %esi, %eax
jmp 0x73c4
movl 0xc(%rsp), %eax
movq %r15, %rbx
movq 0x10(%rsp), %r15
jmp 0x73c4
leal 0x1(%r12), %ecx
movq 0x20(%rsp), %r14
movl 0x54(%r14), %eax
cmpl %eax, %r12d
movl %ecx, %r12d
jle 0x713d
cmpl $0x0, 0x30(%rsp)
jg 0x7450
leaq 0x2683f(%rip), %rdx     # 0x2dc60
movl 0x50(%rdx), %ecx
movl 0x54(%rdx), %edx
subl (%r15), %ecx
imull %ecx, %ecx
subl %eax, %edx
imull %edx, %edx
addl %ecx, %edx
cmpl $0x1, %edx
ja 0x745c
cmpl $0x0, 0x1c(%rsp)
jne 0x745c
leaq 0x26819(%rip), %rax     # 0x2dc60
movq 0x50(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x2618c(%rip)     # 0x2d5e8
leaq 0x267fd(%rip), %rcx     # 0x2dc60
movl 0x50(%rcx), %eax
cmpl 0x2617c(%rip), %eax     # 0x2d5e8
jne 0x747f
movl 0x54(%rcx), %eax
cmpl 0x26175(%rip), %eax     # 0x2d5ec
movl $0x1, %eax
cmovel %eax, %ebp
movl %ebp, %eax
jmp 0x74d3
movq %r14, %rdi
xorl %eax, %eax
callq 0xf524
movq (%rax), %rbx
movq %rbx, 0x26151(%rip)     # 0x2d5e8
leaq 0x267c2(%rip), %rax     # 0x2dc60
subl 0x50(%rax), %ebx
imull %ebx, %ebx
movl 0x26142(%rip), %ebp     # 0x2d5ec
subl 0x54(%rax), %ebp
imull %ebp, %ebp
movl $0x3e8, %edi            # imm = 0x3E8
xorl %eax, %eax
callq 0xc72d
cmpl $0x4, %eax
jg 0x74c6
andb $-0x3, 0x79(%r14)
xorl %eax, %eax
orl %ebx, %ebp
sete %al
jmp 0x74d3
movl 0x38(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq