// File: /workspace/github_c_makefile_star2_O0/serge-404[P]CPM32/cpm.c
// Function: cpm_file_rw
byte cpm_file_rw( byte *fcbaddr, byte wr, int rnd)
{
    int i, n, p, max;
    struct FCB *fcb = (struct FCB *)fcbaddr;


    if ( rnd) {
        if ( fcb->r2) return 6;
        p = fcb->r0 + ( fcb->r1 << 8);
        fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7);
    } else {
        p = (fcb->cr & 0x7f) + (fcb->ex << 7);
    }
    max = fcb->d[1] + (fcb->d[2] << 8) + (fcb->d[3] << 16);

DEBUGOUT( stderr, "BDOS: %s%s file '%11.11s'. @%d", 
      rnd ? "random ": "", wr ? "write" : "read", fcbaddr + 1, p);

    if (( i = fcb->d[ 0]) == 0 || --i >= MAXFCB || fcbs[ i].fp == NULL) {
        if ( !wr && p >= max) return 1;
        if ( cpm_file_open( fcbaddr, wr ? CF_WROPEN : CF_OPEN)) return 0xff;
        fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7); /* 2012.02 bugfix 8->7 */
        i = fcb->d[ 0] - 1;
    } else if ( !fcbs[ i].mod && wr) {
        fclose( fcbs[ i].fp);
        if ( !mk_filename( filename, fcbaddr) ||
             ( fcbs[ i].fp = fopen( filename, "r+b")) == NULL) {
            cpm_file_close( fcbaddr);
            return 0xff;
        }
        fcbs[ i].pos = 0;
    }

    if ((unsigned)(n = p << 7) != fcbs[ i].pos || fcbs[ i].wr != wr) {
        if (( !wr && p > max) ||
             fseek( fcbs[ i].fp, n, SEEK_SET)) {
            cpm_file_close( fcbaddr);
            return 1;
        }
        fcbs[ i].pos = n;
    }

    if ( wr) {
        fcbs[ i].mod = fcbs[ i].wr = TRUE;
        if ( fwrite( mem + cpm_dma_addr, 128, 1, fcbs[ i].fp) != 1)
             return 1;
        n = 128;
    } else {
        fcbs[ i].wr = FALSE;
        if (( n = fread( mem + cpm_dma_addr, 1, 128, fcbs[ i].fp)) <= 0) {
            if ( !fcbs[ i].mod) cpm_file_close( fcbaddr);
            return 1;
        }
        if ( n < 128) memset( mem + cpm_dma_addr + n, 0x1a, 128 - n);
    }
    fcbs[ i].pos += n;

    ++p;
    if ( !rnd) { fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7); }
    if ( p >= max && !fcbs[ i].mod) cpm_file_close( fcbaddr);
    if ( p > max) {
        fcb->d[ 1] = (byte)p;
        fcb->d[ 2] = (byte)(p >> 8);
        fcb->d[ 3] = (byte)(p >> 16);
        fcb->rc = (byte)(p & 0x7f);
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x18(%rbp)
je 0xf4ce
movq -0x30(%rbp), %rax
cmpb $0x0, 0x23(%rax)
je 0xf496
movb $0x6, -0x1(%rbp)
jmp 0xf953
movq -0x30(%rbp), %rax
movzbl 0x21(%rax), %eax
movq -0x30(%rbp), %rcx
movzbl 0x22(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x20(%rax)
movl -0x24(%rbp), %eax
sarl $0x7, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xc(%rax)
jmp 0xf4e9
movq -0x30(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x7f, %eax
movq -0x30(%rbp), %rcx
movzbl 0xc(%rcx), %ecx
shll $0x7, %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x11(%rax), %eax
movq -0x30(%rbp), %rcx
movzbl 0x12(%rcx), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl 0x13(%rcx), %ecx
shll $0x10, %ecx
addl %ecx, %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, 0x10bf9f(%rip)    # 0x11b4b4
je 0xf56c
movq 0xbada(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movl -0x18(%rbp), %ecx
leaq 0x9050(%rip), %rdx      # 0x1857b
leaq 0x8fd9(%rip), %rax      # 0x1850b
cmpl $0x0, %ecx
cmovneq %rax, %rdx
movzbl -0x11(%rbp), %esi
leaq 0x8fd5(%rip), %rcx      # 0x18519
leaq 0x8fc8(%rip), %rax      # 0x18513
cmpl $0x0, %esi
cmovneq %rax, %rcx
movq -0x10(%rbp), %r8
addq $0x1, %r8
movl -0x24(%rbp), %r9d
leaq 0x8f87(%rip), %rsi      # 0x184ec
movb $0x0, %al
callq 0x2220
movq -0x30(%rbp), %rax
movzbl 0x10(%rax), %eax
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
je 0xf5a2
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
cmpl $0x40, %eax
jge 0xf5a2
movslq -0x1c(%rbp), %rcx
leaq 0x10dd1b(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0xf611
cmpb $0x0, -0x11(%rbp)
jne 0xf5b9
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jl 0xf5b9
movb $0x1, -0x1(%rbp)
jmp 0xf953
movq -0x10(%rbp), %rdi
movzbl -0x11(%rbp), %ecx
xorl %esi, %esi
movl $0x1, %eax
cmpl $0x0, %ecx
cmovnel %eax, %esi
callq 0xee00
cmpb $0x0, %al
je 0xf5e0
movb $-0x1, -0x1(%rbp)
jmp 0xf953
movl -0x24(%rbp), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x20(%rax)
movl -0x24(%rbp), %eax
sarl $0x7, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xc(%rax)
movq -0x30(%rbp), %rax
movzbl 0x10(%rax), %eax
subl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf6c5
movslq -0x1c(%rbp), %rcx
leaq 0x10dc94(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpb $0x0, 0x19(%rax)
jne 0xf6c3
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
je 0xf6c3
movslq -0x1c(%rbp), %rcx
leaq 0x10dc6b(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x20d0
movq -0x10(%rbp), %rsi
leaq 0x10d621(%rip), %rdi    # 0x11cc80
callq 0xe320
cmpq $0x0, %rax
je 0xf698
leaq 0x10d60f(%rip), %rdi    # 0x11cc80
leaq 0x8e4d(%rip), %rsi      # 0x184c5
callq 0x2360
movslq -0x1c(%rbp), %rdx
leaq 0x10dc28(%rip), %rcx    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0xf6aa
movq -0x10(%rbp), %rdi
callq 0xf290
movb $-0x1, -0x1(%rbp)
jmp 0xf953
movslq -0x1c(%rbp), %rcx
leaq 0x10dbfb(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
jmp 0xf6c5
movl -0x24(%rbp), %eax
shll $0x7, %eax
movl %eax, -0x20(%rbp)
movslq -0x1c(%rbp), %rdx
leaq 0x10dbd7(%rip), %rcx    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0xf703
movslq -0x1c(%rbp), %rcx
leaq 0x10dbc0(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movzbl 0x18(%rax), %eax
movzbl -0x11(%rbp), %ecx
cmpl %ecx, %eax
je 0xf760
cmpb $0x0, -0x11(%rbp)
jne 0xf711
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0xf736
movslq -0x1c(%rbp), %rcx
leaq 0x10db94(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movslq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x22f0
cmpl $0x0, %eax
je 0xf748
movq -0x10(%rbp), %rdi
callq 0xf290
movb $0x1, -0x1(%rbp)
jmp 0xf953
movl -0x20(%rbp), %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x10db5a(%rip), %rax    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
cmpb $0x0, -0x11(%rbp)
je 0xf7e4
movslq -0x1c(%rbp), %rcx
leaq 0x10db3f(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x1, 0x18(%rax)
movslq -0x1c(%rbp), %rcx
leaq 0x10db29(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x1, 0x19(%rax)
movq 0xba77(%rip), %rdi      # 0x1b210
movzwl 0xba8a(%rip), %eax      # 0x1b22a
cltq
addq %rax, %rdi
movslq -0x1c(%rbp), %rcx
leaq 0x10db00(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl $0x80, %esi
movl $0x1, %edx
callq 0x23d0
cmpq $0x1, %rax
je 0xf7d8
movb $0x1, -0x1(%rbp)
jmp 0xf953
movl $0x80, -0x20(%rbp)
jmp 0xf89d
movslq -0x1c(%rbp), %rcx
leaq 0x10dac1(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movb $0x0, 0x18(%rax)
movq 0xba0f(%rip), %rdi      # 0x1b210
movzwl 0xba22(%rip), %eax      # 0x1b22a
cltq
addq %rax, %rdi
movslq -0x1c(%rbp), %rcx
leaq 0x10da98(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movl $0x1, %esi
movl $0x80, %edx
callq 0x20c0
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jg 0xf863
movslq -0x1c(%rbp), %rcx
leaq 0x10da6c(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpb $0x0, 0x19(%rax)
jne 0xf85a
movq -0x10(%rbp), %rdi
callq 0xf290
movb $0x1, -0x1(%rbp)
jmp 0xf953
cmpl $0x80, -0x20(%rbp)
jge 0xf89b
movq 0xb99d(%rip), %rdi      # 0x1b210
movzwl 0xb9b0(%rip), %eax      # 0x1b22a
cltq
addq %rax, %rdi
movslq -0x20(%rbp), %rax
addq %rax, %rdi
movl $0x80, %eax
subl -0x20(%rbp), %eax
movslq %eax, %rdx
movl $0x1a, %esi
callq 0x2160
jmp 0xf89d
movl -0x20(%rbp), %ecx
movslq -0x1c(%rbp), %rdx
leaq 0x10da05(%rip), %rax    # 0x11d2b0
shlq $0x5, %rdx
addq %rdx, %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0xf8e5
movl -0x24(%rbp), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x20(%rax)
movl -0x24(%rbp), %eax
sarl $0x7, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xc(%rax)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jl 0xf90e
movslq -0x1c(%rbp), %rcx
leaq 0x10d9b8(%rip), %rax    # 0x11d2b0
shlq $0x5, %rcx
addq %rcx, %rax
cmpb $0x0, 0x19(%rax)
jne 0xf90e
movq -0x10(%rbp), %rdi
callq 0xf290
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0xf94f
movl -0x24(%rbp), %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x11(%rax)
movl -0x24(%rbp), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x12(%rax)
movl -0x24(%rbp), %eax
sarl $0x10, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x13(%rax)
movl -0x24(%rbp), %eax
andl $0x7f, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0xf(%rax)
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)