// File: /workspace/github_c_makefile_star2_O3/mrdotx[P]st/st.c
// Function: copyurl
void
copyurl(const Arg *arg) {
	/* () and [] can appear in urls, but excluding them here will reduce false
	 * positives when figuring out where a given url ends.
	 */
	static char URLCHARS[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz"
		"0123456789-._~:/?#@!$&'*+,;=%";

	static const char* URLSTRINGS[] = {"http://", "https://"};

	/* remove highlighting from previous selection if any */
	if(sel.ob.x >= 0 && sel.oe.x >= 0)
		tsetcolor(sel.nb.y, sel.ob.x, sel.oe.x + 1, defaultfg, defaultbg);

	int i = 0,
		row = 0, /* row of current URL */
		col = 0, /* column of current URL start */
		startrow = 0, /* row of last occurrence */
		colend = 0, /* column of last occurrence */
		passes = 0; /* how many rows have been scanned */

	char *linestr = calloc(term.col+1, sizeof(Rune));
	char *c = NULL,
		 *match = NULL;

	row = (sel.ob.x >= 0 && sel.nb.y > 0) ? sel.nb.y : term.bot;
	LIMIT(row, term.top, term.bot);
	startrow = row;

	colend = (sel.ob.x >= 0 && sel.nb.y > 0) ? sel.nb.x : term.col;
	LIMIT(colend, 0, term.col);

	/*
 	** Scan from (term.bot,term.col) to (0,0) and find
	** next occurrance of a URL
	*/
	while(passes !=term.bot + 2) {
		/* Read in each column of every row until
 		** we hit previous occurrence of URL
		*/
		for (col = 0, i = 0; col < colend; ++col,++i) {
			linestr[i] = term.line[row][col].u;
		}
		linestr[term.col] = '\0';

		if ((match = findlastany(linestr, URLSTRINGS,
						sizeof(URLSTRINGS)/sizeof(URLSTRINGS[0]))))
			break;

		if (--row < term.top)
			row = term.bot;

		colend = term.col;
		passes++;
	};

	if (match) {
		/* must happen before trim */
		selclear();
		sel.ob.x = strlen(linestr) - strlen(match);

		/* trim the rest of the line from the url match */
		for (c = match; *c != '\0'; ++c)
			if (!strchr(URLCHARS, *c)) {
				*c = '\0';
				break;
			}

		/* highlight selection by inverting terminal colors */
		tsetcolor(row, sel.ob.x, sel.ob.x + strlen( match ), defaultbg, defaultfg);

		/* select and copy */
		sel.mode = 1;
		sel.type = SEL_REGULAR;
		sel.oe.x = sel.ob.x + strlen(match)-1;
		sel.ob.y = sel.oe.y = row;
		selnormalize();
		tsetdirt(sel.nb.y, sel.ne.y);
		xsetsel(getsel());
		xclipcopy();
	}

	free(linestr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x1092d(%rip), %r13d    # 0x1a4f4
movl 0x1092f(%rip), %eax     # 0x1a4fc
movl %r13d, %ecx
orl %eax, %ecx
js 0x9c21
movl %eax, %eax
cmpl %eax, %r13d
ja 0x9c21
leaq 0xf9ae(%rip), %rcx      # 0x19590
movl (%rcx), %ecx
leaq 0xf9a9(%rip), %rdx      # 0x19594
movl (%rdx), %edx
movslq 0x108f4(%rip), %rsi     # 0x1a4e8
movq 0x10915(%rip), %rdi     # 0x1a510
movq (%rdi,%rsi,8), %rsi
subq %r13, %rax
incq %rax
movq %r13, %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
movl %ecx, -0x4(%rsi)
movl %edx, (%rsi)
addq $0x10, %rsi
decq %rax
jne 0x9c13
movl 0x108e4(%rip), %r14d    # 0x1a50c
movslq %r14d, %r15
leaq 0x1(%r15), %rdi
movl $0x4, %esi
callq 0x6770
movq %rax, %rbx
movl 0x118b5(%rip), %r8d     # 0x1b4f8
movl %r8d, %eax
addl $0x2, %eax
movl %eax, 0x14(%rsp)
je 0x9ed4
movl 0x1088f(%rip), %edi     # 0x1a4e8
movl 0x11894(%rip), %r9d     # 0x1b4f4
testl %r13d, %r13d
setns %al
testl %edi, %edi
setg %sil
movl %r14d, %ecx
movl %r8d, %edx
testb %sil, %al
je 0x9c7f
movl 0x10867(%rip), %ecx     # 0x1a4e4
movl %edi, %edx
movl %edi, 0x4(%rsp)
leaq -0x1(%rbx), %rax
movq %rax, 0x38(%rsp)
cmpl %r14d, %ecx
movl %r14d, %eax
cmovll %ecx, %eax
cmpl %r8d, %edx
movl %r8d, %r12d
cmovll %edx, %r12d
cmpl %r9d, %edx
cmovll %r9d, %r12d
xorl %edx, %edx
testl %ecx, %ecx
movq 0x1085f(%rip), %rcx     # 0x1a510
movq %rcx, 0x30(%rsp)
cmovsl %edx, %eax
leaq 0xd370(%rip), %rbp      # 0x17030
movl %r14d, 0x10(%rsp)
movq %r15, 0x28(%rsp)
movl %r8d, 0xc(%rsp)
movl %r9d, 0x8(%rsp)
movq %rdx, 0x40(%rsp)
testl %eax, %eax
jle 0x9d00
movslq %r12d, %rcx
movl %eax, %eax
movq 0x30(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
xorl %edx, %edx
movb (%rcx), %sil
movb %sil, (%rbx,%rdx)
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %rax
jne 0x9ced
movb $0x0, (%rbx,%r15)
movq %rbx, %rdi
callq 0x6390
movq %rax, %r15
movq 0x38(%rsp), %rax
leaq (%rax,%r15), %r14
cmpq %rbx, %r14
jae 0x9d52
movl 0x8(%rsp), %r9d
cmpl %r9d, %r12d
leal -0x1(%r12), %ecx
movl 0xc(%rsp), %r8d
cmovlel %r8d, %ecx
movq 0x40(%rsp), %rdx
incl %edx
movl 0x10(%rsp), %eax
movl %ecx, %r12d
cmpl 0x14(%rsp), %edx
movq 0x28(%rsp), %r15
jne 0x9cd4
jmp 0x9ed4
addq %rbx, %r15
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movb $0x1, %r13b
xorl %eax, %eax
movq (%rbp,%rax,8), %r12
movq %r12, %rdi
callq 0x6390
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x61f0
testl %eax, %eax
je 0x9dae
movl $0x1, %eax
testb $0x1, %r13b
movl $0x0, %r13d
jne 0x9d64
decq %r14
decq %r15
cmpq %rbx, %r14
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jae 0x9d5f
jmp 0x9d1e
cmpl $-0x1, 0x18(%rsp)
je 0x9dd8
movl $0x0, 0x10719(%rip)     # 0x1a4d8
movl $0xffffffff, 0x1072b(%rip) # imm = 0xFFFFFFFF
movl 0x10721(%rip), %esi     # 0x1a4f0
movl 0x4(%rsp), %edi
callq 0x72f4
movq %rbx, %rdi
callq 0x6390
movq %rax, %r12
movq %r14, %rdi
callq 0x6390
subq %rax, %r12
movl %r12d, 0x106ff(%rip)    # 0x1a4f4
movb (%r14), %al
testb %al, %al
je 0x9e24
leaq 0xf6fd(%rip), %r13      # 0x19500
movsbl %al, %esi
movq %r13, %rdi
callq 0x6540
testq %rax, %rax
je 0x9e1f
movb (%r15), %al
incq %r15
testb %al, %al
jne 0x9e03
jmp 0x9e24
movb $0x0, -0x1(%r15)
movq %r14, %rdi
callq 0x6390
addl %r12d, %eax
cmpl %r12d, %eax
movq 0x20(%rsp), %r15
jle 0x9e7a
leaq 0xf754(%rip), %rcx      # 0x19594
movl (%rcx), %ecx
leaq 0xf747(%rip), %rdx      # 0x19590
movl (%rdx), %edx
movq 0x106be(%rip), %rsi     # 0x1a510
movslq %r15d, %rdi
movq (%rsi,%rdi,8), %rsi
movslq %r12d, %rdi
cltq
subq %rdi, %rax
shlq $0x4, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
movl %ecx, -0x4(%rsi)
movl %edx, (%rsi)
addq $0x10, %rsi
decq %rax
jne 0x9e6c
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x1064d(%rip)     # 0x1a4d8
movq %r14, %rdi
callq 0x6390
addl %r12d, %eax
decl %eax
movl %eax, 0x1065e(%rip)     # 0x1a4fc
movl %r15d, 0x1065b(%rip)    # 0x1a500
movl %r15d, 0x1064c(%rip)    # 0x1a4f8
callq 0x71f8
movl 0x10631(%rip), %edi     # 0x1a4e8
movl 0x10633(%rip), %esi     # 0x1a4f0
callq 0x72f4
callq 0x75c3
movq %rax, %rdi
callq 0xcf5e
callq 0xcefc
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6360