// File: /workspace/github_c_makefile_star2_O2/AK6DN[P]dec-utilities-for-pdp/lbn2pbn/lbn2pbn.c
// Function: main
int main(int argc, char* argv[])
{
    int reverse = 0;
    const char* infile;
    const char* outfile;
    size_t max_size;
    size_t size;
    FILE* in;
    FILE* out;
    int p, q;

    p = optind;
    while ((q = getopt(argc, argv, "T:S:B:i:k:r")) != EOF) {
        switch (q) {
        case 'T':
            tracks = atoi(optarg);
            break;
        case 'S':
            sectors = atoi(optarg);
            break;
        case 'B':
            sector_size = atoi(optarg);
            if ((sector_size & (sector_size - 1))
                ||  sector_size < 128  ||  sector_size > 512) {
                sector_size = 0;
            }
            break;
        case 'i':
            interleave = atoi(optarg);
            break;
        case 'k':
            track_skew = atoi(optarg);
            break;
        case 'r':
            if (!reverse) {
                reverse = 1;
                break;
            }
            /*FALLTHRU*/
        default:
            usage(argv[0]);
        }
        p = optind;
    }
    if (argv[p]  &&  strcmp(argv[p], "--") == 0)
        ++p;
    if (tracks <= 0  ||  sectors <= 0  ||  interleave <= 0  ||  track_skew < 0
        ||  interleave >= sectors  ||  sector_size <= 0  ||  p != optind
        ||  !(infile = argv[p++])  ||  !(outfile = argv[p++])  ||  argv[p]
        ||  _stricmp(infile, outfile) == 0) {
        usage(argv[0]);
    }

    if (!(in = fopen(infile, "rb"))) {
        perror(infile);
        return 1;
    }

    size = filesize(in);
    q = tracks * sectors;
    max_size = q * sector_size;
    if (size > max_size) {
        fprintf(stderr, "%s: file size (%zd) may not exceed %zu\n", infile, size, max_size);
        return 1;
    }

    if (!(out = fopen(outfile, "wb"))) {
        perror(outfile);
        return 1;
    }

    rewind(in);
    round = lcm(sectors, interleave);
    for (p = 0;  p < q;  ++p) {
        char sector[512];
        int n = lbn2pbn(p);
#ifdef _DEBUG
        printf("LBN %4d (%2d / %2d)   =   PBN %4d (%2d / %2d)\n",
               p, p / sectors, p % sectors,
               n, n / sectors, n % sectors);
#endif
        if ( reverse  &&  fseek(in,  n * sector_size, SEEK_SET) != 0)
            abort();
        if (fread (sector, sector_size, 1, in)  != 1)
            memset(sector, 0, sector_size);
        if (!reverse  &&  fseek(out, n * sector_size, SEEK_SET) != 0)
            abort();
        if (fwrite(sector, sector_size, 1, out) != 1)
            abort();
    }

    fclose(out);
    fclose(in);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %rbx
movl %edi, %ebp
movb $0x1, %al
movl %eax, 0x4(%rsp)
movq 0x2d91(%rip), %r15      # 0x3fd0
leaq 0xdbe(%rip), %r14       # 0x2004
movq 0x2d8b(%rip), %r12      # 0x3fd8
movl (%r15), %r13d
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10e0
cmpl $0x42, %eax
je 0x1298
cmpl $0x53, %eax
je 0x1287
cmpl $0x54, %eax
je 0x12dc
cmpl $0x69, %eax
je 0x12c8
cmpl $0x6b, %eax
jne 0x12f0
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2e2f(%rip)      # 0x40b4
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2e12(%rip)      # 0x40a8
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
leal -0x201(%rax), %esi
xorl %ecx, %ecx
movl $0x0, %edx
cmpl $0xfffffe7f, %esi       # imm = 0xFFFFFE7F
jb 0x12b8
movl %eax, %edx
leal -0x1(%rax), %esi
testl %esi, %eax
cmovnel %ecx, %edx
movl %edx, 0x2de6(%rip)      # 0x40ac
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2dd9(%rip)      # 0x40b0
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2db9(%rip)      # 0x40a4
jmp 0x124d
cmpl $-0x1, %eax
je 0x1316
cmpl $0x72, %eax
jne 0x15eb
testb $0x1, 0x4(%rsp)
movl $0x0, 0x4(%rsp)
jne 0x124d
jmp 0x15eb
movslq %r13d, %rax
movq (%rbx,%rax,8), %rcx
testq %rcx, %rcx
je 0x133b
xorl %eax, %eax
cmpb $0x2d, (%rcx)
jne 0x1338
cmpb $0x2d, 0x1(%rcx)
jne 0x1338
xorl %eax, %eax
cmpb $0x0, 0x2(%rcx)
sete %al
addl %eax, %r13d
cmpl $0x0, 0x2d62(%rip)      # 0x40a4
jle 0x15eb
movl 0x2d5a(%rip), %eax      # 0x40a8
testl %eax, %eax
jle 0x15eb
movl 0x2d54(%rip), %ecx      # 0x40b0
testl %ecx, %ecx
jle 0x15eb
cmpl $0x0, 0x2d49(%rip)      # 0x40b4
js 0x15eb
cmpl %eax, %ecx
jae 0x15eb
cmpl $0x0, 0x2d2c(%rip)      # 0x40ac
jle 0x15eb
cmpl (%r15), %r13d
jne 0x15eb
movslq %r13d, %rax
movq (%rbx,%rax,8), %r14
testq %r14, %r14
je 0x15eb
movq 0x8(%rbx,%rax,8), %r15
testq %r15, %r15
je 0x15eb
cmpq $0x0, 0x10(%rbx,%rax,8)
jne 0x15eb
movq %r14, %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
je 0x15eb
leaq 0xc3d(%rip), %rsi       # 0x2010
movq %r14, %rdi
callq 0x10c0
testq %rax, %rax
je 0x15b4
movq %rax, %rbx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x10b0
testl %eax, %eax
je 0x13fd
pushq $-0x1
popq %rax
jmp 0x1405
movq %rbx, %rdi
callq 0x10a0
movslq 0x2c98(%rip), %rcx      # 0x40a4
movslq 0x2c95(%rip), %rdx      # 0x40a8
imulq %rcx, %rdx
movslq 0x2c8e(%rip), %r8       # 0x40ac
imulq %rdx, %r8
cmpq %r8, %rax
ja 0x15b9
movq %rdx, %r12
leaq 0xc06(%rip), %rsi       # 0x203b
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x15d9
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
movl 0x2c50(%rip), %r8d      # 0x40a8
movl 0x2c52(%rip), %ecx      # 0x40b0
movl %r8d, %edi
movl %ecx, %esi
testl %edi, %edi
je 0x1481
testl %esi, %esi
je 0x1481
cmpl %esi, %edi
jg 0x1478
movl %esi, %eax
cltd
idivl %edi
movl %edx, %esi
jmp 0x1463
movl %edi, %eax
cltd
idivl %esi
movl %edx, %edi
jmp 0x1463
imull %r8d, %ecx
addl %edi, %esi
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x2c26(%rip)      # 0x40b8
xorl %ebp, %ebp
testl %r12d, %r12d
cmovlel %ebp, %r12d
movq %r12, 0x8(%rsp)
leaq 0x10(%rsp), %r15
cmpl %ebp, 0x8(%rsp)
je 0x1590
movl 0x2bf3(%rip), %edi      # 0x40a8
movl %ebp, %eax
cltd
idivl %edi
movl %eax, %ecx
movl %edx, %esi
imull 0x2beb(%rip), %esi      # 0x40b0
movl %esi, %eax
cltd
idivl 0x2bea(%rip)            # 0x40b8
movl 0x2be0(%rip), %edx      # 0x40b4
imull %ecx, %edx
addl %esi, %edx
addl %edx, %eax
cltd
idivl %edi
movl %edx, %esi
incl %ecx
movl %ecx, %eax
cltd
idivl 0x2bb9(%rip)            # 0x40a4
movl %edx, %r13d
imull %edi, %r13d
addl %esi, %r13d
testb $0x1, 0x4(%rsp)
jne 0x151c
movslq 0x2ba9(%rip), %rax      # 0x40ac
movslq %r13d, %rsi
imulq %rax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x15e6
movslq 0x2b89(%rip), %rsi      # 0x40ac
movq %r15, %rdi
pushq $0x1
popq %rdx
movq %rbx, %r12
movq %rbx, %rcx
callq 0x1040
movl 0x2b72(%rip), %ebx      # 0x40ac
cmpq $0x1, %rax
je 0x154d
movslq %ebx, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
testb $0x1, 0x4(%rsp)
je 0x156f
imull %r13d, %ebx
movslq %ebx, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x15e6
movl 0x2b3d(%rip), %ebx      # 0x40ac
movslq %ebx, %rsi
movq %r15, %rdi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x1110
cmpq $0x1, %rax
jne 0x15e6
incl %ebp
movq %r12, %rbx
jmp 0x14a5
movq %r14, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
xorl %eax, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
jmp 0x15dc
movq 0x2a38(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xa49(%rip), %rsi       # 0x2013
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1090
jmp 0x15e1
movq %r15, %rdi
callq 0x10d0
pushq $0x1
popq %rax
jmp 0x15a2
callq 0x1030
movq (%rbx), %rdi
callq 0x15f3