// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
subq $0x30, %rsp
pushq $0x31
popq %rdi
callq 0x6330
leaq 0x18(%rsp), %rsi
xorl %edi, %edi
callq 0x6530
movl 0x10a8e(%rip), %ecx     # 0x1b458
movl %ecx, 0x10a90(%rip)     # 0x1b460
movl 0x10a86(%rip), %esi     # 0x1b45c
movl %esi, 0x10a88(%rip)     # 0x1b464
movl 0x10a86(%rip), %eax     # 0x1b468
cmpl $0x1, %eax
je 0xaa14
cmpl $0x2, %eax
jne 0xaa43
movl $0x2, 0x10a76(%rip)     # 0x1b46c
orb $-0x80, 0x10cd6(%rip)   # 0x1b6d3
cmpl $-0x1, %ecx
je 0xaabb
cmpl $-0x1, %esi
jne 0xab83
jmp 0xaaf0
movl $0x1, 0x10a4e(%rip)     # 0x1b46c
andb $0x7f, 0x10cae(%rip)    # 0x1b6d3
cmpl $-0x1, %ecx
je 0xaafd
cmpl $-0x1, %esi
jne 0xab83
leal 0x1(,%rcx,4), %eax
jmp 0xab48
cmpl $-0x1, %ecx
jne 0xaa5f
movl 0x1c(%rsp), %eax
addl $-0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, %ecx
movl %eax, 0x10a01(%rip)     # 0x1b460
cmpl $-0x1, %esi
jne 0xaa7b
movl 0x20(%rsp), %eax
addl $-0x40, %eax
movl $0x90, %esi
cltd
idivl %esi
movl %eax, %esi
movl %eax, 0x109e9(%rip)     # 0x1b464
movl %ecx, %eax
imull %ecx, %eax
shll $0x2, %eax
movl %esi, %edx
imull %edx, %edx
leal (%rdx,%rdx,2), %edx
cmpl %edx, %eax
jbe 0xaaa5
movl $0x2, 0x109d3(%rip)     # 0x1b46c
orb $-0x80, 0x10c33(%rip)   # 0x1b6d3
jmp 0xab83
movl $0x1, 0x109bd(%rip)     # 0x1b46c
andb $0x7f, 0x10c1d(%rip)    # 0x1b6d3
jmp 0xab83
cmpl $-0x1, %esi
jne 0xab60
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %esi
addl $-0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, %ecx
addl $-0x40, %esi
movl $0x90, %edi
movl %esi, %eax
cltd
idivl %edi
cmpl %eax, %ecx
jge 0xab58
movl %ecx, 0x10970(%rip)     # 0x1b460
movl %ecx, 0x1096e(%rip)     # 0x1b464
movl %ecx, %esi
jmp 0xab83
cmpl $-0x1, %esi
je 0xab0d
leal (%rsi,%rsi,2), %eax
addl $0x2, %eax
pushq $0x4
popq %rcx
jmp 0xab78
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %esi
addl $-0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, %ecx
addl $-0x40, %esi
movl $0x90, %edi
movl %esi, %eax
cltd
idivl %edi
movl %eax, %esi
leal (,%rcx,4), %eax
leal (%rsi,%rsi,2), %edx
cmpl %edx, %eax
jge 0xab6a
movl %ecx, 0x1091b(%rip)     # 0x1b460
orl $0x1, %eax
pushq $0x3
popq %rsi
cltd
idivl %esi
movl %eax, %esi
movl %eax, 0x1090e(%rip)     # 0x1b464
jmp 0xab83
movl %eax, %esi
movl %eax, 0x10904(%rip)     # 0x1b464
movl %esi, 0x108fa(%rip)     # 0x1b460
movl %esi, %ecx
jmp 0xab83
movl %esi, 0x108f4(%rip)     # 0x1b464
addl $0x2, %edx
pushq $0x4
popq %rcx
movl %edx, %eax
cltd
idivl %ecx
movl %eax, %ecx
movl %eax, 0x108dd(%rip)     # 0x1b460
testl %ecx, %ecx
jg 0xab94
movl $0x1, 0x108cf(%rip)     # 0x1b460
pushq $0x1
popq %rcx
testl %esi, %esi
jg 0xaba5
pushq $0x1
popq %rsi
movl $0x1, 0x108bf(%rip)     # 0x1b464
shll $0x8, %ecx
imull $0x90, %esi, %r8d
leaq 0x9de7(%rip), %rdi      # 0x1499d
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
xorl %r9d, %r9d
callq 0x6050
movq %rax, 0x814729(%rip)    # 0x81f2f8
testq %rax, %rax
je 0xac76
movq %rax, %rdi
callq 0x6470
movl 0x1087a(%rip), %ebx     # 0x1b460
shll $0x8, %ebx
imull $0x90, 0x10871(%rip), %ebp # 0x1b464
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 0x6520
xorl %edi, %edi
movl %ebx, %esi
movl %ebp, %edx
movl %r14d, %ecx
movl %eax, %r8d
callq 0x60a0
movq %rax, 0x8146df(%rip)    # 0x81f308
testq %rax, %rax
je 0xac84
movl 0x1082c(%rip), %esi     # 0x1b460
shll $0x3, %esi
movl 0x10827(%rip), %edx     # 0x1b464
shll $0x3, %edx
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
pushq $0x8
popq %rcx
xorl %ebx, %ebx
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x64c0
movq %rax, 0x81469e(%rip)    # 0x81f300
testq %rax, %rax
je 0xac92
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x6f17
jmp 0xacab
callq 0x62d0
leaq 0x9d2d(%rip), %rdi      # 0x149af
jmp 0xac9e
callq 0x62d0
leaq 0x9d3d(%rip), %rdi      # 0x149cd
jmp 0xac9e
callq 0x62d0
leaq 0x9d51(%rip), %rdi      # 0x149ef
movq %rax, %rsi
xorl %eax, %eax
callq 0x6480
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %rbp
retq