// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c0(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x4b20
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x1c0(%rax), %rdi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x4b20
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x1e, (%rax)
je 0x19021
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x1f, (%rax)
je 0x19021
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x20, (%rax)
jne 0x1904e
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq 0x18(%rcx), %rcx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
jmp 0x19050
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x28, (%rax)
jne 0x18fdc
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x190c2
movq -0x8(%rbp), %rax
movq 0x1d0(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x1d0(%rcx), %rcx
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
je 0x19113
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x192e0
movq -0x38(%rbp), %rdi
movq %rax, %rsi
movq -0x8(%rbp), %rax
movq 0x1d0(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
leaq 0x1c0093(%rip), %rdx    # 0x1d9199
movb $0x0, %al
callq 0x3170
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x26, (%rax)
jne 0x1917c
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x192e0
movq -0x40(%rbp), %rdi
movq %rax, %rsi
movq -0x8(%rbp), %rax
movq 0x1d0(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
leaq 0x1c0038(%rip), %rdx    # 0x1d91a7
movb $0x0, %al
callq 0x3170
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x1d0(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x192e0
movl -0x64(%rbp), %edi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl %eax, %r8d
callq 0xe15b0
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x192e0
movq -0x48(%rbp), %rdi
movq %rax, %rsi
leaq 0x1bffa4(%rip), %rdx    # 0x1d91a5
movb $0x0, %al
callq 0x3170
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x25, (%rax)
je 0x19246
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
testb $0x1, (%rax,%rcx)
jne 0x19246
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
testb $0x1, (%rax,%rcx)
je 0x1927e
movq -0x10(%rbp), %rax
movslq -0x30(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl -0x14(%rbp), %eax
subl -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x192e0
movq -0x70(%rbp), %rdi
movq %rax, %rsi
leaq 0x1bff34(%rip), %rdx    # 0x1d91a5
movb $0x0, %al
callq 0x3170
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x19280
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x28, (%rax)
jne 0x19083
movq -0x8(%rbp), %rax
movq 0x1c0(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x4d80
movq -0x8(%rbp), %rax
movq 0x1c0(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x4d80
movl -0x30(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)