// File: /workspace/github_c_makefile_star2_O3/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, 0x121df(%rip)    # 0x188cc
je 0x68c1
movl 0x121e3(%rip), %eax     # 0x188dc
btl $0x2, 0x160bb(%rip)     # 0x1c7bc
sbbl $0x0, %eax
jne 0x68c1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x121c9(%rip), %eax     # 0x188e4
movl 0x121a7(%rip), %ecx     # 0x188c8
subl 0x12199(%rip), %ecx     # 0x188c0
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x5edc
movq %rax, %r14
movl 0x1217b(%rip), %r15d    # 0x188c0
movl 0x1217d(%rip), %eax     # 0x188c8
movq %r14, (%rsp)
cmpl %eax, %r15d
jg 0x68aa
movq (%rsp), %r14
movl %r15d, %ecx
subl 0x16013(%rip), %ecx     # 0x1c778
jge 0x67a8
movl 0x16003(%rip), %edx     # 0x1c770
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 0x12142(%rip), %rdx     # 0x188e0
leaq (%rdx,%rcx,8), %rcx
addq $0x10, %rcx
jmp 0x67b6
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x12132(%rip), %rcx     # 0x188e8
movq (%rcx), %rbp
movl 0x12125(%rip), %ecx     # 0x188e4
movq %rcx, %rdx
shlq $0x4, %rdx
addq %rbp, %rdx
addq $-0xc, %rdx
movl %ecx, %r13d
testl %r13d, %r13d
jle 0x67ec
movzwl (%rdx), %esi
addq $-0x10, %rdx
decl %r13d
testl $0x201, %esi            # imm = 0x201
je 0x67d0
incl %r13d
jmp 0x67f7
movl %ecx, %r13d
testl %ecx, %ecx
jns 0x688d
cmpl $0x2, 0x120b6(%rip)     # 0x188b4
jne 0x6808
movl 0x120b6(%rip), %edx     # 0x188bc
jmp 0x681e
xorl %edx, %edx
cmpl %r15d, 0x120af(%rip)    # 0x188c0
jne 0x6819
movl 0x120a3(%rip), %edx     # 0x188bc
cmpl %r15d, %eax
jne 0x6827
movl 0x1209f(%rip), %r12d    # 0x188c4
jmp 0x682b
leal -0x1(%rcx), %r12d
leal -0x1(%r13), %ecx
cmpl %ecx, %r12d
cmovll %r12d, %ecx
movslq %ecx, %rcx
movq %rcx, %rbx
shlq $0x4, %rbx
addq %rbp, %rbx
cmpl %ecx, %edx
jg 0x6874
movslq %edx, %rax
shlq $0x4, %rax
addq %rax, %rbp
testb $0x8, 0x5(%rbp)
jne 0x6865
movl (%rbp), %edi
movq %r14, %rsi
callq 0x5ff4
addq %rax, %r14
addq $0x10, %rbp
cmpq %rbx, %rbp
jbe 0x6851
movl 0x12054(%rip), %eax     # 0x188c8
cmpl %r13d, %r12d
jge 0x687e
cmpl %eax, %r15d
jge 0x6894
testb $0x2, 0x5(%rbx)
je 0x688d
cmpl $0x2, 0x12029(%rip)     # 0x188b4
jne 0x6894
movb $0xa, (%r14)
incq %r14
leal 0x1(%r15), %ecx
movl 0x1202a(%rip), %eax     # 0x188c8
cmpl %eax, %r15d
movl %ecx, %r15d
jl 0x675c
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