// File: /workspace/github_c_makefile_star2_O1/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 0x84ea
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2330
testl %eax, %eax
je 0x8928
cmpl $0x0, 0x10ce71(%rip)    # 0x1156d4
je 0x8928
movb 0x100(%rsp), %bpl
testb %bpl, %bpl
je 0x889b
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 0x8883
movb (%rsp), %bpl
testb %bpl, %bpl
je 0x88c6
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 0x88ae
movb 0xb957(%rip), %bpl      # 0x14224
testb %bpl, %bpl
je 0x88f7
leaq 0xb94c(%rip), %r14      # 0x14225
callq 0x2030
movq (%rax), %rcx
movsbq %bpl, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%r14)
movb (%r14), %bpl
incq %r14
testb %bpl, %bpl
jne 0x88de
movb 0xb92b(%rip), %bpl      # 0x14229
testb %bpl, %bpl
je 0x8928
leaq 0xb920(%rip), %r14      # 0x1422a
callq 0x2030
movq (%rax), %rcx
movsbq %bpl, %rdx
movb (%rcx,%rdx,4), %cl
movb %cl, -0x1(%r14)
movb (%r14), %bpl
incq %r14
testb %bpl, %bpl
jne 0x890f
cmpl $0x0, 0x10b97d(%rip)    # 0x1142ac
je 0x8a15
xorl %eax, %eax
leaq 0x8466(%rip), %rcx      # 0x10da4
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 0x8972
incq %rax
cmpb %sil, %dl
je 0x893e
movl $0x1, %eax
cmpb %sil, %dl
je 0x8a0f
xorl %ecx, %ecx
leaq 0x841f(%rip), %rdx      # 0x10da8
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 0x89c3
incq %rcx
cmpb %dil, %sil
je 0x8989
cmpb %dil, %sil
je 0x8a0f
xorl %eax, %eax
leaq 0x83db(%rip), %rcx      # 0x10dac
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 0x8a05
incq %rax
cmpb %sil, %dl
je 0x89d1
movl $0x2, %eax
cmpb %sil, %dl
jne 0x8a15
movl %eax, 0x10b89b(%rip)    # 0x1142b0
cmpb $0x0, 0x200(%rsp)
movb (%rsp), %al
je 0x8a4a
testb %al, %al
je 0x8abd
leaq 0x838f(%rip), %rsi      # 0x10dc0
movq %rbx, %rdi
callq 0x2340
movq %rax, %rbx
testq %rax, %rax
jne 0x8bef
jmp 0x8d3a
testb %al, %al
je 0x8b3e
leaq 0x10cc87(%rip), %rdi    # 0x1156e0
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rsp, %r8
callq 0x864f
cmpl $0x0, 0x10b838(%rip)    # 0x1142b0
leaq 0x8331(%rip), %rax      # 0x10db0
leaq 0x8332(%rip), %rbx      # 0x10db8
cmoveq %rax, %rbx
leaq 0x10cc4f(%rip), %rdi    # 0x1156e0
movq %rbx, %rsi
callq 0x86c4
testq %rax, %rax
leaq 0x8446(%rip), %rsi      # 0x10ee9
cmovneq %rax, %rsi
leaq 0x10d032(%rip), %rdi    # 0x115ae0
movl $0x100, %edx            # imm = 0x100
callq 0x2070
jmp 0x8ba2
leaq 0x10d01c(%rip), %rbx    # 0x115ae0
leaq 0xb759(%rip), %r8       # 0x14224
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rbx, %rdi
callq 0x864f
leaq 0x82d6(%rip), %rsi      # 0x10dc0
movq %rbx, %rdi
callq 0x2340
testq %rax, %rax
jne 0x8bcd
leaq 0x10cfde(%rip), %rbx    # 0x115ae0
leaq 0xb720(%rip), %r8       # 0x14229
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rbx, %rdi
callq 0x864f
leaq 0x8298(%rip), %rsi      # 0x10dc0
movq %rbx, %rdi
callq 0x2340
testq %rax, %rax
jne 0x8bcd
jmp 0x8d3a
leaq 0x10cb9b(%rip), %r14    # 0x1156e0
leaq 0xb6d8(%rip), %r8       # 0x14224
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %r14, %rdi
callq 0x864f
leaq 0x8245(%rip), %rbx      # 0x10db0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x86c4
testq %rax, %rax
leaq 0x8369(%rip), %rsi      # 0x10ee9
cmovneq %rax, %rsi
leaq 0x10cf55(%rip), %rdi    # 0x115ae0
movl $0x100, %edx            # imm = 0x100
callq 0x2070
cmpb $0x0, 0x10cf44(%rip)    # 0x115ae0
je 0x8d4a
xorl %ebp, %ebp
cmpb $0x0, 0x10cf35(%rip)    # 0x115ae0
je 0x8d3c
leaq 0x10cf28(%rip), %rdi    # 0x115ae0
leaq 0x8201(%rip), %rsi      # 0x10dc0
callq 0x2340
testq %rax, %rax
je 0x8d3c
movq %rax, %rbx
leaq 0x10cf09(%rip), %rdi    # 0x115ae0
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
movq %rsp, %r8
callq 0x84ea
movl 0x10b6bb(%rip), %eax    # 0x1142b0
testl %eax, %eax
je 0x8c88
cmpl $0x2, %eax
jne 0x8c20
movl $0x100, %edi            # imm = 0x100
addq 0xb5ea(%rip), %rdi      # 0x141f8
movl $0x1, %esi
movl $0x4d, %edx
movq %rbx, %rcx
callq 0x20b0
movl $0x100, %edi            # imm = 0x100
addq 0xb5cc(%rip), %rdi      # 0x141f8
movl $0x1, %esi
movl $0x10, %edx
movq %rbx, %rcx
callq 0x20b0
cmpl $0x10, %eax
jne 0x8d32
movq 0xb5aa(%rip), %rdi      # 0x141f8
movzwl 0x10a(%rdi), %edx
movzwl 0x108(%rdi), %eax
movl %eax, 0x10b652(%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 0x8ca6
addq %rax, %rdi
movl %ecx, %edx
movl $0x1, %esi
jmp 0x8c9e
movl $0x100, %edi            # imm = 0x100
addq 0xb564(%rip), %rdi      # 0x141f8
movl $0x1, %esi
movl $0xfe00, %edx           # imm = 0xFE00
movq %rbx, %rcx
callq 0x20b0
movq %rbx, %rdi
callq 0x20c0
xorl %eax, %eax
leaq 0x810c(%rip), %rcx      # 0x10dc3
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 0x8ceb
incq %rax
cmpb %sil, %dl
je 0x8cb7
cmpb %sil, %dl
jne 0x8cf9
movw $0x122, 0xb51b(%rip)    # imm = 0x122
leaq 0x200(%rsp), %rbx
movq %rbx, %rdi
callq 0x20e0
leaq 0x1(%rax), %rdi
callq 0x2290
movq %rax, 0x10b727(%rip)    # 0x114440
movq %rax, 0x10b600(%rip)    # 0x114320
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2090
movl $0x1, %ebp
jmp 0x8d3c
movq %rbx, %rdi
callq 0x20c0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x10c98f(%rip), %rdi    # 0x1156e0
leaq 0xb4d1(%rip), %r8       # 0x14229
leaq 0x200(%rsp), %rdx
leaq 0x100(%rsp), %rcx
callq 0x864f
jmp 0x8a8a