// File: /workspace/github_c_makefile_star2_O1/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, 0xc(%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 $0x53, %eax
jle 0x128a
cmpl $0x6a, %eax
jg 0x12a9
cmpl $0x54, %eax
je 0x12f2
cmpl $0x69, %eax
jne 0x160d
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2e28(%rip)      # 0x40b0
jmp 0x124d
cmpl $0x42, %eax
je 0x12bf
cmpl $0x53, %eax
jne 0x1327
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2e01(%rip)      # 0x40a8
jmp 0x124d
cmpl $0x6b, %eax
jne 0x1306
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2df7(%rip)      # 0x40b4
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
leal -0x201(%rax), %esi
xorl %ecx, %ecx
movl $0x0, %edx
cmpl $0xfffffe7f, %esi       # imm = 0xFFFFFE7F
jb 0x12df
movl %eax, %edx
leal -0x1(%rax), %esi
testl %esi, %eax
cmovnel %ecx, %edx
movl %edx, 0x2dbf(%rip)      # 0x40ac
jmp 0x124d
movq (%r12), %rdi
callq 0x10f0
movl %eax, 0x2da3(%rip)      # 0x40a4
jmp 0x124d
cmpl $0x72, %eax
jne 0x160d
testb $0x1, 0xc(%rsp)
movl $0x0, 0xc(%rsp)
jne 0x124d
jmp 0x160d
cmpl $-0x1, %eax
jne 0x160d
movslq %r13d, %rax
movq (%rbx,%rax,8), %rdi
testq %rdi, %rdi
je 0x134f
leaq 0xccd(%rip), %rsi       # 0x2010
callq 0x1080
cmpl $0x1, %eax
adcl $0x0, %r13d
cmpl $0x0, 0x2d4e(%rip)      # 0x40a4
jle 0x160d
movl 0x2d46(%rip), %eax      # 0x40a8
testl %eax, %eax
jle 0x160d
movl 0x2d40(%rip), %ecx      # 0x40b0
testl %ecx, %ecx
jle 0x160d
cmpl $0x0, 0x2d35(%rip)      # 0x40b4
js 0x160d
cmpl %eax, %ecx
jge 0x160d
cmpl $0x0, 0x2d18(%rip)      # 0x40ac
jle 0x160d
cmpl (%r15), %r13d
jne 0x160d
movslq %r13d, %rax
movq (%rbx,%rax,8), %r14
testq %r14, %r14
je 0x160d
movq 0x8(%rbx,%rax,8), %r15
testq %r15, %r15
je 0x160d
cmpq $0x0, 0x10(%rbx,%rax,8)
jne 0x160d
movq %r14, %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
je 0x160d
leaq 0xc2c(%rip), %rsi       # 0x2013
movq %r14, %rdi
callq 0x10c0
testq %rax, %rax
je 0x15d9
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10b0
movl %eax, %ecx
movq $-0x1, %rax
testl %ecx, %ecx
jne 0x141f
movq %rbx, %rdi
callq 0x10a0
movslq 0x2c7e(%rip), %rcx      # 0x40a4
movslq 0x2c7b(%rip), %r13      # 0x40a8
imulq %rcx, %r13
movslq 0x2c74(%rip), %r8       # 0x40ac
imulq %r13, %r8
cmpq %r8, %rax
ja 0x15de
leaq 0xbf2(%rip), %rsi       # 0x203e
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x15fe
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
movl 0x2c39(%rip), %r8d      # 0x40a8
movl 0x2c3b(%rip), %ecx      # 0x40b0
testl %r8d, %r8d
sete %al
testl %ecx, %ecx
sete %dl
orb %al, %dl
movl %r8d, %esi
movl %ecx, %edi
jne 0x14aa
movl %ecx, %edi
movl %r8d, %esi
cmpl %edi, %esi
jle 0x149b
movl %esi, %eax
cltd
idivl %edi
movl %edx, %esi
jmp 0x14a2
movl %edi, %eax
cltd
idivl %esi
movl %edx, %edi
testl %esi, %esi
je 0x14aa
testl %edi, %edi
jne 0x148e
addl %edi, %esi
imull %r8d, %ecx
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x2bfd(%rip)      # 0x40b8
testl %r13d, %r13d
jle 0x15b5
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movl 0x2bd7(%rip), %edi      # 0x40a8
movl %ebp, %eax
cltd
idivl %edi
movl %eax, %ecx
movl %edx, %esi
imull 0x2bcf(%rip), %esi      # 0x40b0
movl %esi, %eax
cltd
idivl 0x2bce(%rip)            # 0x40b8
movl 0x2bc4(%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 0x2b9d(%rip)            # 0x40a4
movl %edx, %r12d
imull %edi, %r12d
addl %esi, %r12d
testb $0x1, 0xc(%rsp)
jne 0x1538
movslq 0x2b8d(%rip), %rax      # 0x40ac
movslq %r12d, %rsi
imulq %rax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x1615
movslq 0x2b6d(%rip), %rsi      # 0x40ac
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1040
cmpq $0x1, %rax
je 0x1566
movslq 0x2b50(%rip), %rdx      # 0x40ac
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
testb $0x1, 0xc(%rsp)
je 0x158d
movslq 0x2b38(%rip), %rax      # 0x40ac
movslq %r12d, %rsi
imulq %rax, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x1615
movslq 0x2b18(%rip), %rsi      # 0x40ac
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1110
cmpq $0x1, %rax
jne 0x1615
incl %ebp
cmpl %ebp, %r13d
jne 0x14cb
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 0x1601
movq 0x2a13(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xa27(%rip), %rsi       # 0x2016
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1090
jmp 0x1606
movq %r15, %rdi
callq 0x10d0
movl $0x1, %eax
jmp 0x15c7
movq (%rbx), %rdi
callq 0x161a
callq 0x1030