// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movb %cl, %al
movb %dl, %cl
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %cl
movb %cl, -0x19(%rbp)
andb $0x1, %al
movb %al, -0x1a(%rbp)
movq -0x10(%rbp), %rax
addq $0x50, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
andq $0x200, %rax            # imm = 0x200
cmpq $0x0, %rax
je 0x7717
movq -0x10(%rbp), %rdi
movb $0x0, %al
callq 0x12f00
movq (%rax), %rax
movq %rax, 0x2f3ce(%rip)     # 0x36a64
movl 0x2f3c8(%rip), %eax     # 0x36a64
leaq 0x2fa2d(%rip), %rcx     # 0x370d0
subl 0x50(%rcx), %eax
movl 0x2f3b8(%rip), %ecx     # 0x36a64
leaq 0x2fa1d(%rip), %rdx     # 0x370d0
subl 0x50(%rdx), %ecx
imull %ecx, %eax
movl 0x2f3a9(%rip), %ecx     # 0x36a68
leaq 0x2fa0a(%rip), %rdx     # 0x370d0
subl 0x54(%rdx), %ecx
movl 0x2f399(%rip), %edx     # 0x36a68
leaq 0x2f9fa(%rip), %rsi     # 0x370d0
subl 0x54(%rsi), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl $0x3e8, %edi            # imm = 0x3E8
movb $0x0, %al
callq 0xef20
cmpl $0x5, %eax
jge 0x7705
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
andq $-0x201, %rcx           # imm = 0xFDFF
movq %rcx, 0x78(%rax)
cmpl $0x0, -0x2c(%rbp)
jne 0x7712
movl $0x1, -0x28(%rbp)
jmp 0x7aee
movl $0x0, -0x54(%rbp)
testb $0x1, -0x19(%rbp)
je 0x772d
movl $0x0, -0x34(%rbp)
jmp 0x7734
movl $0x7fff, -0x34(%rbp)    # imm = 0x7FFF
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x2f322(%rip)     # 0x36a64
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x40(%rbp), %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x7a41
movq -0x40(%rbp), %rax
movl (%rax), %eax
subl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x40(%rbp), %rcx
movl (%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x7a31
movl -0x24(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0xaa10
testb $0x1, %al
jne 0x77a4
jmp 0x7a23
movl -0x20(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7b00
testb $0x1, %al
jne 0x77c6
jmp 0x7a23
movl -0x24(%rbp), %edi
movl -0x20(%rbp), %esi
callq 0xa960
movsbl %al, %eax
movl %eax, -0x28(%rbp)
movslq -0x28(%rbp), %rdi
callq 0xdc30
testb $0x1, %al
jne 0x77e9
jmp 0x7a21
movl -0x28(%rbp), %eax
movsbl %al, %edi
callq 0x11a80
testb $0x1, %al
jne 0x77fd
jmp 0x7886
movl -0x24(%rbp), %edi
movl -0x20(%rbp), %esi
movb $0x0, %al
callq 0x12e80
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x7884
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
andq $0x200, %rax            # imm = 0x200
cmpq $0x0, %rax
jne 0x7884
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x7882
movq -0x60(%rbp), %rax
movsbl 0x14(%rax), %eax
cmpl $0x22, %eax
jne 0x785b
movl $0x64, %edi
movb $0x0, %al
callq 0xef20
cmpl $0x50, %eax
jge 0x785b
jmp 0x7a23
movl -0x24(%rbp), %eax
leaq 0x2f86b(%rip), %rcx     # 0x370d0
cmpl 0x54(%rcx), %eax
jne 0x7879
movl -0x20(%rbp), %eax
leaq 0x2f85c(%rip), %rcx     # 0x370d0
cmpl 0x50(%rcx), %eax
je 0x787e
jmp 0x7a23
jmp 0x7880
jmp 0x7882
jmp 0x7884
jmp 0x7886
cmpl $0x3f, -0x28(%rbp)
jne 0x78e0
movl -0x24(%rbp), %edi
movl -0x20(%rbp), %esi
movb $0x0, %al
callq 0xfcb0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x78de
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
cmpl $0x7, 0x28(%rax)
jne 0x78dc
leaq 0x2f817(%rip), %rax     # 0x370d0
movl 0x50(%rax), %eax
cmpl -0x48(%rbp), %eax
jne 0x78d7
leaq 0x2f808(%rip), %rax     # 0x370d0
movl 0x54(%rax), %eax
cmpl -0x44(%rbp), %eax
jne 0x78d7
movl $0x1, -0x58(%rbp)
jmp 0x7a23
jmp 0x78de
jmp 0x78e0
movq -0x10(%rbp), %rax
movsbl 0x8d(%rax), %eax
cmpl $0x56, %eax
jne 0x791e
leaq -0x48(%rbp), %rdi
callq 0x7560
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x791c
movq -0x70(%rbp), %rdi
movl $0x1, %esi
callq 0x19950
cmpl $0x0, %eax
jne 0x791c
jmp 0x7a23
jmp 0x791e
movl -0x24(%rbp), %eax
leaq 0x2f7a8(%rip), %rcx     # 0x370d0
cmpl 0x54(%rcx), %eax
jne 0x795e
movl -0x20(%rbp), %eax
leaq 0x2f799(%rip), %rcx     # 0x370d0
cmpl 0x50(%rcx), %eax
jne 0x795e
testb $0x1, -0x1a(%rbp)
je 0x7959
movq -0x48(%rbp), %rax
movq %rax, 0x2f117(%rip)     # 0x36a64
movl $0x1, -0x4(%rbp)
jmp 0x7af4
jmp 0x7a23
movq -0x18(%rbp), %rax
movl (%rax), %eax
subl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
subl -0x20(%rbp), %ecx
imull %ecx, %eax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rdx
movl 0x4(%rdx), %edx
subl -0x24(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jg 0x79ac
movq -0x48(%rbp), %rax
movq %rax, 0x2f0c4(%rip)     # 0x36a64
movl $0x0, -0x4(%rbp)
jmp 0x7af4
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x79c3
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x79d1
testb $0x1, -0x19(%rbp)
je 0x7a1f
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x7a1f
movl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jne 0x7a0f
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x5c(%rcx), %eax
jne 0x7a0f
testb $0x1, -0x19(%rbp)
je 0x7a1d
leaq 0x2f6da(%rip), %rax     # 0x370d0
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jne 0x7a1d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x70(%rax)
je 0x7a1d
movl -0x30(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x7a1f
jmp 0x7a21
jmp 0x7a23
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x777c
jmp 0x7a33
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x775b
cmpl $0x0, -0x54(%rbp)
jle 0x7a54
movq -0x50(%rbp), %rax
movq %rax, 0x2f012(%rip)     # 0x36a64
jmp 0x7ac1
leaq 0x2f675(%rip), %rax     # 0x370d0
movl 0x50(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x50(%rcx), %eax
leaq 0x2f664(%rip), %rcx     # 0x370d0
movl 0x50(%rcx), %ecx
movq -0x10(%rbp), %rdx
subl 0x50(%rdx), %ecx
imull %ecx, %eax
leaq 0x2f650(%rip), %rcx     # 0x370d0
movl 0x54(%rcx), %ecx
movq -0x10(%rbp), %rdx
subl 0x54(%rdx), %ecx
leaq 0x2f63f(%rip), %rdx     # 0x370d0
movl 0x54(%rdx), %edx
movq -0x10(%rbp), %rsi
subl 0x54(%rsi), %edx
imull %edx, %ecx
addl %ecx, %eax
cmpl $0x2, %eax
jge 0x7abf
cmpl $0x0, -0x58(%rbp)
jne 0x7abd
leaq 0x2f61e(%rip), %rax     # 0x370d0
movq 0x50(%rax), %rax
movq %rax, 0x2efa7(%rip)     # 0x36a64
jmp 0x7abf
jmp 0x7ac1
leaq 0x2f608(%rip), %rax     # 0x370d0
movl 0x50(%rax), %eax
cmpl 0x2ef93(%rip), %eax     # 0x36a64
jne 0x7aec
leaq 0x2f5f6(%rip), %rax     # 0x370d0
movl 0x54(%rax), %eax
cmpl 0x2ef85(%rip), %eax     # 0x36a68
jne 0x7aec
movl $0x1, -0x28(%rbp)
jmp 0x7aee
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)