// File: /workspace/github_c_makefile_star2_O1/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 0x10dd9(%rip), %r15d    # 0x1a504
movl 0x10ddb(%rip), %eax     # 0x1a50c
movl %r15d, %ecx
orl %eax, %ecx
js 0x9785
movl %eax, %eax
cmpl %eax, %r15d
jg 0x9785
leaq 0xfe5a(%rip), %rcx      # 0x195a0
movl (%rcx), %ecx
leaq 0xfe55(%rip), %rdx      # 0x195a4
movl (%rdx), %edx
movslq 0x10da0(%rip), %rsi     # 0x1a4f8
movq 0x10dc1(%rip), %rdi     # 0x1a520
movq (%rdi,%rsi,8), %rsi
subq %r15, %rax
incq %rax
movq %r15, %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
movl %ecx, -0x4(%rsi)
movl %edx, (%rsi)
addq $0x10, %rsi
decq %rax
jne 0x9777
testl %r15d, %r15d
setns %bl
movl 0x10d8a(%rip), %r14d    # 0x1a51c
movslq %r14d, %rcx
movq %rcx, 0x40(%rsp)
leaq 0x1(%rcx), %rdi
movl $0x4, %esi
callq 0x6780
movq %rax, %r12
movl 0x10d47(%rip), %edi     # 0x1a4f8
testl %edi, %edi
setg %al
andb %bl, %al
movl 0x11d4a(%rip), %edx     # 0x1b508
movl %edx, %ecx
cmovnel %edi, %ecx
movl 0x11d3b(%rip), %esi     # 0x1b504
cmpl %edx, %ecx
movl %edx, %r8d
cmovll %ecx, %r8d
cmpl %esi, %ecx
movl %esi, 0x18(%rsp)
cmovll %esi, %r8d
movl %edx, 0x1c(%rsp)
addl $0x2, %edx
movl %edx, 0x14(%rsp)
je 0x9901
movl %edi, 0xc(%rsp)
movq %r15, 0x20(%rsp)
movl %r14d, %ecx
testb %al, %al
je 0x9803
movl 0x10cf1(%rip), %ecx     # 0x1a4f4
cmpl %r14d, %ecx
movl %r14d, %eax
cmovll %ecx, %eax
xorl %edx, %edx
testl %ecx, %ecx
cmovsl %edx, %eax
leaq -0x1(%r12), %rcx
movq %rcx, 0x38(%rsp)
movq 0x10cfc(%rip), %rbp     # 0x1a520
leaq 0xd805(%rip), %r13      # 0x17030
xorl %ebx, %ebx
movq %r12, 0x28(%rsp)
movl %r14d, 0x10(%rsp)
testl %eax, %eax
jle 0x9861
movslq %r8d, %rcx
movl %eax, %eax
shlq $0x4, %rax
movq %r12, %rdx
xorl %esi, %esi
movq (%rbp,%rcx,8), %rdi
movb (%rdi,%rsi), %dil
movb %dil, (%rdx)
addq $0x10, %rsi
incq %rdx
cmpq %rsi, %rax
jne 0x9849
movq %r8, 0x30(%rsp)
movq 0x40(%rsp), %rax
movb $0x0, (%r12,%rax)
movq %r12, %rdi
callq 0x6390
movq %rax, %r15
addq 0x38(%rsp), %r15
cmpq %r12, %r15
jae 0x988a
xorl %r14d, %r14d
jmp 0x98d2
xorl %r12d, %r12d
movq (%r12,%r13), %r14
movq %r14, %rdi
callq 0x6390
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x61f0
testl %eax, %eax
je 0x98ca
addq $0x8, %r12
cmpq $0x8, %r12
je 0x988d
decq %r15
movl $0x0, %r14d
movq 0x28(%rsp), %r12
cmpq %r12, %r15
jae 0x988a
jmp 0x98d2
movq %r15, %r14
movq 0x28(%rsp), %r12
testq %r14, %r14
jne 0x9906
movq 0x30(%rsp), %rax
cmpl 0x18(%rsp), %eax
leal -0x1(%rax), %eax
movl %eax, %r8d
cmovlel 0x1c(%rsp), %r8d
incl %ebx
movl 0x10(%rsp), %eax
cmpl 0x14(%rsp), %ebx
jne 0x9837
xorl %r14d, %r14d
jmp 0x990b
xorl %r14d, %r14d
jmp 0x9914
movq 0x30(%rsp), %r8
movq 0x20(%rsp), %r15
movl 0xc(%rsp), %edi
testq %r14, %r14
je 0x9a42
movq %r8, %r13
cmpl $-0x1, %r15d
je 0x9945
movl $0x0, 0x10bb8(%rip)     # 0x1a4e8
movl $0xffffffff, 0x10bca(%rip) # imm = 0xFFFFFFFF
movl 0x10bc0(%rip), %esi     # 0x1a500
callq 0x7304
movq %r12, %rdi
callq 0x6390
movq %rax, %r15
movq %r14, %rdi
callq 0x6390
subl %eax, %r15d
movl %r15d, 0x10ba2(%rip)    # 0x1a504
movb (%r14), %al
testb %al, %al
je 0x9993
leaq 0x1(%r14), %rbx
leaq 0xfb9c(%rip), %r15      # 0x19510
movsbl %al, %esi
movq %r15, %rdi
callq 0x6540
testq %rax, %rax
je 0x998f
movb (%rbx), %al
incq %rbx
testb %al, %al
jne 0x9974
jmp 0x9993
movb $0x0, -0x1(%rbx)
movslq 0x10b6a(%rip), %rbx     # 0x1a504
movq %r14, %rdi
callq 0x6390
addl %ebx, %eax
cmpl %eax, %ebx
jge 0x99e9
leaq 0xfbf5(%rip), %rcx      # 0x195a4
movl (%rcx), %ecx
leaq 0xfbe8(%rip), %rdx      # 0x195a0
movl (%rdx), %edx
movq 0x10b5f(%rip), %rsi     # 0x1a520
movslq %r13d, %rdi
movq (%rsi,%rdi,8), %rsi
cltq
subq %rbx, %rax
movq %rbx, %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
movl %ecx, -0x4(%rsi)
movl %edx, (%rsi)
addq $0x10, %rsi
decq %rax
jne 0x99db
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x10aee(%rip)     # 0x1a4e8
movq %r14, %rdi
callq 0x6390
addl %ebx, %eax
decl %eax
movl %eax, 0x10b00(%rip)     # 0x1a50c
movl %r13d, 0x10afd(%rip)    # 0x1a510
movl %r13d, 0x10aee(%rip)    # 0x1a508
callq 0x7208
movl 0x10ad3(%rip), %edi     # 0x1a4f8
movl 0x10ad5(%rip), %esi     # 0x1a500
callq 0x7304
callq 0x74ed
movq %rax, %rdi
callq 0xc71e
callq 0xc6bc
movq %r12, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6360