// File: /workspace/github_c_makefile_star2_O1/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, 0x122c3(%rip)    # 0x188dc
je 0x67ec
movl 0x122c7(%rip), %eax     # 0x188ec
btl $0x2, 0x1619f(%rip)     # 0x1c7cc
sbbl $0x0, %eax
jne 0x67ec
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x122ad(%rip), %eax     # 0x188f4
movl 0x1228b(%rip), %ecx     # 0x188d8
subl 0x1227d(%rip), %ecx     # 0x188d0
incl %ecx
leal 0x4(,%rax,4), %eax
imull %ecx, %eax
movslq %eax, %rdi
callq 0x5eec
movq %rax, %r14
movl 0x1225f(%rip), %r15d    # 0x188d0
movl 0x12261(%rip), %eax     # 0x188d8
movq %r14, (%rsp)
cmpl %eax, %r15d
jg 0x67d5
movq (%rsp), %r14
movl %r15d, %ecx
subl 0x160f7(%rip), %ecx     # 0x1c788
jge 0x66d4
movl 0x160e7(%rip), %edx     # 0x1c780
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 0x12226(%rip), %rdx     # 0x188f0
leaq (%rdx,%rcx,8), %rcx
addq $0x10, %rcx
jmp 0x66e2
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x12216(%rip), %rcx     # 0x188f8
movq (%rcx), %r13
movl 0x12209(%rip), %ecx     # 0x188f4
movl %ecx, %ebp
sarl $0x1f, %ebp
andl %ecx, %ebp
movq %rcx, %rdx
shlq $0x4, %rdx
leaq (%rdx,%r13), %rsi
addq $-0xc, %rsi
movl %ecx, %edx
testl %edx, %edx
jle 0x671c
movzwl (%rsi), %edi
addq $-0x10, %rsi
decl %edx
testl $0x201, %edi            # imm = 0x201
je 0x6703
incl %edx
movl %edx, %ebp
testl %ebp, %ebp
je 0x67b8
cmpl $0x2, 0x12199(%rip)     # 0x188c4
jne 0x6735
movl 0x12199(%rip), %edx     # 0x188cc
jmp 0x674b
xorl %edx, %edx
cmpl %r15d, 0x12192(%rip)    # 0x188d0
jne 0x6746
movl 0x12186(%rip), %edx     # 0x188cc
cmpl %r15d, %eax
jne 0x6754
movl 0x12182(%rip), %r12d    # 0x188d4
jmp 0x6758
leal -0x1(%rcx), %r12d
leal -0x1(%rbp), %eax
cmpl %eax, %r12d
cmovll %r12d, %eax
cltq
movq %rax, %rbx
shlq $0x4, %rbx
addq %r13, %rbx
cmpl %eax, %edx
jg 0x679b
movslq %edx, %rax
shlq $0x4, %rax
addq %rax, %r13
testb $0x8, 0x5(%r13)
jne 0x6792
movl (%r13), %edi
movq %r14, %rsi
callq 0x6004
addq %rax, %r14
addq $0x10, %r13
cmpq %rbx, %r13
jbe 0x677c
cmpl %ebp, %r12d
jge 0x67a9
cmpl 0x12131(%rip), %r15d    # 0x188d8
jge 0x67bf
testb $0x2, 0x5(%rbx)
je 0x67b8
cmpl $0x2, 0x1210e(%rip)     # 0x188c4
jne 0x67bf
movb $0xa, (%r14)
incq %r14
leal 0x1(%r15), %ecx
movl 0x1210f(%rip), %eax     # 0x188d8
cmpl %eax, %r15d
movl %ecx, %r15d
jl 0x6688
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