// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r14
movq 0x1c0(%rdi), %rdi
movslq 0xc(%r14), %rsi
incq %rsi
callq 0x7a70
movq %rax, %r15
movq 0x1c0(%r14), %rdi
movslq 0xc(%r14), %rsi
incq %rsi
callq 0x7a70
movq %r14, 0x18(%rsp)
movq (%r14), %rsi
movb $0x1, %r9b
movq %rsi, %rcx
movq %r15, 0x28(%rsp)
movl (%rcx), %edi
leal -0x1e(%rdi), %r8d
cmpl $0x2, %r8d
ja 0x14809
movb %r9b, (%r15)
movslq 0x18(%rcx), %r8
movb %r9b, (%rax,%r8)
incq %r15
addq $0x20, %rcx
cmpl $0x28, %edi
jne 0x147f2
movq %rax, 0x20(%rsp)
movl %ebx, 0xc(%rsp)
xorl %r15d, %r15d
xorl %edi, %edi
xorl %ebp, %ebp
xorl %ecx, %ecx
xorl %r12d, %r12d
movq 0x18(%rsp), %rbx
movq %r13, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x1d0(%rbx), %rdx
testq %rbp, %rbp
movq %rdi, 0x38(%rsp)
je 0x14857
movq (%rdx,%rdi), %rcx
cmpq -0x18(%rdx,%rdi), %rcx
jne 0x1485a
jmp 0x1488e
movq (%rdx), %rcx
movslq %r12d, %r14
leaq (%r14,%r13), %rdi
movl 0xc(%rsp), %eax
movl %eax, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %r15d, %esi
leaq 0x628f2(%rip), %rdx     # 0x77169
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rsi
movq 0x1d0(%rbx), %rdx
movq %rbp, 0x40(%rsp)
cmpl $0x26, (%rsi,%rbp)
jne 0x148e4
movslq %r12d, %r14
leaq (%r14,%r13), %rdi
movl 0xc(%rsp), %r15d
movl %r15d, %esi
subl %r14d, %esi
testl %esi, %esi
movl $0x0, %ebp
cmovlel %ebp, %esi
movq 0x38(%rsp), %rax
movq 0x8(%rdx,%rax), %rcx
leaq 0x628b1(%rip), %rdx     # 0x77177
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq 0x18(%rsp), %rbx
movq (%rbx), %rsi
movq 0x1d0(%rbx), %rdx
jmp 0x148f0
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %rbx
xorl %ebp, %ebp
movslq %r12d, %r14
leaq (%r14,%r13), %rcx
movl %r15d, %r8d
subl %r14d, %r8d
testl %r8d, %r8d
cmovlel %ebp, %r8d
movq 0x10(%rsp), %rdi
callq 0x41894
addl %r14d, %eax
movslq %eax, %r14
leaq (%r14,%r13), %rdi
movl %r15d, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %ebp, %esi
leaq 0x6284b(%rip), %rdx     # 0x77175
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rsi
movq 0x40(%rsp), %rbp
movl (%rsi,%rbp), %eax
cmpl $0x25, %eax
movl $0x0, %r15d
je 0x1496e
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdx
cmpb $0x0, (%rcx,%rdx)
jne 0x1496e
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %rdx
cmpb $0x1, 0x1(%rcx,%rdx)
jne 0x149c3
movslq %r12d, %r14
movq 0x30(%rsp), %r13
leaq (%r14,%r13), %rdi
movl 0xc(%rsp), %eax
movl %eax, %esi
subl %r14d, %esi
testl %esi, %esi
cmovlel %r15d, %esi
leaq 0x627e5(%rip), %rdx     # 0x77175
xorl %eax, %eax
callq 0x3170
movl %eax, %r12d
addl %r14d, %r12d
movq (%rbx), %rsi
movl (%rsi,%rbp), %eax
movq 0x10(%rsp), %rcx
incq %rcx
addq $0x20, %rbp
movq 0x38(%rsp), %rdi
addq $0x18, %rdi
cmpl $0x28, %eax
jne 0x14834
jmp 0x149ca
movq 0x30(%rsp), %r13
jmp 0x149a3
movq 0x1c0(%rbx), %rdi
movq 0x28(%rsp), %rsi
callq 0x7bb0
movq 0x1c0(%rbx), %rdi
movq 0x20(%rsp), %rsi
callq 0x7bb0
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop