// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
movq %rsi, -0x40(%rbp)
cmpl $0x0, 0x50(%rdi)
je 0x216cd
movq %r15, %xmm0
ucomisd %xmm0, %xmm0
jnp 0x216cd
movq %r15, %rax
shrq $0x2f, %rax
andl $0xf, %eax
cmpl $0xf, %eax
ja 0x21870
movl $0xa007, %ecx           # imm = 0xA007
btl %eax, %ecx
jae 0x21870
cmpl $0x0, 0x4c(%rbx)
jne 0x218bb
movq %r15, -0x30(%rbp)
movl %r14d, -0x38(%rbp)
movl 0x50(%rbx), %eax
leaq 0x18(%rbx), %r12
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x2174d
leaq -0x58(%rbp), %rsi
pushq $0x18
popq %rdx
movq %r12, %rdi
callq 0x21fc7
testl %eax, %eax
je 0x21795
leaq -0x40(%rbp), %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x24c37
testl %eax, %eax
jne 0x218cf
movl 0x4(%rbx), %eax
movl 0x8(%rbx), %ecx
cmpl %ecx, %eax
movl -0x38(%rbp), %r14d
jle 0x2172b
addl (%rbx), %ecx
subl %eax, %ecx
cmpl 0x48(%rbx), %ecx
jle 0x21857
cmpl $0x2, %r14d
jne 0x217da
movq %rbx, %rdi
callq 0x21593
jmp 0x21846
leaq -0x58(%rbp), %r13
pushq $0x18
popq %r14
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x21fc7
testl %eax, %eax
jne 0x21704
movl -0x48(%rbp), %eax
movq -0x50(%rbp), %r15
cmpl 0x48(%r15), %eax
jne 0x21755
cmpl $0x2, -0x44(%rbp)
jne 0x2184b
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x21b45
movq %r15, %rdi
movq %rax, %rsi
jmp 0x21852
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
rolq $0x20, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, -0x60(%rbp)
movq %rbx, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movupd -0x78(%rbp), %xmm0
movups -0x68(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
leaq 0x903(%rip), %rsi       # 0x220d6
callq 0x22006
jmp 0x21857
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
leaq -0x78(%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,2), %eax
movl %eax, 0x14(%rsi)
leaq 0x30(%rbx), %rdi
pushq $0x18
popq %rdx
callq 0x24c37
movq %rbx, %rdi
callq 0x21593
cmpl $0x0, -0x34(%rbp)
je 0x21846
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq %r15, %rdi
callq 0x4b3ab
movq %rax, %rdi
callq 0x28c2a
pushq $0x1
popq %rax
jmp 0x21861
movq %r15, %rdi
movq -0x30(%rbp), %rsi
callq 0x209ca
movq %rbx, %rdi
callq 0x21593
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x12760
testq %rax, %rax
je 0x218e9
movq %rax, %r12
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x15e80
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0x20000, %ecx          # imm = 0x20000
callq 0x2d99f
movabsq $-0x2800000000000, %rsi # imm = 0xFFFD800000000000
movq %r12, %rdi
callq 0x4b3ab
movq %rax, %r15
movq %rax, -0x40(%rbp)
jmp 0x216cd
movq %rbx, %rdi
callq 0x21593
leaq 0x353d2(%rip), %rdi     # 0x56c9c
callq 0x177b1
movq %rbx, %rdi
callq 0x21593
leaq 0x353dd(%rip), %rdi     # 0x56cbb
movq -0x30(%rbp), %rsi
xorl %eax, %eax
callq 0x176c9
movq 0x5e700(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x2b70a(%rip), %rsi     # 0x4d004
leaq 0x34f67(%rip), %rdx     # 0x56868
movl $0x2ee, %ecx            # imm = 0x2EE
xorl %eax, %eax
callq 0x125e0
pushq $0x1
popq %rdi
callq 0x12a60