// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x58(%rsp)
testq %rax, %rax
je 0x4c12
leaq 0x40(%rsp), %rbx
leaq 0x58(%rsp), %r14
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x4bf4
movq %rax, (%rsi)
addq $0x8, 0x48(%rsp)
jmp 0x4bff
movq %rbx, %rdi
movq %r14, %rdx
callq 0x7bf6
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x58(%rsp)
testq %rax, %rax
jne 0x4bdd
movq %r15, 0x80(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xe841(%rip), %rax      # 0x13476
movq %rax, 0x8e(%rsp)
movq 0xe82c(%rip), %rax      # 0x13470
movq %rax, 0x88(%rsp)
movq 0x40(%rsp), %rax
cmpq %rax, 0x48(%rsp)
je 0x4f66
leaq 0x84fd(%rip), %r15      # 0xd160
xorl %r14d, %r14d
movq %rsp, %rbp
leaq 0xe600(%rip), %r12      # 0x13270
cmpq $0x6, %r14
jb 0x4ce1
movl $0x9, %edx
movq 0xe36e(%rip), %rdi      # 0x12ff0
leaq 0x8672(%rip), %rsi      # 0xd2fb
callq 0x3260
movq 0xe35b(%rip), %rdi      # 0x12ff0
movq %r14, %rsi
callq 0x3140
movq %rax, %r13
movl $0x7, %edx
movq %rax, %rdi
leaq 0x8656(%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 0x4f45
movq (%rax,%r14,8), %rsi
movq %rbp, %rdi
callq 0x5226
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x32b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4d15
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
movslq (%r15), %r13
leaq 0xe751(%rip), %rax      # 0x13470
cmpb $0x1, (%r13,%rax)
jne 0x4db3
movl $0x7, %edx
movq %r12, %rdi
leaq 0x85d3(%rip), %rsi      # 0xd30d
callq 0x3260
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x8670(%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 0x878d(%rip), %rsi      # 0xd520
callq 0x3260
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4db3
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
leaq 0xe6b6(%rip), %rax      # 0x13470
movb $0x1, (%r13,%rax)
movq 0x20(%rsp), %rax
cmpb $0x24, (%rax)
jne 0x4e3c
movl $0x6, %edx
movq %r12, %rdi
leaq 0x853c(%rip), %rsi      # 0xd315
callq 0x3260
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x85d1(%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 0x86ee(%rip), %rsi      # 0xd520
callq 0x3260
jmp 0x4f2a
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rax
movq 0x28(%rax), %rax
cmpl $0x4, (%rax)
jne 0x4ebd
movl $0x6, %edx
movq %r12, %rdi
leaq 0x84bf(%rip), %rsi      # 0xd31c
callq 0x3260
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x854d(%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 0x866a(%rip), %rsi      # 0xd520
callq 0x3260
jmp 0x4f2a
movl $0x6, %edx
movq %r12, %rdi
leaq 0x8457(%rip), %rsi      # 0xd323
callq 0x3260
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x3260
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x84de(%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 0x85fb(%rip), %rsi      # 0xd520
callq 0x3260
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4f45
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
incq %r14
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x4, %r15
cmpq %rcx, %r14
jb 0x4c70
movq 0x80(%rsp), %rax
movq 0x28(%rax), %rax
leaq 0x10(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x78b8
movq 0xe05b(%rip), %rdi      # 0x12ff0
leaq 0x838e(%rip), %rsi      # 0xd32a
movl $0xb, %edx
callq 0x3260
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0xe03a(%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 0x834a(%rip), %rsi      # 0xd336
movq %rsp, %rdi
callq 0x30a0
testl %eax, %eax
je 0x501a
leaq 0x8350(%rip), %rsi      # 0xd34f
movq %rsp, %rdi
callq 0x30a0
testl %eax, %eax
jne 0x5035
movl $0xa, %r8d
leaq 0x833e(%rip), %rcx      # 0xd356
jmp 0x5027
movl $0x12, %r8d
leaq 0x8315(%rip), %rcx      # 0xd33c
movq %rsp, %rdi
movq 0x8(%rdi), %rdx
xorl %esi, %esi
callq 0x3320
leaq 0xe234(%rip), %rdi      # 0x13270
leaq 0x831e(%rip), %rsi      # 0xd361
movl $0x6, %edx
callq 0x3260
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0xe213(%rip), %rdi      # 0x13270
callq 0x3260
leaq 0x84b7(%rip), %rsi      # 0xd520
movl $0x1, %edx
movq %rax, %rdi
callq 0x3260
leaq 0x80e3(%rip), %rbp      # 0xd160
xorl %r14d, %r14d
leaq 0xe3e9(%rip), %r15      # 0x13470
leaq 0xe1e2(%rip), %rbx      # 0x13270
leaq 0x848b(%rip), %r12      # 0xd520
movb 0x88(%rsp,%r14), %al
movb %al, (%r14,%r15)
cmpb $0x1, %al
jne 0x5119
movl $0x5, %edx
movq %rbx, %rdi
leaq 0x82b4(%rip), %rsi      # 0xd368
callq 0x3260
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rbx, %rdi
callq 0x3260
movq %rax, %r13
movl (%rbp), %esi
leaq 0x60(%rsp), %rdi
callq 0x39a2
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %r13, %rdi
callq 0x3260
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x3260
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x5119
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3200
incq %r14
addq $0x4, %rbp
cmpq $0xe, %r14
jne 0x5095
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x5145
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x5161
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x5178
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3200
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x51e8
jmp 0x51e8
jmp 0x51e8
jmp 0x51e8
jmp 0x51e8
jmp 0x51a7
jmp 0x51a7
movq %rax, %rbx
jmp 0x5207
jmp 0x51a7
jmp 0x51e8
jmp 0x51a7
jmp 0x51a7
jmp 0x51a7
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x51eb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x51eb
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x51aa
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x51aa
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdi
cmpq %rax, %rdi
je 0x5207
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x521e
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x3200
movq %rbx, %rdi
callq 0x33d0