// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movq %rdi, %rbx
movabsq $-0x77fffffffffff, %r14 # imm = 0xFFF8800000000001
movq 0x10(%rbp), %r15
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq 0x1ba98
testl %r13d, %r13d
jle 0x42ac6
movq 0x8(%r15), %rax
testq %rax, %rax
movq %rbx, -0x40(%rbp)
movl %r13d, -0x34(%rbp)
je 0x42ada
movl -0x4(%rax), %eax
movl %eax, -0x38(%rbp)
jmp 0x42ae1
leaq 0x261fb(%rip), %rsi     # 0x68cc8
movq %r15, %rdi
callq 0x1bb46
jmp 0x42fa0
movl $0x0, -0x38(%rbp)
leaq 0x26200(%rip), %rcx     # 0x68ce8
leaq -0x168(%rbp), %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x1be43
movq (%r12), %rdi
leaq -0xd0(%rbp), %r13
movq %rdi, (%r13)
movq %r13, %rsi
movl $0x18, %edx
callq 0x43d7b
movl %eax, -0x2c(%rbp)
testl %eax, %eax
setne %r14b
addb %r14b, %r14b
orb $0x1d, %r14b
movq (%r13), %rdi
movq %r13, %rsi
movl $0x19, %edx
callq 0x43d7b
movl %eax, %ebx
testl %eax, %eax
movzbl %r14b, %eax
movl $0x20, %ecx
cmovnel %ecx, %eax
movl %eax, -0x44(%rbp)
movq (%r13), %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), %r13
movq %r13, %rdi
movq %rsi, -0x98(%rbp)
callq 0x1c7be
movl 0x10(%r13), %r8d
btl $0x10, %r8d
jb 0x42bd2
movl %r8d, -0x30(%rbp)
cmpl $0x0, -0x2c(%rbp)
setne %al
addb %al, %al
addb $0x1e, %al
testl %ebx, %ebx
movzbl %al, %eax
movl $0x1f, %ecx
cmovel %eax, %ecx
movq -0x80(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x90(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl %cl, %esi
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21d94
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %edi
jmp 0x42c4d
movq -0x90(%rbp), %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
movq %rax, %rdx
andq %rcx, %rdx
movabsq $-0x77fffffffffff, %r14 # imm = 0xFFF8800000000001
leaq -0x1(%r14), %rsi
cmpl $0x0, -0x2c(%rbp)
movl -0x34(%rbp), %edi
je 0x42c0e
cmpq %rsi, %rdx
movq -0x40(%rbp), %rbx
je 0x42c42
jmp 0x42f98
testl %ebx, %ebx
movq -0x40(%rbp), %rbx
je 0x42c20
cmpq %rsi, %rdx
jne 0x42c42
jmp 0x42f98
cmpq %rsi, %rdx
je 0x42f98
incq %rcx
andq %rcx, %rax
movabsq $-0x7000000000000, %rcx # imm = 0xFFF9000000000000
cmpq %rcx, %rax
je 0x42f98
movl %r8d, -0x30(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x1, %edi
je 0x42cb6
movl -0x34(%rbp), %ebx
movl $0x1, %r13d
leaq -0xc8(%rbp), %r14
movl $0x40000, -0x50(%rbp)   # imm = 0x40000
movq (%r12,%r13,8), %rsi
movq -0x50(%rbp), %rax
movq %rax, 0x20(%rsp)
movups -0x70(%rbp), %xmm0
movups -0x60(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x1c7be
movq -0xb8(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xc8(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1bd19
incq %r13
cmpq %r13, %rbx
jne 0x42c62
testb $0x10, -0xfc(%rbp)
jne 0x42d62
movq 0x8(%r15), %rax
xorl %r14d, %r14d
movl $0x0, %r12d
testq %rax, %rax
je 0x42cd9
movl -0x4(%rax), %r12d
movq %r15, %rdi
movl $0x1c, %esi
callq 0x21728
movq 0x8(%r15), %rax
testq %rax, %rax
movq -0x40(%rbp), %rbx
je 0x42cf7
movl -0x4(%rax), %r14d
btl $0x10, -0x30(%rbp)
movl -0x38(%rbp), %esi
jb 0x42d12
movl %r14d, %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)
cmpl %r14d, %esi
jge 0x42d55
movslq %esi, %rcx
movslq %r14d, %rdx
shll $0x8, %r14d
shll $0x8, %esi
subl %esi, %r14d
orl $0x1c, %r14d
cmpl $0x9c, (%rax,%rcx,4)
jne 0x42d46
movl %r14d, (%rax,%rcx,4)
incq %rcx
addl $0xffffff00, %r14d      # imm = 0xFFFFFF00
cmpq %rcx, %rdx
jne 0x42d39
movq %r15, %rdi
callq 0x1beec
jmp 0x42f8c
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1ba98
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 %r15, %rdi
callq 0x1beec
movq 0x8(%r15), %rax
testq %rax, %rax
movl -0x38(%rbp), %ecx
je 0x42da9
movl %ecx, -0x4(%rax)
movq 0x10(%r15), %rax
testq %rax, %rax
movl -0x34(%rbp), %r13d
je 0x42db9
movl %ecx, -0x4(%rax)
leaq 0x25f2e(%rip), %rcx     # 0x68cee
leaq -0x168(%rbp), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x1be43
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 0x1c7be
movq 0x10(%rbx), %rax
movq %rax, -0x80(%rbp)
movups (%rbx), %xmm0
movaps %xmm0, -0x90(%rbp)
testb $0x1, -0x7e(%rbp)
jne 0x42e49
movq -0x80(%rbp), %rax
movq %rax, 0x10(%rsp)
movaps -0x90(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl -0x44(%rbp), %esi
movq %r15, %rdi
movl $0x2, %edx
xorl %ecx, %ecx
callq 0x21d94
movq %r15, %rdi
movl $0x4, %esi
callq 0x21728
cmpl $0x1, %r13d
je 0x42ebe
cmpl $0x3, %r13d
movl $0x2, %r14d
cmovgel %r13d, %r14d
movl $0x1, %r13d
leaq -0xb0(%rbp), %rbx
movl $0x40000, -0x50(%rbp)   # imm = 0x40000
movq (%r12,%r13,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 0x1c7be
movq -0xa0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x1bd19
incq %r13
cmpq %r13, %r14
jne 0x42e6a
leaq -0x138(%rbp), %rdi
xorl %esi, %esi
callq 0x40b28
movl %eax, %ebx
movl %eax, %r14d
shll $0x8, %r14d
leal 0x2e(%r14), %esi
movq %r15, %rdi
callq 0x21728
orl $0x36, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x21728
movq (%r15), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x40b80
movq %r15, %rdi
callq 0x1da6f
movq %rax, %rbx
leaq 0x25de7(%rip), %rdi     # 0x68cf9
callq 0x4482c
movq %rax, 0x48(%rbx)
movq %rbx, %rdi
callq 0x1d9dc
movq %r15, %rdi
movq %rbx, %rsi
callq 0x43d00
movl %eax, %r14d
movq (%r15), %rdi
addq $0x30, %rdi
xorl %esi, %esi
callq 0x40b28
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 0x21728
movl %ebx, %esi
shll $0x10, %esi
orl %r12d, %esi
orl $0x35, %esi
movq %r15, %rdi
callq 0x21728
movq (%r15), %rdi
addq $0x30, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x40b80
movq (%r15), %rax
orl $0x10, 0x6c(%rax)
movq -0x40(%rbp), %rbx
movabsq $-0x77fffffffffff, %r14 # imm = 0xFFF8800000000001
jmp 0x42fa0
movq %r15, %rdi
callq 0x1beec
movq %r14, %rdi
callq 0x4ece6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1baf6
movq %rbx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq