// File: /workspace/github_cpp_makefile_star2_O2/sighingnow[P]compile-pascal/pl0_tac_gen.cpp
// Function: pl0_tac_call_func[abi:cxx11](pl0_ast_call_func const*)
pair<Value *, string> pl0_tac_call_func(pl0_ast_call_func const *stmt) {
    cout << __func__;
    // a single identifier can be a function id or just simple variable. STRATEGY: choose the nested one.
    string fid = stmt->fn->id;
    if (!stmt->args || stmt->args->args.empty()) {
        int d1 = valtb.depth(fid), d2 = vartb.depth(fid), d3 = functb.depth(fid);
        if (d1 > d2 && d1 > d3) {
            constant val; valtb.find(fid, true, val);
            return make_pair(new Value(val.val, val.dt), "integer");
        }
        if (d2 > d3) {
            variable var; vartb.find(fid, true, var);
            if (var.len != -1) {
                pl0_ast_error(stmt->loc, string("use an array ") + "\"" + var.name + "\"" + " as a factor.");
            }
            return make_pair(new Value(var.name, var.dt), var.type);
        }
    }
    if (!functb.find(fid, true)) {
        pl0_ast_error(stmt->loc, string("use of undeclared identifier ") + "\"" + fid + "\"");
    }
    std::vector<std::pair<Value *, std::string>> args;
    if (stmt->args) {
        for (auto argexpr: stmt->args->args) {
            auto arg = pl0_tac_expr(argexpr->arg);
            args.emplace(args.begin(), arg);
        }
    }
    func fn;
    functb.find(fid, true, fn);
    std::vector<std::pair<Value *, bool>> pushes; // <Value * val, bool is_ref>
    if (args.size() != fn.param_t.size()) {
        pl0_ast_error(stmt->args->loc, string("unmatched number of parameters and arguments."));
    }
    else {
        size_t len = args.size();
        for (size_t i = 0; i < args.size(); ++i) {
            bool is_ref = fn.param_t[len-1-i].length() > 4 && fn.param_t[len-1-i].substr(0, 4) == "ref_";
            // if (args[i].second != (is_ref ? fn.param_t[len-1-i].substr(4) : fn.param_t[len-1-i])) {
            //     pl0_ast_error(stmt->args->args[i]->loc, string("unmatched type of parameter and argument."));
            // }
            if (is_ref) {
                if (args[i].first->t == Value::TYPE::IMM || (args[i].first->sv.back() != '#' && args[i].first->sv.front() == '~')) {
                    pl0_ast_error(stmt->args->args[i]->loc, string("use constant or expression as reference value."));
                }
                pushes.emplace_back(make_pair(args[i].first, true));
            }
            else {
                pushes.emplace_back(make_pair(args[i].first, false));
            }
        }
    }
    string retval = irb.makeret();
    irb.emit("call", new Value(fn.name, "string"), pushes, new Value(retval, fn.rettype));
    irb.emit("label", new Value(irb.makelabel(), "integer"), new Value("allsuffix", "string"));
    return make_pair(new Value(retval, fn.rettype), fn.rettype);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movq %rsi, %r14
movq %rdi, %r12
movq 0x454a8(%rip), %rdi     # 0x8afd8
leaq 0x15a08(%rip), %rsi     # 0x5b53f
callq 0x5250
movq 0x8(%r14), %rsi
addq $0x8, %rsi
leaq 0x78(%rsp), %rdi
callq 0x5340
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x45b65
movq 0x8(%rax), %rcx
cmpq 0x10(%rax), %rcx
jne 0x45de1
leaq 0x46adc(%rip), %rdi     # 0x8c648
leaq 0x78(%rsp), %rsi
callq 0x47a70
movl %eax, %ebx
leaq 0x46a99(%rip), %rdi     # 0x8c618
leaq 0x78(%rsp), %rsi
callq 0x47422
movl %eax, %ebp
leaq 0x46b16(%rip), %rdi     # 0x8c6a8
leaq 0x78(%rsp), %rsi
callq 0x47324
cmpl %ebp, %ebx
setle %cl
cmpl %eax, %ebx
setle %dl
orb %cl, %dl
jne 0x45c4b
leaq 0x98(%rsp), %rdi
callq 0x47a24
leaq 0x46a86(%rip), %rdi     # 0x8c648
leaq 0x78(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0x98(%rsp), %rcx
callq 0x47b6e
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r14
leaq 0xc0(%rsp), %rsi
movl -0x8(%rsi), %ebx
leaq 0x328(%rsp), %rdi
callq 0x5340
movb $0x1, %bpl
leaq 0x328(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x46ac6
leaq 0x30(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x134f1(%rip), %rax     # 0x5910f
movq %rax, 0x8(%rsi)
xorl %ebp, %ebp
movq %r12, %rdi
callq 0x47cdc
leaq 0x328(%rsp), %rdi
callq 0x5120
leaq 0x98(%rsp), %rdi
callq 0x46f8a
jmp 0x463de
cmpl %eax, %ebp
jle 0x45de1
leaq 0x98(%rsp), %rdi
callq 0x472c8
leaq 0x469b1(%rip), %rdi     # 0x8c618
leaq 0x78(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0x98(%rsp), %rcx
callq 0x47682
cmpl $-0x1, 0xf8(%rsp)
je 0x45d4d
movq (%r14), %rbx
leaq 0x14fe4(%rip), %rsi     # 0x5ac78
leaq 0x108(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0xfa26
leaq 0x15376(%rip), %rdx     # 0x5b023
leaq 0x50(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xfd14
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0xfda4
leaq 0x15346(%rip), %rdx     # 0x5b023
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xfd14
leaq 0x14f1b(%rip), %rdx     # 0x5ac0e
leaq 0x308(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xfd14
leaq 0x308(%rsp), %rsi
movq %rbx, %rdi
callq 0x3da88
leaq 0x308(%rsp), %rdi
callq 0x5120
leaq 0x30(%rsp), %rdi
callq 0x5120
leaq 0x10(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x5120
leaq 0x108(%rsp), %rdi
callq 0x5120
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r14
leaq 0x2e8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x5340
leaq 0xb8(%rsp), %rsi
leaq 0x2c8(%rsp), %rdi
callq 0x5340
movb $0x1, %bpl
leaq 0x2e8(%rsp), %rsi
leaq 0x2c8(%rsp), %rdx
movq %r14, %rdi
callq 0x46fc0
leaq 0xd8(%rsp), %rsi
movq %r14, (%r12)
leaq 0x8(%r12), %rdi
xorl %ebp, %ebp
callq 0x5340
leaq 0x2c8(%rsp), %rdi
callq 0x5120
leaq 0x2e8(%rsp), %rdi
callq 0x5120
leaq 0x98(%rsp), %rdi
callq 0x46fa0
jmp 0x463de
leaq 0x468c0(%rip), %rdi     # 0x8c6a8
leaq 0x78(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x47106
testb %al, %al
jne 0x45e9d
movq (%r14), %rbx
leaq 0x14c4c(%rip), %rsi     # 0x5aa53
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0xfa26
leaq 0x15206(%rip), %rdx     # 0x5b023
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xfd14
leaq 0x98(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0xfda4
leaq 0x151d9(%rip), %rdx     # 0x5b023
leaq 0x2a8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0xfd14
leaq 0x2a8(%rsp), %rsi
movq %rbx, %rdi
callq 0x3da88
leaq 0x2a8(%rsp), %rdi
callq 0x5120
leaq 0x98(%rsp), %rdi
callq 0x5120
leaq 0x30(%rsp), %rdi
callq 0x5120
leaq 0x10(%rsp), %rdi
callq 0x5120
movq %r12, 0x100(%rsp)
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq %r14, 0x70(%rsp)
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x45f10
movq 0x8(%rax), %r14
movq 0x10(%rax), %r13
leaq 0xa0(%rsp), %r15
leaq 0x98(%rsp), %rbx
leaq 0x10(%rsp), %r12
cmpq %r13, %r14
je 0x45f10
movq (%r14), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x44859
movq 0x10(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x499b2
movq %r15, %rdi
callq 0x5120
addq $0x8, %r14
jmp 0x45ede
leaq 0x98(%rsp), %rdi
callq 0x47520
leaq 0x46784(%rip), %rdi     # 0x8c6a8
leaq 0x78(%rsp), %rsi
pushq $0x1
popq %rdx
leaq 0x98(%rsp), %rcx
callq 0x47574
xorps %xmm0, %xmm0
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %rcx, %rax
subq %rdi, %rax
pushq $0x28
popq %rsi
cqto
idivq %rsi
movq %rax, %r15
movq 0xe0(%rsp), %rax
subq 0xd8(%rsp), %rax
sarq $0x5, %rax
cmpq %rax, %r15
jne 0x460ae
shlq $0x5, %r15
addq $-0x20, %r15
xorl %ebp, %ebp
leaq 0x30(%rsp), %r12
leaq 0x348(%rsp), %r14
xorl %r13d, %r13d
subq %rdi, %rcx
movq %rcx, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %rax, %r13
jae 0x460f0
movq 0xd8(%rsp), %rsi
cmpq $0x5, 0x8(%rsi,%r15)
jb 0x46079
movq %r14, %rbx
addq %r15, %rsi
movq %r12, %rdi
xorl %edx, %edx
pushq $0x4
popq %rcx
callq 0x52a0
movq %r12, %rdi
leaq 0x14a0c(%rip), %rsi     # 0x5a9ed
callq 0x15c52
movl %eax, %r14d
movq %r12, %rdi
callq 0x5120
movq 0x10(%rsp), %rdi
testb %r14b, %r14b
je 0x46076
movq (%rdi,%rbp), %rax
cmpl $0x0, (%rax)
movq %rbx, %r14
je 0x4601b
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
cmpb $0x23, -0x1(%rcx,%rdx)
je 0x4605d
cmpb $0x7e, (%rcx)
jne 0x4605d
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %rax
movq (%rax), %rbx
movq %r14, %rdi
leaq 0x14af6(%rip), %rsi     # 0x5ab2f
movq %r12, %rdx
callq 0xfa26
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3da88
movq %r14, %rdi
callq 0x5120
movq 0x10(%rsp), %rax
movq (%rax,%rbp), %rax
movq %rax, 0x30(%rsp)
movb $0x1, 0x38(%rsp)
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x47932
jmp 0x46094
movq %rbx, %r14
movq (%rdi,%rbp), %rax
movq %rax, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x47932
incq %r13
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
addq $0x28, %rbp
addq $-0x20, %r15
jmp 0x45f99
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rbx
leaq 0x14a40(%rip), %rsi     # 0x5ab01
leaq 0x288(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0xfa26
leaq 0x288(%rsp), %rsi
movq %rbx, %rdi
callq 0x3da88
leaq 0x288(%rsp), %rdi
callq 0x5120
leaq 0x464d9(%rip), %rsi     # 0x8c5d0
leaq 0x30(%rsp), %rdi
callq 0x3bbee
leaq 0x1398b(%rip), %rsi     # 0x59a93
leaq 0x268(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0xfa26
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x248(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x5340
leaq 0x15164(%rip), %rsi     # 0x5b2a8
leaq 0x228(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0xfa26
movb $0x1, %bpl
leaq 0x248(%rsp), %rsi
leaq 0x228(%rsp), %rdx
movq %r15, %rdi
callq 0x46fc0
xorl %ebp, %ebp
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r12
leaq 0x208(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5340
leaq 0xb8(%rsp), %r14
leaq 0x1e8(%rsp), %rdi
movq %r14, %rsi
callq 0x5340
movb $0x1, %bpl
leaq 0x208(%rsp), %rsi
leaq 0x1e8(%rsp), %rdx
movq %r12, %rdi
callq 0x46fc0
xorl %ebp, %ebp
leaq 0x46404(%rip), %rdi     # 0x8c5d0
leaq 0x268(%rsp), %rsi
leaq 0x50(%rsp), %rcx
movq %r15, %rdx
movq %r12, %r8
callq 0x4796a
leaq 0x1e8(%rsp), %rdi
callq 0x5120
leaq 0x208(%rsp), %rdi
callq 0x5120
leaq 0x228(%rsp), %rdi
callq 0x5120
leaq 0x248(%rsp), %rdi
callq 0x5120
leaq 0x268(%rsp), %rdi
callq 0x5120
leaq 0x14932(%rip), %rsi     # 0x5ab5e
leaq 0x1c8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0xfa26
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x4637d(%rip), %rdi     # 0x8c5d0
callq 0x3bb88
movl %eax, %ebx
leaq 0x12eae(%rip), %rsi     # 0x5910f
leaq 0x1a8(%rsp), %rdi
leaq 0xd(%rsp), %rdx
callq 0xfa26
movb $0x1, %bpl
leaq 0x1a8(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
callq 0x46ac6
xorl %ebp, %ebp
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r12
leaq 0x148c8(%rip), %rsi     # 0x5ab64
leaq 0x188(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0xfa26
leaq 0x14ff3(%rip), %rsi     # 0x5b2a8
leaq 0x168(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0xfa26
movb $0x1, %r13b
leaq 0x188(%rsp), %rsi
leaq 0x168(%rsp), %rdx
movq %r12, %rdi
callq 0x46fc0
xorl %r13d, %r13d
leaq 0x462e4(%rip), %rdi     # 0x8c5d0
leaq 0x1c8(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x46a38
leaq 0x168(%rsp), %rdi
callq 0x5120
leaq 0x188(%rsp), %rdi
callq 0x5120
leaq 0x1a8(%rsp), %rdi
callq 0x5120
leaq 0x1c8(%rsp), %rdi
callq 0x5120
pushq $0x48
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x148(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5340
movq 0x100(%rsp), %r12
leaq 0x128(%rsp), %rdi
movq %r14, %rsi
callq 0x5340
movb $0x1, %bpl
leaq 0x148(%rsp), %rsi
leaq 0x128(%rsp), %rdx
movq %r15, %rdi
callq 0x46fc0
movq %r15, (%r12)
leaq 0x8(%r12), %rdi
xorl %ebp, %ebp
movq %r14, %rsi
callq 0x5340
leaq 0x128(%rsp), %rdi
callq 0x5120
leaq 0x148(%rsp), %rdi
callq 0x5120
leaq 0x30(%rsp), %rdi
callq 0x5120
leaq 0x50(%rsp), %rdi
callq 0x3ce3a
leaq 0x98(%rsp), %rdi
callq 0x4726c
leaq 0x10(%rsp), %rdi
callq 0x479f8
leaq 0x78(%rsp), %rdi
callq 0x5120
movq %r12, %rax
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x308(%rsp), %rdi
callq 0x5120
jmp 0x46412
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5120
jmp 0x46421
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5120
jmp 0x46430
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5120
jmp 0x4643f
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x5120
jmp 0x46501
jmp 0x46468
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x464e4
movq %rax, %rbx
jmp 0x464f6
jmp 0x46468
movq %rax, %rbx
jmp 0x46501
jmp 0x465e6
movq %rax, %rbx
leaq 0x288(%rsp), %rdi
jmp 0x466ef
jmp 0x46709
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
callq 0x5120
jmp 0x4649f
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x5120
jmp 0x464b1
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5120
jmp 0x464c0
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5120
jmp 0x4673f
jmp 0x465e6
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
callq 0x5120
leaq 0x2e8(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x46501
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x5270
leaq 0x98(%rsp), %rdi
callq 0x46fa0
jmp 0x4673f
movq %rax, %rbx
jmp 0x465c5
jmp 0x4651d
movq %rax, %rbx
jmp 0x465d0
jmp 0x465e6
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x465fe
movq %rax, %rbx
jmp 0x46614
jmp 0x46594
movq %rax, %rbx
movb $0x1, %r13b
jmp 0x46634
movq %rax, %rbx
jmp 0x46646
jmp 0x46554
movq %rax, %rbx
jmp 0x4666a
movq %rax, %rbx
jmp 0x46675
jmp 0x46594
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x4668f
movq %rax, %rbx
jmp 0x466a1
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x466c0
movq %rax, %rbx
jmp 0x466d2
movq %rax, %rbx
jmp 0x466dd
movq %rax, %rbx
jmp 0x466ea
jmp 0x46709
movq %rax, %rbx
jmp 0x46716
jmp 0x46725
jmp 0x465e6
movq %rax, %rbx
leaq 0x328(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x465d0
pushq $0x48
popq %rsi
movq %r14, %rdi
callq 0x5270
leaq 0x98(%rsp), %rdi
callq 0x46f8a
jmp 0x4673f
jmp 0x465e6
jmp 0x465e6
movq %rax, %rbx
jmp 0x4673f
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5120
leaq 0x148(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x466ea
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x5270
jmp 0x466ea
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
callq 0x5120
leaq 0x188(%rsp), %rdi
callq 0x5120
testb %r13b, %r13b
je 0x46651
pushq $0x48
popq %rsi
movq %r12, %rdi
callq 0x5270
xorl %ebp, %ebp
jmp 0x46658
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x46675
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x5270
leaq 0x1c8(%rsp), %rdi
jmp 0x466e5
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x5120
leaq 0x208(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x466ac
pushq $0x48
popq %rsi
movq %r12, %rdi
callq 0x5270
xorl %ebp, %ebp
jmp 0x466b3
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x5120
leaq 0x248(%rsp), %rdi
callq 0x5120
testb %bpl, %bpl
je 0x466dd
pushq $0x48
popq %rsi
movq %r15, %rdi
callq 0x5270
leaq 0x268(%rsp), %rdi
callq 0x5120
leaq 0x30(%rsp), %rdi
callq 0x5120
jmp 0x4670c
movq %rax, %rbx
leaq 0x348(%rsp), %rdi
jmp 0x466ef
jmp 0x46709
jmp 0x46709
jmp 0x46709
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x3ce3a
leaq 0x98(%rsp), %rdi
callq 0x4726c
jmp 0x46735
movq %rax, %rbx
jmp 0x46735
movq %rax, %rbx
movq %r15, %rdi
callq 0x5120
leaq 0x10(%rsp), %rdi
callq 0x479f8
leaq 0x78(%rsp), %rdi
callq 0x5120
movq %rbx, %rdi
callq 0x53f0
nop