// File: /workspace/github_c_makefile_star2_O2/odknt[P]st/st.c
// Function: getsel
char *
getsel(void)
{
	char *str, *ptr;
	int y, bufsize, lastx, linelen;
	const Glyph *gp, *last;

	if (sel.ob.x == -1)
		return NULL;

	bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
	ptr = str = xmalloc(bufsize);

	/* append every set & selected glyph to the selection */
	for (y = sel.nb.y; y <= sel.ne.y; y++) {
		if ((linelen = tlinelen(y)) == 0) {
			*ptr++ = '\n';
			continue;
		}

		if (sel.type == SEL_RECTANGULAR) {
			gp = &TLINE(y)[sel.nb.x];
			lastx = sel.ne.x;
		} else {
			gp = &TLINE(y)[sel.nb.y == y ? sel.nb.x : 0];
			lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
		}
		last = &TLINE(y)[MIN(lastx, linelen-1)];
		while (last >= gp && last->u == ' ')
			--last;

		for ( ; gp <= last; ++gp) {
			if (gp->mode & ATTR_WDUMMY)
				continue;

			ptr += utf8encode(gp->u, ptr);
		}

		/*
		 * Copy and pasting of line endings is inconsistent
		 * in the inconsistent terminal and GUI world.
		 * The best solution seems like to produce '\n' when
		 * something is copied from st and convert '\n' to
		 * '\r', when something to be pasted is received by
		 * st.
		 * FIXME: Fix the computer world.
		 */
		if ((y < sel.ne.y || lastx >= linelen) &&
		    (!(last->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
			*ptr++ = '\n';
	}
	*ptr = 0;
	return str;
}

// Assembly:
cmpl $-0x1, 0x1132a(%rip)    # 0x1843c
je 0x7300
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x1132b(%rip), %eax     # 0x18454
movl 0x11309(%rip), %ecx     # 0x18438
subl 0x112fb(%rip), %ecx     # 0x18430
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x6bcc
movq %rax, %r14
movl 0x112de(%rip), %ebp     # 0x18430
movq %rax, (%rsp)
movl 0x112db(%rip), %r12d    # 0x18438
cmpl %r12d, %ebp
jg 0x72e9
movl %ebp, %edi
callq 0x7303
testl %eax, %eax
je 0x72db
movl %eax, %r15d
cmpl $0x2, 0x112a5(%rip)     # 0x18424
movl 0x15167(%rip), %ecx     # 0x1c2ec
movl %ebp, %eax
jne 0x71b6
subl %ecx, %eax
jge 0x71e3
movl 0x15155(%rip), %edx     # 0x1c2e8
addl %edx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %esi            # imm = 0x7D0
idivl %esi
movslq %edx, %rax
leaq 0x112a4(%rip), %rdx     # 0x18450
leaq (%rdx,%rax,8), %rax
addq $0x18, %rax
jmp 0x71f0
subl %ecx, %eax
jge 0x7200
movl 0x15128(%rip), %edx     # 0x1c2e8
addl %edx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %esi            # imm = 0x7D0
idivl %esi
movslq %edx, %rax
leaq 0x11277(%rip), %rdx     # 0x18450
leaq (%rdx,%rax,8), %rax
addq $0x18, %rax
jmp 0x720d
cltq
shlq $0x3, %rax
addq 0x11268(%rip), %rax     # 0x18458
movslq 0x11235(%rip), %rbx     # 0x1842c
shlq $0x4, %rbx
addq (%rax), %rbx
jmp 0x722c
cltq
shlq $0x3, %rax
addq 0x1124b(%rip), %rax     # 0x18458
xorl %edx, %edx
cmpl %ebp, 0x1121b(%rip)     # 0x18430
jne 0x721d
movl 0x1120f(%rip), %edx     # 0x1842c
movslq %edx, %rbx
shlq $0x4, %rbx
addq (%rax), %rbx
cmpl %ebp, %r12d
jne 0x7235
movl 0x11201(%rip), %r12d    # 0x18434
jmp 0x723f
movl 0x11218(%rip), %r12d    # 0x18454
decl %r12d
movl %ebp, %eax
subl %ecx, %eax
jge 0x726e
movl 0x1509d(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0x111ec(%rip), %rcx     # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x727b
cltq
shlq $0x3, %rax
addq 0x111dd(%rip), %rax     # 0x18458
leal -0x1(%r15), %ecx
cmpl %ecx, %r12d
cmovll %r12d, %ecx
movslq %ecx, %r13
shlq $0x4, %r13
addq (%rax), %r13
cmpq %rbx, %r13
jb 0x72b9
cmpl $0x20, (%r13)
jne 0x72b9
addq $-0x10, %r13
jmp 0x7290
testb $0x4, 0x5(%rbx)
jne 0x72b5
movl (%rbx), %edi
movq %r14, %rsi
callq 0x6ce2
addq %rax, %r14
addq $0x10, %rbx
cmpq %r13, %rbx
jbe 0x72a2
cmpl 0x11174(%rip), %ebp     # 0x18438
jl 0x72cb
cmpl %r15d, %r12d
jl 0x72e2
testb $0x1, 0x5(%r13)
je 0x72db
cmpl $0x2, 0x1114b(%rip)     # 0x18424
jne 0x72e2
movb $0xa, (%r14)
incq %r14
incl %ebp
jmp 0x7156
movb $0x0, (%r14)
movq (%rsp), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq