// File: /workspace/github_c_makefile_star2_O2/ajpaulson[P]srogue11/chase.c
// Function: do_chase
int do_chase(struct linked_list *mon)
{
	struct thing *th;
	struct room *rer, *ree, *rxx;
	int mindist, i, dist;
	struct stats *st;
	bool stoprun = FALSE, ondoor = FALSE, link = FALSE;
	unsigned long runaway, dofight, wound, sch, ch;
	struct coord this;
	struct trap *trp;

	th = THINGPTR(mon);
	wound = th->t_flags & ISWOUND;
	if (wound)
		mindist = 0;
	else
		mindist = FARAWAY;
	runaway = wound;
	dofight = !runaway;
	rer = th->t_room;
	if (th->t_type == 'V') {
		if (rer != NULL && !rf_on(rer, ISDARK)) {
			/*
			 * Vampires can't stand the light
			 */
			if (cansee(th->t_pos.y, th->t_pos.x))
				msg("The vampire vaporizes into thin air !");
			killed(mon, FALSE);
			return(-1);
		}
	}
	ree = roomin(th->t_dest);	/* room of chasee */
	this = *th->t_dest;
	/*
	 * If the object of our desire is in a different
	 * room, then run to the door nearest to our goal.
	 */
	if (mvinch(th->t_pos.y, th->t_pos.x) == DOOR)
		ondoor = TRUE;
	rxx = NULL;
	if (rer != NULL || ree != NULL) {
		/*
		 * Monster not in room, hero in room. Run to closest door
		 * in hero's room if not wounded. Run away if wounded.
		 */
		if (rer == NULL && ree != NULL) {
			if (!wound)
				rxx = ree;
		}
		/*
		 * Monster in a room, hero not in room. If on a door,
		 * then use closest distance. If not on a door, then
		 * run to closest door in monsters room.
		 */
		else if (rer != NULL && ree == NULL) {
			if (!ondoor) {
				rxx = rer;
				if (wound)
					runaway = FALSE;
			}
		}
		/*
		 * Both hero and monster in a DIFFERENT room. Set flag to
		 * check for links between the monster's and hero's rooms.
		 * If no links are found, then the closest door in the
		 * monster's room is used.
		 */
		else if (rer != ree) {
			if (!wound) {
				link = TRUE;
				if (ondoor)
					rxx = ree;	/* if on door, run to heros room */
				else
					rxx = rer;	/* else to nearest door this room */
			}
		}
		/*
		 * Both hero and monster in same room. If monster is
		 * wounded, find the best door to run to.
		 */
		else if (wound) {
			struct coord *ex;
			int poss, mdtd, hdtd, ghdtd, nx, gx = 0, best;

			best = ghdtd = -FARAWAY;
			for (nx = 0; nx < ree->r_nexits; nx++) {
				ex = &ree->r_exit[nx];
				if (mvinch(ex->y, ex->x) == SECRETDOOR)
					continue;
				gx += 1;
				mdtd = abs(th->t_pos.y - ex->y) + abs(th->t_pos.x - ex->x);
				hdtd = abs(hero.y - ex->y) + abs(hero.x - ex->x);
				poss = hdtd - mdtd;				/* possible move */
				if (poss > best) {
					best = poss;
					this = *ex;
				}
				else if (poss == best && hdtd > ghdtd) {
					ghdtd = hdtd;
					best = poss;
					this = *ex;
				}
			}
			runaway = FALSE;		/* go for target */
			if (best < 1)
				dofight = TRUE;		/* fight if we must */
			mdtd = (gx <= 1 && best < 1);
			if (ondoor || mdtd) {
				this = hero;
				runaway = TRUE;
				if (!mdtd)
					dofight = FALSE;
			}
		}
		if (rxx != NULL) {
			for (i = 0; i < rxx->r_nexits; i += 1) {
				dist = RDIST(th->t_dest, rxx->r_exit[i]);
				if (link && rxx->r_ptr[i] == ree)
					dist = -1;
				if ((!wound && dist < mindist) ||
				    (wound && dist > mindist)) {
					this = rxx->r_exit[i];
					mindist = dist;
				}
			}
		}
	}
	else if (DISTANCE(hero.y, hero.x, th->t_pos.y, th->t_pos.x) <= 3)
		dofight = TRUE;
	/*
	 * this now contains what we want to run to this time
	 * so we run to it.  If we hit it we either want to
	 * fight it or stop running.
	 */
	if (chase(th, &this, runaway, dofight) == FIGHT) {
		return( attack(th) );
	}
	else if ((th->t_flags & (ISSTUCK | ISPARA)))
		return(0);				/* if paralyzed or stuck */
	if ((trp = trap_at(ch_ret.y, ch_ret.x)) != NULL) {
		ch = be_trapped(&ch_ret, th);
		if (ch == GONER || nlmove) {
			if (ch == GONER)
				remove_monster(&th->t_pos, mon);
			nlmove = FALSE;
			return((ch == GONER) ? -1 : 0);
		}
	}
	if (pl_off(ISBLIND))
		mvwaddch(cw,th->t_pos.y,th->t_pos.x,th->t_oldch);
	sch = mvwinch(cw, ch_ret.y, ch_ret.x);
	if (rer != NULL && rf_on(rer,ISDARK) && sch == FLOOR &&
	  DISTANCE(ch_ret.y,ch_ret.x,th->t_pos.y,th->t_pos.x) < 3 &&
	  pl_off(ISBLIND))
		th->t_oldch = ' ';
	else
		th->t_oldch = sch;
	if (cansee(unc(ch_ret)) && off(*th, ISINVIS))
		mvwaddch(cw, ch_ret.y, ch_ret.x, th->t_type);
	mvwaddch(mw, th->t_pos.y, th->t_pos.x, ' ');
	mvwaddch(mw, ch_ret.y, ch_ret.x, th->t_type);
	th->t_oldpos = th->t_pos;
	th->t_pos = ch_ret;
	th->t_room = roomin(&ch_ret);
	i = 5;
	if (th->t_flags & ISREGEN)
		i = 40;
	st = &th->t_stats;
	if (rnd(100) < i) {
		if (++st->s_hpt > st->s_maxhp)
			st->s_hpt = st->s_maxhp;
		if (!monhurt(th))
			th->t_flags &= ~ISWOUND;
	}
	if (stoprun && ce(th->t_pos, *(th->t_dest)))
		th->t_flags &= ~ISRUN;
	return CHASE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x10(%rdi), %rbx
movq 0x70(%rbx), %r15
movq 0x78(%rbx), %r12
xorl %eax, %eax
andl $0x80, %r12d
movl $0x7fff, %r14d          # imm = 0x7FFF
cmovnel %eax, %r14d
cmpb $0x56, 0x8d(%rbx)
sete %al
testq %r15, %r15
setne %cl
andb %al, %cl
pushq $-0x1
popq %rbp
cmpb $0x1, %cl
jne 0x694a
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x12f50
testl %eax, %eax
je 0x6a40
movq %r15, 0x8(%rsp)
movl %r12d, %r15d
shrl $0x7, %r15d
movq 0x60(%rbx), %rdi
callq 0x6f44
movq %rax, %r13
movq 0x60(%rbx), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x10(%rsp)
movq 0x235be(%rip), %rbp     # 0x29f38
movq (%rbp), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6470
cmpl $-0x1, %eax
je 0x69a0
movq (%rbp), %rdi
callq 0x6320
cmpl $0x2b, %eax
sete %r8b
jmp 0x69a3
xorl %r8d, %r8d
movq %r15, %rcx
xorq $0x1, %rcx
movq 0x8(%rsp), %rsi
movq %rsi, %rax
orq %r13, %rax
pushq $0x1
popq %rdx
pushq $-0x1
popq %r11
je 0x69ec
testq %r13, %r13
setne %al
testq %rsi, %rsi
sete %dil
andb %al, %dil
cmpb $0x1, %dil
jne 0x6a13
testq %r12, %r12
movq 0x28(%rsp), %r15
jne 0x6b00
xorl %eax, %eax
movq %r13, %r10
jmp 0x6a97
leaq 0x2725d(%rip), %rax     # 0x2dc50
movl 0x50(%rbx), %edi
movl 0x54(%rbx), %esi
subl 0x50(%rax), %edi
imull %edi, %edi
subl 0x54(%rax), %esi
imull %esi, %esi
addl %edi, %esi
cmpl $0x4, %esi
cmovbq %rdx, %rcx
jmp 0x6b00
testq %rsi, %rsi
setne %al
testq %r13, %r13
sete %dil
andb %al, %dil
cmpb $0x1, %dil
movq 0x28(%rsp), %r15
jne 0x6a6e
testb %r8b, %r8b
jne 0x6b00
xorl %eax, %eax
movq 0x8(%rsp), %r10
jmp 0x6a97
movl 0x50(%rbx), %esi
movl 0x54(%rbx), %edi
callq 0x6ec2
testb %al, %al
je 0x6a5d
leaq 0x1661c(%rip), %rdi     # 0x1d072
xorl %eax, %eax
callq 0xb178
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x9d41
jmp 0x6e61
cmpq %r13, %rsi
je 0x6c0d
testq %r12, %r12
jne 0x6b00
testb %r8b, %r8b
movq 0x8(%rsp), %r10
cmovneq %r13, %r10
movb $0x1, %al
testq %r10, %r10
je 0x6eb7
movslq 0x60(%r10), %rdx
xorl %esi, %esi
cmpq %rdx, %rsi
jge 0x6af8
movq 0x60(%rbx), %r8
movl 0x18(%r10,%rsi,8), %r9d
movl 0x1c(%r10,%rsi,8), %edi
subl (%r8), %r9d
subl 0x4(%r8), %edi
imull %r9d, %r9d
imull %edi, %edi
addl %r9d, %edi
testb %al, %al
je 0x6ace
cmpq %r13, 0x38(%r10,%rsi,8)
cmovel %r11d, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpl %r14d, %edi
setge %r8b
setle %r9b
testq %r12, %r12
cmovel %r8d, %r9d
testb %r9b, %r9b
jne 0x6af3
movq 0x18(%r10,%rsi,8), %r15
movl %edi, %r14d
incq %rsi
jmp 0x6a9d
movq %r15, 0x10(%rsp)
xorl %r12d, %r12d
xorl %edx, %edx
testq %r12, %r12
setne %dl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x6fb0
cmpl $0x1, %eax
jne 0x6b30
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa091
xorl %ebp, %ebp
testb $0x3, 0x78(%rbx)
jne 0x6e61
movl 0x26a9a(%rip), %edi     # 0x2d5dc
movl 0x26a90(%rip), %esi     # 0x2d5d8
xorl %eax, %eax
callq 0xea05
testq %rax, %rax
je 0x6ba4
leaq 0x26a7d(%rip), %rdi     # 0x2d5d8
movq %rbx, %rsi
callq 0xdbe6
movl %eax, %r12d
cmpb $0x2, %al
jne 0x6b7d
addq $0x50, %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0xa93f
jmp 0x6b89
leaq 0x26d05(%rip), %rax     # 0x2d889
cmpb $0x0, (%rax)
je 0x6ba4
xorl %ebp, %ebp
cmpb $0x2, %r12b
sete %bpl
leaq 0x26cef(%rip), %rax     # 0x2d889
movb $0x0, (%rax)
negl %ebp
jmp 0x6e61
pushq $0x1
popq %rdi
callq 0x933b
testb %al, %al
je 0x6bd9
leaq 0x29e19(%rip), %r14     # 0x309d0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6470
cmpl $-0x1, %eax
je 0x6bd9
movq (%r14), %rdi
movsbl 0x8f(%rbx), %esi
callq 0x64a0
leaq 0x29df0(%rip), %r14     # 0x309d0
movq (%r14), %rdi
movl 0x269f3(%rip), %esi     # 0x2d5dc
movl 0x269e9(%rip), %edx     # 0x2d5d8
callq 0x6470
cmpl $-0x1, %eax
je 0x6d0b
movq (%r14), %rdi
callq 0x6320
movl %eax, %r15d
jmp 0x6d11
testq %r12, %r12
je 0x6afd
movq $0x0, 0x18(%rsp)
movl %r8d, 0x24(%rsp)
movl $0xffff8001, 0x4(%rsp)  # imm = 0xFFFF8001
xorl %r12d, %r12d
movl $0xffff8001, (%rsp)     # imm = 0xFFFF8001
movq 0x232fb(%rip), %rbp     # 0x29f38
movslq 0x60(%r13), %rax
cmpq %rax, %r12
jge 0x6e72
movq %rcx, %r14
movq (%rbp), %rdi
movl 0x18(%r13,%r12,8), %edx
movl 0x1c(%r13,%r12,8), %esi
callq 0x6470
cmpl $-0x1, %eax
je 0x6c7b
movq (%rbp), %rdi
callq 0x6320
cmpl $0x26, %eax
jne 0x6c7b
movq %r14, %rcx
jmp 0x6d03
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movl 0x50(%rbx), %eax
movl 0x54(%rbx), %ecx
movl 0x18(%r13,%r12,8), %edx
movl 0x1c(%r13,%r12,8), %esi
subl %esi, %ecx
movl %ecx, %edi
negl %edi
cmovsl %ecx, %edi
subl %edx, %eax
movl %eax, %r8d
negl %r8d
cmovsl %eax, %r8d
leaq 0x26f9d(%rip), %rax     # 0x2dc50
movl 0x50(%rax), %ecx
movl 0x54(%rax), %eax
subl %esi, %eax
movl %eax, %esi
negl %esi
cmovsl %eax, %esi
subl %edx, %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
addl %esi, %eax
addl %edi, %r8d
movl %eax, %ecx
subl %r8d, %ecx
cmpl (%rsp), %ecx
jle 0x6ce6
movl 0x4(%rsp), %eax
movl %ecx, (%rsp)
movq %r14, %rcx
jmp 0x6cfa
sete %cl
cmpl 0x4(%rsp), %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
movq %r14, %rcx
jne 0x6d03
movq 0x18(%r13,%r12,8), %r15
movl %eax, 0x4(%rsp)
incq %r12
jmp 0x6c3d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x6d63
pushq $0x1
popq %rsi
callq 0x12f50
testl %eax, %eax
setne %al
cmpq $0x2e, %r15
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6d63
movl 0x50(%rbx), %eax
movl 0x54(%rbx), %ecx
subl 0x26896(%rip), %eax     # 0x2d5d8
subl 0x26894(%rip), %ecx     # 0x2d5dc
imull %eax, %eax
imull %ecx, %ecx
addl %eax, %ecx
cmpl $0x2, %ecx
ja 0x6d63
pushq $0x1
popq %rdi
callq 0x933b
movb $0x20, %cl
testb %al, %al
jne 0x6d66
movl %r15d, %ecx
movb %cl, 0x8f(%rbx)
movl 0x2686a(%rip), %edi     # 0x2d5dc
movl 0x26860(%rip), %esi     # 0x2d5d8
callq 0x6ec2
testb %al, %al
je 0x6daf
testb $0x10, 0x78(%rbx)
jne 0x6daf
movq (%r14), %rdi
movl 0x2684c(%rip), %esi     # 0x2d5dc
movl 0x26842(%rip), %edx     # 0x2d5d8
callq 0x6470
cmpl $-0x1, %eax
je 0x6daf
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64a0
leaq 0x29c2a(%rip), %r14     # 0x309e0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6470
cmpl $-0x1, %eax
je 0x6dd4
movq (%r14), %rdi
pushq $0x20
popq %rsi
callq 0x64a0
movq (%r14), %rdi
movl 0x267ff(%rip), %esi     # 0x2d5dc
movl 0x267f5(%rip), %edx     # 0x2d5d8
callq 0x6470
cmpl $-0x1, %eax
je 0x6dfc
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64a0
movq 0x50(%rbx), %rax
movq %rax, 0x58(%rbx)
leaq 0x267cd(%rip), %rdi     # 0x2d5d8
movq 0x267c6(%rip), %rax     # 0x2d5d8
movq %rax, 0x50(%rbx)
callq 0x6f44
movq %rax, 0x70(%rbx)
btl $0xa, 0x78(%rbx)
pushq $0x5
popq %rax
pushq $0x28
popq %r14
cmovael %eax, %r14d
xorl %ebp, %ebp
pushq $0x64
popq %rdi
xorl %eax, %eax
callq 0xc004
cmpl %r14d, %eax
jge 0x6e61
movl 0x30(%rbx), %eax
movl 0x34(%rbx), %ecx
leal 0x1(%rax), %edx
cmpl %ecx, %eax
cmovgel %ecx, %edx
movl %edx, 0x30(%rbx)
movq %rbx, %rdi
callq 0xd4f6
testb %al, %al
jne 0x6e61
andb $0x7f, 0x78(%rbx)
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x10(%rsp)
cmpl $0x0, (%rsp)
setle %dl
pushq $0x1
popq %r12
cmovleq %r12, %rcx
cmpl $0x2, 0x18(%rsp)
setl %al
andb %dl, %al
movl 0x24(%rsp), %edx
orb %al, %dl
cmpb $0x1, %dl
jne 0x6afd
leaq 0x26daa(%rip), %rcx     # 0x2dc50
movq 0x50(%rcx), %rcx
movq %rcx, 0x10(%rsp)
movzbl %al, %ecx
jmp 0x6b00
xorl %r12d, %r12d
movq %rdx, %rcx
jmp 0x6b00