// File: /workspace/github_c_makefile_star2_O1/janet-lang[P]janet/src/core/ev.c
// Function: janet_channel_push_with_lock
static int janet_channel_push_with_lock(JanetChannel *channel, Janet x, int mode) {
    JanetChannelPending reader;
    int is_empty;
    if (janet_chan_pack(channel, &x)) {
        janet_chan_unlock(channel);
        janet_panicf("failed to pack value for channel: %v", x);
    }
    if (channel->closed) {
        janet_chan_unlock(channel);
        janet_panic("cannot write to closed channel");
    }
    int is_threaded = janet_chan_is_threaded(channel);
    if (is_threaded) {
        /* don't dereference fiber from another thread */
        is_empty = janet_q_pop(&channel->read_pending, &reader, sizeof(reader));
    } else {
        do {
            is_empty = janet_q_pop(&channel->read_pending, &reader, sizeof(reader));
        } while (!is_empty && (reader.sched_id != reader.fiber->sched_id));
    }
    if (is_empty) {
        /* No pending reader */
        if (janet_q_push(&channel->items, &x, sizeof(Janet))) {
            janet_chan_unlock(channel);
            janet_panicf("channel overflow: %v", x);
        } else if (janet_q_count(&channel->items) > channel->limit) {
            /* No root fiber, we are in completion on a root fiber. Don't block. */
            if (mode == 2) {
                janet_chan_unlock(channel);
                return 1;
            }
            /* Pushed successfully, but should block. */
            JanetChannelPending pending;
            pending.thread = &janet_vm;
            pending.fiber = janet_vm.root_fiber,
            pending.sched_id = janet_vm.root_fiber->sched_id,
            pending.mode = mode ? JANET_CP_MODE_CHOICE_WRITE : JANET_CP_MODE_WRITE;
            janet_q_push(&channel->write_pending, &pending, sizeof(pending));
            janet_chan_unlock(channel);
            if (is_threaded) {
                janet_gcroot(janet_wrap_fiber(pending.fiber));
            }
            return 1;
        }
    } else {
        /* Pending reader */
        if (is_threaded) {
            JanetVM *vm = reader.thread;
            JanetEVGenericMessage msg;
            msg.tag = reader.mode;
            msg.fiber = reader.fiber;
            msg.argi = (int32_t) reader.sched_id;
            msg.argp = channel;
            msg.argj = x;
            janet_ev_post_event(vm, janet_thread_chan_cb, msg);
        } else {
            if (reader.mode == JANET_CP_MODE_CHOICE_READ) {
                janet_schedule(reader.fiber, make_read_result(channel, x));
            } else {
                janet_schedule(reader.fiber, x);
            }
        }
    }
    janet_chan_unlock(channel);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movl %edx, %r14d
movq %rdi, %rbx
movq %rsi, -0x28(%rbp)
cmpl $0x0, 0x50(%rdi)
je 0x23acb
movq -0x28(%rbp), %xmm0
movq %xmm0, %rcx
shrq $0x2f, %rcx
andl $0xf, %ecx
xorl %eax, %eax
ucomisd %xmm0, %xmm0
cmovpl %ecx, %eax
cmpl $0xf, %eax
ja 0x23c84
movl $0xa007, %ecx           # imm = 0xA007
btl %eax, %ecx
jae 0x23c84
cmpl $0x0, 0x4c(%rbx)
jne 0x23d06
movl 0x50(%rbx), %r12d
testl %r12d, %r12d
je 0x23aeb
movslq 0x1c(%rbx), %rdx
movl 0x20(%rbx), %esi
cmpl %esi, %edx
jne 0x23b36
jmp 0x23b64
movl 0x1c(%rbx), %esi
movl 0x20(%rbx), %edx
movl %esi, %r8d
cmpl %edx, %esi
je 0x23b27
leal 0x1(%rsi), %r8d
cmpl 0x18(%rbx), %r8d
jl 0x23b05
xorl %r8d, %r8d
movq 0x28(%rbx), %rcx
movslq %esi, %rax
leaq (%rax,%rax,2), %r9
movq (%rcx,%r9,8), %rdi
movq 0x8(%rcx,%r9,8), %r15
movl 0x10(%rcx,%r9,8), %eax
movl 0x14(%rcx,%r9,8), %ecx
movl %r8d, 0x1c(%rbx)
cmpl %edx, %esi
je 0x23b68
movl %r8d, %esi
cmpl 0x48(%r15), %eax
jne 0x23af1
jmp 0x23ba9
movq 0x28(%rbx), %rcx
leaq (%rdx,%rdx,2), %r8
movq (%rcx,%r8,8), %rdi
movq 0x8(%rcx,%r8,8), %r15
movl 0x10(%rcx,%r8,8), %eax
leal 0x1(%rdx), %r9d
xorl %r10d, %r10d
cmpl 0x18(%rbx), %r9d
movl 0x14(%rcx,%r8,8), %ecx
cmovll %r9d, %r10d
movl %r10d, 0x1c(%rbx)
cmpl %esi, %edx
jne 0x23ba9
leaq -0x28(%rbp), %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x272ae
testl %eax, %eax
jne 0x23d1a
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
jle 0x23b8d
addl (%rbx), %ecx
subl %eax, %ecx
cmpl 0x48(%rbx), %ecx
jle 0x23ce1
cmpl $0x2, %r14d
jne 0x23c08
movl $0x1, %r14d
jmp 0x23ce4
testl %r12d, %r12d
je 0x23be8
movl %ecx, -0x48(%rbp)
movq %r15, -0x30(%rbp)
movl %eax, -0x44(%rbp)
movq %rbx, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movupd -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
leaq 0xa1f(%rip), %rsi       # 0x245fd
callq 0x2454b
jmp 0x23ce1
movq -0x28(%rbp), %rsi
cmpl $0x2, %ecx
jne 0x23cd5
movq %rbx, %rdi
callq 0x23f9e
movq %r15, %rdi
movq %rax, %rsi
jmp 0x23cd8
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 %r14d, %r14d
setne %al
leal 0x1(%rax,%rax), %eax
movl %eax, 0x14(%rsi)
leaq 0x30(%rbx), %rdi
movl $0x18, %edx
callq 0x272ae
cmpl $0x0, 0x50(%rbx)
je 0x23c5d
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1462a
testl %r12d, %r12d
je 0x23c7c
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq %r15, %rdi
callq 0x4f9f2
movq %rax, %rdi
callq 0x2b5e5
movl $0x1, %r14d
jmp 0x23cf6
movl $0x20, %edi
callq 0x12770
testq %rax, %rax
je 0x23d34
movq %rax, %r15
movq %rax, %rdi
movl $0xa, %esi
callq 0x17678
movq -0x28(%rbp), %rsi
movq %r15, %rdi
xorl %edx, %edx
movl $0x20000, %ecx          # imm = 0x20000
callq 0x30552
movabsq $-0x2800000000000, %rsi # imm = 0xFFFD800000000000
movq %r15, %rdi
callq 0x4f9f2
movq %rax, -0x28(%rbp)
jmp 0x23acb
movq %r15, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22b9f
xorl %r14d, %r14d
cmpl $0x0, 0x50(%rbx)
je 0x23cf6
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1462a
movl %r14d, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x23977
leaq 0x37f87(%rip), %rdi     # 0x5bc9c
callq 0x19263
movq %rbx, %rdi
callq 0x23977
movq -0x28(%rbp), %rsi
leaq 0x37f8e(%rip), %rdi     # 0x5bcbb
xorl %eax, %eax
callq 0x1917b
callq 0x13470