// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %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)
movq %rbx, %rdi
callq 0x2157d
cmpl %r13d, 0x48(%r14)
jne 0x2214c
cmpl $0x3, %r12d
ja 0x22202
movl %r12d, %eax
leaq 0x32e8f(%rip), %rcx     # 0x54fb0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x24d89
testl %eax, %eax
jne 0x222ab
movq -0x30(%rbp), %rsi
movq %r14, %rdi
jmp 0x22217
cmpl $0x4, %r12d
je 0x2221c
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x22232
leaq 0x30(%rbx), %rdi
leaq -0x68(%rbp), %rsi
pushq $0x18
popq %rdx
callq 0x21fc7
testl %eax, %eax
jne 0x2221c
movq -0x68(%rbp), %r14
movq -0x58(%rbp), %rax
rolq $0x20, %rax
movq -0x60(%rbp), %rcx
movq %rax, -0x50(%rbp)
movq %rcx, -0x38(%rbp)
movq %rbx, -0x48(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4b313
movq %rax, -0x40(%rbp)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0xec(%rip), %rsi       # 0x220d6
movq %r14, %rdi
jmp 0x2227e
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x24d89
testl %eax, %eax
jne 0x22285
movq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x21b45
jmp 0x22211
movq %rbx, %rdi
callq 0x21915
jmp 0x22211
movq %rbx, %rdi
callq 0x24d75
jmp 0x22211
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4b313
movq %r14, %rdi
movq %rax, %rsi
callq 0x209ca
movq %rbx, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21593
leaq 0x18(%rbx), %rdi
leaq -0x68(%rbp), %rsi
pushq $0x18
popq %rdx
callq 0x21fc7
testl %eax, %eax
jne 0x2221c
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
rolq $0x20, %rcx
movq %rcx, -0x50(%rbp)
movq %rax, -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 -0x1a8(%rip), %rsi      # 0x220d6
callq 0x22006
jmp 0x2221c
movq 0x5dd64(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x2dd7a(%rip), %rsi     # 0x50010
leaq 0x345cb(%rip), %rcx     # 0x56868
leaq 0x34a40(%rip), %r8      # 0x56ce4
movl $0x38a, %edx            # imm = 0x38A
jmp 0x222cf
movq 0x5dd3e(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x2dd54(%rip), %rsi     # 0x50010
leaq 0x345a5(%rip), %rcx     # 0x56868
leaq 0x34a1a(%rip), %r8      # 0x56ce4
movl $0x38f, %edx            # imm = 0x38F
xorl %eax, %eax
callq 0x125e0
callq 0x12100