// File: /workspace/github_c_makefile_star2_O3/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 0x2345(%rip), %xmm0     # 0x5010
movsd 0x2345(%rip), %xmm1     # 0x5018
movsd 0x2345(%rip), %xmm2     # 0x5020
leaq 0x1d0563a(%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 0x2ce2
movzbl 0x1d05614(%rip), %edx   # 0x1d08320
movl %edx, %eax
shrl $0x2, %eax
xorl %ecx, %ecx
cmpl $0x4, %edx
jb 0x2d42
xorl %ecx, %ecx
leaq 0x1d0563f(%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 0x2d21
movl %eax, %ecx
xorl $0xff, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movl %ecx, %eax
leaq 0x1d05609(%rip), %rcx   # 0x1d08360
leaq (%rcx,%rax,4), %rcx
addq $0x3, %rcx
xorl %edx, %edx
movsd 0x22be(%rip), %xmm11    # 0x5028
movss 0x2492(%rip), %xmm1     # 0x5204
xorpd %xmm2, %xmm2
movss 0x248a(%rip), %xmm3     # 0x5208
movsd 0x22aa(%rip), %xmm4     # 0x5030
movss 0x247e(%rip), %xmm5     # 0x520c
movsd 0x22a2(%rip), %xmm6     # 0x5038
movl $0x1c, %esi
xorps %xmm7, %xmm7
cvtsi2ss %edx, %xmm7
divss %xmm0, %xmm7
mulss %xmm7, %xmm7
xorps %xmm8, %xmm8
cvtss2sd %xmm7, %xmm8
movapd %xmm11, %xmm9
subsd %xmm8, %xmm9
movaps %xmm7, %xmm8
mulss %xmm1, %xmm8
cvtss2sd %xmm8, %xmm8
movapd %xmm9, %xmm10
mulsd %xmm2, %xmm10
addsd %xmm8, %xmm10
cvttsd2si %xmm10, %edi
movb %dil, -0x3(%rcx,%rdx,4)
movaps %xmm7, %xmm8
mulss %xmm3, %xmm8
cvtss2sd %xmm8, %xmm8
movapd %xmm9, %xmm10
mulsd %xmm4, %xmm10
addsd %xmm8, %xmm10
cvttsd2si %xmm10, %edi
movb %dil, -0x2(%rcx,%rdx,4)
mulss %xmm5, %xmm7
cvtss2sd %xmm7, %xmm7
mulsd %xmm6, %xmm9
addsd %xmm7, %xmm9
cvttsd2si %xmm9, %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 0x2d9b
movl $0x0, 0x1d05910(%rip)   # 0x1d08760
xorl %r14d, %r14d
movsd 0x21e5(%rip), %xmm1     # 0x5040
leaq 0x1d0590e(%rip), %r15   # 0x1d08770
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x2393(%rip), %xmm1     # 0x5210
cvtss2sd %xmm1, %xmm1
xorps %xmm3, %xmm3
cvtss2sd %xmm0, %xmm3
movapd %xmm11, %xmm2
subsd %xmm3, %xmm2
movapd %xmm3, %xmm4
movsd %xmm3, -0x30(%rbp)
movapd %xmm2, %xmm3
mulsd 0x21a2(%rip), %xmm3     # 0x5048
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
movb %al, (%r15,%r14,4)
movaps %xmm0, %xmm1
mulss 0x2357(%rip), %xmm1     # 0x5214
cvtss2sd %xmm1, %xmm1
movapd %xmm2, %xmm3
mulsd 0x2183(%rip), %xmm3     # 0x5050
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
mulss 0x233b(%rip), %xmm0     # 0x5218
cvtss2sd %xmm0, %xmm0
mulsd 0x216f(%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 0x2159(%rip), %xmm0     # 0x5060
callq 0x2470
addsd 0x2154(%rip), %xmm0     # 0x5068
mulsd 0x2154(%rip), %xmm0     # 0x5070
addsd -0x30(%rbp), %xmm0
mulsd 0x214f(%rip), %xmm0     # 0x5078
mulsd 0x214f(%rip), %xmm0     # 0x5080
cvttsd2si %xmm0, %eax
movb %al, 0x3(%r15,%r14,4)
cmpb $0x2, %al
ja 0x2f45
movl %r14d, 0x1d0581b(%rip)  # 0x1d08760
incq %r14
cmpq $0x100, %r14            # imm = 0x100
movsd 0x20d0(%rip), %xmm11    # 0x5028
movsd 0x20e0(%rip), %xmm1     # 0x5040
jne 0x2e62
movl $0xf231, %edi           # imm = 0xF231
callq 0x2230
leaq 0x230f(%rip), %rdi      # 0x5286
leaq 0x232d(%rip), %rsi      # 0x52ab
callq 0x2420
callq 0x2a2f
movl 0x52da(%rip), %edi      # 0x8268
leaq 0x5303(%rip), %rcx      # 0x8298
leaq 0x1d053bc(%rip), %r8    # 0x1d08358
movl %edi, %esi
xorl %edx, %edx
callq 0x2100
testl %eax, %eax
js 0x35a9
movq 0x1d053a4(%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, 0x52b6(%rip) # 0x82a0
movl 0x5278(%rip), %eax      # 0x8268
movl %eax, 0x52b2(%rip)      # 0x82a8
movl %eax, 0x52b0(%rip)      # 0x82ac
xorl %edi, %edi
callq 0x24a0
leaq 0x1900014(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0xa00008(%rbx), %rax
movq %rax, -0x48(%rbp)
leaq 0x1200010(%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 0x30f2
movl -0x88(%rbp), %eax
cmpl $0x400, %eax            # imm = 0x400
jle 0x3076
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jb 0x30a8
leal -0x603(%rax), %ecx
cmpl $0x2, %ecx
jae 0x30b9
movq %r15, %rdi
callq 0x29b4
jmp 0x3039
cmpl $0x300, %eax            # imm = 0x300
jg 0x3095
cmpl $0x200, %eax            # imm = 0x200
je 0x30d1
cmpl $0x300, %eax            # imm = 0x300
jne 0x3039
movl -0x78(%rbp), %edi
callq 0x2725
jmp 0x3039
cmpl $0x301, %eax            # imm = 0x301
je 0x30e5
cmpl $0x400, %eax            # imm = 0x400
jne 0x3039
movl -0x78(%rbp), %esi
jmp 0x30ac
movzbl -0x77(%rbp), %esi
movl -0x74(%rbp), %edx
movl -0x70(%rbp), %ecx
callq 0x2aff
jmp 0x3039
cmpl $0x602, %eax            # imm = 0x602
jne 0x3039
movq %r15, %rdi
callq 0x294c
jmp 0x3039
cmpb $0x5, -0x7c(%rbp)
jne 0x3039
callq 0x26b0
jmp 0x3039
movl -0x78(%rbp), %edi
callq 0x2877
jmp 0x3039
movq 0x1d0525f(%rip), %rdi   # 0x1d08358
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2360
movq 0x1d0524a(%rip), %rdi   # 0x1d08358
callq 0x2250
leaq 0x1d0520e(%rip), %rdi   # 0x1d08328
callq 0x38ec
cmpl $0x0, 0x600000(%rbx)
jle 0x3171
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 0x36d1
movb $0x0, 0x1600014(%rbx,%r12)
incl %r15d
addq $0x6, %r13
cmpl 0x600000(%rbx), %r15d
jl 0x312f
movl $0x0, 0x600000(%rbx)
leaq 0x1d051a6(%rip), %rdi   # 0x1d08328
callq 0x39c2
cmpl $0x0, 0x120000c(%rbx)
jle 0x31b4
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 0x3192
movl $0x0, 0x120000c(%rbx)
movl 0xe00008(%rbx), %eax
testl %eax, %eax
leaq 0x1d05193(%rip), %r15   # 0x1d08360
movsd 0x1e53(%rip), %xmm4     # 0x5028
movsd 0x1e63(%rip), %xmm5     # 0x5040
leaq 0x1d0558c(%rip), %r12   # 0x1d08770
movsd 0x1e94(%rip), %xmm6     # 0x5080
movss 0x2028(%rip), %xmm7     # 0x521c
movss 0x2023(%rip), %xmm8     # 0x5220
movss 0x201e(%rip), %xmm9     # 0x5224
movl $0x1c, %r13d
jle 0x323c
xorl %ecx, %ecx
movslq 0xa00008(%rbx,%rcx,4), %rax
movzbl 0x1800014(%rbx,%rax), %edx
movl (%r12,%rdx,4), %edx
bswapl %edx
movl %edx, 0x1900014(%rbx,%rax,4)
incq %rcx
movslq 0xe00008(%rbx), %rax
cmpq %rax, %rcx
jl 0x3210
cmpl $0x0, 0xa00004(%rbx)
jle 0x3465
xorl %eax, %eax
movl 0x600004(%rbx,%rax,4), %esi
movslq %esi, %rcx
movzbl 0x1700014(%rbx,%rcx), %edx
movb 0x1800014(%rbx,%rcx), %r8b
cmpl $0x6, %edx
jb 0x32cd
testb %r8b, %r8b
jne 0x3288
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 0x32b2
movl $0x87, %esi
subl %edx, %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
divss %xmm7, %xmm0
addss %xmm8, %xmm0
mulss %xmm9, %xmm0
cvttss2si %xmm0, %esi
movzbl 0x1800014(%rbx,%rcx), %r8d
cmpl %r8d, %esi
jle 0x32f1
movb %sil, 0x1800014(%rbx,%rcx)
movl %esi, %r8d
jmp 0x32f1
testb %r8b, %r8b
jne 0x32f1
movslq 0x120000c(%rbx), %rsi
leal 0x1(%rsi), %edi
movl %edi, 0x120000c(%rbx)
movl %ecx, 0xe0000c(%rbx,%rsi,4)
movb 0x1800014(%rbx,%rcx), %r8b
movzbl (%r15,%rdx,4), %r9d
movzbl 0x1(%r15,%rdx,4), %edi
movzbl 0x2(%r15,%rdx,4), %esi
cmpl $0xff, %edx
je 0x341a
testb %r8b, %r8b
je 0x341a
movzbl %r8b, %r10d
movzbl 0x3(%r12,%r10,4), %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
divsd %xmm5, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm5, %xmm1
movapd %xmm4, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movzbl (%r12,%r10,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 %xmm6, %xmm1
cvttsd2si %xmm1, %r9d
movzbl 0x1(%r12,%r10,4), %r11d
xorps %xmm1, %xmm1
cvtsi2ss %edi, %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 %xmm6, %xmm1
cvttsd2si %xmm1, %edi
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
movzbl 0x2(%r12,%r10,4), %esi
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
addss %xmm1, %xmm0
addss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm6, %xmm0
cvttsd2si %xmm0, %esi
addl %r8d, %edx
cmpl %r14d, %r9d
cmovgel %r14d, %r9d
cmpl %r14d, %edi
cmovgel %r14d, %edi
cmpl %r14d, %esi
cmovgel %r14d, %esi
cmpl %r14d, %edx
cmovael %r14d, %edx
shll $0x18, %r9d
shll $0x10, %edi
orl %edx, %edi
shll $0x8, %esi
orl %r9d, %esi
jmp 0x343b
testb %r8b, %r8b
movl %edx, %r8d
cmovel %r13d, %r8d
cmpb $0x1c, %dl
cmovael %edx, %r8d
shll $0x18, %r9d
orl %r8d, %r9d
shll $0x10, %edi
orl %r9d, %edi
shll $0x8, %esi
orl %edi, %esi
movl %esi, 0x1900014(%rbx,%rcx,4)
movb $0x0, 0x1700014(%rbx,%rcx)
incq %rax
movslq 0xa00004(%rbx), %rcx
cmpq %rcx, %rax
jl 0x324b
movl 0xe00008(%rbx), %eax
xorl %ecx, %ecx
movl %ecx, 0xa00004(%rbx)
movl %ecx, 0x1600010(%rbx)
testl %eax, %eax
jle 0x353d
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 0x1b62(%rip), %xmm0     # 0x5008
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x1bba(%rip), %xmm0     # 0x5070
mulsd 0x1bca(%rip), %xmm0     # 0x5088
addsd 0x1b62(%rip), %xmm0     # 0x5028
mulsd 0x1bc2(%rip), %xmm0     # 0x5090
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl %eax, 0x1d0527c(%rip)   # 0x1d08760
jle 0x34fd
movb $0x0, 0x1800014(%rbx,%r13)
movl $0x0, 0x1900014(%rbx,%r13,4)
jmp 0x351d
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 0x347e
movslq 0x1600010(%rbx), %rdx
shlq $0x2, %rdx
jmp 0x353f
xorl %edx, %edx
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x21e0
movl 0x1600010(%rbx), %eax
movl %eax, 0xe00008(%rbx)
movq -0x30(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq -0x50(%rbp), %rdx
movl $0x1000, %ecx           # imm = 0x1000
callq 0x21b0
movl $0x1e, %edi
callq 0x2190
movq 0x1d04dd8(%rip), %rdi   # 0x1d08358
movq %r15, %rsi
xorl %edx, %edx
leaq 0x4d14(%rip), %rcx      # 0x82a0
callq 0x2270
movq 0x1d04dc0(%rip), %rdi   # 0x1d08358
callq 0x2070
leaq -0x88(%rbp), %r15
jmp 0x3039
callq 0x21a0
leaq 0x1cf8(%rip), %rdi      # 0x52ad
movq %rax, %rsi
xorl %eax, %eax
callq 0x25ac