// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
subq $0x410, %rsp            # imm = 0x410
leaq 0x69ed4(%rip), %rdi     # 0x73d06
callq 0x32d18
leaq 0x69edc(%rip), %rdi     # 0x73d1a
callq 0x1fbee
movl $0x1, %edi
callq 0x21fd1
movq %rax, %rbx
callq 0x21f45
testq %rax, %rax
je 0x9e6d
movq %rax, %r14
movq %rax, %rdi
callq 0x1fbee
movq %r14, %rdi
callq 0x61c0
testq %rbx, %rbx
je 0x9e9e
leaq 0x69ec1(%rip), %rdx     # 0x73d3a
movq %rsp, %r14
movl $0x401, %esi            # imm = 0x401
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r14, %rdi
callq 0x1fbee
movq %rbx, %rdi
callq 0x61c0
leaq 0x69ea1(%rip), %rdi     # 0x73d46
xorl %esi, %esi
callq 0x6160
testl %eax, %eax
je 0x9f1e
xorl %edi, %edi
callq 0x21fd1
testq %rax, %rax
je 0x9f38
movq %rax, %rbx
leaq 0x69ea1(%rip), %rdx     # 0x73d67
leaq 0x69e90(%rip), %r8      # 0x73d5d
movq %rsp, %r14
movl $0x401, %esi            # imm = 0x401
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x64f0
movq %r14, %rdi
callq 0x1fbee
cmpl $-0x2, %eax
jne 0x9f08
movq %rsp, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x6290
testl %eax, %eax
jne 0x9f14
movq %rsp, %rdi
callq 0x1fbee
testl %eax, %eax
jne 0x9f14
movq %rsp, %rdi
callq 0x6ca0
movq %rbx, %rdi
callq 0x61c0
jmp 0x9f38
movq %rsp, %rdi
movl $0x401, %esi            # imm = 0x401
callq 0x63c0
testq %rax, %rax
je 0x9f38
movq %rsp, %rdi
callq 0x1fbee
leaq 0x69e2e(%rip), %rdi     # 0x73d6d
callq 0x1ff01
leaq 0x174159(%rip), %rax    # 0x17e0a4
movl $0x8, (%rax)
leaq 0x69e1f(%rip), %rdi     # 0x73d77
callq 0x1de6c
xorl %ebx, %ebx
testl %eax, %eax
jns 0x9f74
leaq 0x69e17(%rip), %rdi     # 0x73d81
callq 0x32d18
movl $0x1, %ebx
leaq 0x9a005(%rip), %rdi     # 0xa3f80
leaq 0x8973e(%rip), %rsi     # 0x936c0
movl $0xa, %ecx
rep  movsq (%rsi), %es:(%rdi)
cmpb $0x0, 0x89723(%rip)     # 0x936b4
je 0x9f9a
movb $0x0, 0x8971a(%rip)     # 0x936b4
callq 0x25344
testl %eax, %eax
je 0x9fcc
leaq 0x1aea76(%rip), %rax    # 0x1b8a20
movq (%rax), %rdx
leaq 0x69dfc(%rip), %rdi     # 0x73db0
leaq 0x69e17(%rip), %rsi     # 0x73dd2
xorl %eax, %eax
callq 0x6d6a4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xa0af
movdqa 0x6922c(%rip), %xmm0    # 0x73200
xorl %eax, %eax
movdqa 0x69232(%rip), %xmm1    # 0x73210
movdqa 0x6923a(%rip), %xmm2    # 0x73220
movdqa %xmm0, %xmm3
paddb %xmm1, %xmm3
movdqa %xmm3, (%rsp,%rax)
addq $0x10, %rax
paddb %xmm2, %xmm0
cmpq $0x100, %rax            # imm = 0x100
jne 0x9fe6
movq %rsp, %rsi
xorl %ebp, %ebp
movl $0x10, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x3c128
leaq 0x99fce(%rip), %rax     # 0xa3ff4
movl $0x22, (%rax)
leaq 0x1238ed(%rip), %rax    # 0x12d920
movl $0x0, (%rax)
leaq 0x1238e4(%rip), %rax    # 0x12d924
movw %bp, (%rax)
leaq 0x353a78(%rip), %rax    # 0x35dac2
movb $0x2, (%rax)
leaq 0x353a70(%rip), %rax    # 0x35dac4
movl $0x70, (%rax)
leaq 0x35385f(%rip), %rax    # 0x35d8c0
movabsq $0x3000200010000, %rcx  # imm = 0x3000200010000
movq %rcx, (%rax)
leaq 0x353a4b(%rip), %rax    # 0x35dac0
movw $0x2, (%rax)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x9b75a(%rip)    # 0xa57e0
movdqu %xmm0, 0x9b762(%rip)    # 0xa57f0
movw %bp, 0x9b76b(%rip)      # 0xa5800
movw $0xffff, 0x8967a(%rip)  # imm = 0xFFFF
leaq 0x18(%rip), %rax        # 0xa0bd
leaq 0x1334f4(%rip), %rcx    # 0x13d5a0
movq %rax, (%rcx)
movl %ebx, %eax
addq $0x410, %rsp            # imm = 0x410
popq %rbx
popq %r14
popq %rbp
retq