// File: /workspace/github_c_makefile_star2_O1/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, 0x10e31(%rip)    # 0x1843c
je 0x785a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x10e32(%rip), %eax     # 0x18454
movl 0x10e10(%rip), %ecx     # 0x18438
subl 0x10e02(%rip), %ecx     # 0x18430
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x709c
movq %rax, %r15
movl 0x10de4(%rip), %r14d    # 0x18430
movl 0x10de6(%rip), %ebx     # 0x18438
movq %rax, (%rsp)
cmpl %ebx, %r14d
jg 0x7843
movq (%rsp), %r15
movl %r14d, %edi
callq 0x785d
testl %eax, %eax
je 0x7826
movl %eax, %r12d
cmpl $0x2, 0x10da7(%rip)     # 0x18424
movl 0x14c69(%rip), %ecx     # 0x1c2ec
movl %r14d, %eax
jne 0x76ce
subl %ecx, %eax
jge 0x7710
movl 0x14c52(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %eax
cltq
leaq 0x10d8c(%rip), %rcx     # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x771d
subl %ecx, %eax
jge 0x7733
movl 0x14c10(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %eax
cltq
leaq 0x10d4a(%rip), %rcx     # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x7740
cltq
shlq $0x3, %rax
addq 0x10d3b(%rip), %rax     # 0x18458
movslq 0x10d08(%rip), %rbx     # 0x1842c
shlq $0x4, %rbx
addq (%rax), %rbx
movl 0x10d03(%rip), %ebp     # 0x18434
jmp 0x7770
cltq
shlq $0x3, %rax
addq 0x10d18(%rip), %rax     # 0x18458
xorl %ecx, %ecx
cmpl %r14d, 0x10ce7(%rip)    # 0x18430
jne 0x7751
movl 0x10cdb(%rip), %ecx     # 0x1842c
cmpl %r14d, %ebx
jne 0x775e
movl 0x10cd8(%rip), %ebp     # 0x18434
jmp 0x7766
movl 0x10cf0(%rip), %ebp     # 0x18454
decl %ebp
movslq %ecx, %rbx
shlq $0x4, %rbx
addq (%rax), %rbx
movl %r14d, %eax
subl 0x14b73(%rip), %eax     # 0x1c2ec
jge 0x77b9
movl 0x14b67(%rip), %ecx     # 0x1c2e8
addl %ecx, %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x27, %rcx
addl %edx, %ecx
imull $0x7d0, %ecx, %ecx      # imm = 0x7D0
subl %ecx, %eax
cltq
leaq 0x10ca1(%rip), %rcx     # 0x18450
leaq (%rcx,%rax,8), %rax
addq $0x18, %rax
jmp 0x77c6
cltq
shlq $0x3, %rax
addq 0x10c92(%rip), %rax     # 0x18458
leal -0x1(%r12), %ecx
cmpl %ecx, %ebp
cmovll %ebp, %ecx
movslq %ecx, %r13
shlq $0x4, %r13
addq (%rax), %r13
cmpq %rbx, %r13
jb 0x7803
cmpl $0x20, (%r13)
jne 0x7803
addq $-0x10, %r13
jmp 0x77da
testb $0x4, 0x5(%rbx)
jne 0x77ff
movl (%rbx), %edi
movq %r15, %rsi
callq 0x71b4
addq %rax, %r15
addq $0x10, %rbx
cmpq %r13, %rbx
jbe 0x77ec
cmpl 0x10c29(%rip), %r14d    # 0x18438
jl 0x7816
cmpl %r12d, %ebp
jl 0x782d
testb $0x1, 0x5(%r13)
je 0x7826
cmpl $0x2, 0x10c00(%rip)     # 0x18424
jne 0x782d
movb $0xa, (%r15)
incq %r15
leal 0x1(%r14), %eax
movl 0x10c01(%rip), %ebx     # 0x18438
cmpl %ebx, %r14d
movl %eax, %r14d
jl 0x7663
movb $0x0, (%r15)
movq (%rsp), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq