// File: /workspace/github_cpp_makefile_star2_O2/besm6[P]pascal-re/dtran.cc
// Function: main
int
main (int argc, char **argv)
{
    int basereg = 0;
    bool nolabels = false, noequs = false, nooctal = false, litconst = false;

    const char * usage = "Usage: %s [-l] [-e] [-o] [-c] [-Rbase] [-d] objfile\n";
    char opt;

    while ((opt = getopt(argc, argv, "cdeloR:")) != -1) {
        switch (opt) {
        case 'l':
            // To produce a compilable assembly code,
            // not needed for decompile.
            nolabels = true;
            break;
        case 'e':
            // To produce a compilable assembly code
            // and to simplify decompilation.
            // If not used, the output will match DTRAN closely.
            noequs = true;
            break;
        case 'o':
            // If not used, the output will match DTRAN closely,
            // if used, the offsets will always be printed in decimal.
            nooctal = true;
            break;
        case 'c':
            litconst = true;
            // References to the const section printed as literals;
            // used for decompilation.
            break;
        case 'R':	/* -RN (decimal) use reg N as base */
            // Say -R8 for decompilation
            basereg = 0;
            while (*optarg >= '0' && *optarg <= '9') {
                basereg *= 10;
                basereg += *optarg - '0';
                ++optarg;
            }
            if (basereg == 0 || basereg > 017) {
                fprintf(stderr, "Bad base reg %o, need 1 <= R <= 15\n",
                        basereg);
                exit(1);
            }
            break;
        case 'd':
            // Decompilation all-in-one mode.
            noequs = true;
            nooctal = true;
            litconst = true;
            basereg = 8;
            break;
        default: /* '?' */
            fprintf(stderr, usage, argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    if (optind >= argc) {
        fprintf (stderr, usage, argv[0]);
        exit (EXIT_FAILURE);
    }
    fprintf(stderr, "Decompiling file %s\n", argv[optind]);
    Dtran dtr(argv[optind], basereg, nolabels, noequs, nooctal);
    dtr.dump_symtab();
    dtr.prtext(litconst);
    dtr.prconst(dtr.cmd_len, dtr.const_len, litconst);
    if (dtr.data_len) {
        printf(" ,DATA,\n");
        if (litconst) {
            dtr.prdata();
        } else {
            dtr.prconst(dtr.cmd_len + dtr.const_len, dtr.data_len, false);
            dtr.prsets();
        }
    }
    printf(" ,END,\n");
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40088, %rsp          # imm = 0x40088
movq %rsi, %rbx
movl %edi, %r14d
movl $0x0, (%rsp)
leaq 0x2d9a(%rip), %r15      # 0x5287
movq 0x5ad4(%rip), %r13      # 0x7fc8
leaq 0x2b09(%rip), %r12      # 0x5004
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movl %r14d, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2040
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x25cb
sarl $0x18, %eax
cmpl $0x52, %eax
jne 0x2570
movq (%r13), %rax
incq %rax
xorl %ebp, %ebp
movb -0x1(%rax), %cl
addb $-0x30, %cl
cmpb $0x9, %cl
ja 0x255e
imull $0xa, %ebp, %edx
movzbl %cl, %ebp
addl %edx, %ebp
movq %rax, (%r13)
incq %rax
jmp 0x2542
testl %ebp, %ebp
je 0x26e7
cmpl $0x10, %ebp
jl 0x2515
jmp 0x26e7
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2589
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x2515
cmpl $0x6f, %eax
jne 0x26ca
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x2515
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x2515
movb $0x1, %al
movl %eax, (%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
pushq $0x8
popq %rbp
jmp 0x2515
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x2515
movq 0x5a0e(%rip), %r15      # 0x7fe0
movslq (%r15), %rax
movq 0x59fc(%rip), %rcx      # 0x7fd8
movq (%rcx), %rdi
cmpl %r14d, %eax
jge 0x26d4
movq (%rbx,%rax,8), %rdx
leaq 0x2cc0(%rip), %rsi      # 0x52b3
xorl %eax, %eax
callq 0x2200
movslq (%r15), %rax
movq (%rbx,%rax,8), %rsi
movzbl 0xc(%rsp), %ecx
andl $0x1, %ecx
movzbl 0x4(%rsp), %r8d
andl $0x1, %r8d
movzbl (%rsp), %r9d
andl $0x1, %r9d
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %edx
callq 0x271e
movq %rbx, %rdi
callq 0x28a6
movl 0x8(%rsp), %ebp
movzbl %bpl, %ebx
andl $0x1, %ebx
leaq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x28e2
leaq 0x10(%rsp), %rdi
movl 0x18(%rdi), %esi
movl 0x20(%rdi), %edx
movl %ebx, %ecx
callq 0x2a38
cmpl $0x0, 0x1c(%rsp)
je 0x26a0
leaq 0x30eb(%rip), %rdi      # 0x5755
callq 0x2220
testb $0x1, %bpl
je 0x2681
leaq 0x10(%rsp), %rdi
callq 0x2bf4
jmp 0x26a0
leaq 0x10(%rsp), %rdi
movl 0xc(%rdi), %edx
movl 0x20(%rdi), %esi
addl 0x18(%rdi), %esi
xorl %ecx, %ecx
callq 0x2a38
leaq 0x10(%rsp), %rdi
callq 0x2f5c
leaq 0x30b6(%rip), %rdi      # 0x575d
callq 0x2220
leaq 0x10(%rsp), %rdi
callq 0x303e
xorl %eax, %eax
addq $0x40088, %rsp          # imm = 0x40088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5907(%rip), %rax      # 0x7fd8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x2b74(%rip), %rsi      # 0x5252
xorl %eax, %eax
callq 0x2200
jmp 0x2701
movq 0x58ea(%rip), %rax      # 0x7fd8
movq (%rax), %rdi
leaq 0x2b97(%rip), %rsi      # 0x528f
movl %ebp, %edx
xorl %eax, %eax
callq 0x2200
pushq $0x1
popq %rdi
callq 0x2190
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x303e
movq %rbx, %rdi
callq 0x2280