// File: /workspace/github_c_makefile_star2_O3/serge-404[P]CPM32/cpm.c
// Function: cpm_file_open
byte cpm_file_open( byte *fcbaddr, int cr)
{
    int i;
    int d = 0;
    struct FCB *fcb = (struct FCB *)fcbaddr;
#ifdef __linux__
	DIR *hFndFile;	
	WIN32_FIND_DATA aFindData;
	char dir[ _MAX_DIR], fname[ _MAX_FNAME], ext[ _MAX_EXT];
#endif
    if (( i = fcb->d[ 0]) > 0 && --i < MAXFCB && 
      fcbs[ i].addr == fcbaddr && fcbs[ i].fp) {
DEBUGOUT( stderr, "REOPEN %d - ", i);
      fclose( fcbs[ i].fp); fcbs[ i].fp = NULL;
    } else { // 2008.12 FCB���N���A�����A�v���ɑΉ�
      for ( i = 0; i < MAXFCB && fcbs[ i].fp; i++) {
        if ( fcbs[ i].addr == fcbaddr) {
           fclose( fcbs[ i].fp); fcbs[ i].fp = NULL; break;
        }
      }
    }
    if ( i >= MAXFCB || !mk_filename( filename, fcbaddr)) return 0xff;

    if ( cr == CF_CREATE) {
		if ( access( filename, F_OK) == 0) return 0xff;
		if (( fcbs[ i].fp = fopen( filename, "w+b")) == NULL) return 0xff;
		fcbs[ i].mod = fcbs[ i].wr = TRUE;
    } else {
	if (( fcbs[ i].fp = fopen( filename, cr ? "r+b" : "rb")) == NULL)
	{
#ifdef __linux__
		if ((hFndFile=FindFirstFile( filename, &aFindData))==INVALID_HANDLE_VALUE) 
			return 0xff;
		_splitpath( filename, NULL, dir, fname, ext);
		fcbs[ i].fp = fopen( buildname(dir, aFindData.cFileName), cr ? "r+b" : "rb");
		FindClose( hFndFile);
		if (fcbs[ i].fp == NULL)
			return 0xff;
#else
		return 0xff;
#endif
	}
	fcbs[ i].mod = fcbs[ i].wr = FALSE;
	fseek( fcbs[ i].fp, 0, SEEK_END);
	d = ftell( fcbs[ i].fp);
	fseek( fcbs[ i].fp, 0, SEEK_SET);
	d = ( d + 0x7f) >> 7;
    }
    fcbs[ i].pos = 0;
    fcbs[ i].addr = fcbaddr;

    memset( fcbaddr + 12, 0, 33-12);
    fcb->d[0] = (byte)(i + 1);
    fcb->d[1] = (byte)d;
    fcb->d[2] = (byte)(d >> 8);
    fcb->d[3] = (byte)(d >> 16);
    fcb->rc = (byte)(( d >= 127) ? 127 : d);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl %esi, %r15d
movq %rdi, %rbx
movzbl 0x10(%rdi), %r14d
testl %r14d, %r14d
je 0x99ba
cmpb $0x40, %r14b
ja 0x99ba
decl %r14d
movl %r14d, %eax
shlq $0x5, %rax
leaq 0x10c761(%rip), %r12    # 0x116110
cmpq %rbx, 0x10(%rax,%r12)
je 0x9b4b
leaq 0x10c74f(%rip), %r12    # 0x116110
xorl %r14d, %r14d
movq (%r12), %rdi
testq %rdi, %rdi
je 0x99f6
cmpq %rbx, 0x10(%r12)
je 0x99e9
incq %r14
addq $0x20, %r12
cmpq $0x40, %r14
jne 0x99c4
movb $-0x1, %bpl
jmp 0x9b37
callq 0x20c0
movq $0x0, (%r12)
leaq 0x10c0e3(%rip), %rdi    # 0x115ae0
movq %rbx, %rsi
callq 0x91e6
movb $-0x1, %bpl
testq %rax, %rax
je 0x9b37
cmpl $0x2, %r15d
jne 0x9a6b
leaq 0x10c0c2(%rip), %rdi    # 0x115ae0
xorl %esi, %esi
callq 0x2320
testl %eax, %eax
je 0x9b37
leaq 0x10c0ac(%rip), %rdi    # 0x115ae0
leaq 0x7424(%rip), %rsi      # 0x10e5f
callq 0x2330
movslq %r14d, %r13
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10c6bf(%rip), %rdx    # 0x116110
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x9b37
addq %rcx, %rdx
movw $0x101, 0x18(%rdx)      # imm = 0x101
xorl %ecx, %ecx
jmp 0x9aea
testl %r15d, %r15d
leaq 0x737b(%rip), %rax      # 0x10df0
leaq 0x73e7(%rip), %r15      # 0x10e63
cmoveq %rax, %r15
leaq 0x10c059(%rip), %rdi    # 0x115ae0
movq %r15, %rsi
callq 0x2330
movslq %r14d, %r13
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10c670(%rip), %rdx    # 0x116110
leaq (%rdx,%rcx), %r12
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x9b84
movw $0x0, 0x18(%r12)
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x22c0
movq (%r12), %rdi
callq 0x2220
movq %rax, %r15
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22c0
movq %r15, %rcx
addl $0x7f, %ecx
sarl $0x7, %ecx
shlq $0x5, %r13
leaq 0x10c61b(%rip), %rax    # 0x116110
movl $0x0, 0x8(%r13,%rax)
movq %rbx, 0x10(%r13,%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rbx)
movq $0x0, 0x19(%rbx)
incb %r14b
movb %r14b, 0x10(%rbx)
movb %cl, 0x11(%rbx)
movb %ch, 0x12(%rbx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x13(%rbx)
cmpl $0x7f, %ecx
movl $0x7f, %eax
cmovll %ecx, %eax
movb %al, 0xf(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rax, %r12
movq (%r12), %rdi
testq %rdi, %rdi
je 0x99ba
cmpl $0x0, 0x10a7b2(%rip)    # 0x114314
jne 0x9c0b
callq 0x20c0
movq $0x0, (%r12)
movb $-0x1, %bpl
cmpl $0x3f, %r14d
jbe 0x99f6
jmp 0x9b37
leaq 0x10bf55(%rip), %rdi    # 0x115ae0
leaq 0x110(%rsp), %rsi
callq 0x939f
testq %rax, %rax
je 0x9b37
leaq 0x10bf3c(%rip), %rdi    # 0x115ae0
leaq 0x10(%rsp), %rdx
leaq 0x340(%rsp), %rcx
leaq 0x240(%rsp), %r8
movq %rax, 0x8(%rsp)
callq 0x88e9
leaq 0x128(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x89d9
movq %rax, %rdi
movq %r15, %rsi
callq 0x2330
movq %rax, (%r12)
movq 0x8(%rsp), %rdi
callq 0x21a0
movq $0x0, 0x10c2e7(%rip) # 0x115ee0
movq (%r12), %rax
testq %rax, %rax
jne 0x9ab1
jmp 0x9b37
movq 0xa3e6(%rip), %rax      # 0x13ff8
movq (%rax), %rdi
leaq 0x7236(%rip), %rsi      # 0x10e52
movl %r14d, %edx
xorl %eax, %eax
callq 0x2200
movq (%r12), %rdi
jmp 0x9b68