// File: /workspace/github_c_makefile_star2_O1/janet-lang[P]janet/src/core/specials.c
// Function: janetc_while
static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv) {
    JanetCompiler *c = opts.compiler;
    JanetSlot cond;
    JanetFopts subopts = janetc_fopts_default(c);
    JanetScope tempscope;
    int32_t labelwt, labeld, labeljt, labelc, i;
    int infinite = 0;
    int is_nil_form = 0;
    int is_notnil_form = 0;
    uint8_t ifjmp = JOP_JUMP_IF;
    uint8_t ifnjmp = JOP_JUMP_IF_NOT;

    if (argn < 1) {
        janetc_cerror(c, "expected at least 1 argument to while");
        return janetc_cslot(janet_wrap_nil());
    }

    labelwt = janet_v_count(c->buffer);

    janetc_scope(&tempscope, c, JANET_SCOPE_WHILE, "while");

    /* Check for `(= nil _)` or `(not= nil _)` in condition, and if so, use the
     * jmpnl or jmpnn instructions. This let's us implement `(each ...)`
     * more efficiently. */
    Janet condform = argv[0];
    if (janetc_check_nil_form(condform, &condform, JANET_FUN_EQ)) {
        is_nil_form = 1;
        ifjmp = JOP_JUMP_IF_NIL;
        ifnjmp = JOP_JUMP_IF_NOT_NIL;
    }
    if (janetc_check_nil_form(condform, &condform, JANET_FUN_NEQ)) {
        is_notnil_form = 1;
        ifjmp = JOP_JUMP_IF_NOT_NIL;
        ifnjmp = JOP_JUMP_IF_NIL;
    }

    /* Compile condition */
    cond = janetc_value(subopts, condform);

    /* Check for constant condition */
    if (cond.flags & JANET_SLOT_CONSTANT) {
        /* Loop never executes */
        int never_executes = is_nil_form
                             ? !janet_checktype(cond.constant, JANET_NIL)
                             : is_notnil_form
                             ? janet_checktype(cond.constant, JANET_NIL)
                             : !janet_truthy(cond.constant);
        if (never_executes) {
            janetc_popscope(c);
            return janetc_cslot(janet_wrap_nil());
        }
        /* Infinite loop */
        infinite = 1;
    }

    /* Infinite loop does not need to check condition */
    labelc = infinite
             ? 0
             : janetc_emit_si(c, ifnjmp, cond, 0, 0);

    /* Compile body */
    for (i = 1; i < argn; i++) {
        subopts.flags = JANET_FOPTS_DROP;
        janetc_freeslot(c, janetc_value(subopts, argv[i]));
    }

    /* Check if closure created in while scope. If so,
     * recompile in a function scope. */
    if (tempscope.flags & JANET_SCOPE_CLOSURE) {
        subopts = janetc_fopts_default(c);
        tempscope.flags |= JANET_SCOPE_UNUSED;
        janetc_popscope(c);
        if (c->buffer) janet_v__cnt(c->buffer) = labelwt;
        if (c->mapbuffer) janet_v__cnt(c->mapbuffer) = labelwt;

        janetc_scope(&tempscope, c, JANET_SCOPE_FUNCTION, "while-iife");

        /* Recompile in the function scope */
        cond = janetc_value(subopts, condform);
        if (!(cond.flags & JANET_SLOT_CONSTANT)) {
            /* If not an infinite loop, return nil when condition false */
            janetc_emit_si(c, ifjmp, cond, 2, 0);
            janetc_emit(c, JOP_RETURN_NIL);
        }
        for (i = 1; i < argn; i++) {
            subopts.flags = JANET_FOPTS_DROP;
            janetc_freeslot(c, janetc_value(subopts, argv[i]));
        }
        /* But now add tail recursion */
        int32_t tempself = janetc_regalloc_temp(&tempscope.ra, JANETC_REGTEMP_0);
        janetc_emit(c, JOP_LOAD_SELF | (tempself << 8));
        janetc_emit(c, JOP_TAILCALL | (tempself << 8));
        janetc_regalloc_freetemp(&c->scope->ra, tempself, JANETC_REGTEMP_0);
        /* Compile function */
        JanetFuncDef *def = janetc_pop_funcdef(c);
        def->name = janet_cstring("_while");
        janet_def_addflags(def);
        int32_t defindex = janetc_addfuncdef(c, def);
        /* And then load the closure and call it. */
        int32_t cloreg = janetc_regalloc_temp(&c->scope->ra, JANETC_REGTEMP_0);
        janetc_emit(c, JOP_CLOSURE | (cloreg << 8) | (defindex << 16));
        janetc_emit(c, JOP_CALL | (cloreg << 8) | (cloreg << 16));
        janetc_regalloc_freetemp(&c->scope->ra, cloreg, JANETC_REGTEMP_0);
        c->scope->flags |= JANET_SCOPE_CLOSURE;
        return janetc_cslot(janet_wrap_nil());
    }

    /* Compile jump to :whiletop */
    labeljt = janet_v_count(c->buffer);
    janetc_emit(c, JOP_JUMP);

    /* Calculate jumps */
    labeld = janet_v_count(c->buffer);
    if (!infinite) c->buffer[labelc] |= (uint32_t)(labeld - labelc) << 16;
    c->buffer[labeljt] |= (uint32_t)(labelwt - labeljt) << 8;

    /* Calculate breaks */
    for (int32_t i = labelwt; i < labeld; i++) {
        if (c->buffer[i] == (0x80 | JOP_JUMP)) {
            c->buffer[i] = JOP_JUMP | ((labeld - i) << 8);
        }
    }

    /* Pop scope and return nil slot */
    janetc_popscope(c);

    return janetc_cslot(janet_wrap_nil());
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdx, %r12
movl %esi, %r13d
movq %rdi, %r14
movabsq $-0x77fffffffffff, %rbx # imm = 0xFFF8800000000001
movq 0x10(%rbp), %r15
leaq -0x90(%rbp), %rdi
movq %r15, %rsi
callq 0x1bbf0
testl %r13d, %r13d
jle 0x434a8
movl %r13d, -0x34(%rbp)
movq %r14, -0x98(%rbp)
movq 0x8(%r15), %rax
movl $0x0, -0x2c(%rbp)
movl $0x0, %ecx
testq %rax, %rax
je 0x433a1
movl -0x4(%rax), %ecx
movl %ecx, -0x38(%rbp)
leaq 0x2695d(%rip), %rcx     # 0x69d08
leaq -0x170(%rbp), %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x1bf9c
movq (%r12), %rdi
leaq -0xd8(%rbp), %r13
movq %rdi, (%r13)
movq %r13, %rsi
movl $0x18, %edx
callq 0x44693
movl %eax, -0x30(%rbp)
testl %eax, %eax
setne %bl
addb %bl, %bl
leal 0x1e(%rbx), %r14d
orb $0x1d, %bl
movq (%r13), %rdi
movq %r13, %rsi
movl $0x19, %edx
callq 0x44693
movl %eax, -0x3c(%rbp)
testl %eax, %eax
movzbl %r14b, %eax
movl $0x1f, %ecx
cmovel %eax, %ecx
movl %ecx, -0x44(%rbp)
movzbl %bl, %eax
movl $0x20, %ecx
cmovnel %ecx, %eax
movl %eax, -0x40(%rbp)
movq (%r13), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x20(%rsp)
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x100(%rbp), %r13
movq %r13, %rdi
movq %rsi, -0xa0(%rbp)
callq 0x1ca05
movq 0x10(%r13), %rax
movq %rax, -0x50(%rbp)
movups (%r13), %xmm0
movaps %xmm0, -0x60(%rbp)
testb $0x1, -0x4e(%rbp)
movq -0x98(%rbp), %r14
je 0x43527
movq -0x60(%rbp), %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
movq %rax, %rdx
andq %rcx, %rdx
cmpl $0x0, -0x30(%rbp)
movabsq $-0x77fffffffffff, %r13 # imm = 0xFFF8800000000001
je 0x434c4
leaq -0x1(%r13), %rax
cmpq %rax, %rdx
setne %bl
jmp 0x434f2
leaq 0x26839(%rip), %rsi     # 0x69ce8
movq %r15, %rdi
callq 0x1bc9e
movq %rbx, %rdi
callq 0x4f95a
jmp 0x438bf
leaq -0x1(%r13), %rsi
cmpq %rsi, %rdx
sete %bl
cmpl $0x0, -0x3c(%rbp)
jne 0x434f2
cmpq %rsi, %rdx
sete %dl
incq %rcx
andq %rcx, %rax
movabsq $-0x7000000000000, %rcx # imm = 0xFFF9000000000000
cmpq %rcx, %rax
sete %bl
orb %dl, %bl
movl $0x1, -0x2c(%rbp)
testb %bl, %bl
je 0x4351f
movq %r15, %rdi
callq 0x1c045
movq %r13, %rdi
callq 0x4f95a
movq %r14, %rdi
movq %rax, %rsi
callq 0x1bc4e
movl $0x0, -0x2c(%rbp)
testb %bl, %bl
jne 0x438ca
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
jne 0x43550
movq -0x50(%rbp), %rax
movq %rax, 0x10(%rsp)
movaps -0x60(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl -0x44(%rbp), %esi
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x220f0
movl %eax, -0x30(%rbp)
cmpl $0x1, -0x34(%rbp)
je 0x435cc
movl -0x34(%rbp), %eax
cmpl $0x3, %eax
movl $0x2, %r13d
cmovgel %eax, %r13d
movl $0x1, %ebx
leaq -0xd0(%rbp), %r14
movl $0x40000, -0x70(%rbp)   # imm = 0x40000
movq (%r12,%rbx,8), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x20(%rsp)
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1ca05
movq -0xc0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xd0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1be71
incq %rbx
cmpq %rbx, %r13
jne 0x43575
testb $0x10, -0x104(%rbp)
jne 0x4367d
movq 0x8(%r15), %rax
xorl %r14d, %r14d
movl $0x0, %r12d
testq %rax, %rax
je 0x435ef
movl -0x4(%rax), %r12d
movq %r15, %rdi
movl $0x1c, %esi
callq 0x21a88
movq 0x8(%r15), %rax
testq %rax, %rax
movabsq $-0x77fffffffffff, %rbx # imm = 0xFFF8800000000001
je 0x43613
movl -0x4(%rax), %r14d
cmpl $0x0, -0x2c(%rbp)
movl -0x38(%rbp), %esi
jne 0x4362d
movl %r14d, %ecx
movl -0x30(%rbp), %edx
subl %edx, %ecx
shll $0x10, %ecx
movslq %edx, %rdx
orl %ecx, (%rax,%rdx,4)
movl %esi, %ecx
subl %r12d, %ecx
shll $0x8, %ecx
movslq %r12d, %rdx
orl %ecx, (%rax,%rdx,4)
cmpl %r14d, %esi
jge 0x43670
movslq %esi, %rcx
movslq %r14d, %rdx
shll $0x8, %r14d
shll $0x8, %esi
subl %esi, %r14d
orl $0x1c, %r14d
cmpl $0x9c, (%rax,%rcx,4)
jne 0x43661
movl %r14d, (%rax,%rcx,4)
incq %rcx
addl $0xffffff00, %r14d      # imm = 0xFFFFFF00
cmpq %rcx, %rdx
jne 0x43654
movq %r15, %rdi
callq 0x1c045
jmp 0x438b0
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1bbf0
movq 0x20(%rbx), %rax
movq %rax, -0x70(%rbp)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm1, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
orb $0x8, -0x104(%rbp)
movq %r15, %rdi
callq 0x1c045
movq 0x8(%r15), %rax
testq %rax, %rax
movl -0x38(%rbp), %ecx
je 0x436c7
movl %ecx, -0x4(%rax)
movq 0x10(%r15), %rax
testq %rax, %rax
movl -0x34(%rbp), %r13d
je 0x436d7
movl %ecx, -0x4(%rax)
leaq 0x26630(%rip), %rcx     # 0x69d0e
leaq -0x170(%rbp), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x1bf9c
movq -0x70(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x90(%rbp), %xmm0
movaps -0x80(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq -0xa0(%rbp), %rsi
callq 0x1ca05
movq 0x10(%rbx), %rax
movq %rax, -0x50(%rbp)
movups (%rbx), %xmm0
movaps %xmm0, -0x60(%rbp)
testb $0x1, -0x4e(%rbp)
jne 0x43764
movq -0x50(%rbp), %rax
movq %rax, 0x10(%rsp)
movaps -0x60(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl -0x40(%rbp), %esi
movq %r15, %rdi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x220f0
movq %r15, %rdi
movl $0x4, %esi
callq 0x21a88
cmpl $0x1, %r13d
je 0x437dc
cmpl $0x3, %r13d
movl $0x2, %r14d
cmovgel %r13d, %r14d
movl $0x1, %r13d
leaq -0xb8(%rbp), %rbx
movl $0x40000, -0x70(%rbp)   # imm = 0x40000
movq (%r12,%r13,8), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x90(%rbp), %xmm0
movaps -0x80(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1ca05
movq -0xa8(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb8(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1be71
incq %r13
cmpq %r13, %r14
jne 0x43785
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
callq 0x4129e
movl %eax, %ebx
movl %eax, %r14d
shll $0x8, %r14d
leal 0x2e(%r14), %esi
movq %r15, %rdi
callq 0x21a88
orl $0x36, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x21a88
movq (%r15), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x412f6
movq %r15, %rdi
callq 0x1ddc8
movq %rax, %rbx
leaq 0x264e9(%rip), %rdi     # 0x69d19
callq 0x4516c
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x1dd35
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4461a
movl %eax, %r14d
movq (%r15), %rdi
addq $0x30, %rdi
xorl %esi, %esi
callq 0x4129e
movl %eax, %ebx
movl %eax, %r12d
shll $0x8, %r12d
shll $0x10, %r14d
orl %r12d, %r14d
orl $0x30, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x21a88
movl %ebx, %esi
shll $0x10, %esi
orl %r12d, %esi
orl $0x35, %esi
movq %r15, %rdi
callq 0x21a88
movq (%r15), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x412f6
movq (%r15), %rax
orl $0x10, 0x6c(%rax)
movabsq $-0x77fffffffffff, %rbx # imm = 0xFFF8800000000001
movq %rbx, %rdi
callq 0x4f95a
movq -0x98(%rbp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x1bc4e
movq %r14, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq