// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_gen.c
// Function: genAsm
int genAsm(CodeGen *gen, char *buf, int size)
{
    bool *jumpFrom = storageAdd(gen->storage, gen->capacity + 1);
    bool *jumpTo = storageAdd(gen->storage, gen->capacity + 1);

    int ip = 0;
    do
    {
        if (gen->code[ip].opcode == OP_GOTO || gen->code[ip].opcode == OP_GOTO_IF || gen->code[ip].opcode == OP_GOTO_IF_NOT)
        {
            jumpFrom[ip] = true;
            jumpTo[(int)gen->code[ip].operand.intVal] = true;
        }
    } while (gen->code[ip++].opcode != OP_HALT);

    ip = 0;
    int chars = 0;
    do
    {
        if (ip == 0 || gen->debugPerInstr[ip].fileName != gen->debugPerInstr[ip - 1].fileName)
            chars += snprintf(buf + chars, nonneg(size - chars), "\nModule: %s\n\n", gen->debugPerInstr[ip].fileName);

        if (gen->code[ip].opcode == OP_ENTER_FRAME)
            chars += snprintf(buf + chars, nonneg(size - chars), "\nFunction: %s\n\n", gen->debugPerInstr[ip].fnName);

        chars += vmAsm(ip, gen->code, gen->debugPerInstr, buf + chars, nonneg(size - chars));
        chars += snprintf(buf + chars, nonneg(size - chars), "\n");

        if (gen->code[ip].opcode == OP_RETURN || jumpFrom[ip] || jumpTo[ip + 1])
            chars += snprintf(buf + chars, nonneg(size - chars), "\n");

    } while (gen->code[ip++].opcode != OP_HALT);

    storageRemove(gen->storage, jumpFrom);
    storageRemove(gen->storage, jumpTo);

    return chars;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x1c0(%rdi), %rdi
movslq 0xc(%rbx), %rsi
incq %rsi
callq 0xcca8
movq %rax, %r14
movq 0x1c0(%rbx), %rdi
movslq 0xc(%rbx), %rsi
incq %rsi
callq 0xcca8
movq (%rbx), %r8
movb $0x1, %cl
movq %r14, 0x10(%rsp)
movl (%r8), %esi
leal -0x1e(%rsi), %edi
cmpl $0x2, %edi
ja 0x19841
movb %cl, (%r14)
movslq 0x18(%r8), %rdi
movb %cl, (%rax,%rdi)
incq %r14
addq $0x20, %r8
cmpl $0x28, %esi
jne 0x1982c
movq %rax, 0x8(%rsp)
movl %ebp, 0x4(%rsp)
xorl %r13d, %r13d
xorl %edx, %edx
xorl %ebp, %ebp
movq $0x0, 0x20(%rsp)
xorl %r12d, %r12d
movq %r15, 0x18(%rsp)
testq %rbp, %rbp
movq %rdx, 0x28(%rsp)
je 0x1988a
movq 0x1d0(%rbx), %rax
movq (%rax,%rdx), %rcx
cmpq -0x18(%rax,%rdx), %rcx
je 0x198bf
movslq %r12d, %r14
leaq (%r15,%r14), %rdi
movl 0x4(%rsp), %eax
movl %eax, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %r13d, %esi
movq 0x1d0(%rbx), %rax
movq (%rax,%rdx), %rcx
leaq 0x5e8b7(%rip), %rdx     # 0x78169
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rax
movq %rbp, 0x30(%rsp)
cmpl $0x26, (%rax,%rbp)
jne 0x19910
movslq %r12d, %r14
leaq (%r15,%r14), %rdi
movl 0x4(%rsp), %r13d
movl %r13d, %esi
subl %r14d, %esi
testl %esi, %esi
movl $0x0, %ebp
cmovlel %ebp, %esi
movq 0x1d0(%rbx), %rax
movq 0x28(%rsp), %rcx
movq 0x8(%rax,%rcx), %rcx
leaq 0x5e876(%rip), %rdx     # 0x78177
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
jmp 0x19917
movl 0x4(%rsp), %r13d
xorl %ebp, %ebp
movq (%rbx), %rsi
movq 0x1d0(%rbx), %rdx
movslq %r12d, %r14
leaq (%r15,%r14), %rcx
movl %r13d, %r8d
subl %r14d, %r8d
testl %r8d, %r8d
cmovlel %ebp, %r8d
movq 0x20(%rsp), %r12
movl %r12d, %edi
callq 0x44d02
addl %r14d, %eax
movslq %eax, %r14
leaq (%r15,%r14), %rdi
movl %r13d, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %ebp, %esi
movq %r12, %rbp
leaq 0x5e814(%rip), %rdx     # 0x78175
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rax
movq 0x30(%rsp), %rcx
cmpl $0x25, (%rax,%rcx)
movl $0x0, %r13d
je 0x19999
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0x19999
movq 0x8(%rsp), %rax
cmpb $0x1, 0x1(%rax,%rbp)
jne 0x199f1
movslq %r12d, %r14
movq 0x18(%rsp), %r15
leaq (%r15,%r14), %rdi
movl 0x4(%rsp), %eax
movl %eax, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %r13d, %esi
leaq 0x5e7ba(%rip), %rdx     # 0x78175
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rax
incq %rbp
movq %rbp, 0x20(%rsp)
movq 0x28(%rsp), %rdx
addq $0x18, %rdx
movq 0x30(%rsp), %rcx
cmpl $0x28, (%rax,%rcx)
leaq 0x20(%rcx), %rbp
jne 0x1986e
jmp 0x199f8
movq 0x18(%rsp), %r15
jmp 0x199c8
movq 0x1c0(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0xcde8
movq 0x1c0(%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0xcde8
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq