// File: /workspace/github_c_makefile_star2_O3/janet-lang[P]janet/src/core/ev.c
// Function: janet_thread_chan_cb
static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
    uint32_t sched_id = (uint32_t) msg.argi;
    JanetFiber *fiber = msg.fiber;
    int mode = msg.tag;
    JanetChannel *channel = (JanetChannel *) msg.argp;
    Janet x = msg.argj;
    janet_chan_lock(channel);
    if (fiber->sched_id == sched_id) {
        if (mode == JANET_CP_MODE_CHOICE_READ) {
            janet_assert(!janet_chan_unpack(channel, &x, 0), "packing error");
            janet_schedule(fiber, make_read_result(channel, x));
        } else if (mode == JANET_CP_MODE_CHOICE_WRITE) {
            janet_schedule(fiber, make_write_result(channel));
        } else if (mode == JANET_CP_MODE_READ) {
            janet_assert(!janet_chan_unpack(channel, &x, 0), "packing error");
            janet_schedule(fiber, x);
        } else if (mode == JANET_CP_MODE_WRITE) {
            janet_schedule(fiber, janet_wrap_channel(channel));
        } else { /* (mode == JANET_CP_MODE_CLOSE) */
            janet_schedule(fiber, janet_wrap_nil());
        }
    } else if (mode != JANET_CP_MODE_CLOSE) {
        /* Fiber has already been cancelled or resumed. */
        /* Resend event to another waiting thread, depending on mode */
        int is_read = (mode == JANET_CP_MODE_CHOICE_READ) || (mode == JANET_CP_MODE_READ);
        if (is_read) {
            JanetChannelPending reader;
            if (!janet_q_pop(&channel->read_pending, &reader, sizeof(reader))) {
                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 {
            JanetChannelPending writer;
            if (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
                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);
            }
        }
    }
    janet_chan_unlock(channel);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x10(%rbp), %r12d
movl 0x14(%rbp), %r13d
movq 0x28(%rbp), %r14
movq 0x18(%rbp), %rbx
movq 0x20(%rbp), %r15
movq %r15, -0x30(%rbp)
cmpl $0x0, 0x50(%rbx)
je 0x24250
leaq 0x58(%rbx), %rdi
callq 0x14610
cmpl %r13d, 0x48(%r14)
jne 0x24295
cmpl $0x3, %r12d
ja 0x24361
movl %r12d, %eax
leaq 0x34d46(%rip), %rcx     # 0x58fb0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x50(%rbx), %edi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x26f27
testl %eax, %eax
jne 0x24415
movq -0x30(%rbp), %rsi
movq %r14, %rdi
jmp 0x24376
cmpl $0x4, %r12d
je 0x243e1
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x24381
movslq 0x34(%rbx), %rax
cmpl 0x38(%rbx), %eax
je 0x243e1
movq 0x40(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %r14
movq 0x8(%rcx,%rdx,8), %rsi
movq 0x10(%rcx,%rdx,8), %rcx
rolq $0x20, %rcx
incl %eax
xorl %edx, %edx
cmpl 0x30(%rbx), %eax
cmovll %eax, %edx
movl %edx, 0x34(%rbx)
movq %rcx, -0x50(%rbp)
movq %rsi, -0x38(%rbp)
movq %rbx, -0x48(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %rax, -0x40(%rbp)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0xff(%rip), %rsi       # 0x24218
movq %r14, %rdi
jmp 0x243dc
movl 0x50(%rbx), %edi
leaq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x26f27
testl %eax, %eax
jne 0x24410
movq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x23bcd
jmp 0x24370
movq %rbx, %rdi
callq 0x23972
jmp 0x24370
movabsq $-0x1000000000000, %rsi # imm = 0xFFFF000000000000
movq %rbx, %rdi
callq 0x4ed7e
jmp 0x24370
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4ece6
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2283b
jmp 0x243e1
movslq 0x1c(%rbx), %rax
cmpl 0x20(%rbx), %eax
je 0x243e1
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)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x1c4(%rip), %rsi      # 0x24218
callq 0x24172
cmpl $0x0, 0x50(%rbx)
je 0x24401
addq $0x58, %rbx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1461a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1352c
callq 0x134f8