// File: /workspace/github_c_makefile_star2_O2/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 0x2424(%rip), %xmm0     # 0x5010
movsd 0x2424(%rip), %xmm1     # 0x5018
movsd 0x2424(%rip), %xmm2     # 0x5020
leaq 0x1d05719(%rip), %rcx   # 0x1d0831c
cmpq $0x8, %rax
je 0x2c25
cvtsi2sd %eax, %xmm3
divsd %xmm0, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %edx
movb %dl, (%rax,%rcx)
incq %rax
jmp 0x2c03
movzbl 0x1d056f4(%rip), %eax   # 0x1d08320
shrl $0x2, %eax
xorl %ecx, %ecx
leaq 0x1d05728(%rip), %rsi   # 0x1d08360
pushq $0x1c
popq %r8
cmpq %rcx, %rax
je 0x2c5a
movl $0xffeb6300, (%rsi,%rcx,4) # imm = 0xFFEB6300
cmpl $0x1d, %ecx
movl %r8d, %edx
cmovael %ecx, %edx
movb %dl, 0x3(%rsi,%rcx,4)
incq %rcx
jmp 0x2c3c
movzbl %al, %ecx
xorl $0xff, %ecx
cvtsi2ss %ecx, %xmm0
leaq (%rsi,%rax,4), %rcx
addq $0x3, %rcx
xorl %edx, %edx
movss 0x258b(%rip), %xmm1     # 0x5204
xorpd %xmm2, %xmm2
movss 0x2583(%rip), %xmm3     # 0x5208
movsd 0x23a3(%rip), %xmm4     # 0x5030
movss 0x2577(%rip), %xmm5     # 0x520c
movsd 0x239b(%rip), %xmm6     # 0x5038
leal (%rax,%rdx), %esi
cmpl $0x100, %esi            # imm = 0x100
je 0x2d4b
cvtsi2ss %edx, %xmm7
divss %xmm0, %xmm7
mulss %xmm7, %xmm7
cvtss2sd %xmm7, %xmm8
movsd 0x2362(%rip), %xmm9     # 0x5028
subsd %xmm8, %xmm9
movaps %xmm7, %xmm8
mulss %xmm1, %xmm8
cvtss2sd %xmm8, %xmm8
movapd %xmm9, %xmm10
mulsd %xmm2, %xmm10
addsd %xmm8, %xmm10
cvttsd2si %xmm10, %esi
movb %sil, -0x3(%rcx,%rdx,4)
movaps %xmm7, %xmm8
mulss %xmm3, %xmm8
cvtss2sd %xmm8, %xmm8
movapd %xmm9, %xmm10
mulsd %xmm4, %xmm10
addsd %xmm8, %xmm10
cvttsd2si %xmm10, %esi
mulss %xmm5, %xmm7
cvtss2sd %xmm7, %xmm7
mulsd %xmm6, %xmm9
addsd %xmm7, %xmm9
cvttsd2si %xmm9, %edi
movb %sil, -0x2(%rcx,%rdx,4)
movb %dil, -0x1(%rcx,%rdx,4)
leal (%rax,%rdx), %esi
cmpl $0x1d, %esi
cmovbl %r8d, %esi
movb %sil, (%rcx,%rdx,4)
incq %rdx
jmp 0x2c9d
andl $0x0, 0x1d05a0e(%rip)   # 0x1d08760
xorl %r14d, %r14d
cmpq $0x100, %r14            # imm = 0x100
je 0x2e55
cvtsi2sd %r14d, %xmm0
divsd 0x22d1(%rip), %xmm0     # 0x5040
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
mulss 0x2492(%rip), %xmm1     # 0x5210
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm3
movsd 0x229a(%rip), %xmm2     # 0x5028
subsd %xmm3, %xmm2
movapd %xmm3, %xmm4
movsd %xmm3, -0x30(%rbp)
movapd %xmm2, %xmm3
mulsd 0x22a1(%rip), %xmm3     # 0x5048
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
leaq 0x1d059ba(%rip), %r15   # 0x1d08770
movb %al, (%r15,%r14,4)
movaps %xmm0, %xmm1
mulss 0x244f(%rip), %xmm1     # 0x5214
cvtss2sd %xmm1, %xmm1
movapd %xmm2, %xmm3
mulsd 0x227b(%rip), %xmm3     # 0x5050
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
mulss 0x2433(%rip), %xmm0     # 0x5218
cvtss2sd %xmm0, %xmm0
mulsd 0x2267(%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 0x2251(%rip), %xmm0     # 0x5060
callq 0x2470
addsd 0x224c(%rip), %xmm0     # 0x5068
mulsd 0x224c(%rip), %xmm0     # 0x5070
addsd -0x30(%rbp), %xmm0
mulsd 0x2247(%rip), %xmm0     # 0x5078
mulsd 0x2247(%rip), %xmm0     # 0x5080
cvttsd2si %xmm0, %eax
movb %al, 0x3(%r15,%r14,4)
cmpb $0x2, %al
ja 0x2e4d
movl %r14d, 0x1d05913(%rip)  # 0x1d08760
incq %r14
jmp 0x2d55
movl $0xf231, %edi           # imm = 0xF231
callq 0x2230
leaq 0x2420(%rip), %rdi      # 0x5286
leaq 0x243e(%rip), %rsi      # 0x52ab
callq 0x2420
callq 0x297f
movl 0x53eb(%rip), %edi      # 0x8268
leaq 0x5414(%rip), %rcx      # 0x8298
leaq 0x1d054cd(%rip), %r8    # 0x1d08358
movl %edi, %esi
xorl %edx, %edx
callq 0x2100
testl %eax, %eax
js 0x3414
movq 0x1d054b5(%rip), %rdi   # 0x1d08358
pushq $0x1
popq %r14
movl $0x16462004, %esi       # imm = 0x16462004
movl %r14d, %edx
movl $0x400, %ecx            # imm = 0x400
movl $0x400, %r8d            # imm = 0x400
callq 0x2460
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movl %r14d, %esi
callq 0x22a0
andq $0x0, 0x53ca(%rip)      # 0x82a0
movl 0x538c(%rip), %eax      # 0x8268
movl %eax, 0x53c6(%rip)      # 0x82a8
movl %eax, 0x53c4(%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 0x2f92
movl -0x88(%rbp), %eax
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jb 0x33d4
leal -0x603(%rax), %ecx
cmpl $0x2, %ecx
jb 0x33c7
cmpl $0x602, %eax            # imm = 0x602
je 0x33ed
cmpl $0x300, %eax            # imm = 0x300
je 0x3407
cmpl $0x301, %eax            # imm = 0x301
je 0x33fa
cmpl $0x400, %eax            # imm = 0x400
je 0x33da
cmpl $0x200, %eax            # imm = 0x200
jne 0x2f25
movq %r15, %rdi
callq 0x2a96
jmp 0x2f25
movq 0x1d053bf(%rip), %rdi   # 0x1d08358
xorl %r12d, %r12d
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2360
movq 0x1d053a7(%rip), %rdi   # 0x1d08358
callq 0x2250
leaq 0x1d0536b(%rip), %rdi   # 0x1d08328
callq 0x3704
movq -0x38(%rbp), %r13
cmpl 0x600000(%rbx), %r12d
jge 0x300a
movzwl -0x2(%r13), %edx
movl %edx, %eax
shll $0xa, %eax
movzwl -0x4(%r13), %esi
leaq (%rsi,%rax), %r15
movzbl 0x1600014(%rbx,%r15), %ecx
movzbl (%r13), %r8d
movq %rbx, %rdi
callq 0x3523
movb $0x0, 0x1600014(%rbx,%r15)
incl %r12d
addq $0x6, %r13
jmp 0x2fc6
andl $0x0, 0x600000(%rbx)
leaq 0x1d05310(%rip), %rdi   # 0x1d08328
callq 0x37d8
xorl %eax, %eax
leaq 0x1d0533a(%rip), %r15   # 0x1d08360
pushq $0x1c
popq %r12
movslq 0x120000c(%rbx), %rcx
cmpq %rcx, %rax
jge 0x304b
movslq 0xe0000c(%rbx,%rax,4), %rcx
andl $0x0, 0x1900014(%rbx,%rcx,4)
incq %rax
jmp 0x302a
andl $0x0, 0x120000c(%rbx)
xorl %eax, %eax
movslq 0xe00008(%rbx), %rcx
cmpq %rcx, %rax
jge 0x3088
movslq 0xa00008(%rbx,%rax,4), %rcx
movzbl 0x1800014(%rbx,%rcx), %edx
leaq 0x1d056f9(%rip), %rsi   # 0x1d08770
movl (%rsi,%rdx,4), %edx
bswapl %edx
movl %edx, 0x1900014(%rbx,%rcx,4)
incq %rax
jmp 0x3054
xorl %eax, %eax
movslq 0xa00004(%rbx), %rcx
cmpq %rcx, %rax
jge 0x32a5
movl 0x600004(%rbx,%rax,4), %esi
movslq %esi, %rcx
movzbl 0x1700014(%rbx,%rcx), %edx
movb 0x1800014(%rbx,%rcx), %r8b
cmpl $0x6, %edx
jb 0x3123
testb %r8b, %r8b
jne 0x30d7
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 0x3108
movl $0x87, %esi
subl %edx, %esi
cvtsi2ss %esi, %xmm0
divss 0x2128(%rip), %xmm0     # 0x521c
addss 0x2124(%rip), %xmm0     # 0x5220
mulss 0x2120(%rip), %xmm0     # 0x5224
cvttss2si %xmm0, %esi
movzbl 0x1800014(%rbx,%rcx), %r8d
cmpl %r8d, %esi
jle 0x3147
movb %sil, 0x1800014(%rbx,%rcx)
movl %esi, %r8d
jmp 0x3147
testb %r8b, %r8b
jne 0x3147
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 0x326b
testb %r8b, %r8b
je 0x326b
movzbl %r8b, %r10d
leaq 0x1d055f8(%rip), %r13   # 0x1d08770
movzbl 0x3(%r13,%r10,4), %r8d
cvtsi2sd %r8d, %xmm0
movsd 0x1eb5(%rip), %xmm2     # 0x5040
divsd %xmm2, %xmm0
cvtsi2sd %edx, %xmm1
divsd %xmm2, %xmm1
movsd 0x1e89(%rip), %xmm2     # 0x5028
subsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm0
movzbl (%r13,%r10,4), %r11d
cvtsi2ss %r9d, %xmm1
cvtsi2ss %r11d, %xmm2
movaps %xmm2, %xmm3
mulss %xmm0, %xmm3
addss %xmm1, %xmm3
addss %xmm1, %xmm3
addss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm1
movsd 0x1ea6(%rip), %xmm4     # 0x5080
mulsd %xmm4, %xmm1
cvttsd2si %xmm1, %r9d
movzbl 0x1(%r13,%r10,4), %r11d
cvtsi2ss %edi, %xmm1
cvtsi2ss %r11d, %xmm2
movaps %xmm2, %xmm3
mulss %xmm0, %xmm3
addss %xmm1, %xmm3
addss %xmm1, %xmm3
addss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm1
mulsd %xmm4, %xmm1
cvttsd2si %xmm1, %edi
cvtsi2ss %esi, %xmm1
movzbl 0x2(%r13,%r10,4), %esi
cvtsi2ss %esi, %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
addss %xmm1, %xmm0
addss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm4, %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 0x328c
testb %r8b, %r8b
movl %edx, %r8d
cmovel %r12d, %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
jmp 0x308a
andl $0x0, 0xa00004(%rbx)
andl $0x0, 0x1600010(%rbx)
xorl %r12d, %r12d
movslq 0xe00008(%rbx), %rax
cmpq %rax, %r12
jge 0x3354
movl 0xa00008(%rbx,%r12,4), %eax
movslq %eax, %r13
movzbl 0x1800014(%rbx,%r13), %r15d
callq 0x2661
cvtss2sd %xmm0, %xmm0
addsd 0x1d85(%rip), %xmm0     # 0x5070
mulsd 0x1d95(%rip), %xmm0     # 0x5088
addsd 0x1d2d(%rip), %xmm0     # 0x5028
mulsd 0x1d8d(%rip), %xmm0     # 0x5090
cvtsi2sd %r15d, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
cmpl %eax, 0x1d0544a(%rip)   # 0x1d08760
jle 0x332c
movb $0x0, 0x1800014(%rbx,%r13)
andl $0x0, 0x1900014(%rbx,%r13,4)
jmp 0x334c
movb %al, 0x1800014(%rbx,%r13)
movslq 0x1600010(%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, 0x1600010(%rbx)
movl %r13d, 0x1200010(%rbx,%rax,4)
incq %r12
jmp 0x32b6
movslq 0x1600010(%rbx), %rdx
shlq $0x2, %rdx
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
pushq $0x1e
popq %rdi
callq 0x2190
movq 0x1d04fba(%rip), %rdi   # 0x1d08358
movq %r15, %rsi
xorl %edx, %edx
leaq 0x4ef6(%rip), %rcx      # 0x82a0
callq 0x2270
movq 0x1d04fa2(%rip), %rdi   # 0x1d08358
callq 0x2070
leaq -0x88(%rbp), %r15
jmp 0x2f25
movq %r15, %rdi
callq 0x2937
jmp 0x2f25
movzbl -0x77(%rbp), %esi
jmp 0x33dd
movl -0x78(%rbp), %esi
movl -0x74(%rbp), %edx
movl -0x70(%rbp), %ecx
callq 0x2a44
jmp 0x2f25
movq %r15, %rdi
callq 0x28eb
jmp 0x2f25
movl -0x78(%rbp), %edi
callq 0x2839
jmp 0x2f25
movl -0x78(%rbp), %edi
callq 0x270a
jmp 0x2f25
callq 0x21a0
leaq 0x1e8d(%rip), %rdi      # 0x52ad
movq %rax, %rsi
xorl %eax, %eax
callq 0x25ac