// File: /workspace/github_cpp_makefile_star2_O2/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
leaq 0x30(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
leaq 0x18(%rdi), %rax
leaq 0x28(%rsp), %r14
movq (%rax), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x4456
movq %rbx, %rdi
movq %r14, %rsi
callq 0x62cc
movq 0x28(%rsp), %rax
addq $0x20, %rax
jmp 0x4433
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %eax, %eax
cmpq $0xe, %rax
je 0x4482
leaq 0xdff2(%rip), %rcx      # 0x12468
movb (%rax,%rcx), %cl
movb %cl, 0x7a(%rsp,%rax)
incq %rax
jmp 0x4469
movq %r15, 0x50(%rsp)
leaq 0x6cc2(%rip), %r13      # 0xb150
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbp
leaq 0x58(%rsp), %rbx
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r14
jae 0x46c5
cmpq $0x6, %r14
jb 0x44f5
movq 0xdb2e(%rip), %rdi      # 0x11ff0
leaq 0x6e22(%rip), %rsi      # 0xb2eb
callq 0x31d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3140
movq %rax, %rdi
leaq 0x6e12(%rip), %rsi      # 0xb2f5
callq 0x31d0
movq %rax, %rdi
callq 0x3100
jmp 0x46b9
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x488e
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3280
movq %rbp, %rdi
callq 0x30f0
movslq (%r13), %r15
leaq 0xdf49(%rip), %rax      # 0x12468
cmpb $0x1, (%r15,%rax)
jne 0x4583
leaq 0xdd3b(%rip), %rdi      # 0x12268
leaq 0x6dc9(%rip), %rsi      # 0xb2fd
callq 0x31d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6e75(%rip), %rsi      # 0xb3c3
callq 0x31d0
movq %rax, %r12
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3922
movq %r12, %rdi
movq %rbp, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6f9a(%rip), %rsi      # 0xb510
callq 0x31d0
movq %rbp, %rdi
callq 0x30f0
leaq 0xdede(%rip), %rax      # 0x12468
movb $0x1, (%r15,%rax)
movq 0x58(%rsp), %rax
cmpb $0x24, (%rax)
jne 0x45f3
leaq 0xdcc8(%rip), %rdi      # 0x12268
leaq 0x6d5e(%rip), %rsi      # 0xb305
callq 0x31d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6e02(%rip), %rsi      # 0xb3c3
callq 0x31d0
movq %rax, %r12
movq %rbp, %rdi
movl %r15d, %esi
callq 0x380f
movq %r12, %rdi
movq %rbp, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6f27(%rip), %rsi      # 0xb510
callq 0x31d0
jmp 0x46b1
movq 0x30(%rsp), %rax
movq (%rax,%r14,8), %rax
movq 0x28(%rax), %rax
cmpl $0x4, (%rax)
jne 0x465c
leaq 0xdc5c(%rip), %rdi      # 0x12268
leaq 0x6cf9(%rip), %rsi      # 0xb30c
callq 0x31d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6d96(%rip), %rsi      # 0xb3c3
callq 0x31d0
movq %rax, %r12
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3922
movq %r12, %rdi
movq %rbp, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6ebb(%rip), %rsi      # 0xb510
callq 0x31d0
jmp 0x46b1
leaq 0xdc05(%rip), %rdi      # 0x12268
leaq 0x6ca9(%rip), %rsi      # 0xb313
callq 0x31d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6d3f(%rip), %rsi      # 0xb3c3
callq 0x31d0
movq %rax, %r12
movq %rbp, %rdi
movl %r15d, %esi
callq 0x3922
movq %r12, %rdi
movq %rbp, %rsi
callq 0x31c0
movq %rax, %rdi
leaq 0x6e64(%rip), %rsi      # 0xb510
callq 0x31d0
movq %rbp, %rdi
callq 0x30f0
incq %r14
addq $0x4, %r13
jmp 0x449b
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rsi
addq $0x8, %rsi
leaq 0x8(%rsp), %rdi
callq 0x32b0
movq 0xd90d(%rip), %rdi      # 0x11ff0
leaq 0x6c30(%rip), %rsi      # 0xb31a
callq 0x31d0
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
callq 0x3100
leaq 0x6c1b(%rip), %rsi      # 0xb326
leaq 0x8(%rsp), %rdi
callq 0x62e9
testb %al, %al
je 0x4722
leaq 0x6c0c(%rip), %rsi      # 0xb32c
jmp 0x473e
leaq 0x6c16(%rip), %rsi      # 0xb33f
leaq 0x8(%rsp), %rdi
callq 0x62e9
testb %al, %al
je 0x4748
leaq 0x6c08(%rip), %rsi      # 0xb346
leaq 0x8(%rsp), %rdi
callq 0x33c0
leaq 0xdb19(%rip), %rdi      # 0x12268
leaq 0x6bfb(%rip), %rsi      # 0xb351
callq 0x31d0
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x31c0
leaq 0x6da1(%rip), %rsi      # 0xb510
movq %rax, %rdi
callq 0x31d0
leaq 0x69d2(%rip), %rbx      # 0xb150
xorl %r14d, %r14d
leaq 0x58(%rsp), %r15
leaq 0x88(%rsp), %r12
leaq 0x6d7b(%rip), %r13      # 0xb510
cmpq $0xe, %r14
je 0x4801
movb 0x7a(%rsp,%r14), %al
leaq 0xdcc1(%rip), %rcx      # 0x12468
movb %al, (%r14,%rcx)
cmpb $0x1, %al
jne 0x47f8
leaq 0xdab2(%rip), %rdi      # 0x12268
leaq 0x6b9b(%rip), %rsi      # 0xb358
callq 0x31d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x31c0
movq %rax, %rbp
movl (%rbx), %esi
movq %r12, %rdi
callq 0x3922
movq %rbp, %rdi
movq %r12, %rsi
callq 0x31c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x31d0
movq %r12, %rdi
callq 0x30f0
incq %r14
addq $0x4, %rbx
jmp 0x4795
leaq 0x8(%rsp), %rdi
callq 0x30f0
leaq 0x58(%rsp), %rdi
callq 0x30f0
leaq 0x30(%rsp), %rdi
callq 0x6776
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x486f
jmp 0x486f
jmp 0x486f
jmp 0x4849
jmp 0x486f
jmp 0x4849
jmp 0x486f
jmp 0x4849
jmp 0x4849
jmp 0x4849
jmp 0x486f
jmp 0x4849
movq %rax, %rbx
jmp 0x485e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x30f0
leaq 0x8(%rsp), %rdi
callq 0x30f0
jmp 0x4872
movq %rax, %rbx
jmp 0x487c
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x30f0
leaq 0x30(%rsp), %rdi
callq 0x6776
movq %rbx, %rdi
callq 0x3390