// File: /workspace/github_c_makefile_star2_O3/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, 0x11d5b(%rip)    # 0x1942c
je 0x793a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x11d59(%rip), %eax     # 0x19444
movl 0x11d37(%rip), %ecx     # 0x19428
subl 0x11d29(%rip), %ecx     # 0x19420
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x707c
movq %rax, %r15
movl 0x11d0b(%rip), %r14d    # 0x19420
movl 0x11d0d(%rip), %ebp     # 0x19428
movq %rax, 0x8(%rsp)
cmpl %ebp, %r14d
jg 0x7922
movq 0x8(%rsp), %r15
movl %r14d, %edi
callq 0x793d
testl %eax, %eax
je 0x7905
cmpl $0x2, 0x11ccf(%rip)     # 0x19414
movl 0x15b91(%rip), %edi     # 0x1d2dc
movl %r14d, %ecx
jne 0x7799
subl %edi, %ecx
jge 0x77de
movl 0x15b7a(%rip), %edx     # 0x1d2d8
addl %edx, %ecx
addl $0x7d1, %ecx            # imm = 0x7D1
movslq %ecx, %rcx
imulq $0x10624dd3, %rcx, %rdx # imm = 0x10624DD3
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x27, %rdx
addl %esi, %edx
imull $0x7d0, %edx, %edx      # imm = 0x7D0
subl %edx, %ecx
movslq %ecx, %rcx
leaq 0x11cb1(%rip), %rdx     # 0x19440
leaq (%rdx,%rcx,8), %rcx
addq $0x18, %rcx
jmp 0x77ec
subl %edi, %ecx
jge 0x7802
movl 0x15b35(%rip), %edx     # 0x1d2d8
addl %edx, %ecx
addl $0x7d1, %ecx            # imm = 0x7D1
movslq %ecx, %rcx
imulq $0x10624dd3, %rcx, %rdx # imm = 0x10624DD3
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x27, %rdx
addl %esi, %edx
imull $0x7d0, %edx, %edx      # imm = 0x7D0
subl %edx, %ecx
movslq %ecx, %rcx
leaq 0x11c6c(%rip), %rdx     # 0x19440
leaq (%rdx,%rcx,8), %rcx
addq $0x18, %rcx
jmp 0x7810
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x11c5c(%rip), %rcx     # 0x19448
movslq 0x11c29(%rip), %r13     # 0x1941c
shlq $0x4, %r13
addq (%rcx), %r13
movl 0x11c24(%rip), %ebx     # 0x19424
jmp 0x7840
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x11c38(%rip), %rcx     # 0x19448
xorl %edx, %edx
cmpl %r14d, 0x11c07(%rip)    # 0x19420
jne 0x7821
movl 0x11bfb(%rip), %edx     # 0x1941c
cmpl %r14d, %ebp
jne 0x782e
movl 0x11bf8(%rip), %ebx     # 0x19424
jmp 0x7836
movl 0x11c10(%rip), %ebx     # 0x19444
decl %ebx
movslq %edx, %r13
shlq $0x4, %r13
addq (%rcx), %r13
movl %r14d, %ecx
subl %edi, %ecx
jge 0x7888
movl 0x15a8b(%rip), %edx     # 0x1d2d8
addl %edx, %ecx
addl $0x7d1, %ecx            # imm = 0x7D1
movslq %ecx, %rsi
imulq $0x10624dd3, %rsi, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %esi
movslq %esi, %rdx
leaq 0x11bc2(%rip), %rcx     # 0x19440
leaq (%rcx,%rdx,8), %rdx
addq $0x18, %rdx
jmp 0x7896
movslq %ecx, %rdx
shlq $0x3, %rdx
addq 0x11bb2(%rip), %rdx     # 0x19448
movq %rax, 0x10(%rsp)
leal -0x1(%rax), %ecx
cmpl %ecx, %ebx
cmovll %ebx, %ecx
movslq %ecx, %r12
shlq $0x4, %r12
addq (%rdx), %r12
cmpq %r13, %r12
jb 0x78bf
cmpl $0x20, (%r12)
jne 0x78bf
addq $-0x10, %r12
jmp 0x78ad
cmpq %r12, %r13
ja 0x78e9
testb $0x4, 0x5(%r13)
jne 0x78da
movl (%r13), %edi
movq %r15, %rsi
callq 0x7194
addq %rax, %r15
addq $0x10, %r13
cmpq %r12, %r13
jbe 0x78c4
movl 0x11b3f(%rip), %ebp     # 0x19428
cmpl %ebp, %r14d
jl 0x78f4
cmpl 0x10(%rsp), %ebx
jl 0x790c
testb $0x1, 0x5(%r12)
je 0x7905
cmpl $0x2, 0x11b11(%rip)     # 0x19414
jne 0x790c
movb $0xa, (%r15)
incq %r15
leal 0x1(%r14), %eax
movl 0x11b12(%rip), %ebp     # 0x19428
cmpl %ebp, %r14d
movl %eax, %r14d
jl 0x772e
movb $0x0, (%r15)
movq 0x8(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq