// File: /workspace/github_c_makefile_star2_O1/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 0x95a3
cmpb $0x40, %r14b
ja 0x95a3
decl %r14d
movl %r14d, %eax
shlq $0x5, %rax
leaq 0x10cb74(%rip), %r12    # 0x116110
cmpq %rbx, 0x10(%rax,%r12)
je 0x95d2
leaq 0x10cb66(%rip), %r12    # 0x116110
xorl %r14d, %r14d
movq (%r12), %rdi
testq %rdi, %rdi
je 0x95fa
cmpq %rbx, 0x10(%r12)
je 0x95ed
incq %r14
addq $0x20, %r12
cmpq $0x40, %r14
jne 0x95ad
movl $0x40, %r14d
jmp 0x95fa
addq %rax, %r12
cmpq $0x0, (%r12)
je 0x95a3
cmpl $0x0, 0x10ad31(%rip)    # 0x114314
jne 0x97dc
movq (%r12), %rdi
callq 0x20c0
movq $0x0, (%r12)
movb $-0x1, %bpl
cmpl $0x3f, %r14d
jg 0x9743
leaq 0x10c4d2(%rip), %rdi    # 0x115ae0
movq %rbx, %rsi
callq 0x8dfc
testq %rax, %rax
je 0x9743
movslq %r14d, %r13
cmpl $0x2, %r15d
jne 0x9679
leaq 0x10c4b1(%rip), %rdi    # 0x115ae0
xorl %esi, %esi
callq 0x2330
testl %eax, %eax
je 0x9743
leaq 0x10c49b(%rip), %rdi    # 0x115ae0
leaq 0x77e3(%rip), %rsi      # 0x10e2f
callq 0x2340
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10cab1(%rip), %rdx    # 0x116110
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x9743
addq %rcx, %rdx
movw $0x101, 0x18(%rdx)      # imm = 0x101
xorl %ecx, %ecx
jmp 0x96f6
testl %r15d, %r15d
leaq 0x773d(%rip), %rax      # 0x10dc0
leaq 0x77a9(%rip), %r15      # 0x10e33
cmoveq %rax, %r15
leaq 0x10c44b(%rip), %rdi    # 0x115ae0
movq %r15, %rsi
callq 0x2340
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10ca65(%rip), %rdx    # 0x116110
leaq (%rdx,%rcx), %r12
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x9757
movw $0x0, 0x18(%r12)
movq (%r12), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x22d0
movq (%r12), %rdi
callq 0x2230
movq %rax, %r15
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x22d0
movq %r15, %rcx
addl $0x7f, %ecx
sarl $0x7, %ecx
shlq $0x5, %r13
leaq 0x10ca0f(%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
leaq 0x10c382(%rip), %rdi    # 0x115ae0
leaq 0x110(%rsp), %rsi
callq 0x8f8c
testq %rax, %rax
je 0x9743
leaq 0x10c369(%rip), %rdi    # 0x115ae0
leaq 0x10(%rsp), %rdx
leaq 0x340(%rsp), %rcx
leaq 0x240(%rsp), %r8
movq %rax, 0x8(%rsp)
callq 0x84ea
leaq 0x128(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x85e1
movq %rax, %rdi
movq %r15, %rsi
callq 0x2340
movq %rax, (%r12)
movq 0x8(%rsp), %rdi
callq 0x21a0
movq $0x0, 0x10c714(%rip) # 0x115ee0
cmpq $0x0, (%r12)
jne 0x96bc
jmp 0x9743
movq 0xa815(%rip), %rax      # 0x13ff8
movq (%rax), %rdi
leaq 0x7635(%rip), %rsi      # 0x10e22
movl %r14d, %edx
xorl %eax, %eax
callq 0x2210
jmp 0x95e9