// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x10(%rax), %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jle 0xeed7
movl -0x18(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x18(%rbp)
cmpl $0x40, %eax
jge 0xeed7
movslq -0x18(%rbp), %rcx
leaq 0x10e45e(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
cmpq -0x10(%rbp), %rax
jne 0xeed7
movslq -0x18(%rbp), %rcx
leaq 0x10e442(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
je 0xeed7
cmpl $0x0, 0x10c632(%rip)    # 0x11b4b4
je 0xee9f
movq 0xc16d(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
leaq 0x961c(%rip), %rsi      # 0x184b4
movb $0x0, %al
callq 0x2220
movslq -0x18(%rbp), %rcx
leaq 0x10e406(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x20d0
movslq -0x18(%rbp), %rcx
leaq 0x10e3ec(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, (%rax)
jmp 0xef7a
movl $0x0, -0x18(%rbp)
xorl %eax, %eax
cmpl $0x40, -0x18(%rbp)
movb %al, -0x461(%rbp)
jge 0xef0b
movslq -0x18(%rbp), %rcx
leaq 0x10e3b9(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
setne %al
movb %al, -0x461(%rbp)
movb -0x461(%rbp), %al
testb $0x1, %al
jne 0xef17
jmp 0xef78
movslq -0x18(%rbp), %rcx
leaq 0x10e38e(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
cmpq -0x10(%rbp), %rax
jne 0xef68
movslq -0x18(%rbp), %rcx
leaq 0x10e372(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x20d0
movslq -0x18(%rbp), %rcx
leaq 0x10e358(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq $0x0, (%rax)
jmp 0xef78
jmp 0xef6a
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xeede
jmp 0xef7a
cmpl $0x40, -0x18(%rbp)
jge 0xef96
movq -0x10(%rbp), %rsi
leaq 0x10dcf5(%rip), %rdi    # 0x11cc80
callq 0xe320
cmpq $0x0, %rax
jne 0xef9f
movb $-0x1, -0x1(%rbp)
jmp 0xf27d
cmpl $0x2, -0x14(%rbp)
jne 0xf02d
leaq 0x10dcd0(%rip), %rdi    # 0x11cc80
xorl %esi, %esi
callq 0x2350
cmpl $0x0, %eax
jne 0xefc5
movb $-0x1, -0x1(%rbp)
jmp 0xf27d
leaq 0x10dcb4(%rip), %rdi    # 0x11cc80
leaq 0x94ee(%rip), %rsi      # 0x184c1
callq 0x2360
movslq -0x18(%rbp), %rdx
leaq 0x10e2cd(%rip), %rcx    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xeffc
movb $-0x1, -0x1(%rbp)
jmp 0xf27d
movslq -0x18(%rbp), %rcx
leaq 0x10e2a9(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x1, 0x18(%rax)
movslq -0x18(%rbp), %rcx
leaq 0x10e293(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x1, 0x19(%rax)
jmp 0xf1ce
movl -0x14(%rbp), %ecx
leaq 0x941b(%rip), %rsi      # 0x18452
leaq 0x9487(%rip), %rax      # 0x184c5
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x10dc34(%rip), %rdi    # 0x11cc80
callq 0x2360
movslq -0x18(%rbp), %rdx
leaq 0x10e254(%rip), %rcx    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xf136
leaq 0x10dc09(%rip), %rdi    # 0x11cc80
leaq -0x160(%rbp), %rsi
callq 0xe5e0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0xf096
movb $-0x1, -0x1(%rbp)
jmp 0xf27d
leaq -0x260(%rbp), %rdx
leaq -0x360(%rbp), %rcx
leaq -0x460(%rbp), %r8
leaq 0x10dbce(%rip), %rdi    # 0x11cc80
xorl %eax, %eax
movl %eax, %esi
callq 0xd6b0
leaq -0x260(%rbp), %rdi
leaq -0x160(%rbp), %rsi
addq $0x18, %rsi
callq 0xd820
movq %rax, %rdi
movl -0x14(%rbp), %ecx
leaq 0x9373(%rip), %rsi      # 0x18452
leaq 0x93df(%rip), %rax      # 0x184c5
cmpl $0x0, %ecx
cmovneq %rax, %rsi
callq 0x2360
movq %rax, %rcx
movslq -0x18(%rbp), %rdx
leaq 0x10e1b0(%rip), %rax    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rdi
callq 0xea00
movslq -0x18(%rbp), %rcx
leaq 0x10e192(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0xf134
movb $-0x1, -0x1(%rbp)
jmp 0xf27d
jmp 0xf136
movslq -0x18(%rbp), %rcx
leaq 0x10e16f(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0x18(%rax)
movslq -0x18(%rbp), %rcx
leaq 0x10e159(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0x19(%rax)
movslq -0x18(%rbp), %rcx
leaq 0x10e143(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x22f0
movslq -0x18(%rbp), %rcx
leaq 0x10e120(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x2240
movl %eax, -0x1c(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0x10e103(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x22f0
movl -0x1c(%rbp), %eax
addl $0x7f, %eax
sarl $0x7, %eax
movl %eax, -0x1c(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0x10e0d7(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x10(%rbp), %rcx
movslq -0x18(%rbp), %rdx
leaq 0x10e0ba(%rip), %rax    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
xorl %esi, %esi
movl $0x15, %edx
callq 0x2160
movl -0x18(%rbp), %eax
addl $0x1, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x10(%rax)
movl -0x1c(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x11(%rax)
movl -0x1c(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x12(%rax)
movl -0x1c(%rbp), %eax
sarl $0x10, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x13(%rax)
cmpl $0x7f, -0x1c(%rbp)
jl 0xf261
movl $0x7f, %eax
movl %eax, -0x468(%rbp)
jmp 0xf26a
movl -0x1c(%rbp), %eax
movl %eax, -0x468(%rbp)
movl -0x468(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xf(%rax)
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopl (%rax)