// File: /workspace/github_c_makefile_star2_O1/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 0x24635
leaq 0x58(%rbx), %rdi
callq 0x14620
cmpl %r13d, 0x48(%r14)
jne 0x24677
cmpl $0x3, %r12d
ja 0x246dc
movl %r12d, %eax
leaq 0x35961(%rip), %rcx     # 0x59fb0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x273b5
testl %eax, %eax
jne 0x24819
movq -0x30(%rbp), %rsi
movq %r14, %rdi
jmp 0x246f1
cmpl $0x4, %r12d
je 0x247e5
testl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
je 0x246ff
movslq 0x34(%rbx), %rax
movl 0x38(%rbx), %ecx
cmpl %ecx, %eax
jne 0x2470f
jmp 0x2473d
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x273b5
testl %eax, %eax
jne 0x24814
movq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x23f9e
jmp 0x246eb
movq %rbx, %rdi
callq 0x23d39
jmp 0x246eb
movabsq $-0x1000000000000, %rsi # imm = 0xFFFF000000000000
movq %rbx, %rdi
callq 0x4f9f2
jmp 0x246eb
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22b9f
jmp 0x247e5
movslq 0x1c(%rbx), %rax
movl 0x20(%rbx), %ecx
cmpl %ecx, %eax
jne 0x24783
jmp 0x247b1
movq 0x40(%rbx), %rdi
leaq (%rax,%rax,2), %r8
movq (%rdi,%r8,8), %r14
movq 0x8(%rdi,%r8,8), %rsi
movl 0x10(%rdi,%r8,8), %edx
leal 0x1(%rax), %r9d
xorl %r10d, %r10d
cmpl 0x30(%rbx), %r9d
movl 0x14(%rdi,%r8,8), %edi
cmovll %r9d, %r10d
movl %r10d, 0x34(%rbx)
cmpl %ecx, %eax
je 0x247e5
movl %edi, -0x50(%rbp)
movq %rsi, -0x38(%rbp)
movl %edx, -0x4c(%rbp)
movq %rbx, -0x48(%rbp)
movabsq $-0x77fffffffffff, %rdi # imm = 0xFFF8800000000001
callq 0x4f95a
movq %rax, -0x40(%rbp)
movups -0x50(%rbp), %xmm0
movups -0x40(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x181(%rip), %rsi      # 0x245fd
movq %r14, %rdi
jmp 0x247e0
movq 0x28(%rbx), %r8
leaq (%rax,%rax,2), %r9
movq (%r8,%r9,8), %rdi
movq 0x8(%r8,%r9,8), %rsi
movl 0x10(%r8,%r9,8), %edx
leal 0x1(%rax), %r10d
xorl %r11d, %r11d
cmpl 0x18(%rbx), %r10d
movl 0x14(%r8,%r9,8), %r8d
cmovll %r10d, %r11d
movl %r11d, 0x1c(%rbx)
cmpl %ecx, %eax
je 0x247e5
movl %r8d, -0x50(%rbp)
movq %rsi, -0x38(%rbp)
movl %edx, -0x4c(%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 -0x1e3(%rip), %rsi      # 0x245fd
callq 0x2454b
cmpl $0x0, 0x50(%rbx)
je 0x24805
addq $0x58, %rbx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1462a
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1353c
callq 0x13508