// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/src/bstub.c
// Function: ExtInit
int ExtInit(void)
{
	int i, rv = 0;
	unsigned char remapbuf[256];

	buildputs("Tekwar BUILD V1.2\n\n");

#if defined(DATADIR)
    {
        const char *datadir = DATADIR;
        if (datadir && datadir[0]) {
            addsearchpath(datadir);
        }
    }
#endif

     {
          char *supportdir = Bgetsupportdir(1);
          char *appdir = Bgetappdir();
          char dirpath[BMAX_PATH+1];

          // the OSX app bundle, or on Windows the directory where the EXE was launched
          if (appdir) {
               addsearchpath(appdir);
               free(appdir);
          }

          // the global support files directory
          if (supportdir) {
               Bsnprintf(dirpath, sizeof(dirpath), "%s/JFTekWar", supportdir);
               addsearchpath(dirpath);
               free(supportdir);
          }
     }

     // default behaviour is to write to the user profile directory, but
     // creating a 'user_profiles_disabled' file in the current working
     // directory where the game was launched makes the installation
     // "portable" by writing into the working directory
     if (access("user_profiles_disabled", F_OK) == 0) {
          char cwd[BMAX_PATH+1];
          if (getcwd(cwd, sizeof(cwd))) {
               addsearchpath(cwd);
          }
     } else {
          char *supportdir;
          char dirpath[BMAX_PATH+1];
          int asperr;

          if ((supportdir = Bgetsupportdir(0))) {
#if defined(_WIN32) || defined(__APPLE__)
               const char *confdir = "JFTekWar";
#else
               const char *confdir = ".jftekwar";
#endif
               Bsnprintf(dirpath, sizeof(dirpath), "%s/%s", supportdir, confdir);
               asperr = addsearchpath(dirpath);
               if (asperr == -2) {
                    if (Bmkdir(dirpath, S_IRWXU) == 0) {
                         asperr = addsearchpath(dirpath);
                    } else {
                         asperr = -1;
                    }
               }
               if (asperr == 0) {
                    chdir(dirpath);
               }
               free(supportdir);
          }
     }

	initgroupfile("stuff.dat");
	bpp = 8;
	if (loadsetup("build.cfg") < 0) buildputs("Configuration file not found, using defaults.\n"), rv = 1;
	memcpy((void *)buildkeys,(void *)keys,sizeof(buildkeys));   //Trick to make build use setup.dat keys
	if (option[4] > 0) option[4] = 0;
	if (initengine()) {
		wm_msgbox("Build Engine Initialisation Error",
				"There was a problem initialising the Build engine: %s", engineerrstr);
		return -1;
	}

		//You can load your own palette lookup tables here if you just
		//copy the right code!
	for(i=0;i<256;i++)
		remapbuf[i] = ((i+32)&255);  //remap colors for screwy palette sectors
	makepalookup(16,remapbuf,0,0,0,1);

	kensplayerheight = 34;
	zmode = 0;
	defaultspritecstat = 0;

    parallaxtype=2;
    parallaxyoffs=112;
    pskyoff[0]=0;   
    pskyoff[1]=1;   
    pskyoff[2]=2; 
    pskyoff[3]=3;   
    pskybits=2;     // 4 tiles
    memset(&recXT, 0, sizeof(struct spriteextension));
    recordXT=-1;

    ExtDeleteSprite = DeleteSprite;

	return rv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
leaq 0x5fa11(%rip), %rdi     # 0x68cc4
callq 0x30c8d
leaq 0x5fa19(%rip), %rdi     # 0x68cd8
callq 0x1d4fa
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x1f64b
movq %rax, %r14
callq 0x1f5bf
testq %rax, %rax
je 0x92ee
movq %rax, %r15
movq %rax, %rdi
callq 0x1d4fa
movq %r15, %rdi
callq 0x61c0
testq %r14, %r14
je 0x931f
leaq 0x5f9fe(%rip), %rdx     # 0x68cf8
movq %rsp, %r15
movl $0x401, %esi            # imm = 0x401
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r15, %rdi
callq 0x1d4fa
movq %r14, %rdi
callq 0x61c0
leaq 0x5f9de(%rip), %rdi     # 0x68d04
xorl %esi, %esi
callq 0x6160
testl %eax, %eax
je 0x939f
xorl %edi, %edi
callq 0x1f64b
testq %rax, %rax
je 0x93b9
movq %rax, %r14
leaq 0x5f9de(%rip), %rdx     # 0x68d25
leaq 0x5f9cd(%rip), %r8      # 0x68d1b
movq %rsp, %r15
movl $0x401, %esi            # imm = 0x401
movq %r15, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r15, %rdi
callq 0x1d4fa
cmpl $-0x2, %eax
jne 0x9389
movq %rsp, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x6290
testl %eax, %eax
jne 0x9395
movq %rsp, %rdi
callq 0x1d4fa
testl %eax, %eax
jne 0x9395
movq %rsp, %rdi
callq 0x6ca0
movq %r14, %rdi
callq 0x61c0
jmp 0x93b9
movq %rsp, %rdi
movl $0x401, %esi            # imm = 0x401
callq 0x63c0
testq %rax, %rax
je 0x93b9
movq %rsp, %rdi
callq 0x1d4fa
leaq 0x5f96b(%rip), %rdi     # 0x68d2b
callq 0x1d803
leaq 0x16dcd8(%rip), %rax    # 0x1770a4
movl $0x8, (%rax)
leaq 0x5f95c(%rip), %rdi     # 0x68d35
callq 0x1b934
xorl %ebp, %ebp
testl %eax, %eax
jns 0x93f2
leaq 0x5f954(%rip), %rdi     # 0x68d3f
callq 0x30c8d
movl %ebx, %ebp
leaq 0x93b87(%rip), %rdi     # 0x9cf80
leaq 0x832c0(%rip), %rsi     # 0x8c6c0
pushq $0xa
popq %rcx
rep  movsq (%rsi), %es:(%rdi)
cmpb $0x0, 0x832a7(%rip)     # 0x8c6b4
je 0x9416
movb $0x0, 0x8329e(%rip)     # 0x8c6b4
callq 0x2254a
testl %eax, %eax
je 0x9446
leaq 0x1a85fa(%rip), %rax    # 0x1b1a20
movq (%rax), %rdx
leaq 0x5f93e(%rip), %rdi     # 0x68d6e
leaq 0x5f959(%rip), %rsi     # 0x68d90
xorl %eax, %eax
callq 0x61fbc
pushq $-0x1
popq %rbp
jmp 0x94fc
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x945b
leal 0x20(%rax), %ecx
movb %cl, (%rsp,%rax)
incq %rax
jmp 0x9448
pushq $0x10
popq %rdi
movq %rsp, %rsi
pushq $0x1
popq %r9
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x38685
leaq 0x93b7c(%rip), %rax     # 0x9cff4
movl $0x22, (%rax)
leaq 0x11d49b(%rip), %rax    # 0x126920
andl $0x0, (%rax)
leaq 0x11d495(%rip), %rax    # 0x126924
andw $0x0, (%rax)
leaq 0x34d628(%rip), %rax    # 0x356ac2
movb $0x2, (%rax)
leaq 0x34d620(%rip), %rax    # 0x356ac4
movl $0x70, (%rax)
leaq 0x34d40f(%rip), %rax    # 0x3568c0
movabsq $0x3000200010000, %rcx  # imm = 0x3000200010000
movq %rcx, (%rax)
leaq 0x34d5fb(%rip), %rax    # 0x356ac0
movw $0x2, (%rax)
xorps %xmm0, %xmm0
andw $0x0, 0x9532b(%rip)     # 0x9e800
movups %xmm0, 0x95304(%rip)    # 0x9e7e0
orw $-0x1, 0x83234(%rip)    # 0x8c718
movups %xmm0, 0x95305(%rip)    # 0x9e7f0
leaq 0x1a(%rip), %rax        # 0x950c
leaq 0x12d0a7(%rip), %rcx    # 0x1365a0
movq %rax, (%rcx)
movl %ebp, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r14
popq %r15
popq %rbp
retq