// File: /workspace/github_cpp_makefile_star2_O1/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 $0x40098, %rsp          # imm = 0x40098
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x2c13(%rip), %rdx      # 0x5287
callq 0x2040
shll $0x18, %eax
xorl %r13d, %r13d
movl $0x0, %r12d
movl $0x0, %ecx
movq %rcx, (%rsp)
movl $0x0, %ecx
movq %rcx, 0x8(%rsp)
movl $0x0, %r14d
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x27ad
xorl %r14d, %r14d
movq 0x5915(%rip), %r13      # 0x7fc8
leaq 0x2bcd(%rip), %r15      # 0x5287
leaq 0x2943(%rip), %r12      # 0x5004
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
sarl $0x18, %eax
leal -0x63(%rax), %ecx
cmpl $0x9, %ecx
ja 0x2704
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movb $0x1, %al
movq %rax, 0x18(%rsp)
jmp 0x2788
cmpl $0x6f, %eax
je 0x2781
cmpl $0x52, %eax
jne 0x28b9
movq (%r13), %rax
movb (%rax), %cl
leal -0x30(%rcx), %edx
xorl %r14d, %r14d
cmpb $0x9, %dl
ja 0x2748
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 0x2729
testl %r14d, %r14d
je 0x28c1
cmpl $0x10, %r14d
jl 0x2788
jmp 0x28c1
movl $0x8, %r14d
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, 0x18(%rsp)
movb $0x1, %al
movq %rax, (%rsp)
jmp 0x2788
movb $0x1, %al
movq %rax, 0x8(%rsp)
jmp 0x2788
movb $0x1, %al
movq %rax, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2040
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x26e4
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
movq 0x582c(%rip), %r15      # 0x7fe0
movslq (%r15), %rcx
movq 0x581a(%rip), %rax      # 0x7fd8
movq (%rax), %rax
cmpl %ebp, %ecx
jge 0x28c9
movq (%rbx,%rcx,8), %rdx
leaq 0x2adf(%rip), %rsi      # 0x52b3
movq %rax, %rdi
xorl %eax, %eax
callq 0x21d0
movslq (%r15), %rax
movq (%rbx,%rax,8), %rsi
movzbl 0x8(%rsp), %ecx
andl $0x1, %ecx
movzbl (%rsp), %r8d
andl $0x1, %r8d
movzbl %r13b, %r9d
andl $0x1, %r9d
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl %r14d, %edx
callq 0x28fa
movq %rbx, %rdi
callq 0x2a68
movzbl %r12b, %ebx
andl $0x1, %ebx
leaq 0x20(%rsp), %rdi
movl %ebx, %esi
callq 0x2aa8
leaq 0x20(%rsp), %rdi
movl 0x18(%rdi), %esi
movl 0x20(%rdi), %edx
movl %ebx, %ecx
callq 0x2c12
cmpl $0x0, 0x2c(%rsp)
je 0x287f
leaq 0x2f0c(%rip), %rdi      # 0x5755
callq 0x21e0
testb $0x1, %r12b
je 0x2860
leaq 0x20(%rsp), %rdi
callq 0x2e30
jmp 0x287f
leaq 0x20(%rsp), %rdi
movl 0xc(%rdi), %edx
movl 0x20(%rdi), %esi
addl 0x18(%rdi), %esi
xorl %ecx, %ecx
callq 0x2c12
leaq 0x20(%rsp), %rdi
callq 0x32f0
leaq 0x2ed7(%rip), %rdi      # 0x575d
callq 0x21e0
leaq 0x40080(%rsp), %rdi
callq 0x35f0
leaq 0x40068(%rsp), %rdi
callq 0x35f0
xorl %eax, %eax
addq $0x40098, %rsp          # imm = 0x40098
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x22b8
movl %r14d, %edi
callq 0x2295
movq %rbx, %rdi
movq %rax, %rsi
callq 0x22df
movq %rax, %rbx
leaq 0x40080(%rsp), %rdi
callq 0x35f0
leaq 0x40068(%rsp), %rdi
callq 0x35f0
movq %rbx, %rdi
callq 0x2220
nop