// File: /workspace/github_c_makefile_star2_O2/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 0x82fa
cmpb $0x40, %r14b
ja 0x82fa
decl %r14d
movl %r14d, %eax
shlq $0x5, %rax
leaq 0x10ce21(%rip), %r12    # 0x115110
cmpq %rbx, 0x10(%rax,%r12)
je 0x847f
leaq 0x10ce0f(%rip), %r12    # 0x115110
xorl %r14d, %r14d
cmpq $0x40, %r14
je 0x8323
movq (%r12), %rdi
testq %rdi, %rdi
je 0x8335
cmpq %rbx, 0x10(%r12)
je 0x832b
incq %r14
addq $0x20, %r12
jmp 0x8304
movb $-0x1, %bpl
jmp 0x846b
callq 0x20c0
andq $0x0, (%r12)
leaq 0x10c7a4(%rip), %rdi    # 0x114ae0
movq %rbx, %rsi
callq 0x7c2a
movb $-0x1, %bpl
testq %rax, %rax
je 0x846b
cmpl $0x2, %r15d
jne 0x83aa
leaq 0x10c783(%rip), %rdi    # 0x114ae0
xorl %esi, %esi
callq 0x2330
testl %eax, %eax
je 0x846b
leaq 0x10c76d(%rip), %rdi    # 0x114ae0
leaq 0x7ab5(%rip), %rsi      # 0xfe2f
callq 0x2340
movslq %r14d, %r13
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10cd80(%rip), %rdx    # 0x115110
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x846b
addq %rcx, %rdx
movw $0x101, 0x18(%rdx)      # imm = 0x101
xorl %ecx, %ecx
jmp 0x8426
testl %r15d, %r15d
leaq 0x7a0c(%rip), %rax      # 0xfdc0
leaq 0x7a78(%rip), %r15      # 0xfe33
cmoveq %rax, %r15
leaq 0x10c71a(%rip), %rdi    # 0x114ae0
movq %r15, %rsi
callq 0x2340
movslq %r14d, %r13
movq %r13, %rcx
shlq $0x5, %rcx
leaq 0x10cd31(%rip), %rdx    # 0x115110
leaq (%rdx,%rcx), %r12
movq %rax, (%rcx,%rdx)
testq %rax, %rax
je 0x84b5
andw $0x0, 0x18(%r12)
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x22d0
movq (%r12), %rdi
callq 0x2220
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 0x10ccdf(%rip), %rax    # 0x115110
andl $0x0, 0x8(%r13,%rax)
movq %rbx, 0x10(%r13,%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0xc(%rbx)
andq $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
pushq $0x7f
popq %rax
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 0x82fa
cmpl $0x0, 0x10ae7e(%rip)    # 0x113314
jne 0x8531
callq 0x20c0
andq $0x0, (%r12)
movb $-0x1, %bpl
cmpl $0x3f, %r14d
jbe 0x8335
jmp 0x846b
leaq 0x10c624(%rip), %rdi    # 0x114ae0
leaq 0x110(%rsp), %rsi
callq 0x7d9a
testq %rax, %rax
je 0x846b
leaq 0x10c60b(%rip), %rdi    # 0x114ae0
leaq 0x10(%rsp), %rdx
leaq 0x340(%rsp), %rcx
leaq 0x240(%rsp), %r8
movq %rax, 0x8(%rsp)
callq 0x74e6
leaq 0x128(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x75d4
movq %rax, %rdi
movq %r15, %rsi
callq 0x2340
movq %rax, (%r12)
movq 0x8(%rsp), %rdi
callq 0x807b
movq (%r12), %rax
testq %rax, %rax
jne 0x83f0
jmp 0x846b
movq 0xaac0(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
leaq 0x78e0(%rip), %rsi      # 0xfe22
movl %r14d, %edx
xorl %eax, %eax
callq 0x2200
movq (%r12), %rdi
jmp 0x849c