// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq 0x2d89(%rip), %rax      # 0x3fd0
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xe6e(%rip), %rdx       # 0x20c8
callq 0x10e0
movl %eax, -0x50(%rbp)
cmpl $-0x1, %eax
je 0x1379
movl -0x50(%rbp), %eax
addl $-0x42, %eax
movl %eax, %ecx
movq %rcx, -0x260(%rbp)
subl $0x30, %eax
ja 0x135c
movq -0x260(%rbp), %rax
leaq 0xd73(%rip), %rcx       # 0x2004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x2d37(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10f0
movl %eax, 0x2df5(%rip)      # 0x40a4
jmp 0x1368
movq 0x2d1d(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10f0
movl %eax, 0x2ddf(%rip)      # 0x40a8
jmp 0x1368
movq 0x2d03(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10f0
movl %eax, 0x2dc9(%rip)      # 0x40ac
movl 0x2dc3(%rip), %eax      # 0x40ac
movl 0x2dbd(%rip), %ecx      # 0x40ac
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x1311
cmpl $0x80, 0x2da9(%rip)     # 0x40ac
jl 0x1311
cmpl $0x200, 0x2d9d(%rip)    # imm = 0x200
jle 0x131b
movl $0x0, 0x2d91(%rip)      # 0x40ac
jmp 0x1368
movq 0x2cb4(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10f0
movl %eax, 0x2d7e(%rip)      # 0x40b0
jmp 0x1368
movq 0x2c9d(%rip), %rax      # 0x3fd8
movq (%rax), %rdi
callq 0x10f0
movl %eax, 0x2d6b(%rip)      # 0x40b4
jmp 0x1368
cmpl $0x0, -0x14(%rbp)
jne 0x135a
movl $0x1, -0x14(%rbp)
jmp 0x1368
jmp 0x135c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1670
movq 0x2c61(%rip), %rax      # 0x3fd0
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
jmp 0x124c
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x13ae
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xd39(%rip), %rsi       # 0x20d4
callq 0x1080
cmpl $0x0, %eax
jne 0x13ae
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, 0x2cef(%rip)      # 0x40a4
jle 0x1468
cmpl $0x0, 0x2ce6(%rip)      # 0x40a8
jle 0x1468
cmpl $0x0, 0x2ce1(%rip)      # 0x40b0
jle 0x1468
cmpl $0x0, 0x2cd8(%rip)      # 0x40b4
jl 0x1468
movl 0x2cc8(%rip), %eax      # 0x40b0
cmpl 0x2cba(%rip), %eax      # 0x40a8
jge 0x1468
cmpl $0x0, 0x2cb5(%rip)      # 0x40ac
jle 0x1468
movl -0x4c(%rbp), %eax
movq 0x2bcd(%rip), %rcx      # 0x3fd0
cmpl (%rcx), %eax
jne 0x1468
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x4c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x1468
movq -0x10(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x4c(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x1468
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x1468
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x1080
cmpl $0x0, %eax
jne 0x1474
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1670
movq -0x20(%rbp), %rdi
leaq 0xc58(%rip), %rsi       # 0x20d7
callq 0x10c0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x14a3
movq -0x20(%rbp), %rdi
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x40(%rbp), %rdi
callq 0x16c0
movq %rax, -0x38(%rbp)
movl 0x2bee(%rip), %eax      # 0x40a4
imull 0x2beb(%rip), %eax      # 0x40a8
movl %eax, -0x50(%rbp)
movl -0x50(%rbp), %eax
imull 0x2be2(%rip), %eax      # 0x40ac
cltq
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jbe 0x150a
movq 0x2b17(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq 0xbe3(%rip), %rsi       # 0x20da
movb $0x0, %al
callq 0x1090
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x28(%rbp), %rdi
leaq 0xbed(%rip), %rsi       # 0x2102
callq 0x10c0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x1539
movq -0x28(%rbp), %rdi
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x1657
movq -0x40(%rbp), %rdi
callq 0x1060
movl 0x2b60(%rip), %edi      # 0x40a8
movl 0x2b62(%rip), %esi      # 0x40b0
callq 0x1710
movl %eax, 0x2b5f(%rip)      # 0x40b8
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x163e
movl -0x4c(%rbp), %edi
callq 0x1750
movl %eax, -0x254(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x15a5
movq -0x40(%rbp), %rdi
movl -0x254(%rbp), %eax
imull 0x2b1b(%rip), %eax      # 0x40ac
movslq %eax, %rsi
xorl %edx, %edx
callq 0x10b0
cmpl $0x0, %eax
je 0x15a5
callq 0x1030
leaq -0x250(%rbp), %rdi
movslq 0x2af9(%rip), %rsi      # 0x40ac
movq -0x40(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
cmpq $0x1, %rax
je 0x15dc
leaq -0x250(%rbp), %rdi
movslq 0x2ad7(%rip), %rdx      # 0x40ac
xorl %esi, %esi
callq 0x1070
cmpl $0x0, -0x14(%rbp)
jne 0x1607
movq -0x48(%rbp), %rdi
movl -0x254(%rbp), %eax
imull 0x2ab9(%rip), %eax      # 0x40ac
movslq %eax, %rsi
xorl %edx, %edx
callq 0x10b0
cmpl $0x0, %eax
je 0x1607
callq 0x1030
leaq -0x250(%rbp), %rdi
movslq 0x2a97(%rip), %rsi      # 0x40ac
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x1110
cmpq $0x1, %rax
je 0x162e
callq 0x1030
jmp 0x1630
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1560
movq -0x48(%rbp), %rdi
callq 0x1050
movq -0x40(%rbp), %rdi
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw %cs:(%rax,%rax)