// File: besm6[P]pascal-re/dtran.cc
// Function: Dtran::mklabels(unsigned int, unsigned int, bool)
void
mklabels(uint32 memaddr, uint32 opcode, bool litconst) {
    int arg1 = (opcode & 07777) + (opcode & 0x040000 ? 070000 : 0);
    int arg2 = opcode & 077777;
    int struc = opcode & 02000000;
    uint reg = opcode >> 20;
    if (basereg && basereg == reg) {
        if (baseaddr == ~0u &&
            (opcode & 03740000) == 02440000) {
            // Setting base reg by a relative address or a symbol
            if ((opcode & 074000) == 074000) {
                uint sympos = opcode & 03777;
                if (memory[table_off + sympos] >> 15 != 0410) {
                    fprintf(stderr, "@%05o Base register set to non-relative address\n", memaddr);
                    exit(1);
                }
                baseaddr = memory[table_off + sympos] & 077777;
            } else {
                baseaddr = opcode & 037777;
            }
            baseop = opcode;
            fprintf(stderr, "@%05o Base register set to %05o\n", memaddr, baseaddr);
        } else if (struc) {
            // Must be the same opcode as used originally.
            if (baseaddr == ~0u || baseop != opcode) {
                fprintf(stderr,
                        "@%05o Base register used in a long-address insn\n", memaddr);
                exit(1);
            }
        } else if (baseaddr == ~0u) {
            fprintf(stderr,
                    "@%05o Base register used but not yet set\n", memaddr);
            exit(1);
        } else if (arg1 >= 04000) {
            fprintf(stderr,
                    "@%05o Base register used with a symbol (%04o)\n", memaddr, arg1);
            exit(1);
        } else {
            uint off = baseaddr + arg1;
            if (off >= labels.size()) {
                fprintf(stderr, "@%05o Base offset %05o too large\n", memaddr, arg1);
                exit(1);
            }
            if (labels[off].empty())
                labels[off] = litconst ? get_literal(off) : strprintf("*%04oB", off);
        }
    } else if (struc && arg2 >= 074000) {
        // nothing
    } else if (struc && arg2 >= 040000 &&
               (arg2 & 037777) < labels.size()) {
        uint off = arg2 & 037777;
        if (labels[off].empty())
            labels[off] = off < 010000 ?
                                strprintf("*%04oB", off) :
                                strprintf("*%05o", off);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %cl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x10(%rbp), %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movl -0x10(%rbp), %edi
andl $0x40000, %edi          # imm = 0x40000
xorl %edx, %edx
movl $0x7000, %esi           # imm = 0x7000
cmpl $0x0, %edi
cmovnel %esi, %edx
addl %edx, %ecx
movl %ecx, -0x18(%rbp)
movl -0x10(%rbp), %ecx
andl $0x7fff, %ecx           # imm = 0x7FFF
movl %ecx, -0x1c(%rbp)
movl -0x10(%rbp), %ecx
andl $0x80000, %ecx          # imm = 0x80000
movl %ecx, -0x20(%rbp)
movl -0x10(%rbp), %ecx
shrl $0x14, %ecx
movl %ecx, -0x24(%rbp)
cmpl $0x0, 0x34(%rax)
je 0x6782
movq -0x80(%rbp), %rax
movl 0x34(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0x6782
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x38(%rax)
jne 0x65fb
movl -0x10(%rbp), %eax
andl $0xfc000, %eax          # imm = 0xFC000
cmpl $0xa4000, %eax          # imm = 0xA4000
jne 0x65fb
movl -0x10(%rbp), %eax
andl $0x7800, %eax           # imm = 0x7800
cmpl $0x7800, %eax           # imm = 0x7800
jne 0x65be
movq -0x80(%rbp), %rax
movl -0x10(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
movl %ecx, -0x28(%rbp)
movl 0x24(%rax), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
movq 0x48(%rax,%rcx,8), %rax
shrq $0xf, %rax
cmpq $0x108, %rax            # imm = 0x108
je 0x65a1
movq 0x4a55(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
leaq 0x2014(%rip), %rsi      # 0x85a4
movb $0x0, %al
callq 0x32e0
movl $0x1, %edi
callq 0x3250
movq -0x80(%rbp), %rax
movl 0x24(%rax), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
movq 0x48(%rax,%rcx,8), %rcx
andq $0x7fff, %rcx           # imm = 0x7FFF
movl %ecx, 0x38(%rax)
jmp 0x65ce
movq -0x80(%rbp), %rax
movl -0x10(%rbp), %ecx
andl $0x3fff, %ecx           # imm = 0x3FFF
movl %ecx, 0x38(%rax)
movq -0x80(%rbp), %rax
movl -0x10(%rbp), %ecx
movl %ecx, 0x3c(%rax)
movq 0x49f9(%rip), %rcx      # 0xafd8
movq (%rcx), %rdi
movl -0xc(%rbp), %edx
movl 0x38(%rax), %ecx
leaq 0x1fe6(%rip), %rsi      # 0x85d5
movb $0x0, %al
callq 0x32e0
jmp 0x677d
cmpl $0x0, -0x20(%rbp)
je 0x6641
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x38(%rax)
je 0x6617
movq -0x80(%rbp), %rax
movl 0x3c(%rax), %eax
cmpl -0x10(%rbp), %eax
je 0x663c
movq 0x49ba(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
leaq 0x1fcb(%rip), %rsi      # 0x85f6
movb $0x0, %al
callq 0x32e0
movl $0x1, %edi
callq 0x3250
jmp 0x677b
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x38(%rax)
jne 0x6670
movq 0x4986(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
leaq 0x1fc8(%rip), %rsi      # 0x8627
movb $0x0, %al
callq 0x32e0
movl $0x1, %edi
callq 0x3250
cmpl $0x800, -0x18(%rbp)     # imm = 0x800
jl 0x66a1
movq 0x4958(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq 0x1fc1(%rip), %rsi      # 0x8651
movb $0x0, %al
callq 0x32e0
movl $0x1, %edi
callq 0x3250
movq -0x80(%rbp), %rdi
movl 0x38(%rdi), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq %rax, -0x88(%rbp)
addq $0x40060, %rdi          # imm = 0x40060
callq 0x5230
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
jb 0x66fb
movq 0x48fe(%rip), %rax      # 0xafd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq 0x1f96(%rip), %rsi      # 0x8680
movb $0x0, %al
callq 0x32e0
movl $0x1, %edi
callq 0x3250
movq -0x80(%rbp), %rdi
addq $0x40060, %rdi          # imm = 0x40060
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x6380
movq %rax, %rdi
callq 0x3360
testb $0x1, %al
jne 0x671e
jmp 0x6775
testb $0x1, -0x11(%rbp)
je 0x6736
movq -0x80(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x50(%rbp), %rdi
callq 0x6fd0
jmp 0x674b
movl -0x2c(%rbp), %edx
leaq -0x50(%rbp), %rdi
leaq 0x1df9(%rip), %rsi      # 0x853d
movb $0x0, %al
callq 0x34c0
movq -0x80(%rbp), %rdi
addq $0x40060, %rdi          # imm = 0x40060
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x6380
movq %rax, %rdi
leaq -0x50(%rbp), %rsi
callq 0x3260
leaq -0x50(%rbp), %rdi
callq 0x3110
jmp 0x6777
jmp 0x6779
jmp 0x677b
jmp 0x677d
jmp 0x6874
cmpl $0x0, -0x20(%rbp)
je 0x6796
cmpl $0x7800, -0x1c(%rbp)    # imm = 0x7800
jl 0x6796
jmp 0x6872
cmpl $0x0, -0x20(%rbp)
je 0x6870
cmpl $0x4000, -0x1c(%rbp)    # imm = 0x4000
jl 0x6870
movq -0x80(%rbp), %rdi
movl -0x1c(%rbp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
movq %rax, -0x90(%rbp)
addq $0x40060, %rdi          # imm = 0x40060
callq 0x5230
movq %rax, %rcx
movq -0x90(%rbp), %rax
cmpq %rcx, %rax
jae 0x6870
movq -0x80(%rbp), %rdi
movl -0x1c(%rbp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movl %eax, -0x54(%rbp)
addq $0x40060, %rdi          # imm = 0x40060
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6380
movq %rax, %rdi
callq 0x3360
testb $0x1, %al
jne 0x680f
jmp 0x686e
cmpl $0x1000, -0x54(%rbp)    # imm = 0x1000
jae 0x682f
movl -0x54(%rbp), %edx
leaq -0x78(%rbp), %rdi
leaq 0x1d17(%rip), %rsi      # 0x853d
movb $0x0, %al
callq 0x34c0
jmp 0x6844
movl -0x54(%rbp), %edx
leaq -0x78(%rbp), %rdi
leaq 0x1d07(%rip), %rsi      # 0x8544
movb $0x0, %al
callq 0x34c0
movq -0x80(%rbp), %rdi
addq $0x40060, %rdi          # imm = 0x40060
movl -0x54(%rbp), %eax
movl %eax, %esi
callq 0x6380
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3260
leaq -0x78(%rbp), %rdi
callq 0x3110
jmp 0x6870
jmp 0x6872
jmp 0x6874
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)