// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x10(%rdi), %rbx
movq 0x78(%rbx), %r12
xorl %eax, %eax
andl $0x80, %r12d
movl $0x7fff, %r15d          # imm = 0x7FFF
cmovnel %eax, %r15d
movq 0x70(%rbx), %r14
cmpb $0x56, 0x8d(%rbx)
sete %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x69d1
movl $0x1, %esi
movq %r14, %rdi
callq 0x13e74
testl %eax, %eax
je 0x6adc
movq %r14, 0x30(%rsp)
movq %r12, 0x20(%rsp)
movl %r12d, %eax
shrl $0x7, %eax
movq %rax, %r12
movq 0x60(%rbx), %rdi
callq 0x7052
movq %rax, %r13
movq 0x60(%rbx), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x2352d(%rip), %r14     # 0x29f30
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movq %rbx, %rbp
movl 0x54(%rbx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6a29
movq (%r14), %rdi
callq 0x6330
cmpl $0x2b, %eax
sete %dl
jmp 0x6a2b
xorl %edx, %edx
movq %r12, %rcx
xorq $0x1, %rcx
movq 0x30(%rsp), %r14
movq %r14, %rax
orq %r13, %rax
je 0x6a78
movq %r13, %rsi
movl %edx, 0xc(%rsp)
testq %r13, %r13
sete %al
testq %r14, %r14
setne %dl
orb %al, %dl
movq %rbp, %rbx
jne 0x6aa7
xorl %r11d, %r11d
movq 0x20(%rsp), %r13
testq %r13, %r13
movl $0x0, %edx
cmoveq %rsi, %rdx
movq %r13, %rdi
movq %rsi, %rbp
jmp 0x6ca7
leaq 0x271e1(%rip), %rax     # 0x2dc60
movq %rbp, %rbx
movl 0x50(%rbp), %esi
movl 0x54(%rbp), %edx
subl 0x50(%rax), %esi
subl 0x54(%rax), %edx
imull %esi, %esi
imull %edx, %edx
addl %esi, %edx
cmpl $0x4, %edx
movl $0x1, %eax
cmovbq %rax, %rcx
jmp 0x6d4b
testq %r14, %r14
sete %al
testq %rsi, %rsi
setne %dl
orb %al, %dl
movq 0x20(%rsp), %r13
movq %rsi, %rbp
jne 0x6b0f
xorl %r11d, %r11d
cmpb $0x0, 0xc(%rsp)
movl $0x0, %edi
cmovneq %r13, %rdi
movq %r14, %rdx
cmovneq %r11, %rdx
jmp 0x6ca7
movl 0x50(%rbx), %esi
movl 0x54(%rbx), %edi
callq 0x6fcc
testb %al, %al
je 0x6af9
leaq 0x18580(%rip), %rdi     # 0x1f072
xorl %eax, %eax
callq 0xb6e0
movq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0xa109
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6fbb
cmpq %rbp, %r14
movl 0xc(%rsp), %eax
je 0x6b34
testq %r13, %r13
jne 0x6c62
testb %al, %al
movq %r14, %rdx
cmovneq %rbp, %rdx
movb $0x1, %r11b
xorl %edi, %edi
jmp 0x6ca7
testq %r13, %r13
je 0x6ca0
movq 0x10(%rsp), %rsi
cmpl $0x0, 0x60(%rbp)
jle 0x6c6a
movq %rsi, 0x38(%rsp)
xorl %r14d, %r14d
movl $0xffff8001, 0x8(%rsp)  # imm = 0xFFFF8001
movl $0x0, 0x2c(%rsp)
movl $0xffff8001, 0x4(%rsp)  # imm = 0xFFFF8001
movq 0x233bd(%rip), %r12     # 0x29f30
movq %rcx, 0x18(%rsp)
movq (%r12), %rdi
movl 0x18(%rbp,%r14,8), %edx
movl 0x1c(%rbp,%r14,8), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6ba8
movq (%r12), %rdi
callq 0x6330
cmpl $0x26, %eax
jne 0x6ba8
movq 0x18(%rsp), %rcx
jmp 0x6c36
incl 0x2c(%rsp)
movl 0x50(%rbx), %eax
movl 0x54(%rbx), %ecx
movl 0x18(%rbp,%r14,8), %edx
movl 0x1c(%rbp,%r14,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 0x27088(%rip), %rax     # 0x2dc60
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, %edi
subl %r8d, %edi
cmpl 0x8(%rsp), %edi
jle 0x6c0b
movl 0x4(%rsp), %eax
movq 0x18(%rsp), %rcx
jmp 0x6c24
sete %dl
cmpl 0x4(%rsp), %eax
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0x18(%rsp), %rcx
jne 0x6c36
movq 0x18(%rbp,%r14,8), %rdx
movq %rdx, 0x38(%rsp)
movl %eax, 0x4(%rsp)
movl %edi, 0x8(%rsp)
incq %r14
movslq 0x60(%rbp), %rax
cmpq %rax, %r14
jl 0x6b78
cmpl $0x0, 0x8(%rsp)
setle %dl
cmpl $0x2, 0x2c(%rsp)
setl %al
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rsi
jmp 0x6c6e
xorl %r11d, %r11d
movq %r13, %rdi
jmp 0x6ca5
movb $0x1, %al
movb $0x1, %dl
testb %dl, %dl
movl $0x1, %edi
cmovneq %rdi, %rcx
movq %rsi, 0x10(%rsp)
andb %dl, %al
movl 0xc(%rsp), %edx
orb %al, %dl
testb $0x1, %dl
je 0x6ca0
leaq 0x26fce(%rip), %rdx     # 0x2dc60
movq 0x50(%rdx), %rdx
movq %rdx, 0x10(%rsp)
movzbl %al, %ecx
jmp 0x6ca2
xorl %edi, %edi
xorl %r11d, %r11d
xorl %edx, %edx
testq %rdx, %rdx
je 0x6d50
movq %rdi, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq 0x10(%rsp), %rsi
cmpl $0x0, 0x60(%rdx)
jle 0x6d3c
movq 0x60(%rbx), %r8
movl (%r8), %edi
movl 0x4(%r8), %r8d
movl 0x60(%rdx), %r10d
cmpl $0x2, %r10d
movl $0x1, %r9d
cmovgel %r10d, %r9d
xorl %r10d, %r10d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl 0x18(%rdx,%r10,8), %eax
subl %edi, %eax
movl 0x1c(%rdx,%r10,8), %r14d
subl %r8d, %r14d
imull %eax, %eax
imull %r14d, %r14d
addl %eax, %r14d
testb %r11b, %r11b
je 0x6d11
cmpq %rbp, 0x38(%rdx,%r10,8)
cmovel %ecx, %r14d
xorl %eax, %eax
xorl %r12d, %r12d
cmpl %r15d, %r14d
setge %al
setle %r12b
testq %r13, %r13
cmovel %eax, %r12d
testb %r12b, %r12b
jne 0x6d34
movq 0x18(%rdx,%r10,8), %rsi
movl %r14d, %r15d
incq %r10
cmpq %r10, %r9
jne 0x6cea
movq %rsi, 0x10(%rsp)
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rdi
xorl %edx, %edx
testq %rdi, %rdi
setne %dl
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x70c5
cmpl $0x1, %eax
jne 0x6d80
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa4b4
xorl %ebp, %ebp
testb $0x3, 0x78(%rbx)
jne 0x6fbb
movl 0x2685a(%rip), %edi     # 0x2d5ec
movl 0x26850(%rip), %esi     # 0x2d5e8
xorl %eax, %eax
callq 0xf4ea
testq %rax, %rax
je 0x6df4
leaq 0x2683d(%rip), %rdi     # 0x2d5e8
movq %rbx, %rsi
callq 0xe528
movl %eax, %r12d
cmpb $0x2, %al
jne 0x6dcd
addq $0x50, %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0xae8c
jmp 0x6dd9
leaq 0x26ac5(%rip), %rax     # 0x2d899
cmpb $0x0, (%rax)
je 0x6df4
xorl %ebp, %ebp
cmpb $0x2, %r12b
sete %bpl
leaq 0x26aaf(%rip), %rax     # 0x2d899
movb $0x0, (%rax)
negl %ebp
jmp 0x6fbb
movq %r14, %r12
movl $0x1, %edi
callq 0x95a4
testb %al, %al
je 0x6e2e
leaq 0x29bd4(%rip), %r14     # 0x309e0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6e2e
movq (%r14), %rdi
movsbl 0x8f(%rbx), %esi
callq 0x64c0
leaq 0x29bab(%rip), %r14     # 0x309e0
movq (%r14), %rdi
movl 0x267ae(%rip), %esi     # 0x2d5ec
movl 0x267a4(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6e5b
movq (%r14), %rdi
callq 0x6330
movl %eax, %r15d
jmp 0x6e61
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x6eb5
movq %r12, %rdi
movl $0x1, %esi
callq 0x13e74
testl %eax, %eax
setne %al
cmpq $0x2e, %r15
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6eb5
movl 0x50(%rbx), %eax
movl 0x54(%rbx), %ecx
subl 0x26756(%rip), %eax     # 0x2d5e8
subl 0x26754(%rip), %ecx     # 0x2d5ec
imull %eax, %eax
imull %ecx, %ecx
addl %eax, %ecx
cmpl $0x2, %ecx
ja 0x6eb5
movl $0x1, %edi
callq 0x95a4
movb $0x20, %cl
testb %al, %al
jne 0x6eb8
movl %r15d, %ecx
movb %cl, 0x8f(%rbx)
movl 0x26728(%rip), %edi     # 0x2d5ec
movl 0x2671e(%rip), %esi     # 0x2d5e8
callq 0x6fcc
testb %al, %al
je 0x6f01
testb $0x10, 0x78(%rbx)
jne 0x6f01
movq (%r14), %rdi
movl 0x2670a(%rip), %esi     # 0x2d5ec
movl 0x26700(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6f01
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64c0
leaq 0x29ae8(%rip), %r14     # 0x309f0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6f28
movq (%r14), %rdi
movl $0x20, %esi
callq 0x64c0
movq (%r14), %rdi
movl 0x266bb(%rip), %esi     # 0x2d5ec
movl 0x266b1(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6f50
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64c0
movq 0x50(%rbx), %rax
movq %rax, 0x58(%rbx)
leaq 0x26689(%rip), %rdi     # 0x2d5e8
movq 0x26682(%rip), %rax     # 0x2d5e8
movq %rax, 0x50(%rbx)
callq 0x7052
movq %rax, 0x70(%rbx)
btl $0xa, 0x78(%rbx)
movl $0x5, %eax
movl $0x28, %r14d
cmovael %eax, %r14d
xorl %ebp, %ebp
movl $0x64, %edi
xorl %eax, %eax
callq 0xc72d
cmpl %r14d, %eax
jge 0x6fbb
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 0xdd69
testb %al, %al
jne 0x6fbb
andb $0x7f, 0x78(%rbx)
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq