// File: /workspace/github_c_makefile_star2_O0/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
subq $0xc0, %rsp
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x32570
cmpl $0x0, %eax
je 0x2c903
movq -0x18(%rbp), %rdi
callq 0x2c720
movq -0x10(%rbp), %rsi
leaq 0x5b3b0(%rip), %rdi     # 0x87cac
movb $0x0, %al
callq 0x1ac40
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x2c922
movq -0x18(%rbp), %rdi
callq 0x2c720
leaq 0x5b3b4(%rip), %rdi     # 0x87cd1
callq 0x1ad60
movq -0x18(%rbp), %rdi
callq 0x32520
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x2c94f
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
movl $0x18, %edx
callq 0x2d630
movl %eax, -0x3c(%rbp)
jmp 0x2c997
jmp 0x2c951
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
movl $0x18, %edx
callq 0x2d630
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0x91(%rbp)
jne 0x2c98b
movl -0x28(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x48(%rcx), %eax
setne %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
testb $0x1, %al
jne 0x2c951
jmp 0x2c997
cmpl $0x0, -0x3c(%rbp)
je 0x2cab8
movq -0x18(%rbp), %rdi
leaq -0x10(%rbp), %rsi
movl $0x8, %edx
callq 0x32320
cmpl $0x0, %eax
je 0x2c9d3
movq -0x18(%rbp), %rdi
callq 0x2c720
movq -0x10(%rbp), %rsi
leaq 0x5b324(%rip), %rdi     # 0x87cf0
movb $0x0, %al
callq 0x1ac40
movq -0x18(%rbp), %rdi
callq 0x2c870
movq -0x18(%rbp), %rcx
cmpl 0x48(%rcx), %eax
jle 0x2cab1
cmpl $0x2, -0x1c(%rbp)
jne 0x2ca04
movq -0x18(%rbp), %rdi
callq 0x2c720
movl $0x1, -0x4(%rbp)
jmp 0x2cb86
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq %rax, -0x58(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x50(%rbp)
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movq 0x28(%rax), %rax
movl 0x48(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x1c(%rbp), %edx
movl $0x1, %eax
movl $0x3, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
leaq -0x58(%rbp), %rsi
movl $0x18, %edx
callq 0x32320
movq -0x18(%rbp), %rdi
callq 0x2c720
cmpl $0x0, -0x40(%rbp)
je 0x2caa5
movq -0x50(%rbp), %rdi
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
callq 0x7b610
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0x39200
movl $0x1, -0x4(%rbp)
jmp 0x2cb86
jmp 0x2cab3
jmp 0x2cb76
cmpl $0x0, -0x40(%rbp)
je 0x2cb2b
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rdi
leaq 0xd15(%rip), %rsi       # 0x2d810
leaq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x2d6d0
jmp 0x2cb74
cmpl $0x2, -0x24(%rbp)
jne 0x2cb65
movq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2cf20
movq -0xa0(%rbp), %rdi
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rsi
callq 0x2b160
jmp 0x2cb72
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x2b160
jmp 0x2cb74
jmp 0x2cb76
movq -0x18(%rbp), %rdi
callq 0x2c720
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)