// File: /workspace/github_c_makefile_star2_O2/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 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %rsp, %r8
callq 0x74e6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2330
testl %eax, %eax
je 0x7878
cmpl $0x0, 0x10ce8b(%rip)    # 0x1146d4
je 0x7878
leaq 0x200(%rsp), %rdi
callq 0x74b0
movq %rsp, %rdi
callq 0x74b0
leaq 0xb9bd(%rip), %rdi      # 0x13224
callq 0x74b0
leaq 0xb9b6(%rip), %rdi      # 0x13229
callq 0x74b0
cmpl $0x0, 0x10ba2d(%rip)    # 0x1132ac
je 0x78c6
pushq $0x1
popq %rbp
leaq 0x8519(%rip), %rsi      # 0xfda4
movq %rsp, %rdi
callq 0x77d0
testl %eax, %eax
je 0x78c0
leaq 0x850a(%rip), %rsi      # 0xfda8
movq %rsp, %rdi
callq 0x77d0
testl %eax, %eax
je 0x78c0
leaq 0x84fb(%rip), %rsi      # 0xfdac
movq %rsp, %rdi
callq 0x77d0
testl %eax, %eax
jne 0x78c6
pushq $0x2
popq %rbp
movl %ebp, 0x10b9ea(%rip)    # 0x1132b0
cmpb $0x0, 0x100(%rsp)
movb (%rsp), %al
je 0x78fb
testb %al, %al
je 0x7963
leaq 0x84de(%rip), %rsi      # 0xfdc0
movq %rbx, %rdi
callq 0x2340
movq %rax, %rbx
testq %rax, %rax
jne 0x7a5d
jmp 0x7b6b
testb %al, %al
je 0x79d3
leaq 0x10cdd6(%rip), %rdi    # 0x1146e0
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %rsp, %r8
callq 0x7640
cmpl $0x0, 0x10b987(%rip)    # 0x1132b0
leaq 0x8480(%rip), %rax      # 0xfdb0
leaq 0x8481(%rip), %rbx      # 0xfdb8
cmoveq %rax, %rbx
leaq 0x10cd9e(%rip), %rdi    # 0x1146e0
leaq 0x10d197(%rip), %rdx    # 0x114ae0
movq %rbx, %rsi
callq 0x77ab
cmpb $0x0, 0x10d188(%rip)    # 0x114ae0
jne 0x7a1f
jmp 0x7b6b
leaq 0x10d176(%rip), %rbx    # 0x114ae0
leaq 0xb8b3(%rip), %r8       # 0x13224
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %rbx, %rdi
callq 0x7640
leaq 0x8430(%rip), %rsi      # 0xfdc0
movq %rbx, %rdi
callq 0x2340
testq %rax, %rax
jne 0x7a3b
leaq 0x10d138(%rip), %rbx    # 0x114ae0
leaq 0xb87a(%rip), %r8       # 0x13229
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %rbx, %rdi
callq 0x7640
leaq 0x83f2(%rip), %rsi      # 0xfdc0
movq %rbx, %rdi
jmp 0x7a2d
leaq 0x10cd06(%rip), %r14    # 0x1146e0
leaq 0xb843(%rip), %r8       # 0x13224
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %r14, %rdi
callq 0x7640
leaq 0x83b0(%rip), %rbx      # 0xfdb0
leaq 0x10d0d9(%rip), %rdx    # 0x114ae0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x77ab
cmpb $0x0, 0x10d0c7(%rip)    # 0x114ae0
je 0x7b79
leaq 0x10d0ba(%rip), %rdi    # 0x114ae0
leaq 0x8393(%rip), %rsi      # 0xfdc0
callq 0x2340
testq %rax, %rax
je 0x7b6b
movq %rax, %rbx
leaq 0x10d09b(%rip), %rdi    # 0x114ae0
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
movq %rsp, %r8
callq 0x74e6
movl 0x10b84d(%rip), %eax    # 0x1132b0
testl %eax, %eax
je 0x7aec
cmpl $0x2, %eax
jne 0x7a8a
movl $0x100, %edi            # imm = 0x100
addq 0xb77c(%rip), %rdi      # 0x131f8
pushq $0x1
popq %rsi
pushq $0x4d
popq %rdx
movq %rbx, %rcx
callq 0x20b0
movl $0x100, %edi            # imm = 0x100
addq 0xb762(%rip), %rdi      # 0x131f8
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
movq %rbx, %rcx
callq 0x20b0
cmpl $0x10, %eax
jne 0x7b63
movq 0xb744(%rip), %rdi      # 0x131f8
movzwl 0x10a(%rdi), %edx
movzwl 0x108(%rdi), %eax
movl %eax, 0x10b7ec(%rip)    # 0x1132b4
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 0x7b08
addq %rax, %rdi
movl %ecx, %edx
pushq $0x1
popq %rsi
jmp 0x7b00
movl $0x100, %edi            # imm = 0x100
addq 0xb700(%rip), %rdi      # 0x131f8
pushq $0x1
popq %rsi
movl $0xfe00, %edx           # imm = 0xFE00
movq %rbx, %rcx
callq 0x20b0
movq %rbx, %rdi
callq 0x20c0
leaq 0x82ac(%rip), %rsi      # 0xfdc3
movq %rsp, %rdi
callq 0x77d0
testl %eax, %eax
jne 0x7b2c
movw $0x122, 0xb6e8(%rip)    # imm = 0x122
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
callq 0x20e0
leaq 0x1(%rax), %rdi
callq 0x2280
movq %rax, 0x10b8f4(%rip)    # 0x113440
movq %rax, 0x10b7cd(%rip)    # 0x113320
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2090
pushq $0x1
popq %rax
jmp 0x7b6d
movq %rbx, %rdi
callq 0x20c0
xorl %eax, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x10cb60(%rip), %rdi    # 0x1146e0
leaq 0xb6a2(%rip), %r8       # 0x13229
leaq 0x100(%rsp), %rdx
leaq 0x200(%rsp), %rcx
callq 0x7640
jmp 0x793b