// File: /workspace/github_c_makefile_star2_O0/odknt[P]st/x.c
// Function: run
void
run(void)
{
	XEvent ev;
	int w = win.w, h = win.h;
	fd_set rfd;
	int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
	struct timespec seltv, *tv, now, lastblink, trigger;
	double timeout;

	/* Waiting for window mapping */
	do {
		XNextEvent(xw.dpy, &ev);
		/*
		 * This XFilterEvent call is required because of XOpenIM. It
		 * does filter out the key event and some client message for
		 * the input method too.
		 */
		if (XFilterEvent(&ev, None))
			continue;
		if (ev.type == ConfigureNotify) {
			w = ev.xconfigure.width;
			h = ev.xconfigure.height;
		}
	} while (ev.type != MapNotify);

	ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
	cresize(w, h);

	for (timeout = -1, drawing = 0, lastblink = (struct timespec){0}; canloop;) {
		FD_ZERO(&rfd);
		FD_SET(ttyfd, &rfd);
		FD_SET(xfd, &rfd);

		if (XPending(xw.dpy))
			timeout = 0;  /* existing events might not set xfd */

		seltv.tv_sec = timeout / 1E3;
		seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec);
		tv = timeout >= 0 ? &seltv : NULL;

		if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
			if (errno == EBADF)
				break;
			if (errno == EINTR)
				continue;
			die("select failed: %s\n", strerror(errno));
		}
		clock_gettime(CLOCK_MONOTONIC, &now);

		if (FD_ISSET(ttyfd, &rfd))
			ttyread();

		xev = 0;
		while (XPending(xw.dpy)) {
			xev = 1;
			XNextEvent(xw.dpy, &ev);
			if (XFilterEvent(&ev, None))
				continue;
			if (handler[ev.type])
				(handler[ev.type])(&ev);
		}

		/*
		 * To reduce flicker and tearing, when new content or event
		 * triggers drawing, we first wait a bit to ensure we got
		 * everything, and if nothing new arrives - we draw.
		 * We start with trying to wait minlatency ms. If more content
		 * arrives sooner, we retry with shorter and shorter periods,
		 * and eventually draw even without idle after maxlatency ms.
		 * Typically this results in low latency while interacting,
		 * maximum latency intervals during `cat huge.txt`, and perfect
		 * sync with periodic updates from animations/key-repeats/etc.
		 */
		if (FD_ISSET(ttyfd, &rfd) || xev) {
			if (!drawing) {
				trigger = now;
				drawing = 1;
			}
			timeout = (maxlatency - TIMEDIFF(now, trigger)) \
			          / maxlatency * minlatency;
			if (timeout > 0)
				continue;  /* we have time, try to find idle */
		}

		/* idle detected or maxlatency exhausted -> draw */
		timeout = -1;
		if (blinktimeout && tattrset(ATTR_BLINK)) {
			timeout = blinktimeout - TIMEDIFF(now, lastblink);
			if (timeout <= 0) {
				if (-timeout > blinktimeout) /* start visible */
					win.mode |= MODE_BLINK;
				win.mode ^= MODE_BLINK;
				tsetdirtattr(ATTR_BLINK);
				lastblink = now;
				timeout = blinktimeout;
			}
		}

		draw();
		XFlush(xw.dpy);
		drawing = 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movl 0x11b5f(%rip), %eax     # 0x226c0
movl %eax, -0xc4(%rbp)
movl 0x11b57(%rip), %eax     # 0x226c4
movl %eax, -0xc8(%rbp)
movq 0x11a16(%rip), %rdi     # 0x22590
callq 0x62f0
movl %eax, -0x14c(%rbp)
movq 0x11a04(%rip), %rdi     # 0x22590
leaq -0xc0(%rbp), %rsi
callq 0x6510
leaq -0xc0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x63c0
cmpl $0x0, %eax
je 0x10baf
jmp 0x10bd2
cmpl $0x16, -0xc0(%rbp)
jne 0x10bd0
movl -0x88(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x10bd2
cmpl $0x13, -0xc0(%rbp)
jne 0x10b85
movq 0x11b2e(%rip), %rdi     # 0x22710
movq 0x93f7(%rip), %rsi      # 0x19fe0
movq 0x11b18(%rip), %rdx     # 0x22708
movq 0x11b29(%rip), %rcx     # 0x22720
callq 0x7990
movl %eax, -0x150(%rbp)
movl -0xc4(%rbp), %edi
movl -0xc8(%rbp), %esi
callq 0x122e0
movsd 0x42fd(%rip), %xmm0     # 0x14f18
movsd %xmm0, -0x1a8(%rbp)
movl $0x0, -0x158(%rbp)
movq $0x0, -0x1b8(%rbp)
movq $0x0, -0x1b0(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x188(%rbp)
cmpl $0x0, 0x967a(%rip)      # 0x1a2e0
je 0x11170
jmp 0x10c6e
leaq -0x148(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movl $0x0, -0x1bc(%rbp)
movl -0x1bc(%rbp), %eax
cmpq $0x10, %rax
jae 0x10cb8
movq -0x1c8(%rbp), %rax
movl -0x1bc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x10c86
jmp 0x10cba
movl -0x150(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x150(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x148(%rbp,%rax,8), %rcx
movq %rcx, -0x148(%rbp,%rax,8)
movl -0x14c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x14c(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x148(%rbp,%rax,8), %rcx
movq %rcx, -0x148(%rbp,%rax,8)
movq 0x11855(%rip), %rdi     # 0x22590
callq 0x6a90
cmpl $0x0, %eax
je 0x10d50
xorps %xmm0, %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x1a8(%rbp), %xmm0
movsd 0x41d0(%rip), %xmm1     # 0x14f30
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x168(%rbp)
movsd -0x1a8(%rbp), %xmm0
cvtsi2sdq -0x168(%rbp), %xmm1
movsd 0x419f(%rip), %xmm2     # 0x14f28
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x4187(%rip), %xmm0     # 0x14f20
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x160(%rbp)
movsd -0x1a8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x10dca
leaq -0x168(%rbp), %rax
movq %rax, -0x1d0(%rbp)
jmp 0x10dd5
xorl %eax, %eax
movq %rax, -0x1d0(%rbp)
jmp 0x10dd5
movq -0x1d0(%rbp), %rax
movq %rax, -0x170(%rbp)
movl -0x14c(%rbp), %eax
cmpl -0x150(%rbp), %eax
jge 0x10dff
movl -0x150(%rbp), %eax
movl %eax, -0x1d4(%rbp)
jmp 0x10e0b
movl -0x14c(%rbp), %eax
movl %eax, -0x1d4(%rbp)
movl -0x1d4(%rbp), %edi
addl $0x1, %edi
movq -0x170(%rbp), %r8
leaq -0x148(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %rdx
movq %r9, %rcx
callq 0x6370
cmpl $0x0, %eax
jge 0x10e72
callq 0x66e0
cmpl $0x9, (%rax)
jne 0x10e46
jmp 0x11170
callq 0x66e0
cmpl $0x4, (%rax)
jne 0x10e55
jmp 0x10c5f
callq 0x66e0
movl (%rax), %edi
callq 0x6570
movq %rax, %rsi
leaq 0x3c49(%rip), %rdi      # 0x14ab4
movb $0x0, %al
callq 0x6c20
movl $0x1, %edi
leaq -0x180(%rbp), %rsi
callq 0x6140
movl -0x150(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x148(%rbp,%rax,8), %rax
movq %rax, -0x1e0(%rbp)
movl -0x150(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1e0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x10ed2
callq 0x8130
movl $0x0, -0x154(%rbp)
movq 0x116ad(%rip), %rdi     # 0x22590
callq 0x6a90
cmpl $0x0, %eax
je 0x10f50
movl $0x1, -0x154(%rbp)
movq 0x11692(%rip), %rdi     # 0x22590
leaq -0xc0(%rbp), %rsi
callq 0x6510
leaq -0xc0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x63c0
cmpl $0x0, %eax
je 0x10f21
jmp 0x10edc
movslq -0xc0(%rbp), %rcx
leaq 0x9431(%rip), %rax      # 0x1a360
cmpq $0x0, (%rax,%rcx,8)
je 0x10f4e
movslq -0xc0(%rbp), %rcx
leaq -0xc0(%rbp), %rdi
leaq 0x9415(%rip), %rax      # 0x1a360
callq *(%rax,%rcx,8)
jmp 0x10edc
movl -0x150(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x148(%rbp,%rax,8), %rax
movq %rax, -0x1e8(%rbp)
movl -0x150(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1e8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x10fa7
cmpl $0x0, -0x154(%rbp)
je 0x1104f
cmpl $0x0, -0x158(%rbp)
jne 0x10fd6
movq -0x180(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x198(%rbp)
movl $0x1, -0x158(%rbp)
movsd 0x9012(%rip), %xmm0     # 0x19ff0
movq -0x180(%rbp), %rax
subq -0x1a0(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cvtsi2sd %rax, %xmm1
movq -0x178(%rbp), %rax
subq -0x198(%rbp), %rax
cvtsi2sd %rax, %xmm2
movsd 0x3f0d(%rip), %xmm3     # 0x14f20
divsd %xmm3, %xmm2
addsd %xmm2, %xmm1
subsd %xmm1, %xmm0
divsd 0x8fc9(%rip), %xmm0     # 0x19ff0
mulsd 0x8fb9(%rip), %xmm0     # 0x19fe8
movsd %xmm0, -0x1a8(%rbp)
movsd -0x1a8(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1104d
jmp 0x10c5f
jmp 0x1104f
movsd 0x3ec1(%rip), %xmm0     # 0x14f18
movsd %xmm0, -0x1a8(%rbp)
cmpl $0x0, 0x8f92(%rip)      # 0x19ff8
je 0x11150
movl $0x10, %edi
callq 0x88a0
cmpl $0x0, %eax
je 0x11150
movl 0x8f73(%rip), %eax      # 0x19ff8
cvtsi2sd %rax, %xmm0
movq -0x180(%rbp), %rax
subq -0x190(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cvtsi2sd %rax, %xmm1
movq -0x178(%rbp), %rax
subq -0x188(%rbp), %rax
cvtsi2sd %rax, %xmm2
movsd 0x3e61(%rip), %xmm3     # 0x14f20
divsd %xmm3, %xmm2
addsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x1a8(%rbp)
xorps %xmm0, %xmm0
ucomisd -0x1a8(%rbp), %xmm0
jb 0x1114e
movaps 0x3e79(%rip), %xmm1     # 0x14f60
movsd -0x1a8(%rbp), %xmm0
pxor %xmm1, %xmm0
movl 0x8eff(%rip), %eax      # 0x19ff8
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x11115
movl 0x115ca(%rip), %eax     # 0x226d4
orl $0x800, %eax            # imm = 0x800
movl %eax, 0x115bf(%rip)     # 0x226d4
movb 0x115ba(%rip), %al      # 0x226d5
xorb $0x8, %al
movb %al, 0x115b2(%rip)      # 0x226d5
movl $0x10, %edi
callq 0x8930
movups -0x180(%rbp), %xmm0
movaps %xmm0, -0x190(%rbp)
movl 0x8eb7(%rip), %eax      # 0x19ff8
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x1a8(%rbp)
jmp 0x11150
callq 0x9be0
movq 0x11434(%rip), %rdi     # 0x22590
callq 0x66f0
movl $0x0, -0x158(%rbp)
jmp 0x10c5f
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
nopl (%rax)