// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, -0x30(%rbp)
cmpl $0x0, 0x50(%rdi)
je 0x23725
movq %r15, %xmm0
ucomisd %xmm0, %xmm0
jnp 0x23725
movq %r15, %rax
shrq $0x2f, %rax
andl $0xf, %eax
cmpl $0xf, %eax
ja 0x238f1
movl $0xa007, %ecx           # imm = 0xA007
btl %eax, %ecx
jae 0x238f1
cmpl $0x0, 0x4c(%rbx)
jne 0x23940
movl 0x50(%rbx), %r13d
testl %r13d, %r13d
je 0x2378a
movslq 0x1c(%rbx), %rax
cmpl 0x20(%rbx), %eax
jne 0x237e1
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x26e18
testl %eax, %eax
jne 0x23954
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
jle 0x2376a
addl (%rbx), %ecx
subl %eax, %ecx
cmpl 0x48(%rbx), %ecx
jle 0x238ca
cmpl $0x2, %r14d
jne 0x2383f
movl $0x1, %r14d
jmp 0x238cd
movl 0x1c(%rbx), %ecx
movl 0x20(%rbx), %eax
cmpl %eax, %ecx
je 0x23745
leal 0x1(%rcx), %edx
cmpl 0x18(%rbx), %edx
jl 0x2379e
xorl %edx, %edx
movq 0x28(%rbx), %rsi
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rsi,%rcx,8), %r12
movl 0x10(%rsi,%rcx,8), %edi
movl 0x14(%rsi,%rcx,8), %esi
movl %edx, 0x1c(%rbx)
movl %edx, %ecx
cmpl 0x48(%r12), %edi
jne 0x23790
cmpl $0x2, %esi
jne 0x238bb
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23bcd
movq %r12, %rdi
movq %rax, %rsi
jmp 0x238c1
movq 0x28(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %rdi
movq 0x10(%rcx,%rdx,8), %rsi
rolq $0x20, %rsi
incl %eax
xorl %r8d, %r8d
cmpl 0x18(%rbx), %eax
movq 0x8(%rcx,%rdx,8), %rcx
cmovll %eax, %r8d
movl %r8d, 0x1c(%rbx)
movq %rsi, -0x50(%rbp)
movq %rcx, -0x38(%rbp)
movq %rbx, -0x48(%rbp)
movq %r15, -0x40(%rbp)
movupd -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
leaq 0x9e3(%rip), %rsi       # 0x24218
callq 0x24172
jmp 0x238ca
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
leaq -0x50(%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 0x26e18
cmpl $0x0, 0x50(%rbx)
je 0x23894
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1461a
testl %r13d, %r13d
je 0x238b3
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq %r15, %rdi
callq 0x4ed7e
movq %rax, %rdi
callq 0x2b13a
movl $0x1, %r14d
jmp 0x238df
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2283b
xorl %r14d, %r14d
cmpl $0x0, 0x50(%rbx)
je 0x238df
addq $0x58, %rbx
movq %rbx, %rdi
callq 0x1461a
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x20, %edi
callq 0x12760
testq %rax, %rax
je 0x2396d
movq %rax, %r12
movq %rax, %rdi
movl $0xa, %esi
callq 0x17618
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x20000, %ecx          # imm = 0x20000
callq 0x2ffb4
movabsq $-0x2800000000000, %rsi # imm = 0xFFFD800000000000
movq %r12, %rdi
callq 0x4ed7e
movq %rax, %r15
movq %rax, -0x30(%rbp)
jmp 0x23725
movq %rbx, %rdi
callq 0x235d1
leaq 0x3734d(%rip), %rdi     # 0x5ac9c
callq 0x191a3
movq %rbx, %rdi
callq 0x235d1
leaq 0x37358(%rip), %rdi     # 0x5acbb
movq %r15, %rsi
xorl %eax, %eax
callq 0x190bb
callq 0x13460