// File: /workspace/github_cpp_makefile_star2_O3/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 0x104d3(%rip), %ecx     # 0x1c448
movl %ecx, 0x104d5(%rip)     # 0x1c450
movl 0x104cb(%rip), %eax     # 0x1c44c
movl %eax, 0x104cd(%rip)     # 0x1c454
movl 0x104cb(%rip), %edx     # 0x1c458
cmpl $0x1, %edx
je 0xbfbf
cmpl $0x2, %edx
jne 0xbff0
movl $0x2, 0x104bb(%rip)     # 0x1c45c
orb $-0x80, 0x1071b(%rip)   # 0x1c6c3
cmpl $-0x1, %ecx
je 0xc078
cmpl $-0x1, %eax
jne 0xc169
jmp 0xc0c1
movl $0x1, 0x10493(%rip)     # 0x1c45c
andb $0x7f, 0x106f3(%rip)    # 0x1c6c3
cmpl $-0x1, %ecx
je 0xc0ce
cmpl $-0x1, %eax
jne 0xc169
leal 0x1(,%rcx,4), %eax
cltq
jmp 0xc13f
cmpl $-0x1, %ecx
jne 0xc010
movl 0x4(%rsp), %ecx
leal -0x40(%rcx), %edx
addl $0xbf, %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
sarl $0x8, %ecx
movl %ecx, 0x10440(%rip)     # 0x1c450
cmpl $-0x1, %eax
jne 0xc038
movl 0x8(%rsp), %eax
addl $-0x40, %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rax
addl %edx, %eax
movl %eax, 0x1041c(%rip)     # 0x1c454
movl %ecx, %edx
imull %ecx, %edx
shll $0x2, %edx
movl %eax, %esi
imull %esi, %esi
leal (%rsi,%rsi,2), %esi
cmpl %esi, %edx
jbe 0xc062
movl $0x2, 0x10406(%rip)     # 0x1c45c
orb $-0x80, 0x10666(%rip)   # 0x1c6c3
jmp 0xc169
movl $0x1, 0x103f0(%rip)     # 0x1c45c
andb $0x7f, 0x10650(%rip)    # 0x1c6c3
jmp 0xc169
cmpl $-0x1, %eax
jne 0xc161
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %eax
leal -0x40(%rcx), %edx
addl $0xbf, %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
sarl $0x8, %ecx
addl $-0x40, %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rax
addl %edx, %eax
cmpl %eax, %ecx
jge 0xc15b
movl %ecx, 0x1038f(%rip)     # 0x1c450
movl %ecx, 0x1038d(%rip)     # 0x1c454
movl %ecx, %eax
jmp 0xc169
cmpl $-0x1, %eax
je 0xc0ef
leal (%rax,%rax,2), %edx
addl $0x2, %edx
leal (%rax,%rax,2), %ecx
addl $0x5, %ecx
testl %edx, %edx
cmovnsl %edx, %ecx
sarl $0x2, %ecx
movl %ecx, 0x10363(%rip)     # 0x1c450
jmp 0xc169
movl 0x4(%rsp), %ecx
leal -0x40(%rcx), %eax
addl $0xbf, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movl 0x8(%rsp), %eax
sarl $0x8, %ecx
addl $-0x40, %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rax
addl %edx, %eax
leal (,%rcx,4), %esi
leal (%rax,%rax,2), %edx
cmpl %edx, %esi
jge 0xc2a8
movl %ecx, 0x10317(%rip)     # 0x1c450
orl $0x1, %esi
movslq %esi, %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rax
addl %edx, %eax
movl %eax, 0x102fb(%rip)     # 0x1c454
jmp 0xc169
movl %eax, 0x102f3(%rip)     # 0x1c454
movl %eax, 0x102e9(%rip)     # 0x1c450
movl %eax, %ecx
testl %ecx, %ecx
jg 0xc17c
movl $0x1, 0x102d9(%rip)     # 0x1c450
movl $0x1, %ecx
testl %eax, %eax
jg 0xc18f
movl $0x1, 0x102ca(%rip)     # 0x1c454
movl $0x1, %eax
shll $0x8, %ecx
shll $0x4, %eax
leal (%rax,%rax,8), %r8d
leaq 0xa8cd(%rip), %rdi      # 0x16a6d
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
xorl %r9d, %r9d
callq 0x6050
movq %rax, 0x81412f(%rip)    # 0x8202e8
testq %rax, %rax
je 0xc266
movq %rax, %rdi
callq 0x6460
movl 0x10280(%rip), %ebx     # 0x1c450
shll $0x8, %ebx
movl 0x1027b(%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, 0x8140e0(%rip)    # 0x8202f8
testq %rax, %rax
je 0xc274
movl 0x1022d(%rip), %esi     # 0x1c450
shll $0x3, %esi
movl 0x10228(%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, 0x8140a0(%rip)    # 0x8202f0
testq %rax, %rax
je 0xc282
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x731e
xorl %eax, %eax
jmp 0xc29d
callq 0x62b0
leaq 0xa80d(%rip), %rdi      # 0x16a7f
jmp 0xc28e
callq 0x62b0
leaq 0xa81d(%rip), %rdi      # 0x16a9d
jmp 0xc28e
callq 0x62b0
leaq 0xa831(%rip), %rdi      # 0x16abf
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
leal 0x2(%rdx), %ecx
addl $0x5, %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
movl %eax, 0x1019b(%rip)     # 0x1c454
sarl $0x2, %edx
movl %edx, 0x1018e(%rip)     # 0x1c450
movl %edx, %ecx
jmp 0xc169