// File: /workspace/github_c_makefile_star2_O1/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), %r15b
cmpb $0x5c, %r15b
je 0x8cca
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r12
incq %r12
movq %r12, %rbx
movzbl (%rbx), %eax
cmpl $0x5c, %eax
je 0x8cb5
testl %eax, %eax
je 0x8cbd
cmpb %r15b, %al
jne 0x8cb8
jmp 0x8cbd
incq %rbx
incq %rbx
jmp 0x8ca2
cmpb %r15b, %al
jne 0x8cdd
movb $0x0, (%rbx)
incq %rbx
jmp 0x8ce4
leaq 0x67cb(%rip), %rdi      # 0xf49c
xorl %eax, %eax
callq 0x4de2
jmp 0x8f01
leaq 0x6a74(%rip), %rbx      # 0xf758
callq 0xde6c
movq %rax, %r13
leaq 0x28(%rsp), %rsi
movb %bpl, -0x18(%rsi)
movq %r12, -0x10(%rsi)
movq %rax, -0x8(%rsi)
leaq 0x38(%rsp), %rcx
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
movq %rax, (%rdx)
leaq 0x48(%rsp), %r8
movb $0x0, (%r8)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%r8)
movl $0x0, -0x10(%r8)
movq 0xdd6c(%rip), %rdi      # 0x16a98
movq %rdi, 0x60(%rsp)
movups 0xdd50(%rip), %xmm0     # 0x16a88
movaps %xmm0, 0x50(%rsp)
leaq 0x88(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x70(%rsp), %r9
movq %rdi, (%r9)
movq %rbx, %rdi
callq 0xd1e5
testb %al, %al
je 0x8d63
movq %rbx, 0x30(%rsp)
jmp 0x8dc1
cmpb $0x0, (%rbx)
je 0x8d7e
leaq 0x674a(%rip), %rdi      # 0xf4b9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x4de2
jmp 0x8ef3
movl $0x1, 0x38(%rsp)
movl $0x10, %edi
callq 0x5830
movq %rax, 0x40(%rsp)
leaq 0x6b28(%rip), %rdi      # 0xf8c4
callq 0x5863
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq (%r13), %rbx
testq %r14, %r14
je 0x8dd4
movq %r14, %rdi
callq 0xd017
jmp 0x8def
leaq 0x80(%rsp), %r14
movl $0x0, (%r14)
movq %rbx, %rdi
callq 0x977e
movl %eax, 0x4(%r14)
movq 0x8(%rbx), %rdi
callq 0xb10c
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xb11f
leaq 0xc(%rsp), %r14
leaq 0x90(%rsp), %r12
movq 0x8(%rbx), %rdi
movl $0x1, %esi
movq %r14, %rdx
callq 0xb19d
testq %rax, %rax
je 0x8ef3
movq %rax, %r13
movl $0x0, 0x28(%rax)
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x8e65
movq (%r13), %rdi
movq 0x8(%r13), %rsi
callq 0xc0c9
testq %rax, %rax
setne %al
cmpb %al, 0x10(%rsp)
je 0x8eea
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x0, (%rax)
movl %ebp, 0x4(%rax)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x8e9d
leaq 0x78(%rsp), %rsi
movq %r12, %rdx
callq 0xcf99
cmpl $0x1, %eax
je 0x8ea1
testl %eax, %eax
jne 0x8ea5
movb $0x1, %al
xorl %ecx, %ecx
jmp 0x8eaa
xorl %ecx, %ecx
jmp 0x8eae
xorl %eax, %eax
jmp 0x8ea7
movb $0x1, %al
movq %r12, %rcx
testb %al, %al
je 0x8eea
movq 0x28(%rsp), %rax
movq 0x40(%rsp), %rsi
movb 0x48(%rsp), %dil
movb 0x11(%rax), %r8b
cmpb $0x1, 0x10(%rax)
jne 0x8edb
xorl %edx, %edx
cmpb %dil, %r8b
setne %dl
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
callq *0x8(%rax)
jmp 0x8eea
xorl %edx, %edx
cmpb %dil, %r8b
setne %dl
movl 0x38(%rsp), %edi
callq *0x8(%rax)
testq %r13, %r13
jne 0x8e16
movq 0x40(%rsp), %rdi
movl 0x38(%rsp), %esi
callq 0xd4a5
cmpb $0x5c, %r15b
setne %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq