// File: /workspace/github_c_makefile_star2_O3/janet-lang[P]janet/src/core/ev.c
// Function: janet_channel_pop_with_lock
static int janet_channel_pop_with_lock(JanetChannel *channel, Janet *item, int is_choice) {
    JanetChannelPending writer;
    if (channel->closed) {
        janet_chan_unlock(channel);
        *item = janet_wrap_nil();
        return 1;
    }
    int is_threaded = janet_chan_is_threaded(channel);
    if (janet_q_pop(&channel->items, item, sizeof(Janet))) {
        /* Queue empty */
        if (is_choice == 2) return 0; // Skip pending read
        JanetChannelPending pending;
        pending.thread = &janet_vm;
        pending.fiber = janet_vm.root_fiber,
        pending.sched_id = janet_vm.root_fiber->sched_id;
        pending.mode = is_choice ? JANET_CP_MODE_CHOICE_READ : JANET_CP_MODE_READ;
        janet_q_push(&channel->read_pending, &pending, sizeof(pending));
        janet_chan_unlock(channel);
        if (is_threaded) {
            janet_gcroot(janet_wrap_fiber(pending.fiber));
        }
        return 0;
    }
    janet_assert(!janet_chan_unpack(channel, item, 0), "bad channel packing");
    if (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
        /* Pending writer */
        if (is_threaded) {
            JanetVM *vm = writer.thread;
            JanetEVGenericMessage msg;
            msg.tag = writer.mode;
            msg.fiber = writer.fiber;
            msg.argi = (int32_t) writer.sched_id;
            msg.argp = channel;
            msg.argj = janet_wrap_nil();
            janet_ev_post_event(vm, janet_thread_chan_cb, msg);
        } else {
            if (writer.mode == JANET_CP_MODE_CHOICE_WRITE) {
                janet_schedule(writer.fiber, make_write_result(channel));
            } else {
                janet_schedule(writer.fiber, janet_wrap_abstract(channel));
            }
        }
    }
    janet_chan_unlock(channel);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x0, 0x4c(%rdi)
movl 0x50(%rdi), %r13d
je 0x23a3a
testl %r13d, %r13d
je 0x23a10
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1461a
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %rax, (%r14)
movl $0x1, %r14d
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq 0x4(%rbx), %rax
cmpl 0x8(%rbx), %eax
jne 0x23acd
movl %edx, %r12d
xorl %r14d, %r14d
cmpl $0x2, %edx
je 0x23a28
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
leaq -0x48(%rbp), %rsi
movq %rax, (%rsi)
movq 0x28(%rax), %r15
movq %r15, 0x8(%rsi)
movl 0x48(%r15), %eax
movl %eax, 0x10(%rsi)
xorl %eax, %eax
testl %r12d, %r12d
setne %al
addl %eax, %eax
movl %eax, 0x14(%rsi)
leaq 0x18(%rbx), %rdi
movl $0x18, %edx
callq 0x26e18
cmpl $0x0, 0x50(%rbx)
je 0x23aa5
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1461a
testl %r13d, %r13d
je 0x23a28
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq %r15, %rdi
callq 0x4ed7e
movq %rax, %rdi
callq 0x2b13a
jmp 0x23a28
movq 0x10(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, (%r14)
movl 0x4(%rbx), %eax
incl %eax
xorl %ecx, %ecx
cmpl (%rbx), %eax
movl 0x50(%rbx), %edi
cmovgel %ecx, %eax
movl %eax, 0x4(%rbx)
movq %r14, %rsi
xorl %edx, %edx
callq 0x26f27
testl %eax, %eax
jne 0x23bc8
movslq 0x34(%rbx), %rcx
cmpl 0x38(%rbx), %ecx
je 0x23ba7
movq 0x40(%rbx), %rdx
leaq (%rcx,%rcx,2), %rsi
movq (%rdx,%rsi,8), %r15
movq 0x8(%rdx,%rsi,8), %r14
movl 0x10(%rdx,%rsi,8), %eax
movl 0x14(%rdx,%rsi,8), %edx
incl %ecx
xorl %esi, %esi
cmpl 0x30(%rbx), %ecx
cmovll %ecx, %esi
movl %esi, 0x34(%rbx)
testl %r13d, %r13d
je 0x23b77
movl %edx, -0x48(%rbp)
movq %r14, -0x30(%rbp)
movl %eax, -0x44(%rbp)
movq %rbx, -0x40(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %rax, -0x38(%rbp)
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x6ab(%rip), %rsi       # 0x24218
movq %r15, %rdi
callq 0x24172
jmp 0x23ba7
cmpl $0x3, %edx
jne 0x23b86
movq %rbx, %rdi
callq 0x23972
jmp 0x23b98
movabsq $-0x1000000000000, %rsi # imm = 0xFFFF000000000000
movq %rbx, %rdi
callq 0x4ed7e
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2283b
movl $0x1, %r14d
cmpl $0x0, 0x50(%rbx)
je 0x23a28
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1461a
jmp 0x23a28
callq 0x13490