// File: /workspace/github_cpp_makefile_star2_O3/dmarto[P]crrs-xlib/main.cpp
// Function: main
int main()
{
	const int W = 300;
	const int H = 300;

	//
	Display *display = XOpenDisplay(NULL);
	Window rWin = XDefaultRootWindow(display);

	Window win = XCreateSimpleWindow(display, rWin, 0, 0, W, H, 0, 0, 0);

	XStoreName(display, win, "Ray Tracer");

	unsigned long eventmask = PointerMotionMask | ButtonPressMask;
	XSelectInput(display, win, eventmask);

	XMapWindow(display, win);

	// TODO: render style
	GC render = XCreateGC(display, win, 0, NULL);
	XSync(display, False);

	// WORLD

	Colors color;

	Sphere sphere(Vec3(W * 0.5, H * 0.5, 40), 30, color.white);
	Sphere sphere1(Vec3(W * 0.3, H * 0.5, 20), 15, color.red);
	Sphere sphere2(Vec3(W * 0.25, H * 0.35, 30), 20, color.green);
	Sphere sphere3(Vec3(W * 0.85, H * 0.65, 40), 35, color.blue);

	Vec3 worldColor = Vec3(94, 0, 182);
	Sphere world(Vec3(W * 0.5, H * 3.8, 650), 1100, worldColor, "world");

	Sphere light(Vec3(W * 0.1, H * 0.5, 0), 40, color.white);

	std::vector<Sphere *> objList = {&world, &sphere, &sphere1, &sphere2, &sphere3};

	double theta = 0.0;

	std::vector<Vec3> background;
	for (int y = 0; y < H * 2; ++y)
		for (int x = 0; x < W * 2; ++x)
			background.emplace_back(Vec3(122, 11, 133) - (x + y) / 5);

	// WORLD LOOP

	XEvent event;
	while (1)
	{
		if (!XCheckMaskEvent(display, eventmask, &event))
			event.type = 0;

		HandleEvents(event, light, world);

		for (int y = 0; y < H; ++y)
			for (int x = 0; x < W; ++x)
			{
				ScreenData scrnData = {color, x, y, light, objList};
				Vec3 pixclr = RenderPixel(scrnData);
				ColorBoundary(pixclr);

				XSetForeground(display, render, _RGB((int)pixclr.x, (int)pixclr.y, (int)pixclr.z));
				XDrawPoint(display, win, render, x, y);
			}

		int startIdx = 1;
		objList[startIdx]->center.x = (W >> 1) + (W / 6) * sin(theta);
		objList[startIdx]->center.y = H / 2.8 + (H / 6) * cos(theta / 2);
		objList[startIdx + 1]->center.x = objList[startIdx + 1]->center.x + 2 * sin(theta);
		objList[startIdx + 1]->center.y = objList[startIdx + 1]->center.y + 2 * cos(theta);
		objList[startIdx + 2]->center.y = objList[startIdx + 2]->center.y + 2 * sin(theta);
		objList[startIdx + 3]->center.y = objList[startIdx + 3]->center.y + 2 * -sin(theta);
		// objList[startIdx + 3]->center.z = 50 + 10 * sin(theta);

		theta += 0.1;

		XFlush(display);
		XSync(display, False);
	}

	XCloseDisplay(display);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1140
movq %rax, %rbx
movq %rax, %rdi
callq 0x1150
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movl $0x0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x12c, %r8d            # imm = 0x12C
movl $0x12c, %r9d            # imm = 0x12C
callq 0x1050
movq %rax, %r15
leaq 0x16fc(%rip), %rdx      # 0x3216
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1030
movl $0x44, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1120
movq %rbx, %rdi
movq %r15, 0x68(%rsp)
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1090
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x11b0
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps 0x1585(%rip), %xmm0     # 0x3100
leaq 0x338(%rsp), %rax
movaps %xmm0, -0x48(%rax)
movaps 0x1582(%rip), %xmm0     # 0x3110
movaps %xmm0, -0x38(%rax)
movaps 0x1587(%rip), %xmm0     # 0x3120
movaps %xmm0, -0x28(%rax)
movabsq $0x406fe00000000000, %r12 # imm = 0x406FE00000000000
movq %r12, -0x18(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
movaps 0x1573(%rip), %xmm0     # 0x3130
leaq 0x2d8(%rsp), %rcx
movaps %xmm0, -0x48(%rcx)
movaps 0x1570(%rip), %xmm0     # 0x3140
movaps %xmm0, -0x38(%rcx)
movsd 0x142c(%rip), %xmm0     # 0x3008
movaps %xmm0, -0x28(%rcx)
movabsq $0x405b800000000000, %rax # imm = 0x405B800000000000
movq %rax, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq %r14, -0x8(%rcx)
movb %r14b, (%rcx)
movaps 0x1550(%rip), %xmm0     # 0x3150
leaq 0x278(%rsp), %rax
movaps %xmm0, -0x48(%rax)
movaps 0x154d(%rip), %xmm0     # 0x3160
movaps %xmm0, -0x38(%rax)
movapd 0x1551(%rip), %xmm0     # 0x3170
movapd %xmm0, -0x28(%rax)
movq %r14, -0x18(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
movaps 0x1546(%rip), %xmm1     # 0x3180
leaq 0x138(%rsp), %rax
movaps %xmm1, -0x48(%rax)
movapd 0x1542(%rip), %xmm1     # 0x3190
movapd %xmm1, -0x38(%rax)
movq %r14, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1596(%rip), %rsi      # 0x3210
leaq 0x1594(%rip), %rdx      # 0x3215
leaq 0x88(%rsp), %rdi
callq 0x23e2
movaps 0x150b(%rip), %xmm0     # 0x31a0
leaq 0x208(%rsp), %rdi
movaps %xmm0, -0x38(%rdi)
movaps 0x1508(%rip), %xmm0     # 0x31b0
movaps %xmm0, -0x28(%rdi)
movsd 0x13bc(%rip), %xmm0     # 0x3070
movapd %xmm0, -0x18(%rdi)
movabsq $0x4066c00000000000, %rax # imm = 0x4066C00000000000
movq %rax, -0x8(%rdi)
leaq 0x218(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
addq %rsi, %rdx
callq 0x234c
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x1d08
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x1110
movaps 0x14b1(%rip), %xmm0     # 0x31c0
leaq 0x1b8(%rsp), %rax
movaps %xmm0, -0x48(%rax)
xorps %xmm0, %xmm0
movhps 0x12eb(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, -0x38(%rax)
movapd 0x13ef(%rip), %xmm0     # 0x3120
movapd %xmm0, -0x28(%rax)
movq %r12, -0x18(%rax)
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1d0(%rsp), %rax
leaq 0x350(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x2f0(%rsp), %rax
movq %rax, 0x8(%rsi)
leaq 0x290(%rsp), %rax
movq %rax, 0x10(%rsi)
leaq 0x230(%rsp), %rax
movq %rax, 0x18(%rsi)
leaq 0xf0(%rsp), %rax
movq %rax, 0x20(%rsi)
leaq 0xa8(%rsp), %rdi
leaq 0xc0(%rsp), %rcx
movl $0x5, %edx
callq 0x2306
movq %rbx, 0x28(%rsp)
leaq 0x50(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
xorl %r13d, %r13d
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
leaq 0x350(%rsp), %r12
xorl %ebx, %ebx
leal (%rbx,%r13), %eax
imulq %rbp, %rax
shrq $0x22, %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x128a(%rip), %xmm1     # 0x3078
subsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x13d2(%rip), %xmm2     # 0x31d0
subpd %xmm0, %xmm2
movapd %xmm2, 0x350(%rsp)
movsd %xmm1, 0x360(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x1e41
movq 0x360(%rsp), %rax
movq %rax, 0x10(%rsi)
movapd 0x350(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x58(%rsp)
jmp 0x1e4c
movq %r14, %rdi
movq %r12, %rdx
callq 0x2512
incl %ebx
cmpl $0x258, %ebx            # imm = 0x258
jne 0x1dd3
incl %r13d
cmpl $0x258, %r13d           # imm = 0x258
jne 0x1dd1
leaq 0x158(%rsp), %r12
leaq 0x350(%rsp), %r14
leaq 0xa8(%rsp), %r13
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %rbx
movsd %xmm0, 0x20(%rsp)
movl $0x44, %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1130
testl %eax, %eax
jne 0x1eb0
movl $0x0, 0x350(%rsp)
movq %r14, %rdi
leaq 0x170(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x1a05
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x70(%rsp)
xorl %ebp, %ebp
movq 0x10(%r12), %rax
movq %rax, 0x40(%rsp)
movups (%r12), %xmm0
movaps %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movabsq $-0x3ff8000000000000, %rax # imm = 0xC008000000000000
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xe8(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x170(%rsp), %rdx
movq %r13, %rcx
callq 0x17a4
movsd 0x30(%rsp), %xmm0
movl $0xff0000, %eax         # imm = 0xFF0000
movsd 0x1096(%rip), %xmm1     # 0x3008
ucomisd %xmm0, %xmm1
jb 0x1f8b
xorl %eax, %eax
ucomisd 0x1106(%rip), %xmm0     # 0x3088
jb 0x1f8b
cvttsd2si %xmm0, %eax
shll $0x10, %eax
movsd 0x38(%rsp), %xmm0
movl $0xff00, %ecx           # imm = 0xFF00
movsd 0x106a(%rip), %xmm1     # 0x3008
ucomisd %xmm0, %xmm1
jb 0x1fb7
xorl %ecx, %ecx
ucomisd 0x10da(%rip), %xmm0     # 0x3088
jb 0x1fb7
cvttsd2si %xmm0, %ecx
shll $0x8, %ecx
movsd 0x40(%rsp), %xmm0
movl $0xff, %edx
movsd 0x103e(%rip), %xmm1     # 0x3008
ucomisd %xmm0, %xmm1
jb 0x1fe0
xorl %edx, %edx
ucomisd 0x10ae(%rip), %xmm0     # 0x3088
jb 0x1fe0
cvttsd2si %xmm0, %edx
addl %eax, %ecx
addl %edx, %ecx
movslq %ecx, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1060
movq %rbx, %rdi
movq 0x68(%rsp), %rsi
movq %r15, %rdx
movl %ebp, %ecx
movl %r14d, %r8d
callq 0x10e0
incl %ebp
cmpl $0x12c, %ebp            # imm = 0x12C
jne 0x1edb
incl %r14d
cmpl $0x12c, %r14d           # imm = 0x12C
jne 0x1ecb
movsd 0x20(%rsp), %xmm0
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x10a0
movq 0xa8(%rsp), %rbx
movq 0x8(%rbx), %r14
movsd 0x20(%rsp), %xmm0
mulsd 0xfe9(%rip), %xmm0      # 0x3040
callq 0x11a0
movsd 0x80(%rsp), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd 0x116b(%rip), %xmm2     # 0x31e0
addpd 0x1173(%rip), %xmm2     # 0x31f0
movupd %xmm2, (%r14)
movq 0x10(%rbx), %rax
movhpd 0x78(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movupd (%rax), %xmm0
addpd %xmm1, %xmm1
addpd %xmm1, %xmm0
movupd %xmm0, (%rax)
movq 0x18(%rbx), %rax
movsd 0x8(%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x20(%rbx), %rax
movq 0x28(%rsp), %rbx
movsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq %rbx, %rdi
callq 0x1160
movsd 0x20(%rsp), %xmm0
leaq 0x350(%rsp), %r14
addsd 0xf9d(%rip), %xmm0      # 0x3080
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x11b0
movsd 0x20(%rsp), %xmm0
jmp 0x1e8b
jmp 0x2104
jmp 0x2104
jmp 0x2104
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x211e
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1110
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x2140
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x1110
jmp 0x2140
movq %rax, %rbx
leaq 0x1b8(%rsp), %rax
movq 0x1a8(%rsp), %rdi
cmpq %rax, %rdi
je 0x2165
movq 0x1b8(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0x21a9
movq 0x218(%rsp), %rsi
jmp 0x219c
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x21a9
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x1110
jmp 0x21a9
movq %rax, %rbx
leaq 0x138(%rsp), %rax
movq 0x128(%rsp), %rdi
cmpq %rax, %rdi
je 0x21ce
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x268(%rsp), %rdi
leaq 0x278(%rsp), %rax
cmpq %rax, %rdi
je 0x21f3
movq 0x278(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x2c8(%rsp), %rdi
leaq 0x2d8(%rsp), %rax
cmpq %rax, %rdi
je 0x2218
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x328(%rsp), %rdi
leaq 0x338(%rsp), %rax
cmpq %rax, %rdi
je 0x223d
movq 0x338(%rsp), %rsi
incq %rsi
callq 0x1110
movq %rbx, %rdi
callq 0x1180
nop