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

	if (sel.ob.x == -1 || sel.alt != IS_SET(MODE_ALTSCREEN))
		return NULL;

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

	/* append every set & selected glyph to the selection */
	for (y = sel.nb.y; y <= sel.ne.y; y++) {
		Line line = TLINE(y);

		if ((linelen = tlinelen(line)) == 0) {
			*ptr++ = '\n';
			continue;
		}

		if (sel.type == SEL_RECTANGULAR) {
			gp = &line[sel.nb.x];
			lastx = sel.ne.x;
		} else {
			gp = &line[sel.nb.y == y ? sel.nb.x : 0];
			lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
		}
		lgp = &line[MIN(lastx, linelen-1)];

		ptr = tgetglyphs(ptr, gp, lgp);
		/*
		 * 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) &&
		    (!(lgp->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
			*ptr++ = '\n';
	}
	*ptr = '\0';
	return str;
}

// Assembly:
cmpl $-0x1, 0x1285c(%rip)    # 0x188dc
je 0x61f9
movl 0x12860(%rip), %eax     # 0x188ec
btl $0x2, 0x16738(%rip)     # 0x1c7cc
sbbl $0x0, %eax
jne 0x61f9
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x12843(%rip), %eax     # 0x188f4
movl 0x12821(%rip), %ecx     # 0x188d8
subl 0x12813(%rip), %ecx     # 0x188d0
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x5a1c
movq %rax, %r14
movslq 0x127f5(%rip), %rbp     # 0x188d0
movq %rbp, %rbx
shlq $0x3, %rbx
movq %rax, 0x10(%rsp)
movl 0x127ea(%rip), %r13d    # 0x188d8
cmpl %r13d, %ebp
jg 0x61fc
movslq 0x1668a(%rip), %rax     # 0x1c788
cmpl %eax, %ebp
jge 0x612e
movl 0x16678(%rip), %ecx     # 0x1c780
subl %eax, %ecx
leal (%rcx,%rbp), %eax
addl $0x7d1, %eax            # imm = 0x7D1
cltd
movl $0x7d0, %ecx            # imm = 0x7D0
idivl %ecx
movslq %edx, %rax
leaq 0x127cc(%rip), %rcx     # 0x188f0
leaq (%rcx,%rax,8), %rcx
addq $0x10, %rcx
jmp 0x613f
movq 0x127c3(%rip), %rcx     # 0x188f8
shlq $0x3, %rax
subq %rax, %rcx
addq %rbx, %rcx
movq (%rcx), %r12
movq %r12, %rdi
callq 0x6214
testl %eax, %eax
je 0x61e7
movl %eax, %r15d
cmpl $0x2, 0x12768(%rip)     # 0x188c4
movq %rbx, 0x8(%rsp)
jne 0x616b
movl 0x12763(%rip), %eax     # 0x188cc
jmp 0x6180
xorl %eax, %eax
cmpl 0x1275d(%rip), %ebp     # 0x188d0
jne 0x617b
movl 0x12751(%rip), %eax     # 0x188cc
cmpl %r13d, %ebp
jne 0x6188
movl 0x1274e(%rip), %ebx     # 0x188d4
jmp 0x6190
movl 0x12766(%rip), %ebx     # 0x188f4
decl %ebx
leal -0x1(%r15), %edx
movl %ebx, %ecx
cmpl %edx, %ebx
jl 0x619c
movl %edx, %ecx
movslq %eax, %rsi
shlq $0x4, %rsi
addq %r12, %rsi
movslq %ecx, %r13
shlq $0x4, %r13
addq %r12, %r13
movq %r14, %rdi
movq %r13, %rdx
callq 0x6248
movq %rax, %r14
cmpl %r15d, %ebx
jge 0x61d2
cmpl 0x1270f(%rip), %ebp     # 0x188d8
jl 0x61d2
movq 0x8(%rsp), %rbx
jmp 0x61ee
testb $0x2, 0x5(%r13)
movq 0x8(%rsp), %rbx
je 0x61e7
cmpl $0x2, 0x126df(%rip)     # 0x188c4
jne 0x61ee
movb $0xa, (%r14)
incq %r14
incl %ebp
addq $0x8, %rbx
jmp 0x60e7
xorl %eax, %eax
retq
movb $0x0, (%r14)
movq 0x10(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq