// File: /workspace/github_c_makefile_star2_O2/bobrippling[P]tim/cmds.c
// Function: c_g
bool c_g(char *cmd, char *gcmd, bool inverse, struct range *range)
{
	const char reg_sep = *gcmd;

	if(reg_sep == '\\'){
		ui_err("can't use '\\' as a separator");
		return false;
	}

	struct
	{
		char *start, *end;
	} regex = { .start = gcmd + 1 };

	for(regex.end = regex.start; *regex.end; regex.end++)
		if(*regex.end == '\\')
			regex.end++;
		else if(*regex.end == reg_sep)
			break;

	char *subcmd = "";
	if(*regex.end == reg_sep){
		*regex.end = '\0';
		subcmd = regex.end + 1;
	}
	/* else, no terminating character */

	window *const win = windows_cur();

	struct g_ctx ctx = {
		.g_inverse = inverse,
		.match = regex.start,
		.win = win,
	};

	struct cmd_t c_p_fallback = {
		.arg0 = "p",
		.f_argv = &c_p,
		.single_arg = false,
	};

	/* parse the range here to check, and on each invocation,
	 * since g/a/-1p means current-pos-less-one */
	if(!parse_ranged_cmd(
			subcmd, &ctx.cmd,
			&ctx.argv, &ctx.argc,
			&ctx.force,
			&(struct range *){
				&(struct range){ 0 }
			}))
	{
		if(!*subcmd){
			ctx.argc = 1;
			ctx.argv = umalloc(2 * sizeof *ctx.argv);
			ctx.argv[0] = ustrdup("p");
			ctx.force = false;
			ctx.str_range = NULL;
			ctx.cmd = &c_p_fallback;
		}else{
			ui_err("g: unknown command: %s", subcmd);
			goto out;
		}
	}else{
		ctx.str_range = subcmd;
	}

	buffer_t *const b = win->buf;

	struct range rng_all;
	if(!range){
		rng_all.start = 0;
		rng_all.end = buffer_nlines(b);
		range = &rng_all;
	}else{
		range_sort(range);
	}

	list_clear_flag(b->head);

	list_flag_range(b->head, range, 1);

	/* need to start at the beginning each time,
	 * in case we've deleted any lines.
	 * Room for optimisation here */
	while(1){
		int y;
		list_t *l = list_flagfind(b->head, 1, &y);
		if(!l)
			break;

		l->flag = 0;
		g_exec(l, y, &ctx);
	}

out:
	free_argv(ctx.argv, ctx.argc);

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movb (%rsi), %r13b
cmpb $0x5c, %r13b
je 0x8706
movq %rcx, %rbx
movl %edx, %ebp
movq %rsi, %r12
incq %r12
movq %r12, %r14
movzbl (%r14), %eax
cmpl $0x5c, %eax
je 0x86fe
testl %eax, %eax
je 0x8719
cmpb %r13b, %al
jne 0x8701
jmp 0x8727
incq %r14
incq %r14
jmp 0x86ea
leaq 0x5d83(%rip), %rdi      # 0xe490
xorl %eax, %eax
callq 0x4c34
jmp 0x87cf
testb %r13b, %r13b
je 0x8727
leaq 0x6024(%rip), %r14      # 0xe749
jmp 0x872e
movb $0x0, (%r14)
incq %r14
callq 0xd170
movq %rax, %r15
leaq 0x28(%rsp), %rsi
movb %bpl, -0x18(%rsi)
movq %r12, -0x10(%rsi)
movq %rax, -0x8(%rsi)
leaq 0x38(%rsp), %rcx
leaq 0x40(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x48(%rsp), %r8
movb $0x0, (%r8)
andl $0x0, -0x10(%r8)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%r8)
movq 0xd326(%rip), %rax      # 0x15a98
movq %rax, 0x60(%rsp)
leaq 0x88(%rsp), %rax
andq $0x0, (%rax)
movups 0xd2fe(%rip), %xmm0     # 0x15a88
movaps %xmm0, 0x50(%rsp)
leaq 0x70(%rsp), %r9
movq %rax, (%r9)
movq %r14, %rdi
callq 0xc5c5
testb %al, %al
je 0x87aa
movq %r14, 0x30(%rsp)
jmp 0x8826
cmpb $0x0, (%r14)
je 0x87e8
leaq 0x5cf6(%rip), %rdi      # 0xe4ad
movq %r14, %rsi
xorl %eax, %eax
callq 0x4c34
movq 0x40(%rsp), %rdi
movl 0x38(%rsp), %esi
callq 0xc856
cmpb $0x5c, %r13b
setne %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0x38(%rsp)
pushq $0x10
popq %rdi
callq 0x5648
movq %rax, 0x40(%rsp)
leaq 0x60b0(%rip), %rdi      # 0xe8b4
callq 0x5672
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x48(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq (%r15), %r14
testq %rbx, %rbx
je 0x8838
movq %rbx, %rdi
callq 0xc3f2
jmp 0x884e
leaq 0x80(%rsp), %rbx
andl $0x0, (%rbx)
movq %r14, %rdi
callq 0x90a8
movl %eax, 0x4(%rbx)
movq 0x8(%r14), %rdi
callq 0xa5d3
movq 0x8(%r14), %rdi
pushq $0x1
popq %rbp
movq %rbx, %rsi
movl %ebp, %edx
callq 0xa5e3
leaq 0xc(%rsp), %rbx
leaq 0x90(%rsp), %r12
movq 0x8(%r14), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0xa61f
testq %rax, %rax
je 0x87c1
andl $0x0, 0x28(%rax)
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x88b7
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0xb57b
testq %rax, %rax
setne %al
cmpb %al, 0x10(%rsp)
je 0x8875
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
andl $0x0, (%rax)
movl %r15d, 0x4(%rax)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x88ec
leaq 0x78(%rsp), %rsi
movq %r12, %rdx
callq 0xc37c
testl %eax, %eax
je 0x88ec
cmpl $0x1, %eax
je 0x8875
movq %r12, %r8
jmp 0x88ef
xorl %r8d, %r8d
movq 0x28(%rsp), %rdi
movl 0x38(%rsp), %esi
movq 0x40(%rsp), %rdx
movzbl 0x48(%rsp), %ecx
callq 0x89fb
jmp 0x8875