// File: CHEN-Zhihan[P]Compiler/Node.cpp
// Function: VarNode::getOffSet(std::vector<int, std::allocator<int>>&, int, int, int) const
void VarNode::getOffSet(vector<int>& sList, int functionID, int blbl, int clbl) const {
    auto dimension = sizeMap[{global?GLOBAL:functionID, i}];
    auto size = vector<int>();
    int acc = 1;
    for (auto j = dimension.size() -1 ; j != 0; --j) {
        acc *= dimension[j];
        size.push_back(acc);
    }
    reverse(size.begin(), size.end());
    vector<int> values;
    bool known = true;
    for (int j = 0; j != subscriptions.size();++j) {
        if (subscriptions[j]->isKnown()) {
            auto value = subscriptions[j]->getValue();
            if (value < 0) {
                cerr << "invalid index of array " << i << endl;
                exit(-1);
            }
            values.push_back(value);
        } else {
            values.push_back(-1);
            known = false;
        }
    }
    vector<int> offsets;
    if (known) {
        for (int i = 0; i != values.size() - 1; ++i) {
            offsets.push_back(size[i] * values[i]);
        }
        offsets.push_back(values.back());
        int sum = accumulate(offsets.begin(), offsets.end(), 0);
        printf("\tpush\t%d\n", sum);
    } else {
        for (int i = 0; i != values.size() - 1; ++i) {
            if (values[i] != -1) {
                printf("\tpush\t%d\n", size[i] * values[i]);
            } else {
                subscriptions[i]->ex(sList, functionID, blbl, clbl);
                printf("\tpush\t%d\n", size[i]);
                printf("\tmul\n");
            }
        }
        if (not subscriptions.back()->isKnown()) {
            subscriptions.back()->ex(sList, functionID, blbl, clbl);
        } else {
            printf("\tpush\t%d\n", subscriptions.back()->getValue());
        }
        for (int i = 0; i != subscriptions.size() - 1; ++i) {
            printf("\tadd\n");
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xf8(%rbp)
testb $0x1, 0xc(%rax)
je 0x274ee
leaq 0x29ff7(%rip), %rax     # 0x514dc
movq %rax, -0x100(%rbp)
jmp 0x274fb
leaq -0x14(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x274fb
movq -0xf8(%rbp), %rdx
movq -0x100(%rbp), %rsi
addq $0x8, %rdx
leaq -0x40(%rbp), %rdi
callq 0x29050
leaq 0x29fc3(%rip), %rdi     # 0x514e0
leaq -0x40(%rbp), %rsi
callq 0x28f70
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x29080
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4130
leaq -0x58(%rbp), %rdi
callq 0x299f0
movl $0x1, -0x5c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x29660
subq $0x1, %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x275b2
movq -0x68(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x29d00
movl (%rax), %ecx
movl -0x5c(%rbp), %eax
imull %ecx, %eax
movl %eax, -0x5c(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x29b00
jmp 0x27591
jmp 0x27593
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x27563
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x27b11
leaq -0x58(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x291d0
movq -0x108(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x29200
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x2a900
jmp 0x275eb
leaq -0xa0(%rbp), %rdi
callq 0x299f0
movb $0x1, -0xa1(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0xf8(%rbp), %rdi
movslq -0xa8(%rbp), %rax
movq %rax, -0x110(%rbp)
addq $0x10, %rdi
callq 0x2a210
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
je 0x2775d
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
movslq -0xa8(%rbp), %rsi
callq 0x2a280
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
callq 0x24560
testb $0x1, %al
jne 0x27669
jmp 0x27721
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
movslq -0xa8(%rbp), %rsi
callq 0x2a280
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
callq 0x24540
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xac(%rbp)
jge 0x2770a
movq 0x2994a(%rip), %rdi     # 0x50ff0
leaq 0xf367(%rip), %rsi      # 0x36a14
callq 0x4210
movq %rax, -0x118(%rbp)
jmp 0x276bb
movq -0x118(%rbp), %rdi
movq -0xf8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x43b0
movq %rax, -0x120(%rbp)
jmp 0x276da
movq -0x120(%rbp), %rdi
movq 0x298b8(%rip), %rsi     # 0x50fa0
callq 0x4240
jmp 0x276ef
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x42c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x27b05
leaq -0xa0(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0x29b00
jmp 0x2771f
jmp 0x27747
movl $0xffffffff, -0xb0(%rbp) # imm = 0xFFFFFFFF
leaq -0xa0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2a940
jmp 0x27740
movb $0x0, -0xa1(%rbp)
jmp 0x27749
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x27608
leaq -0xc8(%rbp), %rdi
callq 0x299f0
testb $0x1, -0xa1(%rbp)
je 0x278c6
movl $0x0, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x29660
movq %rax, %rcx
movq -0x128(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x27838
movslq -0xcc(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x29d00
movl (%rax), %eax
movl %eax, -0x12c(%rbp)
movslq -0xcc(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x29d00
movq %rax, %rcx
movl -0x12c(%rbp), %eax
movl (%rcx), %ecx
imull %ecx, %eax
movl %eax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x2a940
jmp 0x27805
jmp 0x27807
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x27780
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x16fe0
jmp 0x27b05
leaq -0xa0(%rbp), %rdi
callq 0x29d50
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x29b00
jmp 0x27855
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x291d0
movq -0x138(%rbp), %rdi
movq %rax, -0xe0(%rbp)
callq 0x29200
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
xorl %edx, %edx
callq 0x2a970
movl %eax, -0x130(%rbp)
jmp 0x2789f
movl -0x130(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %esi
leaq 0xeda4(%rip), %rdi      # 0x3665c
xorl %eax, %eax
callq 0x4040
jmp 0x278c1
jmp 0x27ad2
movl $0x0, -0xec(%rbp)
movslq -0xec(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x29660
movq %rax, %rcx
movq -0x140(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x279e5
movslq -0xec(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x29d00
cmpl $-0x1, (%rax)
je 0x27961
movslq -0xec(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x29d00
movl (%rax), %eax
movl %eax, -0x144(%rbp)
movslq -0xec(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x29d00
movl -0x144(%rbp), %esi
movl (%rax), %eax
imull %eax, %esi
leaq 0xed06(%rip), %rdi      # 0x3665c
xorl %eax, %eax
callq 0x4040
jmp 0x2795f
jmp 0x279cf
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
movslq -0xec(%rbp), %rsi
callq 0x2a280
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x2799b
movslq -0xec(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x29d00
movl (%rax), %esi
leaq 0xeca8(%rip), %rdi      # 0x3665c
xorl %eax, %eax
callq 0x4040
jmp 0x279bd
leaq 0xf068(%rip), %rdi      # 0x36a2c
xorl %eax, %eax
callq 0x4040
jmp 0x279cd
jmp 0x279cf
jmp 0x279d1
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x278d0
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a640
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
callq 0x24560
testb $0x1, %al
jne 0x27a3e
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a640
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movq (%rdi), %rax
movq (%rax), %rax
callq *%rax
jmp 0x27a3c
jmp 0x27a72
movq -0xf8(%rbp), %rdi
addq $0x10, %rdi
callq 0x2a640
movq %rax, %rdi
callq 0x299a0
movq %rax, %rdi
callq 0x24540
movl %eax, %esi
leaq 0xebf5(%rip), %rdi      # 0x3665c
xorl %eax, %eax
callq 0x4040
jmp 0x27a70
jmp 0x27a72
movl $0x0, -0xf0(%rbp)
movq -0xf8(%rbp), %rdi
movslq -0xf0(%rbp), %rax
movq %rax, -0x150(%rbp)
addq $0x10, %rdi
callq 0x2a210
movq %rax, %rcx
movq -0x150(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
je 0x27ad0
leaq 0xebbc(%rip), %rdi      # 0x36670
xorl %eax, %eax
callq 0x4040
jmp 0x27abd
jmp 0x27abf
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x27a7c
jmp 0x27ad2
leaq -0xc8(%rbp), %rdi
callq 0x16fe0
leaq -0xa0(%rbp), %rdi
callq 0x16fe0
leaq -0x58(%rbp), %rdi
callq 0x16fe0
leaq -0x38(%rbp), %rdi
callq 0x16fe0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0xa0(%rbp), %rdi
callq 0x16fe0
leaq -0x58(%rbp), %rdi
callq 0x16fe0
leaq -0x38(%rbp), %rdi
callq 0x16fe0
movq -0x70(%rbp), %rdi
callq 0x43d0
nopl (%rax)