// File: /workspace/github_c_makefile_star2_O1/bobrippling[P]tim/motion.c
// Function: m_paren
int m_paren(
		motion_arg const *arg, unsigned repeat,
		window *win, point_t *to)
{
	list_t *l = window_current_line(win, false);
	if(!l)
		return MOTION_FAILURE;

	if(arg->i == '%'){
		/* look forward for a paren on this line, then match */
		char paren = 0, opp;
		unsigned x = win->ui_pos->x;
		unsigned y = win->ui_pos->y;

		if(x >= l->len_line)
			x = l->len_line - 1;
		for(unsigned i = x; i < l->len_line; i++)
			if(paren_match(l->line[i], &opp)){
				paren = l->line[i];
				x = i;
				break;
			}

		if(!paren)
			return MOTION_FAILURE;

		const int dir = paren_left(paren) ? 1 : -1;
		point_t pos = { .y = y, .x = x + dir };

		if(find_unnested_paren(opp, paren, dir, &l, &pos)){
			*to = pos;
			return MOTION_SUCCESS;
		}
		return MOTION_FAILURE;
	}

	if(paren_match(arg->i, &(char){ 0 })){
		repeat = DEFAULT_REPEAT(repeat);

		/* look for an unmatched `arg->i' */
		char paren = arg->i, opp = paren_opposite(paren);

		point_t pos = *win->ui_pos;

		int dir = paren_left(paren) ? -1 : 1;
		bool done_one = false;

		for(; repeat > 0; repeat--){
			const point_t before_move = pos;

			if((unsigned)pos.x < l->len_line &&
			(l->line[pos.x] == paren || l->line[pos.x] == opp))
			{
				/* "[{" while on a '{' - step out so we don't stay on it
				 * or
				 * "[{" while on a '}' - step in so we match the other paren
				 */
				l = list_advance_x(l, dir, &pos.y, &pos.x);
			}

			if(!find_unnested_paren(paren, opp,
						dir, &l, &pos))
			{
				if(done_one){
					pos = before_move;
					break;
				}
				return MOTION_FAILURE;
			}

			done_one = true;
		}

		*to = pos;
		return MOTION_SUCCESS;
	}

	return MOTION_FAILURE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %r12
xorl %r14d, %r14d
movq %rdx, %rdi
xorl %esi, %esi
callq 0xdbe9
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0xbd48
movq %rax, %rbx
movl (%r12), %eax
cmpl $0x25, %eax
jne 0xbbe4
movq 0x30(%r15), %rax
movl (%rax), %ecx
movq 0x8(%rbx), %rdx
leal -0x1(%rdx), %ebp
cmpq %rcx, %rdx
cmoval %ecx, %ebp
movl 0x4(%rax), %r15d
xorl %r14d, %r14d
cmpq %rbp, %rdx
jbe 0xbccb
movl %r15d, 0x14(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x7(%rsp), %r15
movq %rbp, %r13
movl %ebp, %r12d
movq (%rbx), %rax
movsbl (%rax,%r13), %edi
movq %r15, %rsi
callq 0xc1cf
testb %al, %al
jne 0xbce6
incl %r12d
movq %r12, %r13
cmpq %r12, 0x8(%rbx)
ja 0xbbb6
xorl %eax, %eax
movl %ebp, %r12d
jmp 0xbced
leaq 0x13(%rsp), %rsi
movb $0x0, (%rsi)
movsbl %al, %edi
callq 0xc1cf
testb %al, %al
je 0xbd48
movq %r13, 0x18(%rsp)
cmpl $0x1, %ebp
adcl $0x0, %ebp
movsbl (%r12), %r14d
movl %r14d, %edi
callq 0xc20b
movl %eax, %ebx
movq 0x30(%r15), %rax
movq (%rax), %rax
leaq 0xc(%rsp), %rcx
movq %rax, -0x4(%rcx)
movl %r14d, %edi
callq 0xc1f0
movzbl %al, %r12d
negl %r12d
orl $0x1, %r12d
movsbl %bl, %r13d
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movl 0x8(%rsp), %eax
movq 0x20(%rsp), %rdi
cmpq %rax, 0x8(%rdi)
jbe 0xbc87
cltq
movq (%rdi), %rcx
movb (%rcx,%rax), %al
movsbl %al, %eax
cmpl %eax, %r14d
sete %cl
cmpb %r13b, %al
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0xbc87
movl %r12d, %esi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0xae3c
movq %rax, 0x20(%rsp)
movl %r14d, %edi
movl %r13d, %esi
movl %r12d, %edx
leaq 0x20(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0xbd5a
testb %al, %al
je 0xbca9
movb $0x1, %bl
xorl %eax, %eax
jmp 0xbcbd
movl $0x1, %eax
testb $0x1, %bl
je 0xbcbd
movq %r15, 0x8(%rsp)
movl $0x5, %eax
testl %eax, %eax
jne 0xbcd2
decl %ebp
jne 0xbc3f
jmp 0xbcd7
movl %ebp, %r12d
xorl %eax, %eax
jmp 0xbcf7
cmpl $0x5, %eax
jne 0xbd45
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
jmp 0xbd3d
movq (%rbx), %rax
movb (%rax,%r13), %al
movq 0x18(%rsp), %r13
movl 0x14(%rsp), %r15d
testb %al, %al
je 0xbd48
movsbl %al, %ebx
movl %ebx, %edi
callq 0xc1f0
movzbl %al, %edx
decl %edx
orl $0x1, %edx
addl %edx, %r12d
leaq 0x8(%rsp), %r8
movl %r12d, (%r8)
movl %r15d, 0x4(%r8)
movsbl 0x7(%rsp), %edi
leaq 0x20(%rsp), %rcx
movl %ebx, %esi
callq 0xbd5a
xorl %r14d, %r14d
testb %al, %al
je 0xbd48
movq 0x8(%rsp), %rax
movq %rax, (%r13)
movl $0x1, %r14d
jmp 0xbd48
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq