// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rdx, %r12
movl %esi, %r15d
movq %rdi, %rbx
movq 0x10(%rbp), %r14
leaq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x19f04
testl %r15d, %r15d
jle 0x3f52e
movq 0x8(%r14), %rax
testq %rax, %rax
movq %rbx, -0x40(%rbp)
movl %r15d, -0x44(%rbp)
je 0x3f54c
movl -0x4(%rax), %eax
movl %eax, -0x38(%rbp)
jmp 0x3f553
leaq 0x257b3(%rip), %rsi     # 0x64ce8
movq %r14, %rdi
callq 0x19fa9
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
jmp 0x3f9e1
movl $0x0, -0x38(%rbp)
leaq 0x257ae(%rip), %rcx     # 0x64d08
leaq -0x168(%rbp), %rdi
pushq $0x20
popq %rdx
movq %r14, %rsi
callq 0x1a294
movq (%r12), %rdi
leaq -0xd0(%rbp), %r15
movq %rdi, (%r15)
pushq $0x18
popq %rdx
movq %r15, %rsi
callq 0x40766
movl %eax, -0x2c(%rbp)
testl %eax, %eax
setne %r13b
addb %r13b, %r13b
orb $0x1d, %r13b
movq (%r15), %rdi
pushq $0x19
popq %rdx
movq %r15, %rsi
callq 0x40766
movl %eax, %ebx
testl %eax, %eax
movzbl %r13b, %r13d
pushq $0x20
popq %rax
cmovnel %eax, %r13d
movq (%r15), %rsi
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movups -0x70(%rbp), %xmm0
movups -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x90(%rbp), %r15
movq %r15, %rdi
movq %rsi, -0x98(%rbp)
callq 0x1abbf
movl 0x10(%r15), %edi
btl $0x10, %edi
jb 0x3f634
movl %edi, -0x34(%rbp)
movl %r13d, -0x30(%rbp)
cmpl $0x0, -0x2c(%rbp)
setne %al
addb %al, %al
addb $0x1e, %al
testl %ebx, %ebx
movzbl %al, %eax
pushq $0x1f
popq %rcx
cmovel %eax, %ecx
movq -0x80(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x90(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl %cl, %esi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1fe75
movl %eax, -0x2c(%rbp)
jmp 0x3f6af
movq -0x90(%rbp), %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
movq %rax, %rdx
andq %rcx, %rdx
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
leaq -0x1(%r15), %rsi
cmpl $0x0, -0x2c(%rbp)
je 0x3f66d
cmpq %rsi, %rdx
movq -0x40(%rbp), %rbx
je 0x3f6a1
jmp 0x3f9d6
testl %ebx, %ebx
movq -0x40(%rbp), %rbx
je 0x3f67f
cmpq %rsi, %rdx
jne 0x3f6a1
jmp 0x3f9d6
cmpq %rsi, %rdx
je 0x3f9d6
incq %rcx
andq %rcx, %rax
movabsq $-0x7000000000000, %rcx # imm = 0xFFF9000000000000
cmpq %rcx, %rax
je 0x3f9d6
movl %edi, -0x34(%rbp)
movl %r13d, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x44(%rbp), %r13d
pushq $0x1
popq %rbx
leaq -0xc8(%rbp), %r15
cmpq %r13, %rbx
jae 0x3f713
movl $0x40000, -0x50(%rbp)   # imm = 0x40000
movq (%r12,%rbx,8), %rsi
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x70(%rbp), %xmm0
movaps -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1abbf
movq -0xb8(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xc8(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1a177
incq %rbx
jmp 0x3f6bd
testb $0x10, -0xfc(%rbp)
jne 0x3f7b1
movq 0x8(%r14), %rax
xorl %r15d, %r15d
movl $0x0, %r12d
testq %rax, %rax
je 0x3f736
movl -0x4(%rax), %r12d
pushq $0x1c
popq %rsi
movq %r14, %rdi
callq 0x1f7c4
movq 0x8(%r14), %rax
testq %rax, %rax
movq -0x40(%rbp), %rbx
je 0x3f752
movl -0x4(%rax), %r15d
btl $0x10, -0x34(%rbp)
movl -0x38(%rbp), %esi
jb 0x3f76d
movl %r15d, %ecx
movl -0x2c(%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)
movslq %esi, %rcx
movslq %r15d, %rdx
shll $0x8, %r15d
shll $0x8, %esi
subl %esi, %r15d
orl $0x1c, %r15d
cmpq %rdx, %rcx
jge 0x3f8ef
cmpl $0x9c, (%rax,%rcx,4)
jne 0x3f7a5
movl %r15d, (%rax,%rcx,4)
incq %rcx
addl $0xffffff00, %r15d      # imm = 0xFFFFFF00
jmp 0x3f78f
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x19f04
movq 0x20(%rbx), %rax
movq %rax, -0x50(%rbp)
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm1, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
orb $0x8, -0xfc(%rbp)
movq %r14, %rdi
callq 0x1a338
movq 0x8(%r14), %rax
testq %rax, %rax
movl -0x38(%rbp), %ecx
je 0x3f7f8
movl %ecx, -0x4(%rax)
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x3f804
movl %ecx, -0x4(%rax)
leaq 0x25503(%rip), %rcx     # 0x64d0e
leaq -0x168(%rbp), %rdi
pushq $0x1
popq %rdx
movq %r14, %rsi
callq 0x1a294
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x70(%rbp), %xmm0
movaps -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq -0x98(%rbp), %rsi
callq 0x1abbf
movq 0x10(%rbx), %rax
movq %rax, -0x80(%rbp)
movups (%rbx), %xmm0
movaps %xmm0, -0x90(%rbp)
testb $0x1, -0x7e(%rbp)
jne 0x3f88e
movq -0x80(%rbp), %rax
movq %rax, 0x10(%rsp)
movaps -0x90(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl -0x30(%rbp), %esi
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x1fe75
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x1f7c4
pushq $0x1
popq %r15
leaq -0xb0(%rbp), %rbx
cmpq %r13, %r15
jae 0x3f8fc
movl $0x40000, -0x50(%rbp)   # imm = 0x40000
movq (%r12,%r15,8), %rsi
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movaps -0x70(%rbp), %xmm0
movaps -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1abbf
movq -0xa0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1a177
incq %r15
jmp 0x3f899
movq %r14, %rdi
callq 0x1a338
jmp 0x3f9ca
leaq -0x138(%rbp), %rdi
xorl %esi, %esi
callq 0x3d50a
movl %eax, %ebx
movl %eax, %r15d
shll $0x8, %r15d
leal 0x2e(%r15), %esi
movq %r14, %rdi
callq 0x1f7c4
orl $0x36, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x1f7c4
movq (%r14), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x3d58b
movq %r14, %rdi
callq 0x1bcb6
movq %rax, %rbx
leaq 0x253c9(%rip), %rdi     # 0x64d19
callq 0x411ea
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x1bc23
movq %r14, %rdi
movq %rbx, %rsi
callq 0x406c6
movl %eax, %r15d
movq (%r14), %rdi
addq $0x30, %rdi
xorl %esi, %esi
callq 0x3d50a
movl %eax, %ebx
movl %eax, %r12d
shll $0x8, %r12d
shll $0x10, %r15d
orl %r12d, %r15d
orl $0x30, %r15d
movq %r14, %rdi
movl %r15d, %esi
callq 0x1f7c4
movl %ebx, %esi
shll $0x10, %esi
orl %r12d, %esi
orl $0x35, %esi
movq %r14, %rdi
callq 0x1f7c4
movq (%r14), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x3d58b
movq (%r14), %rax
orl $0x10, 0x6c(%rax)
movq -0x40(%rbp), %rbx
movabsq $-0x77fffffffffff, %r15 # imm = 0xFFF8800000000001
jmp 0x3f9de
movq %r14, %rdi
callq 0x1a338
movq %r15, %rdi
callq 0x4b313
movq %rbx, %rdi
movq %rax, %rsi
callq 0x19f5c
movq %rbx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq