// File: /workspace/github_c_makefile_star2_O3/serge-404[P]CPM32/cpm.c
// Function: load_program
int load_program( char *pfile)
{
    FILE *fp;
	int ordsize;
#ifdef __linux__
    char* drv="";
#else
    char drv[ _MAX_DRIVE];
#endif	
	char dir[ _MAX_DIR], fname[ _MAX_FNAME], ext[ _MAX_EXT];

   _splitpath( pfile, drv, dir, fname, ext);
#ifdef __linux__
	if ((access(pfile, F_OK) != 0) && uppercase_flag) {
		CharUpperX(fname);
		CharUpperX(ext);
		CharUpperX(scpm);
		CharUpperX(scom);
	}
#endif
	if (orion128) {
		if (stricmp(ext,"ORD")==0) ordfile=1;
		else if (stricmp(ext,"BRU")==0) ordfile=1;
		else if (stricmp(ext,"RKO")==0) ordfile=2;
	} 
	if ( drv[ 0] == '\0' && dir[ 0] == '\0') {
	   if ( ext[ 0] == '\0') {
		   _makepath( filename2, drv, dir, fname, scpm);
		   _searchenv( filename2, CPMPATH, filename);
		   if ( filename[ 0] == '\0') {
			   _makepath( filename2, drv, dir, fname, scom);
			   _searchenv( filename2, CPMPATH, filename);
		   }
	   } else {
		   _makepath( filename2, drv, dir, fname, ext);
		   if (ordfile) _searchenv( filename2, ORDPATH, filename);
		   else  _searchenv( filename2, CPMPATH, filename);
	   }
	   if ( filename[ 0] == '\0') return FALSE;
	   if (( fp = fopen( filename, "rb")) == NULL) return FALSE;
	   _splitpath( filename, drv, dir, fname, ext);
   } else if ( ext[ 0] == '\0'){
	   _makepath( filename, drv, dir, fname, scpm);
	   if (( fp = fopen( filename, "rb")) == NULL) {
		   _makepath( filename, drv, dir, fname, scom);
		   if (( fp = fopen( filename, "rb")) == NULL) return FALSE;
	   }
	   _splitpath( filename, drv, dir, fname, ext);
   } else {
	   if (( fp = fopen( pfile, "rb")) == NULL) return FALSE;
   }
	if (ordfile) {
		if (ordfile==2)											/* ftRko */
			fread( &(mem[0x100]), 1, 77, fp);                     /* skip RKO header */
		ordsize=fread( &(mem[0x100]), 1, 16, fp);
		if (ordsize==16)	{				/* read ordos header */
			ordsize=*((word*)&mem[0x100+10]);
			ordaddr=*((word*)&mem[0x100+8]);
			if (ordsize+ordaddr>=0xf400)
				ordsize=0xf400-ordaddr;
			if (ordsize>0)
				fread( &mem[ordaddr], 1, ordsize, fp);
		}
		else {
			fclose( fp);
			return FALSE;
		}
	}
	else fread( &(mem[0x100]), 1, BDOS_ORG-0x100, fp);
	fclose( fp);
    if ( stricmp( ext, "COM") == 0)
		cpm_version = 0x122;
    cpmdrv0 = cpm_drive[ 0] = (char *)malloc( strlen( drv) + strlen( dir) + 1);
#ifdef __linux__
	strcpy( cpm_drive[ 0], dir);
#else
	_makepath( cpm_drive[ 0], drv, dir, NULL, NULL);
#endif
    return TRUE;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x300, %rsp            # imm = 0x300
movq %rdi, %rbx
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rsp, %r8
callq 0x88e9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2320
testl %eax, %eax
je 0x8d27
cmpl $0x0, 0x10ca70(%rip)    # 0x1156d4
je 0x8d27
movb 0x100(%rsp), %bpl
testb %bpl, %bpl
je 0x8c9c
callq 0x2030
leaq 0x101(%rsp), %rcx
movq (%rax), %rdx
movsbq %bpl, %rsi
movb (%rdx,%rsi,4), %dl
movb %dl, -0x1(%rcx)
movb (%rcx), %bpl
incq %rcx
testb %bpl, %bpl
jne 0x8c84
movb (%rsp), %bpl
testb %bpl, %bpl
je 0x8cc7
callq 0x2030
leaq 0x1(%rsp), %rcx
movq (%rax), %rdx
movsbq %bpl, %rsi
movb (%rdx,%rsi,4), %dl
movb %dl, -0x1(%rcx)
movb (%rcx), %bpl
incq %rcx
testb %bpl, %bpl
jne 0x8caf
movb 0xb54e(%rip), %bpl      # 0x1421c
testb %bpl, %bpl
je 0x8cf7
callq 0x2030
movq (%rax), %rax
leaq 0xb53b(%rip), %rcx      # 0x1421d
movsbq %bpl, %rdx
movb (%rax,%rdx,4), %dl
movb %dl, -0x1(%rcx)
movb (%rcx), %bpl
incq %rcx
testb %bpl, %bpl
jne 0x8ce2
movb 0xb523(%rip), %bpl      # 0x14221
testb %bpl, %bpl
je 0x8d27
callq 0x2030
movq (%rax), %rax
leaq 0xb510(%rip), %rcx      # 0x14222
movsbq %bpl, %rdx
movb (%rax,%rdx,4), %dl
movb %dl, -0x1(%rcx)
movb (%rcx), %bpl
incq %rcx
testb %bpl, %bpl
jne 0x8d12
cmpl $0x0, 0x10b57e(%rip)    # 0x1142ac
je 0x8e14
xorl %eax, %eax
leaq 0x8097(%rip), %rcx      # 0x10dd4
movzbl (%rsp,%rax), %esi
movl %esi, %edx
andb $0x5f, %dl
cmpb $0x61, %sil
movzbl %dl, %edx
cmovll %esi, %edx
movzbl (%rax,%rcx), %edi
movl %edi, %esi
andb $0x5f, %sil
cmpb $0x61, %dil
movzbl %sil, %esi
cmovll %edi, %esi
testb %dl, %dl
je 0x8d71
incq %rax
cmpb %sil, %dl
je 0x8d3d
movl $0x1, %eax
cmpb %sil, %dl
je 0x8e0e
xorl %ecx, %ecx
leaq 0x8050(%rip), %rdx      # 0x10dd8
movzbl (%rsp,%rcx), %edi
movl %edi, %esi
andb $0x5f, %sil
cmpb $0x61, %dil
movzbl %sil, %esi
cmovll %edi, %esi
movzbl (%rcx,%rdx), %r8d
movl %r8d, %edi
andb $0x5f, %dil
cmpb $0x61, %r8b
movzbl %dil, %edi
cmovll %r8d, %edi
testb %sil, %sil
je 0x8dc2
incq %rcx
cmpb %dil, %sil
je 0x8d88
cmpb %dil, %sil
je 0x8e0e
xorl %eax, %eax
leaq 0x800c(%rip), %rcx      # 0x10ddc
movzbl (%rsp,%rax), %esi
movl %esi, %edx
andb $0x5f, %dl
cmpb $0x61, %sil
movzbl %dl, %edx
cmovll %esi, %edx
movzbl (%rax,%rcx), %edi
movl %edi, %esi
andb $0x5f, %sil
cmpb $0x61, %dil
movzbl %sil, %esi
cmovll %edi, %esi
testb %dl, %dl
je 0x8e04
incq %rax
cmpb %sil, %dl
je 0x8dd0
movl $0x2, %eax
cmpb %sil, %dl
jne 0x8e14
movl %eax, 0x10b49c(%rip)    # 0x1142b0
cmpb $0x0, 0x200(%rsp)
movb (%rsp), %al
je 0x8e49
testb %al, %al
je 0x8ec9
leaq 0x7fc0(%rip), %rsi      # 0x10df0
movq %rbx, %rdi
callq 0x2330
movq %rax, %rbx
testq %rax, %rax
jne 0x8fdb
jmp 0x9126
testb %al, %al
je 0x8f39
leaq 0x10c888(%rip), %rdi    # 0x1156e0
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rsp, %r8
callq 0x8a47
cmpl $0x0, 0x10b439(%rip)    # 0x1142b0
leaq 0x7f62(%rip), %rax      # 0x10de0
leaq 0x7f63(%rip), %rbx      # 0x10de8
cmoveq %rax, %rbx
leaq 0x10c850(%rip), %rdi    # 0x1156e0
movq %rbx, %rsi
callq 0x8ad0
testq %rax, %rax
leaq 0x8077(%rip), %rsi      # 0x10f19
cmovneq %rax, %rsi
leaq 0x10cc33(%rip), %rdi    # 0x115ae0
movl $0x100, %edx            # imm = 0x100
callq 0x2070
cmpb $0x0, 0x10cc22(%rip)    # 0x115ae0
jne 0x8f9d
jmp 0x9126
leaq 0x10cc10(%rip), %rbx    # 0x115ae0
leaq 0xb345(%rip), %r8       # 0x1421c
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rbx, %rdi
callq 0x8a47
leaq 0x7efa(%rip), %rsi      # 0x10df0
movq %rbx, %rdi
callq 0x2330
testq %rax, %rax
jne 0x8fb9
leaq 0x10cbd2(%rip), %rbx    # 0x115ae0
leaq 0xb30c(%rip), %r8       # 0x14221
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rbx, %rdi
callq 0x8a47
leaq 0x7ebc(%rip), %rsi      # 0x10df0
movq %rbx, %rdi
jmp 0x8fab
leaq 0x10c7a0(%rip), %r14    # 0x1156e0
leaq 0xb2d5(%rip), %r8       # 0x1421c
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %r14, %rdi
callq 0x8a47
leaq 0x7e7a(%rip), %rbx      # 0x10de0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8ad0
testq %rax, %rax
leaq 0x7f9e(%rip), %rsi      # 0x10f19
cmovneq %rax, %rsi
leaq 0x10cb5a(%rip), %rdi    # 0x115ae0
movl $0x100, %edx            # imm = 0x100
callq 0x2070
cmpb $0x0, 0x10cb49(%rip)    # 0x115ae0
je 0x9134
leaq 0x10cb3c(%rip), %rdi    # 0x115ae0
leaq 0x7e45(%rip), %rsi      # 0x10df0
callq 0x2330
testq %rax, %rax
je 0x9126
movq %rax, %rbx
leaq 0x10cb1d(%rip), %rdi    # 0x115ae0
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rsp, %r8
callq 0x88e9
movl 0x10b2cf(%rip), %eax    # 0x1142b0
testl %eax, %eax
je 0x9074
cmpl $0x2, %eax
jne 0x900c
movl $0x100, %edi            # imm = 0x100
addq 0xb1f6(%rip), %rdi      # 0x141f0
movl $0x1, %esi
movl $0x4d, %edx
movq %rbx, %rcx
callq 0x20b0
movl $0x100, %edi            # imm = 0x100
addq 0xb1d8(%rip), %rdi      # 0x141f0
movl $0x1, %esi
movl $0x10, %edx
movq %rbx, %rcx
callq 0x20b0
cmpl $0x10, %eax
jne 0x911e
movq 0xb1b6(%rip), %rdi      # 0x141f0
movzwl 0x10a(%rdi), %edx
movzwl 0x108(%rdi), %eax
movl %eax, 0x10b266(%rip)    # 0x1142b4
leal (%rax,%rdx), %esi
movl $0xf400, %r8d           # imm = 0xF400
movl $0xf400, %ecx           # imm = 0xF400
subl %eax, %ecx
cmpl %r8d, %esi
cmovbl %edx, %ecx
testl %ecx, %ecx
jle 0x9092
addq %rax, %rdi
movl %ecx, %edx
movl $0x1, %esi
jmp 0x908a
movl $0x100, %edi            # imm = 0x100
addq 0xb170(%rip), %rdi      # 0x141f0
movl $0x1, %esi
movl $0xfe00, %edx           # imm = 0xFE00
movq %rbx, %rcx
callq 0x20b0
movq %rbx, %rdi
callq 0x20c0
xorl %eax, %eax
leaq 0x7d50(%rip), %rcx      # 0x10df3
movzbl (%rsp,%rax), %esi
movl %esi, %edx
andb $0x5f, %dl
cmpb $0x61, %sil
movzbl %dl, %edx
cmovll %esi, %edx
movzbl (%rax,%rcx), %edi
movl %edi, %esi
andb $0x5f, %sil
cmpb $0x61, %dil
movzbl %sil, %esi
cmovll %edi, %esi
testb %dl, %dl
je 0x90d7
incq %rax
cmpb %sil, %dl
je 0x90a3
cmpb %sil, %dl
jne 0x90e5
movw $0x122, 0xb127(%rip)    # imm = 0x122
leaq 0x200(%rsp), %rbx
movq %rbx, %rdi
callq 0x20e0
leaq 0x1(%rax), %rdi
callq 0x2280
movq %rax, 0x10b33b(%rip)    # 0x114440
movq %rax, 0x10b214(%rip)    # 0x114320
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2090
movl $0x1, %eax
jmp 0x9128
movq %rbx, %rdi
callq 0x20c0
xorl %eax, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x10c5a5(%rip), %rdi    # 0x1156e0
leaq 0xb0df(%rip), %r8       # 0x14221
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
callq 0x8a47
jmp 0x8e89