// File: /workspace/github_c_makefile_star2_O1/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 0x23e01
testl %r13d, %r13d
je 0x23dd7
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1462a
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
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 0x23e94
movl %edx, %r12d
xorl %r14d, %r14d
cmpl $0x2, %edx
je 0x23def
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 0x272ae
cmpl $0x0, 0x50(%rbx)
je 0x23e6c
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1462a
testl %r13d, %r13d
je 0x23def
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq %r15, %rdi
callq 0x4f9f2
movq %rax, %rdi
callq 0x2b5e5
jmp 0x23def
movq 0x10(%rbx), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, (%r14)
movl 0x4(%rbx), %eax
incl %eax
xorl %ecx, %ecx
cmpl (%rbx), %eax
cmovgel %ecx, %eax
movl %eax, 0x4(%rbx)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x273b5
testl %eax, %eax
jne 0x23f99
movslq 0x34(%rbx), %rax
movl 0x38(%rbx), %ecx
cmpl %ecx, %eax
jne 0x23ed0
jmp 0x23efc
movq 0x40(%rbx), %rsi
leaq (%rax,%rax,2), %rdi
movq (%rsi,%rdi,8), %r15
movq 0x8(%rsi,%rdi,8), %r14
movl 0x10(%rsi,%rdi,8), %edx
leal 0x1(%rax), %r8d
xorl %r9d, %r9d
cmpl 0x30(%rbx), %r8d
movl 0x14(%rsi,%rdi,8), %esi
cmovll %r8d, %r9d
movl %r9d, 0x34(%rbx)
cmpl %ecx, %eax
je 0x23f78
testl %r13d, %r13d
je 0x23f48
movl %esi, -0x48(%rbp)
movq %r14, -0x30(%rbp)
movl %edx, -0x44(%rbp)
movq %rbx, -0x40(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
movq %rax, -0x38(%rbp)
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x6bf(%rip), %rsi       # 0x245fd
movq %r15, %rdi
callq 0x2454b
jmp 0x23f78
cmpl $0x3, %esi
jne 0x23f57
movq %rbx, %rdi
callq 0x23d39
jmp 0x23f69
movabsq $-0x1000000000000, %rsi # imm = 0xFFFF000000000000
movq %rbx, %rdi
callq 0x4f9f2
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22b9f
movl $0x1, %r14d
cmpl $0x0, 0x50(%rbx)
je 0x23def
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1462a
jmp 0x23def
callq 0x134a0