// File: /workspace/github_cpp_makefile_star2_O3/SunriseFox[P]very-simple-c-compiler/./src/asm.cpp
// Function: handleFuncCall(TreeNode*)
void handleFuncCall(TreeNode* node) {
    vector<ExprNode*> children;
    ExprNode* child = static_cast<ExprNode*>(node->child);
    while (child) {
        children.push_back(child);
        child = static_cast<ExprNode*>(child->sibling);
    }
    string op;
    int argNums[]{5, 4, 3, 2, 6, 7};
    bool originalRegState[14];
    for (int i = 0; i < 14; i++) {
        originalRegState[i] = regUsed[i];
    }
    for (size_t i = 0; i < children.size(); i++) {
        if (i >= 6) {
            cerr << "ignoring " << i << "th args" << endl;
            continue;
        }
        op = handleExprNode(children[i]);
        if (regUsed[argNums[i]]) {
            of << "\tpushq\t" << op << ", " << get8RegName(argNums[i]) << "\n";
        }
        regUsed[argNums[i]] = true;
        if (op[0] == '$') {;
            of << "\tmovl\t" << op << ", " << get4RegName(argNums[i]) << "\n";
        } else if (children[i]->symbol->type == Symbol::VALUE_STRING) {
            of << "\tleaq\t" << op << ", "  << get8RegName(argNums[i]) << "\n";
        } else {
            of << "\tmovq\t" << op << ", "  << get8RegName(argNums[i]) << "\n";
        }
    }
    string funcName = static_cast<DeclarationNode*>(node)->symbol->value;
    cerr << "-- funccall" << funcName << endl;
    if (funcName == "scanf")
        funcName = "__isoc99_scanf@PLT";
    else if (funcName == "printf")
        funcName = "printf@PLT";
    of << "\tcall\t" << funcName << "\n";
    for (int i = 0; i < 14; i++) {
        regUsed[i] = originalRegState[i];
        if (regUsed[i]) {
            of << "\tpop\t" << op << get8RegName(argNums[i]) << "\n";
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
je 0x4f36
leaq 0x20(%rsp), %rbx
leaq 0x68(%rsp), %r14
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x4bb8
movq %rax, (%rsi)
addq $0x8, 0x28(%rsp)
jmp 0x4bc3
movq %rbx, %rdi
movq %r14, %rdx
callq 0x7bf8
movq 0x68(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x68(%rsp)
testq %rax, %rax
jne 0x4ba1
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq 0x50(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq $0x0, -0x8(%rdx)
movq %rdx, 0x60(%rsp)
movb $0x0, (%rdx)
movq 0xe876(%rip), %rdx      # 0x13476
movq %rdx, 0x76(%rsp)
movq 0xe864(%rip), %rdx      # 0x13470
movq %rdx, 0x70(%rsp)
cmpq %rax, %rcx
je 0x4f67
movq %r15, 0xa0(%rsp)
leaq 0x8537(%rip), %r15      # 0xd160
xorl %r14d, %r14d
movq %rsp, %rbp
leaq 0xe63a(%rip), %r12      # 0x13270
cmpq $0x6, %r14
jb 0x4ca7
movl $0x9, %edx
movq 0xe3a8(%rip), %rdi      # 0x12ff0
leaq 0x86ac(%rip), %rsi      # 0xd2fb
callq 0x3260
movq 0xe395(%rip), %rdi      # 0x12ff0
movq %r14, %rsi
callq 0x3140
movq %rax, %r13
movl $0x7, %edx
movq %rax, %rdi
leaq 0x8690(%rip), %rsi      # 0xd305
callq 0x3260
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3150
jmp 0x4f0b
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x5235
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x32b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4cdb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
movslq (%r15), %r13
leaq 0xe78b(%rip), %rax      # 0x13470
cmpb $0x1, (%r13,%rax)
jne 0x4d79
movl $0x7, %edx
movq %r12, %rdi
leaq 0x860d(%rip), %rsi      # 0xd30d
callq 0x3260
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x86aa(%rip), %rsi      # 0xd3d3
callq 0x3260
movq %rbp, %rdi
movl %r13d, %esi
callq 0x39a2
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
leaq 0x87c7(%rip), %rsi      # 0xd520
callq 0x3260
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4d79
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe6f0(%rip), %rax      # 0x13470
movb $0x1, (%r13,%rax)
movq 0x40(%rsp), %rax
cmpb $0x24, (%rax)
jne 0x4e02
movl $0x6, %edx
movq %r12, %rdi
leaq 0x8576(%rip), %rsi      # 0xd315
callq 0x3260
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x860b(%rip), %rsi      # 0xd3d3
callq 0x3260
movq %rbp, %rdi
movl %r13d, %esi
callq 0x386f
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
leaq 0x8728(%rip), %rsi      # 0xd520
callq 0x3260
jmp 0x4ef0
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rax
movq 0x28(%rax), %rax
cmpl $0x4, (%rax)
jne 0x4e83
movl $0x6, %edx
movq %r12, %rdi
leaq 0x84f9(%rip), %rsi      # 0xd31c
callq 0x3260
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8587(%rip), %rsi      # 0xd3d3
callq 0x3260
movq %rbp, %rdi
movl %r13d, %esi
callq 0x39a2
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
leaq 0x86a4(%rip), %rsi      # 0xd520
callq 0x3260
jmp 0x4ef0
movl $0x6, %edx
movq %r12, %rdi
leaq 0x8491(%rip), %rsi      # 0xd323
callq 0x3260
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8518(%rip), %rsi      # 0xd3d3
callq 0x3260
movq %rbp, %rdi
movl %r13d, %esi
callq 0x39a2
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
leaq 0x8635(%rip), %rsi      # 0xd520
callq 0x3260
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4f0b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
incq %r14
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x4, %r15
cmpq %rcx, %r14
jb 0x4c36
movq 0xa0(%rsp), %r15
jmp 0x4f67
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movq %rax, 0x60(%rsp)
movb $0x0, (%rax)
movq 0xe520(%rip), %rax      # 0x13476
movq %rax, 0x76(%rsp)
movq 0xe50e(%rip), %rax      # 0x13470
movq %rax, 0x70(%rsp)
movq 0x28(%r15), %rax
leaq 0x10(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x78ae
movq 0xe062(%rip), %rdi      # 0x12ff0
leaq 0x8395(%rip), %rsi      # 0xd32a
movl $0xb, %edx
callq 0x3260
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0xe041(%rip), %rdi      # 0x12ff0
callq 0x3260
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x31d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3150
leaq 0x8351(%rip), %rsi      # 0xd336
movq %rsp, %rdi
callq 0x30a0
testl %eax, %eax
je 0x5013
leaq 0x8357(%rip), %rsi      # 0xd34f
movq %rsp, %rdi
callq 0x30a0
testl %eax, %eax
jne 0x502e
movl $0xa, %r8d
leaq 0x8345(%rip), %rcx      # 0xd356
jmp 0x5020
movl $0x12, %r8d
leaq 0x831c(%rip), %rcx      # 0xd33c
movq %rsp, %rdi
movq 0x8(%rdi), %rdx
xorl %esi, %esi
callq 0x3320
leaq 0xe23b(%rip), %rdi      # 0x13270
leaq 0x8325(%rip), %rsi      # 0xd361
movl $0x6, %edx
callq 0x3260
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0xe21a(%rip), %rdi      # 0x13270
callq 0x3260
leaq 0x84be(%rip), %rsi      # 0xd520
movl $0x1, %edx
movq %rax, %rdi
callq 0x3260
leaq 0x80ea(%rip), %rbp      # 0xd160
xorl %r14d, %r14d
leaq 0xe3f0(%rip), %r15      # 0x13470
leaq 0xe1e9(%rip), %rbx      # 0x13270
leaq 0x8492(%rip), %r12      # 0xd520
movb 0x70(%rsp,%r14), %al
movb %al, (%r14,%r15)
cmpb $0x1, %al
jne 0x5125
movl $0x5, %edx
movq %rbx, %rdi
leaq 0x82ba(%rip), %rsi      # 0xd368
callq 0x3260
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movq %rax, %r13
movl (%rbp), %esi
leaq 0x80(%rsp), %rdi
callq 0x39a2
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
movq %r13, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3260
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x5125
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3200
incq %r14
addq $0x4, %rbp
cmpq $0xe, %r14
jne 0x508e
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x5151
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x40(%rsp), %rdi
cmpq 0x60(%rsp), %rdi
je 0x516a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5181
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3200
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x51fa
jmp 0x51fa
jmp 0x51fa
jmp 0x51fa
jmp 0x51fa
jmp 0x51b0
jmp 0x51b0
jmp 0x51fa
jmp 0x51b0
jmp 0x51b0
movq %rax, %rbx
jmp 0x5216
jmp 0x51b0
jmp 0x51b0
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x51fd
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x51fd
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x51b3
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x51b3
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq 0x60(%rsp), %rdi
je 0x5216
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x522d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3200
movq %rbx, %rdi
callq 0x33d0