// File: /workspace/github_c_makefile_star2_O0/bobrippling[P]tim/list.c
// Function: list_delregion
list_t *list_delregion(list_t **pl, const region_t *region)
{
	if(region->begin.y > region->end.y)
		return NULL;

	if(region->type != REGION_LINE
	&& region->begin.y == region->end.y
	&& region->begin.x >= region->end.x)
	{
		return NULL;
	}

	list_t **seeked = list_seekp(pl, region->begin.y, false);

	if(!seeked || !*seeked)
		return NULL;

	list_t *deleted = NULL;

	switch(region->type){
		case REGION_LINE:
			deleted = list_dellines(
					seeked, (*seeked)->prev,
					region->end.y - region->begin.y + 1);
			break;
		case REGION_CHAR:
		{
			list_t *l = *seeked;
			size_t line_change = region->end.y - region->begin.y;

			/* create `deleted' */
			{
				deleted = list_new(NULL);

				if((unsigned)region->begin.x < l->len_line){
					char *this_line = l->line;
					size_t len = line_change == 0
							? (unsigned)region->end.x - region->begin.x
							: l->len_line - region->begin.x;

					char *pulled_out = ustrdup_len(
							this_line + region->begin.x,
							len);

					deleted->line = pulled_out;
					deleted->len_line = deleted->len_malloc = len;
				}
				/* else we leave deleted empty */
			}

			if(line_change > 1){
				deleted = list_append(
						deleted,
						list_tail(deleted),
						list_dellines(
							&l->next, l->prev,
							line_change - 1));
			}


			/* wipe lines */
			if(line_change > 0){
				/* join the lines */
				list_t *next = l->next;
				size_t nextlen;
				if((unsigned)region->end.x >= next->len_line){
					nextlen = 0;
				}else{
					nextlen = next->len_line - region->end.x;
				}
				size_t fulllen = region->begin.x + nextlen;

				if(l->len_malloc < fulllen)
					l->line = urealloc(l->line, l->len_malloc = fulllen);

				{
					list_t *pullout = list_new(NULL);
					deleted = list_append(deleted, list_tail(deleted), pullout);

					size_t len = MIN((unsigned)region->end.x, next->len_line);
					pullout->len_malloc = pullout->len_line = len;

					pullout->line = ustrdup_len(next->line, len);
				}

				memcpy(l->line + region->begin.x,
						next->line + region->end.x,
						nextlen);
				l->len_line = fulllen;

				list_free(list_dellines(&l->next, l->prev, 1));

			}else{
				if(!l->len_line || (unsigned)region->end.x > l->len_line)
					return deleted;

				size_t diff = region->end.x - region->begin.x;

				list_t *part = list_new(NULL);
				part->line = umalloc(diff + 1);
				part->len_line = diff;
				part->len_malloc = diff + 1;
				memcpy(part->line, l->line + region->begin.x, diff);

				memmove(
						l->line + region->begin.x,
						l->line + region->end.x,
						l->len_line - region->end.x);

				l->len_line -= diff;
			}
			break;
		}
		case REGION_COL:
		{
			list_t *pos = *seeked;
			point_t begin = region->begin, end = region->end;

			/* should've been sorted also */
			assert(begin.x <= end.x);

			for(int i = begin.y;
					i <= end.y && pos;
					i++, pos = pos->next)
			{
				if((unsigned)begin.x < pos->len_line){
					struct
					{
						char *str;
						size_t len;
					} removed;

					if((unsigned)end.x >= pos->len_line){
						/* delete all */
						removed.len = pos->len_line - begin.x + 1;
						removed.str = ustrdup_len(
								pos->line + begin.x,
								removed.len);

						pos->len_line = begin.x;

					}else{
						char *str = pos->line;

						removed.len = end.x - begin.x;
						removed.str = ustrdup_len(
								str + begin.x,
								removed.len);

						memmove(
								str + begin.x,
								str + end.x,
								pos->len_line - end.x);

						pos->len_line -= end.x - begin.x;
					}

					/* removed text */
					list_t *new = list_new(NULL);
					new->line = removed.str;
					new->len_malloc = new->len_line = removed.len;
					deleted = list_append(deleted, list_tail(deleted), new);
				}else{
					deleted = list_append(
							deleted, list_tail(deleted),
							list_new(NULL));
				}
			}
			break;
		}
	}

	return deleted;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jle 0xd3e0
movq $0x0, -0x8(%rbp)
jmp 0xdb08
movq -0x18(%rbp), %rax
cmpl $0x2, 0x10(%rax)
je 0xd416
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0xd416
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jl 0xd416
movq $0x0, -0x8(%rbp)
jmp 0xdb08
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %esi
xorl %edx, %edx
callq 0xcc90
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xd43d
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xd44a
movq $0x0, -0x8(%rbp)
jmp 0xdb08
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xcc(%rbp)
testl %eax, %eax
je 0xd4b5
jmp 0xd465
movl -0xcc(%rbp), %eax
subl $0x1, %eax
je 0xd85f
jmp 0xd476
movl -0xcc(%rbp), %eax
subl $0x2, %eax
jne 0xdb00
jmp 0xd487
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x18(%rbp), %rax
subl 0x4(%rax), %edx
addl $0x1, %edx
callq 0xdb20
movq %rax, -0x28(%rbp)
jmp 0xdb00
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
cltq
movq %rax, -0x38(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0xc6e0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xd57d
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0xd51f
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
movl %eax, %eax
movq %rax, -0xd8(%rbp)
jmp 0xd538
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
subq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdi
movq -0x48(%rbp), %rsi
callq 0x5fe0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x1, -0x38(%rbp)
jbe 0xd5d8
movq -0x28(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd2d0
movq %rax, -0xe0(%rbp)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x38(%rbp), %rax
subq $0x1, %rax
movl %eax, %edx
callq 0xdb20
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq %rax, %rdx
callq 0xd320
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x38(%rbp)
jbe 0xd760
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x58(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0xd60a
movq $0x0, -0x60(%rbp)
jmp 0xd621
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movslq 0x8(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movslq (%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x68(%rbp), %rax
jae 0xd660
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x30(%rbp), %rax
movq %rsi, 0x10(%rax)
callq 0x5f60
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0xc6e0
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd2d0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
movq -0x70(%rbp), %rdx
callq 0xd320
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x58(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xd6b9
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0xf8(%rbp)
jmp 0xd6c8
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x70(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rsi
callq 0x5fe0
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdi
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0x8(%rax), %rax
addq %rax, %rsi
movq -0x60(%rbp), %rdx
callq 0x4350
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rdi
addq $0x18, %rdi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x1, %edx
callq 0xdb20
movq %rax, %rdi
callq 0xcb10
jmp 0xd85a
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0xd77c
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0xd789
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xdb08
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
subl (%rcx), %eax
cltq
movq %rax, -0x80(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0xc6e0
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
addq $0x1, %rdi
callq 0x5f20
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x80(%rbp), %rcx
addq $0x1, %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rsi
movq -0x80(%rbp), %rdx
callq 0x4350
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movslq (%rax), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0x8(%rax), %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rax
movslq 0x8(%rax), %rax
subq %rax, %rdx
callq 0x4400
movq -0x80(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rax)
jmp 0xdb00
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
movl -0x98(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jg 0xd89a
jmp 0xd8b9
leaq 0x7e62(%rip), %rdi      # 0x15703
leaq 0x7e37(%rip), %rsi      # 0x156df
movl $0x236, %edx            # imm = 0x236
leaq 0x7e60(%rip), %rcx      # 0x15714
callq 0x4210
movl -0x94(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %ecx
xorl %eax, %eax
cmpl -0x9c(%rbp), %ecx
movb %al, -0xf9(%rbp)
jg 0xd8ec
cmpq $0x0, -0x90(%rbp)
setne %al
movb %al, -0xf9(%rbp)
movb -0xf9(%rbp), %al
testb $0x1, %al
jne 0xd8fb
jmp 0xdafe
movl -0x98(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0xda98
movl -0xa0(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0xd983
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x98(%rbp), %rcx
subq %rcx, %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x90(%rbp), %rax
movq (%rax), %rdi
movslq -0x98(%rbp), %rax
addq %rax, %rdi
movq -0xb0(%rbp), %rsi
callq 0x5fe0
movq %rax, -0xb8(%rbp)
movslq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0xda2a
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movl -0xa0(%rbp), %eax
subl -0x98(%rbp), %eax
cltq
movq %rax, -0xb0(%rbp)
movq -0xc0(%rbp), %rdi
movslq -0x98(%rbp), %rax
addq %rax, %rdi
movq -0xb0(%rbp), %rsi
callq 0x5fe0
movq %rax, -0xb8(%rbp)
movq -0xc0(%rbp), %rdi
movslq -0x98(%rbp), %rax
addq %rax, %rdi
movq -0xc0(%rbp), %rsi
movslq -0xa0(%rbp), %rax
addq %rax, %rsi
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0xa0(%rbp), %rax
subq %rax, %rdx
callq 0x4400
movl -0xa0(%rbp), %eax
subl -0x98(%rbp), %eax
movslq %eax, %rdx
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0xc6e0
movq %rax, -0xc8(%rbp)
movq -0xb8(%rbp), %rcx
movq -0xc8(%rbp), %rax
movq %rcx, (%rax)
movq -0xb0(%rbp), %rcx
movq -0xc8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0xc8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd2d0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
movq -0xc8(%rbp), %rdx
callq 0xd320
movq %rax, -0x28(%rbp)
jmp 0xdad6
movq -0x28(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd2d0
movq %rax, -0x110(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0xc6e0
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rsi
movq %rax, %rdx
callq 0xd320
movq %rax, -0x28(%rbp)
jmp 0xdad8
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
movq -0x90(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
jmp 0xd8c5
jmp 0xdb00
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)