// File: /workspace/github_cpp_makefile_star2_O2/corecodec[P]coremake/coremake.c
// Function: main
int main(int argc, char** argv)
{
	int n;
	int dumppost = 0;
	int dump = 0;
	item* i;
	char path[MAX_PATH];
	char root_forced = 0;

	item* root = item_get(NULL,"ROOT",0);
	getconfig(root);

    getcwd(proj_root,sizeof(proj_root));
    pathunix(proj_root);
    addendpath(proj_root);
    proj_root_len = strlen(proj_root);

	for (n=1;n<argc;++n)
	{
		if (argv[n][0]=='-')
		{
			switch (argv[n][1])
			{
			case 'v':
				verbose = 1;
				break;
			case 'h':
				usage();
				return 0;
			case 'd':
				dump = 1;
				break;
			case 'p':
				dumppost = 1;
				break;
			case 'f':
				strcpy(proj,argv[++n]);
				root_forced = 1;
				break;
            case 'r':
                strcpy(src_root,argv[++n]);
                pathunix(src_root);
                if (!ispathabs(src_root))
                {
                    strins(src_root,proj_root,NULL);
                    simplifypath(src_root,0);
                }
                addendpath(src_root);
                src_root_len = strlen(src_root);
                break;
			}
		}
		else
		if (platform[0]==0)
			strcpy(platform,argv[n]);
	}

	strcpy(path,src_root);
    addendpath(path);
	strcat(path,proj);

    if (!src_root_len)
    {
        strcpy(src_root, proj_root);
        src_root_len = proj_root_len;
    }

    if (!load_file(root,path,FLAG_PATH_SOURCE,NULL) && !root_forced)
	{
        // try <directory>.proj
		strcpy(proj,src_root);
        delendpath(proj);
        truncfilename(proj);
		strcat(proj,".proj");
        strins(proj,src_root,NULL);
		if (!load_file(root,proj,FLAG_PATH_SOURCE,NULL))
        {
            // search for *.proj
			DIR* dir = opendir(src_root);
			if (dir)
			{
                int found = 0;
				struct dirent* entry;
				while ((entry = readdir(dir)) != NULL)
					if (entry->d_name[0]!='.' && strrchr(entry->d_name,'.') &&
                        stricmp(strrchr(entry->d_name,'.'),".proj")==0 && load_file(root,entry->d_name,FLAG_PATH_SOURCE,NULL))
                    {
                        found = 1;
                        strcpy(proj,entry->d_name);
                        pathunix(proj);
                        truncfilename(proj);
                        break;
                    }

				closedir(dir);
                if (!found)
                {
                    printf("No root .proj file found !");
                    exit(1);
                }
			}
        }
	}

    if (!getvalue(item_find(root,"workspace")))
    {
        // add default workspace
        item* w = item_get(root,"workspace",0);
        item* i = NULL;
        i=default_workspace(w,i,item_find(root,"exe"));
        i=default_workspace(w,i,item_find(root,"dll"));
        i=default_workspace(w,i,item_find(root,"con"));
        i=default_workspace(w,i,item_find(root,"exe_android"));
    }

	preprocess_project(item_find(root,"project"));

	if (dump)
		dumpitem(root,1);

	if (!platform[0])
	{
		if (!dump)
			usage();
		return 1;
	}

	item_get(item_get(root,"platformname",1),platform,1);

    i = item_get(root,"rootpath",1);
	i = item_get(i,src_root,1);
    set_path_type(i,FLAG_PATH_SOURCE);

    i = item_get(root,"builddir",1);
	i = item_get(i,proj_root,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	sprintf(path,"%srelease/%s/",proj_root,platform);
    i = item_get(root,"outputpath",1);
	i = item_get(i,path,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	sprintf(path,"%sbuild/%s/",proj_root,platform);
    i = item_get(root,"buildpath",1);
	i = item_get(i,path,1);
    set_path_type(i,FLAG_PATH_GENERATED);

	i = getvalue(getroot(root,"platform_files"));
	if (i)
	{
		if (ispathabs(i->value) || !ispathabs(src_root))
			strcpy(coremake_root,i->value);
		else
		{
			strcpy(coremake_root,src_root);
			strcat(coremake_root,i->value);
		}
		simplifypath(coremake_root,1);
	}
	else
    {
#ifdef _WIN32
        HMODULE this1 = GetModuleHandleA("coremake.exe");
        if (this1)
        {
            GetModuleFileNameA(this1,coremake_root,MAX_PATH);
            FreeLibrary(this1);
            pathunix(coremake_root);
            getfilename(coremake_root)[0] = 0;
            strcat(coremake_root,"coremake");
        }
#else
        /// \todo this shouldn't work on UNIX platforms
        ///       since the platform files should be in a dir like /usr/share
        ///       and the argv most likely doesn't contain the path to the exe
        ///       We probably need a --prefix during compilation to hardcode that /usr/share path
        strcpy(coremake_root,"/usr/local/share/coremake");
#endif
        i = item_get(root,"platform_files",1);
        i = item_get(i,coremake_root,1);
        set_path_type(i,FLAG_PATH_COREMAKE);
        strcpy(coremake_root,i->value);
    }
	addendpath(coremake_root);
    coremake_root_len = strlen(coremake_root);

    strcpy(path,coremake_root);
	strcat(path,platform);
	strcat(path,".build");

	strcpy(buildpath[0],proj_root); //safety
    buildflags[0] = FLAG_PATH_GENERATED;
	build = NULL;
	build_file(root,path,FLAG_PATH_COREMAKE);
	if (build)
	{
		fclose(build);
		file_finalize(buildpath[curr_build]);
	}

	if (dumppost)
		dumpitem(root,1);

	item_delete(root);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rsi, %r14
movl %edi, %ebp
leaq 0x2b38(%rip), %rsi      # 0x11458
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
xorl %edx, %edx
callq 0x26a2
movq %rax, %rbx
movq %rax, %rdi
callq 0x2702
leaq 0x80bc(%rip), %r15      # 0x16a00
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdi
callq 0x2180
movq %r15, %rdi
callq 0x3a94
movq %r15, %rdi
callq 0x39f0
movq %r15, %rdi
callq 0x2120
movq %rax, 0x8898(%rip)      # 0x17208
pushq $0x1
popq %r15
leaq 0x16cd(%rip), %r12      # 0x10048
leaq 0x7c7e(%rip), %r13      # 0x16600
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
cmpl %ebp, %r15d
jge 0xea7a
movslq %r15d, %rax
movq (%r14,%rax,8), %rsi
cmpb $0x2d, (%rsi)
jne 0xe9cd
movsbl 0x1(%rsi), %ecx
addl $-0x64, %ecx
rorl %ecx
cmpl $0x9, %ecx
ja 0xea72
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0xea72
cmpb $0x0, 0x884c(%rip)      # 0x17220
jne 0xea72
leaq 0x883f(%rip), %rdi      # 0x17220
callq 0x20b0
jmp 0xea72
movl $0x1, 0x7bfb(%rip)      # 0x165f0
jmp 0xea72
incl %r15d
movq 0x8(%r14,%rax,8), %rsi
leaq 0x77ca(%rip), %rdi      # 0x161d0
callq 0x20b0
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0xea72
movq 0x8(%r14,%rax,8), %rsi
movq %r13, %rdi
callq 0x20b0
movq %r13, %rdi
callq 0x3a94
movq %r13, %rdi
callq 0x3bd2
testl %eax, %eax
jne 0xea50
movq %r13, %rdi
leaq 0x7fc1(%rip), %rsi      # 0x16a00
xorl %edx, %edx
callq 0x334b
movq %r13, %rdi
xorl %esi, %esi
callq 0x3e39
incl %r15d
movq %r13, %rdi
callq 0x39f0
movq %r13, %rdi
callq 0x2120
movq %rax, 0x8796(%rip)      # 0x17200
jmp 0xea72
pushq $0x1
popq %rax
movl %eax, (%rsp)
incl %r15d
jmp 0xe991
leaq 0x7b7f(%rip), %rsi      # 0x16600
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x20b0
movq %r15, %rdi
callq 0x39f0
leaq 0x7733(%rip), %r14      # 0x161d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x2300
cmpq $0x0, 0x8750(%rip)      # 0x17200
jne 0xead3
leaq 0x7b47(%rip), %rdi      # 0x16600
leaq 0x7f40(%rip), %rsi      # 0x16a00
callq 0x20b0
movq 0x873c(%rip), %rax      # 0x17208
movq %rax, 0x872d(%rip)      # 0x17200
leaq 0x10(%rsp), %rsi
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x5271
testl %eax, %eax
setne %al
cmpb $0x0, 0x8(%rsp)
setne %cl
orb %al, %cl
jne 0xec00
leaq 0x7afd(%rip), %r15      # 0x16600
movq %r14, %rdi
movq %r15, %rsi
callq 0x20b0
movq %r14, %rdi
callq 0x3a15
movq %r14, %rdi
callq 0x38af
movq %r14, %rdi
callq 0x2120
movl $0x6f72702e, (%rax,%r14) # imm = 0x6F72702E
movw $0x6a, 0x4(%rax,%r14)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x334b
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x5271
testl %eax, %eax
jne 0xec00
leaq 0x7a9e(%rip), %rdi      # 0x16600
callq 0x2100
testq %rax, %rax
je 0xec00
movq %rax, %r14
pushq $0x2e
popq %r15
leaq 0x1718(%rip), %r12      # 0x10296
pushq $0x1
popq %r13
movq %r14, %rdi
callq 0x2250
testq %rax, %rax
je 0xefd6
movq %rax, %rbp
cmpb $0x2e, 0x13(%rax)
je 0xeb82
addq $0x13, %rbp
movq %rbp, %rdi
movl %r15d, %esi
callq 0x2150
testq %rax, %rax
je 0xeb82
movq %rax, %rdi
movq %r12, %rsi
callq 0x2070
testl %eax, %eax
jne 0xeb82
movq %rbx, %rdi
movq %rbp, %rsi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x5271
testl %eax, %eax
je 0xeb82
leaq 0x75f6(%rip), %r15      # 0x161d0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x20b0
movq %r15, %rdi
callq 0x3a94
movq %r15, %rdi
callq 0x38af
movq %r14, %rdi
callq 0x2190
pushq $0x1
popq %rbp
leaq 0x16b7(%rip), %rsi      # 0x102be
movq %rbx, %rdi
callq 0x262e
movq %rax, %rdi
callq 0x270e
testq %rax, %rax
jne 0xecb0
leaq 0x1697(%rip), %rsi      # 0x102be
movq %rbx, %rdi
xorl %edx, %edx
callq 0x26a2
movq %rax, %r14
leaq 0x24e3(%rip), %rsi      # 0x1111e
movq %rbx, %rdi
callq 0x262e
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0xe87c
movq %rax, %r15
leaq 0x2523(%rip), %rsi      # 0x1117d
movq %rbx, %rdi
callq 0x262e
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe87c
movq %rax, %r15
leaq 0x24f8(%rip), %rsi      # 0x11172
movq %rbx, %rdi
callq 0x262e
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe87c
movq %rax, %r15
leaq 0x1495(%rip), %rsi      # 0x1012f
movq %rbx, %rdi
callq 0x262e
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe87c
leaq 0x1914(%rip), %rsi      # 0x105cb
movq %rbx, %rdi
callq 0x262e
movq %rax, %rdi
callq 0x9c64
cmpl $0x0, 0x4(%rsp)
je 0xece6
movq %rbx, %rdi
movl %ebp, %esi
callq 0x54cd
cmpb $0x0, 0x8541(%rip)      # 0x17220
jne 0xecf3
jmp 0xefb4
cmpb $0x0, 0x8533(%rip)      # 0x17220
je 0xefc8
leaq 0x277e(%rip), %rsi      # 0x11478
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
leaq 0x8512(%rip), %r14      # 0x17220
movq %rax, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x26a2
leaq 0x2763(%rip), %rsi      # 0x11485
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
leaq 0x78cd(%rip), %rsi      # 0x16600
movq %rax, %rdi
movl %ebp, %edx
callq 0x26a2
movl 0x30(%rax), %ecx
andl $-0x10, %ecx
incl %ecx
movl %ecx, 0x30(%rax)
leaq 0x273f(%rip), %rsi      # 0x1148e
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
leaq 0x7ca0(%rip), %r15      # 0x16a00
movq %rax, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x26a2
movl 0x30(%rax), %ecx
andl $-0x10, %ecx
orl $0x2, %ecx
movl %ecx, 0x30(%rax)
leaq 0x2717(%rip), %rsi      # 0x11497
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2310
leaq 0x18e1(%rip), %rsi      # 0x1067d
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
movq %rax, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x26a2
movl 0x30(%rax), %ecx
andl $-0x10, %ecx
orl $0x2, %ecx
movl %ecx, 0x30(%rax)
leaq 0x26df(%rip), %rsi      # 0x114a5
movq %r12, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2310
leaq 0x26d4(%rip), %rsi      # 0x114b1
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
movq %rax, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x26a2
movl 0x30(%rax), %ecx
andl $-0x10, %ecx
orl $0x2, %ecx
movl %ecx, 0x30(%rax)
leaq 0x14fb(%rip), %rsi      # 0x10302
movq %rbx, %rdi
callq 0x268c
movq %rax, %rdi
callq 0x270e
testq %rax, %rax
je 0xee42
movq %rax, %r14
movq (%rax), %r15
movq %r15, %rdi
callq 0x3bd2
testl %eax, %eax
je 0xeea2
leaq 0x7fcb(%rip), %rdi      # 0x16e00
movq %r15, %rsi
callq 0x20b0
jmp 0xeed7
movups 0x267c(%rip), %xmm0     # 0x114c5
movups %xmm0, 0x7fba(%rip)     # 0x16e0a
movups 0x2664(%rip), %xmm0     # 0x114bb
movaps %xmm0, 0x7fa2(%rip)     # 0x16e00
leaq 0x7f9b(%rip), %r14      # 0x16e00
leaq 0x1496(%rip), %rsi      # 0x10302
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0x26a2
movq %rax, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x26a2
movl 0x30(%rax), %ecx
andl $-0x10, %ecx
orl $0x3, %ecx
movl %ecx, 0x30(%rax)
movq (%rax), %rsi
movq %r14, %rdi
callq 0x20b0
movl (%rsp), %ebp
jmp 0xeee9
leaq 0x7757(%rip), %rdi      # 0x16600
callq 0x3bd2
testl %eax, %eax
je 0xee2e
leaq 0x7f43(%rip), %r15      # 0x16e00
leaq 0x773c(%rip), %rsi      # 0x16600
movq %r15, %rdi
callq 0x20b0
movq (%r14), %rsi
movq %r15, %rdi
callq 0x2300
movl (%rsp), %ebp
leaq 0x7f1f(%rip), %rdi      # 0x16e00
pushq $0x1
popq %rsi
callq 0x3e39
leaq 0x7f10(%rip), %r14      # 0x16e00
movq %r14, %rdi
callq 0x39f0
movq %r14, %rdi
callq 0x2120
movq %rax, 0x8309(%rip)      # 0x17210
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x20b0
leaq 0x8302(%rip), %rsi      # 0x17220
movq %r15, %rdi
callq 0x2300
movq %r15, %rdi
callq 0x2120
movl $0x6975622e, 0x10(%rsp,%rax) # imm = 0x6975622E
movl $0x646c69, 0x13(%rsp,%rax) # imm = 0x646C69
leaq 0x86eb(%rip), %r14      # 0x17630
leaq 0x7ab4(%rip), %rsi      # 0x16a00
movq %r14, %rdi
callq 0x20b0
movl $0x2, 0xa6d2(%rip)      # 0x19630
andq $0x0, 0xa6ea(%rip)      # 0x19650
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe7d4
movq 0xa6d5(%rip), %rdi      # 0x19650
testq %rdi, %rdi
je 0xef9b
callq 0x20f0
movslq 0x8694(%rip), %rax      # 0x17620
shlq $0xa, %rax
addq %rax, %r14
movq %r14, %rdi
callq 0x58c7
testl %ebp, %ebp
je 0xefaa
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x54cd
movq %rbx, %rdi
callq 0x2be9
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5557
jmp 0xefb4
callq 0x5557
jmp 0xefb2
movq %r14, %rdi
callq 0x2190
leaq 0x2478(%rip), %rdi      # 0x1145d
xorl %eax, %eax
callq 0x2140
pushq $0x1
popq %rdi
callq 0x2320