// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
leaq 0x2c13(%rip), %rdx      # 0x5287
callq 0x2040
shll $0x18, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, %r14d
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x2799
xorl %r14d, %r14d
movq 0x5912(%rip), %r13      # 0x7fc8
leaq 0x2bca(%rip), %r15      # 0x5287
leaq 0x2940(%rip), %r12      # 0x5004
movl $0x0, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, (%rsp)
sarl $0x18, %eax
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x26ff
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x277e
cmpl $0x6f, %eax
je 0x2779
cmpl $0x52, %eax
jne 0x28ab
movq (%r13), %rax
movb (%rax), %cl
leal -0x30(%rcx), %edx
cmpb $0x9, %dl
ja 0x28b3
incq %rax
xorl %r14d, %r14d
leal (%r14,%r14,4), %edx
addb $-0x30, %cl
movzbl %cl, %ecx
leal (%rcx,%rdx,2), %r14d
movq %rax, (%r13)
movb (%rax), %cl
leal -0x30(%rcx), %edx
incq %rax
cmpb $0xa, %dl
jb 0x2725
testl %r14d, %r14d
je 0x28b6
cmpl $0x10, %r14d
jl 0x277e
jmp 0x28b6
movl $0x8, %r14d
movb $0x1, %al
movl %eax, (%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x277e
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x277e
movb $0x1, %al
movl %eax, (%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2040
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x26e3
movq 0x5840(%rip), %r15      # 0x7fe0
movslq (%r15), %rcx
movq 0x582e(%rip), %rax      # 0x7fd8
movq (%rax), %rax
cmpl %ebp, %ecx
jge 0x28be
movq (%rbx,%rcx,8), %rdx
leaq 0x2af3(%rip), %rsi      # 0x52b3
movq %rax, %rdi
xorl %eax, %eax
callq 0x21d0
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 %r14d, %edx
callq 0x28ee
movq %rbx, %rdi
callq 0x2a5c
movl 0x8(%rsp), %ebp
movzbl %bpl, %ebx
andl $0x1, %ebx
leaq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x2a9c
leaq 0x10(%rsp), %rdi
movl 0x18(%rdi), %esi
movl 0x20(%rdi), %edx
movl %ebx, %ecx
callq 0x2c06
cmpl $0x0, 0x1c(%rsp)
je 0x2871
leaq 0x2f1a(%rip), %rdi      # 0x5755
callq 0x21e0
testb $0x1, %bpl
je 0x2852
leaq 0x10(%rsp), %rdi
callq 0x2e22
jmp 0x2871
leaq 0x10(%rsp), %rdi
movl 0xc(%rdi), %edx
movl 0x20(%rdi), %esi
addl 0x18(%rdi), %esi
xorl %ecx, %ecx
callq 0x2c06
leaq 0x10(%rsp), %rdi
callq 0x32d6
leaq 0x2ee5(%rip), %rdi      # 0x575d
callq 0x21e0
leaq 0x40070(%rsp), %rdi
callq 0x35c4
leaq 0x40058(%rsp), %rdi
callq 0x35c4
xorl %eax, %eax
addq $0x40088, %rsp          # imm = 0x40088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x22b8
xorl %r14d, %r14d
movl %r14d, %edi
callq 0x2295
movq %rbx, %rdi
movq %rax, %rsi
callq 0x22df
movq %rax, %rbx
leaq 0x40070(%rsp), %rdi
callq 0x35c4
leaq 0x40058(%rsp), %rdi
callq 0x35c4
movq %rbx, %rdi
callq 0x2220