// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movb (%rax), %al
movb %al, -0x29(%rbp)
movsbl -0x29(%rbp), %eax
cmpl $0x5c, %eax
jne 0xb137
leaq 0xa455(%rip), %rdi      # 0x1557c
movb $0x0, %al
callq 0x4f30
movb $0x0, -0x1(%rbp)
jmp 0xb3e4
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpb $0x0, (%rax)
je 0xb199
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5c, %eax
jne 0xb176
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb189
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
movsbl -0x29(%rbp), %ecx
cmpl %ecx, %eax
jne 0xb187
jmp 0xb199
jmp 0xb189
jmp 0xb18b
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb153
leaq 0xa698(%rip), %rax      # 0x15838
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
movsbl -0x29(%rbp), %ecx
cmpl %ecx, %eax
jne 0xb1c6
movq -0x38(%rbp), %rax
movb $0x0, (%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
callq 0x13c60
movq %rax, -0x50(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movb %al, -0x90(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq 0x10871(%rip), %rax     # 0x1ba88
movq %rax, -0xa8(%rbp)
movq 0x1086b(%rip), %rax     # 0x1ba90
movq %rax, -0xa0(%rbp)
movq 0x10865(%rip), %rax     # 0x1ba98
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x18, %rsi
leaq -0x90(%rbp), %rdx
addq $0x30, %rdx
leaq -0x90(%rbp), %rcx
addq $0x28, %rcx
leaq -0x90(%rbp), %r8
addq $0x38, %r8
movl $0x0, -0xb8(%rbp)
movl $0x0, -0xb4(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %r9
callq 0x12570
testb $0x1, %al
jne 0xb302
movq -0x48(%rbp), %rax
cmpb $0x0, (%rax)
jne 0xb2e9
movl $0x1, -0x68(%rbp)
movl $0x10, %edi
callq 0x5f20
movq %rax, -0x60(%rbp)
leaq 0xa6e3(%rip), %rdi      # 0x159a4
callq 0x5fa0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movb $0x0, -0x58(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0xb300
movq -0x48(%rbp), %rsi
leaq 0xa2a5(%rip), %rdi      # 0x15599
movb $0x0, %al
callq 0x4f30
jmp 0xb3d4
jmp 0xb30a
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xb348
movl $0x0, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0xc330
movl %eax, -0xc4(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0xb351
movq -0x28(%rbp), %rdi
callq 0x122d0
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xe920
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x28(%rbp), %rsi
movl $0x1, %edx
callq 0xe950
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x1, %esi
leaq -0xcc(%rbp), %rdx
callq 0xe9e0
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0xd8(%rbp)
jne 0xb3a9
jmp 0xb3d2
movq -0xd8(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %esi
leaq -0x90(%rbp), %rdx
callq 0xb400
jmp 0xb37a
jmp 0xb3d4
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
callq 0x129d0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)