// File: /workspace/github_cpp_makefile_star2_O1/mdpkh[P]retro6k/src/main.cpp
// Function: InitMainWindow()
int InitMainWindow()
{
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
	SDL_DisplayMode desktopmode;
	SDL_GetDesktopDisplayMode(0, &desktopmode);
	config.screen.pixwidth = config.screen.specpixwidth;
	config.screen.pixheight = config.screen.specpixheight;
	switch (config.screen.specaspectratio)
	{
	case aspectratiocat::AR_WIDE:
		config.screen.reportedaspectratio = aspectratiocat::AR_WIDE;
		videomode |= 0x80;
		if (config.screen.pixwidth == -1)
		{
			if (config.screen.pixheight == -1)
			{
				int autowidth = (desktopmode.w - 64) / 256;
				int autoheight = (desktopmode.h - 64) / 144;
				if (autowidth >= autoheight)
				{
					config.screen.pixheight = autoheight;
					config.screen.pixwidth = autoheight;
				}
				else
				{
					config.screen.pixwidth = autowidth;
					config.screen.pixheight = autowidth;
				}
			}
			else
			{
				config.screen.pixwidth = config.screen.pixheight;
			}
		}
		else
		{
			if (config.screen.pixheight == -1)
			{
				config.screen.pixheight = config.screen.pixwidth;
			}
		}
		break;
	case aspectratiocat::AR_CLASSIC:
		config.screen.reportedaspectratio = aspectratiocat::AR_CLASSIC;
		videomode &= 0x7f;
		if (config.screen.pixwidth == -1)
		{
			if (config.screen.pixheight == -1)
			{
				int autowidth = (desktopmode.w - 64) / 256;
				int autoheight = (desktopmode.h - 64) / 144;
				if (autowidth * 4 >= autoheight * 3)
				{
					config.screen.pixheight = autoheight;
					config.screen.pixwidth = (autoheight * 3 + 2) / 4;
				}
				else
				{
					config.screen.pixwidth = autowidth;
					config.screen.pixheight = (autowidth * 4 + 1) / 3;
				}
			}
			else
			{
				config.screen.pixwidth = (config.screen.pixheight * 3 + 2) / 4;
			}
		}
		else
		{
			if (config.screen.pixheight == -1)
			{
				config.screen.pixheight = (config.screen.pixwidth * 4 + 1) / 3;
			}
		}
		break;
	case aspectratiocat::AR_FREE:
	default:
		if (config.screen.pixwidth == -1)
			config.screen.pixwidth = (desktopmode.w - 64) / 256;
		if (config.screen.pixheight == -1)
			config.screen.pixheight = (desktopmode.h - 64) / 144;
		if (config.screen.pixwidth * config.screen.pixwidth * 4
			> config.screen.pixheight* config.screen.pixheight * 3)
		{
			config.screen.reportedaspectratio = aspectratiocat::AR_WIDE;
			videomode |= 0x80;
		}
		else
		{
			config.screen.reportedaspectratio = aspectratiocat::AR_CLASSIC;
			videomode &= 0x7f;
		}
		break;
	}
	if (config.screen.pixwidth < 1)
		config.screen.pixwidth = 1;
	if (config.screen.pixheight < 1)
		config.screen.pixheight = 1;
	mainwindow = SDL_CreateWindow("Retro 6k Emulator",
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		config.screen.pixwidth * 256,
		config.screen.pixheight * 144,
		0);
	if (!mainwindow)
	{
		SDL_Log("Could not create a window: %s", SDL_GetError());
		return -1;
	}
	SDL_Surface* mwsurface = SDL_GetWindowSurface(mainwindow);
	framebuffer = SDL_CreateRGBSurfaceWithFormat(
		0,
		config.screen.pixwidth * 256,
		config.screen.pixheight * 144,
		mwsurface->format->BitsPerPixel,
		SDL_MasksToPixelFormatEnum(
			mwsurface->format->BitsPerPixel,
			mwsurface->format->Rmask,
			mwsurface->format->Gmask,
			mwsurface->format->Bmask,
			mwsurface->format->Amask));
	if (!framebuffer)
	{
		SDL_Log("Could not create frame buffer: %s", SDL_GetError());
		return -1;
	}
	cellcanvas = SDL_CreateRGBSurface(0, config.screen.pixwidth * 8, config.screen.pixheight * 8, 8, 0, 0, 0, 0);
	if (!cellcanvas)
	{
		SDL_Log("Could not create cell canvas: %s", SDL_GetError());
		return -1;
	}
	SetHWPalette(cellcanvas->format->palette);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl $0x31, %edi
callq 0x6310
movq %rsp, %rsi
xorl %edi, %edi
callq 0x6500
movl 0x10b2e(%rip), %ecx     # 0x1c448
movl %ecx, 0x10b30(%rip)     # 0x1c450
movl 0x10b26(%rip), %eax     # 0x1c44c
movl %eax, 0x10b28(%rip)     # 0x1c454
movl 0x10b26(%rip), %edx     # 0x1c458
cmpl $0x1, %edx
je 0xb96a
cmpl $0x2, %edx
jne 0xb99b
movl $0x2, 0x10b16(%rip)     # 0x1c45c
orb $-0x80, 0x10d76(%rip)   # 0x1c6c3
cmpl $-0x1, %ecx
je 0xba2b
cmpl $-0x1, %eax
jne 0xbb0b
movl %ecx, 0x10aef(%rip)     # 0x1c454
jmp 0xbb0b
movl $0x1, 0x10ae8(%rip)     # 0x1c45c
andb $0x7f, 0x10d48(%rip)    # 0x1c6c3
cmpl $-0x1, %ecx
je 0xba3b
cmpl $-0x1, %eax
jne 0xbb0b
leal 0x1(,%rcx,4), %eax
cltq
jmp 0xbaf1
cmpl $-0x1, %ecx
jne 0xb9bb
movl 0x4(%rsp), %ecx
leal -0x40(%rcx), %edx
addl $0xbf, %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
sarl $0x8, %ecx
movl %ecx, 0x10a95(%rip)     # 0x1c450
cmpl $-0x1, %eax
jne 0xb9e3
movl 0x8(%rsp), %eax
addl $-0x40, %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
movl %eax, 0x10a71(%rip)     # 0x1c454
movl 0x10a67(%rip), %eax     # 0x1c450
imull %eax, %eax
shll $0x2, %eax
movl 0x10a5f(%rip), %ecx     # 0x1c454
imull %ecx, %ecx
leal (%rcx,%rcx,2), %ecx
cmpl %ecx, %eax
jbe 0xba15
movl $0x2, 0x10a53(%rip)     # 0x1c45c
orb $-0x80, 0x10cb3(%rip)   # 0x1c6c3
jmp 0xbb0b
movl $0x1, 0x10a3d(%rip)     # 0x1c45c
andb $0x7f, 0x10c9d(%rip)    # 0x1c6c3
jmp 0xbb0b
cmpl $-0x1, %eax
je 0xba5f
movl %eax, 0x10a1a(%rip)     # 0x1c450
jmp 0xbb0b
cmpl $-0x1, %eax
je 0xbaa1
leal (%rax,%rax,2), %ecx
addl $0x2, %ecx
leal (%rax,%rax,2), %eax
addl $0x5, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x2, %eax
movl %eax, 0x109f6(%rip)     # 0x1c450
jmp 0xbb0b
movl 0x4(%rsp), %eax
movl 0x8(%rsp), %ecx
leal -0x40(%rax), %edx
addl $0xbf, %eax
testl %edx, %edx
cmovnsl %edx, %eax
sarl $0x8, %eax
addl $-0x40, %ecx
movslq %ecx, %rcx
imulq $0x38e38e39, %rcx, %rcx # imm = 0x38E38E39
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
cmpl %ecx, %eax
jge 0xbc56
movl %eax, 0x109b1(%rip)     # 0x1c450
jmp 0xbb05
movl 0x4(%rsp), %eax
leal -0x40(%rax), %ecx
addl $0xbf, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl 0x8(%rsp), %ecx
sarl $0x8, %eax
addl $-0x40, %ecx
movslq %ecx, %rcx
imulq $0x38e38e39, %rcx, %rcx # imm = 0x38E38E39
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
leal (,%rax,4), %esi
leal (%rcx,%rcx,2), %edx
cmpl %edx, %esi
jge 0xbc67
movl %eax, 0x10965(%rip)     # 0x1c450
orl $0x1, %esi
movslq %esi, %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movl %eax, 0x10949(%rip)     # 0x1c454
cmpl $0x0, 0x1093e(%rip)     # 0x1c450
jg 0xbb1e
movl $0x1, 0x10932(%rip)     # 0x1c450
cmpl $0x0, 0x1092f(%rip)     # 0x1c454
jg 0xbb31
movl $0x1, 0x10923(%rip)     # 0x1c454
movl 0x10919(%rip), %ecx     # 0x1c450
shll $0x8, %ecx
movl 0x10914(%rip), %eax     # 0x1c454
shll $0x4, %eax
leal (%rax,%rax,8), %r8d
leaq 0xae2f(%rip), %rdi      # 0x1697d
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
xorl %r9d, %r9d
callq 0x6050
movq %rax, 0x814781(%rip)    # 0x8202e8
testq %rax, %rax
je 0xbc14
movq %rax, %rdi
callq 0x6460
movl 0x108d2(%rip), %ebx     # 0x1c450
shll $0x8, %ebx
movl 0x108cd(%rip), %ecx     # 0x1c454
shll $0x4, %ecx
leal (%rcx,%rcx,8), %ebp
movq 0x8(%rax), %rax
movzbl 0x10(%rax), %r14d
movl 0x14(%rax), %esi
movl 0x18(%rax), %edx
movl 0x1c(%rax), %ecx
movl 0x20(%rax), %r8d
movl %r14d, %edi
callq 0x64f0
xorl %r15d, %r15d
xorl %edi, %edi
movl %ebx, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x6090
movq %rax, 0x814732(%rip)    # 0x8202f8
testq %rax, %rax
je 0xbc22
movl 0x1087f(%rip), %esi     # 0x1c450
shll $0x3, %esi
movl 0x1087a(%rip), %edx     # 0x1c454
shll $0x3, %edx
xorl %edi, %edi
movl $0x8, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r15
pushq %r15
callq 0x6490
addq $0x10, %rsp
movq %rax, 0x8146f2(%rip)    # 0x8202f0
testq %rax, %rax
je 0xbc30
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x732e
xorl %eax, %eax
jmp 0xbc4b
callq 0x62b0
leaq 0xad6f(%rip), %rdi      # 0x1698f
jmp 0xbc3c
callq 0x62b0
leaq 0xad7f(%rip), %rdi      # 0x169ad
jmp 0xbc3c
callq 0x62b0
leaq 0xad93(%rip), %rdi      # 0x169cf
movq %rax, %rsi
xorl %eax, %eax
callq 0x6470
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, 0x107f8(%rip)     # 0x1c454
movl %ecx, 0x107ee(%rip)     # 0x1c450
jmp 0xbb0b
movl %ecx, 0x107e7(%rip)     # 0x1c454
leal 0x2(%rdx), %eax
addl $0x5, %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
movl %edx, 0x107cf(%rip)     # 0x1c450
jmp 0xbb0b