// File: /workspace/github_c_makefile_star2_O1/aap[P]rx-0/misc/crt.c
// Function: renderthread
static void*
renderthread(void *arg)
{
	CRT *crt;
	SDL_Event ev;
	SDL_MouseButtonEvent *mbev;
	SDL_MouseMotionEvent *mmev;
	SDL_Texture *tex;

	crt = arg;

	crtinit();

	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");

	joyinit();

	if(SDL_CreateWindowAndRenderer(winsize, winsize, 0, &window, &renderer) < 0)
		err("SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
	tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
		SDL_TEXTUREACCESS_STREAMING, 1024, 1024);
	SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
	texrect.x = 0;
	texrect.y = 0;
	texrect.w = winsize;
	texrect.h = winsize;

	SDL_ShowCursor(SDL_DISABLE);

	for(;;){
		while(SDL_PollEvent(&ev))
			switch(ev.type){
			case SDL_WINDOWEVENT:
				winev(&ev.window);
				break;

			case SDL_KEYDOWN:
				keydn(ev.key.keysym.scancode);
//				if(ev.key.keysym.scancode == SDL_SCANCODE_Q)
//					exit(0);
				break;
			case SDL_KEYUP:
				keyup(ev.key.keysym.scancode);
				break;

			case SDL_JOYBUTTONDOWN:
			case SDL_JOYBUTTONUP:
				joybtn(&ev.jbutton);
				break;
			case SDL_JOYHATMOTION:
				joyhat(&ev.jhat);
				break;
			case SDL_JOYDEVICEADDED:
				// ev.jdevice SDL_JoyDeviceEvent
				break;
			case SDL_JOYDEVICEREMOVED:
				break;

			case SDL_MOUSEMOTION:
				mmev = (SDL_MouseMotionEvent*)&ev;
				mouse(0, mmev->state, mmev->x, mmev->y);
				break;
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				mbev = (SDL_MouseButtonEvent*)&ev;
				mouse(mbev->button, mbev->state, mbev->x, mbev->y);
				break;
			}
		SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
		SDL_RenderClear(renderer);

#ifdef AGE
		if(age >= AGETHRESH){
			lock(&crtlock);
			age -= AGETHRESH;
			unlock(&crtlock);
			render(crt);
			SDL_UpdateTexture(tex, nil, crt->pixels, 1024*sizeof(u32));
		}
#else
		render(crt);
		SDL_UpdateTexture(tex, nil, crt->pixels, 1024*sizeof(u32));
		SDL_Delay(30);
#endif

		SDL_RenderCopy(renderer, tex, nil, &texrect);
	//	SDL_RenderCopy(renderer, tex, nil, nil);

		SDL_RenderPresent(renderer);
	}
	return nil;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
xorl %eax, %eax
movsd 0x2235(%rip), %xmm0     # 0x5010
movsd 0x2235(%rip), %xmm1     # 0x5018
movsd 0x2235(%rip), %xmm2     # 0x5020
leaq 0x1d0552a(%rip), %rcx   # 0x1d0831c
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
divsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %edx
movb %dl, (%rax,%rcx)
incq %rax
cmpq $0x8, %rax
jne 0x2df2
movzbl 0x1d05504(%rip), %edx   # 0x1d08320
movl %edx, %eax
shrl $0x2, %eax
xorl %ecx, %ecx
cmpl $0x4, %edx
jb 0x2e50
xorl %ecx, %ecx
leaq 0x1d0552f(%rip), %rdx   # 0x1d08360
cmpl $0x1d, %ecx
movl $0x1c, %esi
cmovael %ecx, %esi
movl $0xffeb6300, (%rdx,%rcx,4) # imm = 0xFFEB6300
movb %sil, 0x3(%rdx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x2e31
cmpl $0xff, %ecx
ja 0x2f65
notb %al
movzbl %al, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movl %ecx, %eax
leaq 0x1d054ef(%rip), %rcx   # 0x1d08360
leaq (%rcx,%rax,4), %rcx
addq $0x3, %rcx
xorl %edx, %edx
movsd 0x21a5(%rip), %xmm1     # 0x5028
movss 0x2379(%rip), %xmm2     # 0x5204
xorpd %xmm3, %xmm3
movss 0x2371(%rip), %xmm4     # 0x5208
movsd 0x2191(%rip), %xmm5     # 0x5030
movss 0x2365(%rip), %xmm6     # 0x520c
movsd 0x2189(%rip), %xmm7     # 0x5038
movl $0x1c, %esi
xorps %xmm8, %xmm8
cvtsi2ss %edx, %xmm8
divss %xmm0, %xmm8
mulss %xmm8, %xmm8
xorps %xmm9, %xmm9
cvtss2sd %xmm8, %xmm9
movapd %xmm1, %xmm10
subsd %xmm9, %xmm10
movaps %xmm8, %xmm9
mulss %xmm2, %xmm9
cvtss2sd %xmm9, %xmm9
movapd %xmm10, %xmm11
mulsd %xmm3, %xmm11
addsd %xmm9, %xmm11
cvttsd2si %xmm11, %edi
movb %dil, -0x3(%rcx,%rdx,4)
movaps %xmm8, %xmm9
mulss %xmm4, %xmm9
cvtss2sd %xmm9, %xmm9
movapd %xmm10, %xmm11
mulsd %xmm5, %xmm11
addsd %xmm9, %xmm11
cvttsd2si %xmm11, %edi
movb %dil, -0x2(%rcx,%rdx,4)
mulss %xmm6, %xmm8
cvtss2sd %xmm8, %xmm8
mulsd %xmm7, %xmm10
addsd %xmm8, %xmm10
cvttsd2si %xmm10, %edi
movb %dil, -0x1(%rcx,%rdx,4)
leal (%rax,%rdx), %edi
cmpl $0x1d, %edi
cmovbl %esi, %edi
movb %dil, (%rcx,%rdx,4)
incq %rdx
leal (%rdx,%rax), %edi
cmpl $0x100, %edi            # imm = 0x100
jne 0x2eb4
movl $0x0, 0x1d057f1(%rip)   # 0x1d08760
xorl %r14d, %r14d
movsd 0x20c6(%rip), %xmm1     # 0x5040
leaq 0x1d057ef(%rip), %r15   # 0x1d08770
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x2274(%rip), %xmm1     # 0x5210
cvtss2sd %xmm1, %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
movsd 0x2079(%rip), %xmm2     # 0x5028
subsd %xmm3, %xmm2
movapd %xmm3, %xmm4
movsd %xmm3, -0x30(%rbp)
movapd %xmm2, %xmm3
mulsd 0x2080(%rip), %xmm3     # 0x5048
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
movb %al, (%r15,%r14,4)
movaps %xmm0, %xmm1
mulss 0x2235(%rip), %xmm1     # 0x5214
cvtss2sd %xmm1, %xmm1
movapd %xmm2, %xmm3
mulsd 0x2061(%rip), %xmm3     # 0x5050
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
mulss 0x2219(%rip), %xmm0     # 0x5218
cvtss2sd %xmm0, %xmm0
mulsd 0x204d(%rip), %xmm2     # 0x5058
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %ecx
movb %al, 0x1(%r15,%r14,4)
movb %cl, 0x2(%r15,%r14,4)
movapd %xmm4, %xmm0
mulsd 0x2037(%rip), %xmm0     # 0x5060
callq 0x2470
addsd 0x2032(%rip), %xmm0     # 0x5068
mulsd 0x2032(%rip), %xmm0     # 0x5070
addsd -0x30(%rbp), %xmm0
mulsd 0x202d(%rip), %xmm0     # 0x5078
mulsd 0x202d(%rip), %xmm0     # 0x5080
cvttsd2si %xmm0, %eax
movb %al, 0x3(%r15,%r14,4)
cmpb $0x2, %al
ja 0x3067
movl %r14d, 0x1d056f9(%rip)  # 0x1d08760
incq %r14
cmpq $0x100, %r14            # imm = 0x100
movsd 0x1fc7(%rip), %xmm1     # 0x5040
jne 0x2f81
movl $0xf231, %edi           # imm = 0xF231
callq 0x2230
leaq 0x21f6(%rip), %rdi      # 0x5286
leaq 0x2214(%rip), %rsi      # 0x52ab
callq 0x2420
callq 0x2b3f
movl 0x51c1(%rip), %edi      # 0x8268
leaq 0x51ea(%rip), %rcx      # 0x8298
leaq 0x1d052a3(%rip), %r8    # 0x1d08358
movl %edi, %esi
xorl %edx, %edx
callq 0x2100
testl %eax, %eax
js 0x36b4
movq 0x1d0528b(%rip), %rdi   # 0x1d08358
movl $0x16462004, %esi       # imm = 0x16462004
movl $0x1, %edx
movl $0x400, %ecx            # imm = 0x400
movl $0x400, %r8d            # imm = 0x400
callq 0x2460
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movl $0x1, %esi
callq 0x22a0
movq $0x0, 0x519d(%rip) # 0x82a0
movl 0x515f(%rip), %eax      # 0x8268
movl %eax, 0x5199(%rip)      # 0x82a8
movl %eax, 0x5197(%rip)      # 0x82ac
xorl %edi, %edi
callq 0x24a0
leaq 0xa00008(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0x1200010(%rbx), %rax
movq %rax, -0x48(%rbp)
leaq 0x1900014(%rbx), %rax
movq %rax, -0x40(%rbp)
leaq 0x4(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq -0x88(%rbp), %r15
movl $0xff, %r14d
movq %r15, %rdi
callq 0x2160
testl %eax, %eax
je 0x320b
movl -0x88(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jle 0x318f
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jb 0x31c1
leal -0x603(%rax), %ecx
cmpl $0x2, %ecx
jae 0x31d2
movq %r15, %rdi
callq 0x2ac4
jmp 0x3152
cmpl $0x300, %eax            # imm = 0x300
jg 0x31ae
cmpl $0x200, %eax            # imm = 0x200
je 0x31ea
cmpl $0x300, %eax            # imm = 0x300
jne 0x3152
movl -0x78(%rbp), %edi
callq 0x2835
jmp 0x3152
cmpl $0x301, %eax            # imm = 0x301
je 0x31fe
cmpl $0x400, %eax            # imm = 0x400
jne 0x3152
movl -0x78(%rbp), %esi
jmp 0x31c5
movzbl -0x77(%rbp), %esi
movl -0x74(%rbp), %edx
movl -0x70(%rbp), %ecx
callq 0x2c0f
jmp 0x3152
cmpl $0x602, %eax            # imm = 0x602
jne 0x3152
movq %r15, %rdi
callq 0x2a5c
jmp 0x3152
cmpb $0x5, -0x7c(%rbp)
jne 0x3152
callq 0x27c0
jmp 0x3152
movl -0x78(%rbp), %edi
callq 0x2987
jmp 0x3152
movq 0x1d05146(%rip), %rdi   # 0x1d08358
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2360
movq 0x1d05131(%rip), %rdi   # 0x1d08358
callq 0x2250
leaq 0x1d050f5(%rip), %rdi   # 0x1d08328
callq 0x3a10
cmpl $0x0, 0x600000(%rbx)
jle 0x328a
xorl %r15d, %r15d
movq -0x38(%rbp), %r13
movzwl -0x2(%r13), %edx
movl %edx, %eax
shll $0xa, %eax
movzwl -0x4(%r13), %esi
leaq (%rsi,%rax), %r12
movzbl 0x1600014(%rbx,%r12), %ecx
movzbl (%r13), %r8d
movq %rbx, %rdi
callq 0x37e1
movb $0x0, 0x1600014(%rbx,%r12)
incl %r15d
addq $0x6, %r13
cmpl 0x600000(%rbx), %r15d
jl 0x3248
movl $0x0, 0x600000(%rbx)
leaq 0x1d0508d(%rip), %rdi   # 0x1d08328
callq 0x3ae6
cmpl $0x0, 0x120000c(%rbx)
jle 0x32cd
xorl %eax, %eax
movslq 0xe0000c(%rbx,%rax,4), %rcx
movl $0x0, 0x1900014(%rbx,%rcx,4)
incq %rax
movslq 0x120000c(%rbx), %rcx
cmpq %rcx, %rax
jl 0x32ab
movl $0x0, 0x120000c(%rbx)
cmpl $0x0, 0xe00008(%rbx)
movsd 0x1d5a(%rip), %xmm4     # 0x5040
leaq 0x1d05483(%rip), %r15   # 0x1d08770
movsd 0x1d8b(%rip), %xmm5     # 0x5080
movss 0x1f1f(%rip), %xmm6     # 0x521c
movss 0x1f1b(%rip), %xmm7     # 0x5220
movss 0x1f16(%rip), %xmm8     # 0x5224
movl $0x1c, %r13d
jle 0x3344
xorl %eax, %eax
movslq 0xa00008(%rbx,%rax,4), %rcx
movzbl 0x1800014(%rbx,%rcx), %edx
movl (%r15,%rdx,4), %edx
bswapl %edx
movl %edx, 0x1900014(%rbx,%rcx,4)
incq %rax
movslq 0xe00008(%rbx), %rcx
cmpq %rcx, %rax
jl 0x3318
cmpl $0x0, 0xa00004(%rbx)
leaq 0x1d0500e(%rip), %r12   # 0x1d08360
jle 0x3570
xorl %eax, %eax
movl 0x600004(%rbx,%rax,4), %esi
movslq %esi, %rcx
movzbl 0x1700014(%rbx,%rcx), %edx
movb 0x1800014(%rbx,%rcx), %dil
cmpl $0x6, %edx
jb 0x33d6
testb %dil, %dil
jne 0x3397
movslq 0xe00008(%rbx), %rdi
leal 0x1(%rdi), %r8d
movl %r8d, 0xe00008(%rbx)
movl %esi, 0xa00008(%rbx,%rdi,4)
movl $0xff, %esi
cmpb $-0x7a, %dl
ja 0x33c0
movl $0x87, %esi
subl %edx, %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
divss %xmm6, %xmm0
addss %xmm7, %xmm0
mulss %xmm8, %xmm0
cvttss2si %xmm0, %esi
movzbl 0x1800014(%rbx,%rcx), %edi
cmpl %edi, %esi
jle 0x33f4
movb %sil, 0x1800014(%rbx,%rcx)
jmp 0x33f4
testb %dil, %dil
jne 0x33f4
movslq 0x120000c(%rbx), %rdi
leal 0x1(%rdi), %r8d
movl %r8d, 0x120000c(%rbx)
movl %esi, 0xe0000c(%rbx,%rdi,4)
movb 0x1800014(%rbx,%rcx), %dil
movzbl %dil, %r8d
movzbl (%r12,%rdx,4), %r10d
movzbl 0x1(%r12,%rdx,4), %r9d
movzbl 0x2(%r12,%rdx,4), %esi
cmpl $0xff, %edx
je 0x352b
testb %dil, %dil
je 0x352b
movzbl 0x3(%r15,%r8,4), %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
divsd %xmm4, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm4, %xmm1
movsd 0x1bde(%rip), %xmm2     # 0x5028
subsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movzbl (%r15,%r8,4), %r11d
xorps %xmm1, %xmm1
cvtsi2ss %r10d, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r11d, %xmm2
movaps %xmm2, %xmm3
mulss %xmm0, %xmm3
addss %xmm1, %xmm3
addss %xmm1, %xmm3
addss %xmm2, %xmm3
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
mulsd %xmm5, %xmm1
cvttsd2si %xmm1, %r10d
movzbl 0x1(%r15,%r8,4), %r11d
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r11d, %xmm2
movaps %xmm2, %xmm3
mulss %xmm0, %xmm3
addss %xmm1, %xmm3
addss %xmm1, %xmm3
addss %xmm2, %xmm3
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
mulsd %xmm5, %xmm1
cvttsd2si %xmm1, %r9d
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
movzbl 0x2(%r15,%r8,4), %esi
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
addss %xmm1, %xmm0
addss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm5, %xmm0
cvttsd2si %xmm0, %esi
addl %edi, %edx
cmpl %r14d, %r10d
cmovgel %r14d, %r10d
cmpl %r14d, %r9d
cmovgel %r14d, %r9d
cmpl %r14d, %esi
cmovgel %r14d, %esi
cmpl %r14d, %edx
cmovgel %r14d, %edx
shll $0x18, %r10d
shll $0x10, %r9d
orl %edx, %r9d
shll $0x8, %esi
orl %r10d, %esi
jmp 0x354b
testq %r8, %r8
movl %edx, %edi
cmovel %r13d, %edi
cmpb $0x1c, %dl
cmovael %edx, %edi
shll $0x18, %r10d
orl %edi, %r10d
shll $0x10, %r9d
orl %r10d, %r9d
shll $0x8, %esi
orl %r9d, %esi
movl %esi, 0x1900014(%rbx,%rcx,4)
movb $0x0, 0x1700014(%rbx,%rcx)
incq %rax
movslq 0xa00004(%rbx), %rcx
cmpq %rcx, %rax
jl 0x335a
xorl %eax, %eax
movl %eax, 0xa00004(%rbx)
movl %eax, 0x1600010(%rbx)
cmpl %eax, 0xe00008(%rbx)
jle 0x363f
xorl %r15d, %r15d
movl 0xa00008(%rbx,%r15,4), %eax
movslq %eax, %r13
movzbl 0x1800014(%rbx,%r13), %r12d
callq 0x20c0
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x1a53(%rip), %xmm0     # 0x5008
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x1aab(%rip), %xmm0     # 0x5070
mulsd 0x1abb(%rip), %xmm0     # 0x5088
addsd 0x1a53(%rip), %xmm0     # 0x5028
mulsd 0x1ab3(%rip), %xmm0     # 0x5090
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl %eax, 0x1d0516d(%rip)   # 0x1d08760
jle 0x360c
movb $0x0, 0x1800014(%rbx,%r13)
movl $0x0, 0x1900014(%rbx,%r13,4)
jmp 0x362c
movb %al, 0x1800014(%rbx,%r13)
movslq 0x1600010(%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x1600010(%rbx)
movl %r13d, 0x1200010(%rbx,%rax,4)
incq %r15
movslq 0xe00008(%rbx), %rax
cmpq %rax, %r15
jl 0x358d
movslq 0x1600010(%rbx), %rdx
shlq $0x2, %rdx
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x21e0
movl 0x1600010(%rbx), %eax
movl %eax, 0xe00008(%rbx)
movq -0x30(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rdx
movl $0x1000, %ecx           # imm = 0x1000
callq 0x21b0
movl $0x1e, %edi
callq 0x2190
movq 0x1d04ccd(%rip), %rdi   # 0x1d08358
movq %r15, %rsi
xorl %edx, %edx
leaq 0x4c09(%rip), %rcx      # 0x82a0
callq 0x2270
movq 0x1d04cb5(%rip), %rdi   # 0x1d08358
callq 0x2070
leaq -0x88(%rbp), %r15
jmp 0x3152
callq 0x21a0
leaq 0x1bed(%rip), %rdi      # 0x52ad
movq %rax, %rsi
xorl %eax, %eax
callq 0x26bc