// File: /workspace/github_c_makefile_star2_O3/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 0x8b96
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r12
incq %r12
movq %r12, %rbx
movzbl (%rbx), %eax
cmpl $0x5c, %eax
je 0x8b8e
testl %eax, %eax
je 0x8ba9
cmpb %r13b, %al
jne 0x8b91
jmp 0x8bb7
incq %rbx
incq %rbx
jmp 0x8b7b
leaq 0x58f3(%rip), %rdi      # 0xe490
xorl %eax, %eax
callq 0x4de2
jmp 0x8dd9
testb %r13b, %r13b
je 0x8bb7
leaq 0x5b94(%rip), %rbx      # 0xe749
jmp 0x8bbd
movb $0x0, (%rbx)
incq %rbx
callq 0xdaf0
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
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 0xce93(%rip), %rdi      # 0x15a98
movq %rdi, 0x60(%rsp)
movups 0xce77(%rip), %xmm0     # 0x15a88
movaps %xmm0, 0x50(%rsp)
leaq 0x88(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x70(%rsp), %r9
movq %rdi, (%r9)
movq %rbx, %rdi
callq 0xce78
testb %al, %al
je 0x8c3c
movq %rbx, 0x30(%rsp)
jmp 0x8c9a
cmpb $0x0, (%rbx)
je 0x8c57
leaq 0x5865(%rip), %rdi      # 0xe4ad
movq %rbx, %rsi
xorl %eax, %eax
callq 0x4de2
jmp 0x8dcb
movl $0x1, 0x38(%rsp)
movl $0x10, %edi
callq 0x5830
movq %rax, 0x40(%rsp)
leaq 0x5c3f(%rip), %rdi      # 0xe8b4
callq 0x585c
movq 0x40(%rsp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x48(%rsp)
movq $0x0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq (%r15), %rbx
testq %r14, %r14
je 0x8cac
movq %r14, %rdi
callq 0xcc9d
jmp 0x8cc7
leaq 0x80(%rsp), %r14
movl $0x0, (%r14)
movq %rbx, %rdi
callq 0x960e
movl %eax, 0x4(%r14)
movq 0x8(%rbx), %rdi
callq 0xae44
movq 0x8(%rbx), %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xae57
movq 0x8(%rbx), %rdi
leaq 0xc(%rsp), %rdx
movl $0x1, %esi
callq 0xae9d
testq %rax, %rax
je 0x8dcb
leaq 0x78(%rsp), %r14
leaq 0x90(%rsp), %r15
leaq 0xc(%rsp), %r12
movl $0x0, 0x28(%rax)
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %rdx
cmpb $0x0, (%rdx)
je 0x8d3c
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0xbd69
testq %rax, %rax
setne %al
cmpb %al, 0x10(%rsp)
je 0x8db1
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movl $0x0, (%rax)
movl %ebp, 0x4(%rax)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x8d71
movq %r14, %rsi
movq %r15, %rdx
callq 0xcc1f
testl %eax, %eax
je 0x8d71
cmpl $0x1, %eax
je 0x8db1
movq %r15, %rcx
jmp 0x8d73
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
movq 0x40(%rsp), %rsi
movb 0x48(%rsp), %dil
movb 0x11(%rdx), %r8b
movq 0x8(%rdx), %rax
cmpb $0x1, 0x10(%rdx)
jne 0x8da3
xorl %edx, %edx
cmpb %dil, %r8b
setne %dl
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
callq *%rax
jmp 0x8db1
xorl %edx, %edx
cmpb %dil, %r8b
setne %dl
movl 0x38(%rsp), %edi
callq *%rax
movq 0x8(%rbx), %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0xae9d
testq %rax, %rax
jne 0x8d0f
movq 0x40(%rsp), %rdi
movl 0x38(%rsp), %esi
callq 0xd124
cmpb $0x5c, %r13b
setne %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq