// File: /workspace/github_c_makefile_star2_O3/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 $0x53, %eax
jle 0x128a
cmpl $0x6a, %eax
jg 0x12a9
cmpl $0x54, %eax
je 0x12f2
cmpl $0x69, %eax
jne 0x161a
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 0x161a
testb $0x1, 0x4(%rsp)
movl $0x0, 0x4(%rsp)
jne 0x124d
jmp 0x161a
cmpl $-0x1, %eax
jne 0x161a
movslq %r13d, %rax
movq (%rbx,%rax,8), %rcx
testq %rcx, %rcx
je 0x1355
xorl %eax, %eax
cmpb $0x2d, (%rcx)
jne 0x1352
cmpb $0x2d, 0x1(%rcx)
jne 0x1352
xorl %eax, %eax
cmpb $0x0, 0x2(%rcx)
sete %al
addl %eax, %r13d
cmpl $0x0, 0x2d48(%rip)      # 0x40a4
jle 0x161a
movl 0x2d40(%rip), %eax      # 0x40a8
testl %eax, %eax
jle 0x161a
movl 0x2d3a(%rip), %ecx      # 0x40b0
testl %ecx, %ecx
jle 0x161a
cmpl $0x0, 0x2d2f(%rip)      # 0x40b4
js 0x161a
cmpl %eax, %ecx
jae 0x161a
cmpl $0x0, 0x2d12(%rip)      # 0x40ac
jle 0x161a
cmpl (%r15), %r13d
jne 0x161a
movslq %r13d, %rax
movq (%rbx,%rax,8), %r14
testq %r14, %r14
je 0x161a
movq 0x8(%rbx,%rax,8), %r15
testq %r15, %r15
je 0x161a
cmpq $0x0, 0x10(%rbx,%rax,8)
jne 0x161a
movq %r14, %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
je 0x161a
leaq 0xc23(%rip), %rsi       # 0x2010
movq %r14, %rdi
callq 0x10c0
testq %rax, %rax
je 0x15e6
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10b0
movl %eax, %ecx
movq $-0x1, %rax
testl %ecx, %ecx
jne 0x1425
movq %rbx, %rdi
callq 0x10a0
movslq 0x2c78(%rip), %rcx      # 0x40a4
movslq 0x2c75(%rip), %rdx      # 0x40a8
imulq %rcx, %rdx
movslq 0x2c6e(%rip), %r8       # 0x40ac
movq %rdx, 0x8(%rsp)
imulq %rdx, %r8
cmpq %r8, %rax
ja 0x15eb
leaq 0xbe4(%rip), %rsi       # 0x203b
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x160b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1060
movl 0x2c2e(%rip), %r8d      # 0x40a8
movl 0x2c30(%rip), %ecx      # 0x40b0
testl %r8d, %r8d
sete %al
testl %ecx, %ecx
sete %dl
orb %al, %dl
movl %r8d, %esi
movl %ecx, %edi
jne 0x14b5
movl %ecx, %edi
movl %r8d, %esi
cmpl %edi, %esi
jle 0x14a6
movl %esi, %eax
cltd
idivl %edi
movl %edx, %esi
jmp 0x14ad
movl %edi, %eax
cltd
idivl %esi
movl %edx, %edi
testl %esi, %esi
je 0x14b5
testl %edi, %edi
jne 0x1499
addl %edi, %esi
imull %r8d, %ecx
movl %ecx, %eax
cltd
idivl %esi
movl %eax, 0x2bf2(%rip)      # 0x40b8
cmpl $0x0, 0x8(%rsp)
jle 0x15c2
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movl 0x2bca(%rip), %edi      # 0x40a8
movl %ebp, %eax
cltd
idivl %edi
movl %eax, %ecx
movl %edx, %esi
imull 0x2bc2(%rip), %esi      # 0x40b0
movl %esi, %eax
cltd
idivl 0x2bc1(%rip)            # 0x40b8
movl 0x2bb7(%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 0x2b90(%rip)            # 0x40a4
movl %edx, %r12d
imull %edi, %r12d
addl %esi, %r12d
testb $0x1, 0x4(%rsp)
jne 0x1545
movslq 0x2b80(%rip), %rax      # 0x40ac
movslq %r12d, %rsi
imulq %rax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x1622
movslq 0x2b60(%rip), %rsi      # 0x40ac
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1040
movl 0x2b49(%rip), %r13d     # 0x40ac
cmpq $0x1, %rax
je 0x1576
movslq %r13d, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
testb $0x1, 0x4(%rsp)
je 0x159d
imull %r12d, %r13d
movslq %r13d, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10b0
testl %eax, %eax
jne 0x1622
movl 0x2b0f(%rip), %r13d     # 0x40ac
movslq %r13d, %rsi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1110
cmpq $0x1, %rax
jne 0x1622
incl %ebp
cmpl %ebp, 0x8(%rsp)
jne 0x14d8
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 0x160e
movq 0x2a06(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xa17(%rip), %rsi       # 0x2013
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1090
jmp 0x1613
movq %r15, %rdi
callq 0x10d0
movl $0x1, %eax
jmp 0x15d4
movq (%rbx), %rdi
callq 0x1627
callq 0x1030