// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq 0x10(%rdi), %r13
movq 0x78(%r13), %r12
xorl %eax, %eax
andl $0x80, %r12d
movl $0x7fff, %r14d          # imm = 0x7FFF
cmovnel %eax, %r14d
movq 0x70(%r13), %rdi
cmpb $0x56, 0x8d(%r13)
sete %al
testq %rdi, %rdi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
movq %rdi, 0x28(%rsp)
jne 0x69da
movl $0x1, %esi
callq 0x13d18
testl %eax, %eax
je 0x6acc
movq %r15, 0x30(%rsp)
movl %r12d, %ebx
shrl $0x7, %ebx
movq 0x60(%r13), %rdi
callq 0x701c
movq %r13, %rcx
movq %rax, %r13
movq 0x60(%rcx), %rax
movq (%rax), %rax
movq %rax, %r15
movq %rax, 0x18(%rsp)
movq 0x23526(%rip), %rbp     # 0x29f30
movq (%rbp), %rdi
movl 0x50(%rcx), %edx
movq %rcx, 0x8(%rsp)
movl 0x54(%rcx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6a35
movq (%rbp), %rdi
callq 0x6330
cmpl $0x2b, %eax
sete %sil
jmp 0x6a37
xorl %esi, %esi
movq %rbx, %rcx
xorq $0x1, %rcx
movq 0x28(%rsp), %rdx
movq %rdx, %rax
orq %r13, %rax
je 0x6a77
movq %r15, %rbp
movl %esi, 0x14(%rsp)
testq %r13, %r13
sete %al
testq %rdx, %rdx
setne %sil
orb %al, %sil
jne 0x6aa8
testq %r12, %r12
jne 0x6ba6
xorl %eax, %eax
movq %r13, %r15
jmp 0x6b2a
leaq 0x271e2(%rip), %rax     # 0x2dc60
movq 0x8(%rsp), %rbx
movl 0x50(%rbx), %esi
movl 0x54(%rbx), %edx
subl 0x50(%rax), %esi
imull %esi, %esi
subl 0x54(%rax), %edx
imull %edx, %edx
addl %esi, %edx
cmpl $0x4, %edx
movl $0x1, %eax
cmovbq %rax, %rcx
jmp 0x6bab
testq %rdx, %rdx
sete %al
testq %r13, %r13
setne %sil
orb %al, %sil
jne 0x6aff
cmpb $0x0, 0x14(%rsp)
jne 0x6ba6
xorl %eax, %eax
movq %rdx, %r15
jmp 0x6b2a
movl 0x50(%r13), %esi
movl 0x54(%r13), %edi
callq 0x6f96
testb %al, %al
je 0x6aeb
leaq 0x1858e(%rip), %rdi     # 0x1f072
xorl %eax, %eax
callq 0xb620
movq %r15, %rdi
xorl %esi, %esi
callq 0xa04b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6f3b
cmpq %r13, %rdx
movl 0x14(%rsp), %esi
je 0x6cba
testq %r12, %r12
jne 0x6ba6
testb %sil, %sil
movq %rdx, %r15
cmovneq %r13, %r15
movb $0x1, %al
testq %r15, %r15
je 0x6f4c
movslq 0x60(%r15), %rdx
testq %rdx, %rdx
jle 0x6b9e
movq 0x8(%rsp), %rsi
movq 0x60(%rsi), %rdi
movl (%rdi), %esi
movl 0x4(%rdi), %edi
xorl %r8d, %r8d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl 0x18(%r15,%r8,8), %r11d
subl %esi, %r11d
movl 0x1c(%r15,%r8,8), %r10d
subl %edi, %r10d
imull %r11d, %r11d
imull %r10d, %r10d
addl %r11d, %r10d
testb %al, %al
je 0x6b71
cmpq %r13, 0x38(%r15,%r8,8)
cmovel %ebx, %r10d
xorl %r11d, %r11d
xorl %r9d, %r9d
cmpl %r14d, %r10d
setge %r11b
setle %r9b
testq %r12, %r12
cmovel %r11d, %r9d
testb %r9b, %r9b
jne 0x6b96
movq 0x18(%r15,%r8,8), %rbp
movl %r10d, %r14d
incq %r8
cmpq %r8, %rdx
jne 0x6b49
movq %rbp, 0x18(%rsp)
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
xorl %edx, %edx
testq %r12, %r12
setne %dl
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x708f
cmpl $0x1, %eax
jne 0x6bdb
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa3f6
xorl %ebp, %ebp
testb $0x3, 0x78(%rbx)
jne 0x6f3b
movl 0x269ff(%rip), %edi     # 0x2d5ec
movl 0x269f5(%rip), %esi     # 0x2d5e8
xorl %eax, %eax
callq 0xf3d8
testq %rax, %rax
je 0x6c4f
leaq 0x269e2(%rip), %rdi     # 0x2d5e8
movq %rbx, %rsi
callq 0xe449
movl %eax, %r12d
cmpb $0x2, %al
jne 0x6c28
addq $0x50, %rbx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0xadd9
jmp 0x6c34
leaq 0x26c6a(%rip), %rax     # 0x2d899
cmpb $0x0, (%rax)
je 0x6c4f
xorl %ebp, %ebp
cmpb $0x2, %r12b
sete %bpl
leaq 0x26c54(%rip), %rax     # 0x2d899
movb $0x0, (%rax)
negl %ebp
jmp 0x6f3b
movl $0x1, %edi
callq 0x9522
testb %al, %al
je 0x6c86
leaq 0x29d7c(%rip), %r14     # 0x309e0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6c86
movq (%r14), %rdi
movsbl 0x8f(%rbx), %esi
callq 0x64c0
leaq 0x29d53(%rip), %r14     # 0x309e0
movq (%r14), %rdi
movl 0x26956(%rip), %esi     # 0x2d5ec
movl 0x2694c(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6dd9
movq (%r14), %rdi
callq 0x6330
movl %eax, %r15d
jmp 0x6ddf
testq %r12, %r12
je 0x6ba3
cmpl $0x0, 0x60(%r13)
jle 0x6f56
xorl %r14d, %r14d
movl $0xffff8001, %ebx       # imm = 0xFFFF8001
movl $0x0, 0x24(%rsp)
movl $0xffff8001, 0x10(%rsp) # imm = 0xFFFF8001
movq 0x23243(%rip), %r12     # 0x29f30
movq %rcx, %r15
movq (%r12), %rdi
movl 0x18(%r13,%r14,8), %edx
movl 0x1c(%r13,%r14,8), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6d1e
movq (%r12), %rdi
callq 0x6330
cmpl $0x26, %eax
jne 0x6d1e
movq %r15, %rcx
jmp 0x6da1
incl 0x24(%rsp)
movq 0x8(%rsp), %rcx
movl 0x50(%rcx), %eax
movl 0x54(%rcx), %ecx
movl 0x18(%r13,%r14,8), %edx
movl 0x1c(%r13,%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 0x26f0d(%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, %ecx
subl %r8d, %ecx
cmpl %ebx, %ecx
jle 0x6d84
movl 0x10(%rsp), %eax
movl %ecx, %ebx
movq %r15, %rcx
jmp 0x6d98
sete %cl
cmpl 0x10(%rsp), %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
movq %r15, %rcx
jne 0x6da1
movq 0x18(%r13,%r14,8), %rbp
movl %eax, 0x10(%rsp)
incq %r14
movslq 0x60(%r13), %rax
cmpq %rax, %r14
jl 0x6ced
cmpl $0x2, 0x24(%rsp)
setl %sil
testl %ebx, %ebx
setle %al
movq %rbp, 0x18(%rsp)
movl $0x1, %edx
cmovleq %rdx, %rcx
andb %sil, %al
movl 0x14(%rsp), %esi
jmp 0x6f62
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6e35
movl $0x1, %esi
callq 0x13d18
testl %eax, %eax
setne %al
cmpq $0x2e, %r15
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6e35
movl 0x50(%rbx), %eax
movl 0x54(%rbx), %ecx
subl 0x267d6(%rip), %eax     # 0x2d5e8
subl 0x267d4(%rip), %ecx     # 0x2d5ec
imull %eax, %eax
imull %ecx, %ecx
addl %eax, %ecx
cmpl $0x2, %ecx
ja 0x6e35
movl $0x1, %edi
callq 0x9522
movb $0x20, %cl
testb %al, %al
jne 0x6e38
movl %r15d, %ecx
movb %cl, 0x8f(%rbx)
movl 0x267a8(%rip), %edi     # 0x2d5ec
movl 0x2679e(%rip), %esi     # 0x2d5e8
callq 0x6f96
testb %al, %al
je 0x6e81
testb $0x10, 0x78(%rbx)
jne 0x6e81
movq (%r14), %rdi
movl 0x2678a(%rip), %esi     # 0x2d5ec
movl 0x26780(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6e81
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64c0
leaq 0x29b68(%rip), %r14     # 0x309f0
movq (%r14), %rdi
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %esi
callq 0x6490
cmpl $-0x1, %eax
je 0x6ea8
movq (%r14), %rdi
movl $0x20, %esi
callq 0x64c0
movq (%r14), %rdi
movl 0x2673b(%rip), %esi     # 0x2d5ec
movl 0x26731(%rip), %edx     # 0x2d5e8
callq 0x6490
cmpl $-0x1, %eax
je 0x6ed0
movq (%r14), %rdi
movsbl 0x8d(%rbx), %esi
callq 0x64c0
movq 0x50(%rbx), %rax
movq %rax, 0x58(%rbx)
leaq 0x26709(%rip), %rdi     # 0x2d5e8
movq 0x26702(%rip), %rax     # 0x2d5e8
movq %rax, 0x50(%rbx)
callq 0x701c
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 0xc674
cmpl %r14d, %eax
jge 0x6f3b
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 0xdd0a
testb %al, %al
jne 0x6f3b
andb $0x7f, 0x78(%rbx)
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ecx
jmp 0x6ba3
movq %rbp, 0x18(%rsp)
movb $0x1, %al
movl $0x1, %ecx
orb %al, %sil
testb $0x1, %sil
movq 0x8(%rsp), %rbx
je 0x6f8e
leaq 0x26ce9(%rip), %rcx     # 0x2dc60
movq 0x50(%rcx), %rcx
movq %rcx, 0x18(%rsp)
movzbl %al, %ecx
movl $0x1, %r12d
jmp 0x6bab
xorl %r12d, %r12d
jmp 0x6bab