// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/ev.c
// Function: janet_go_thread_subr
static JanetEVGenericMessage janet_go_thread_subr(JanetEVGenericMessage args) {
    JanetBuffer *buffer = (JanetBuffer *) args.argp;
    const uint8_t *nextbytes = buffer->data;
    const uint8_t *endbytes = nextbytes + buffer->count;
    uint32_t flags = args.tag;
    args.tag = 0;
    janet_init();
    janet_vm.sandbox_flags = (uint32_t) args.argi;
    JanetTryState tstate;
    JanetSignal signal = janet_try(&tstate);
    if (!signal) {

        /* Set abstract registry */
        if (!(flags & 0x2)) {
            Janet aregv = janet_unmarshal(nextbytes, endbytes - nextbytes,
                                          JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
            if (!janet_checktype(aregv, JANET_TABLE)) janet_panic("expected table for abstract registry");
            janet_vm.abstract_registry = janet_unwrap_table(aregv);
            janet_gcroot(janet_wrap_table(janet_vm.abstract_registry));
        }

        /* Get supervisor */
        if (flags & JANET_THREAD_SUPERVISOR_FLAG) {
            Janet sup =
                janet_unmarshal(nextbytes, endbytes - nextbytes,
                                JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
            /* Hack - use a global variable to avoid longjmp clobber */
            janet_vm.user = janet_unwrap_pointer(sup);
        }

        /* Set cfunction registry */
        if (!(flags & 0x4)) {
            uint32_t count1;
            memcpy(&count1, nextbytes, sizeof(count1));
            size_t count = (size_t) count1;
            /* Use division to avoid overflowing size_t */
            if (count > (endbytes - nextbytes - sizeof(count1)) / sizeof(JanetCFunRegistry)) {
                janet_panic("thread message invalid");
            }
            janet_vm.registry_count = count;
            janet_vm.registry_cap = count;
            janet_vm.registry = janet_malloc(count * sizeof(JanetCFunRegistry));
            if (janet_vm.registry == NULL) {
                JANET_OUT_OF_MEMORY;
            }
            janet_vm.registry_dirty = 1;
            nextbytes += sizeof(uint32_t);
            memcpy(janet_vm.registry, nextbytes, count * sizeof(JanetCFunRegistry));
            nextbytes += count * sizeof(JanetCFunRegistry);
        }

        Janet fiberv = janet_unmarshal(nextbytes, endbytes - nextbytes,
                                       JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
        Janet value = janet_unmarshal(nextbytes, endbytes - nextbytes,
                                      JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
        JanetFiber *fiber;
        if (!janet_checktype(fiberv, JANET_FIBER)) {
            if (!janet_checktype(fiberv, JANET_FUNCTION)) {
                janet_panicf("expected function or fiber, got %v", fiberv);
            }
            JanetFunction *func = janet_unwrap_function(fiberv);
            fiber = janet_fiber(func, 64, func->def->min_arity, &value);
            if (fiber == NULL) {
                janet_panicf("thread function must accept 0 or 1 arguments");
            }
            fiber->flags |=
                JANET_FIBER_MASK_ERROR |
                JANET_FIBER_MASK_USER0 |
                JANET_FIBER_MASK_USER1 |
                JANET_FIBER_MASK_USER2 |
                JANET_FIBER_MASK_USER3 |
                JANET_FIBER_MASK_USER4;
        } else {
            fiber = janet_unwrap_fiber(fiberv);
        }
        if (flags & 0x8) {
            if (NULL == fiber->env) fiber->env = janet_table(0);
            janet_table_put(fiber->env, janet_ckeywordv("task-id"), value);
        }
        fiber->supervisor_channel = janet_vm.user;
        janet_schedule(fiber, value);
        janet_loop();
        args.tag = JANET_EV_TCTAG_NIL;
    } else {
        void *supervisor = janet_vm.user;
        if (NULL != supervisor) {
            /* Got a supervisor, write error there */
            Janet pair[] = {
                janet_ckeywordv("error"),
                tstate.payload
            };
            janet_channel_push((JanetChannel *)supervisor,
                               janet_wrap_tuple(janet_tuple_n(pair, 2)), 2);
        } else if (flags & 0x1) {
            /* No wait, just print to stderr */
            janet_eprintf("thread start failure: %v\n", tstate.payload);
        } else {
            /* Make ev/thread call from parent thread error */
            if (janet_checktype(tstate.payload, JANET_STRING)) {
                args.tag = JANET_EV_TCTAG_ERR_STRINGF;
                args.argp = strdup((const char *) janet_unwrap_string(tstate.payload));
            } else {
                args.tag = JANET_EV_TCTAG_ERR_STRING;
                args.argp = "failed to start thread";
            }
        }
    }
    janet_restore(&tstate);
    janet_buffer_deinit(buffer);
    janet_free(buffer);
    janet_deinit();
    return args;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, -0x40(%rbp)
leaq 0x10(%rbp), %r12
movq 0x8(%r12), %rax
movq 0x18(%rax), %r15
movq %r15, -0x30(%rbp)
movq %rax, -0x68(%rbp)
movslq 0x10(%rax), %r14
movl (%r12), %r13d
andl $0x0, (%r12)
callq 0x4ae26
movl 0x4(%r12), %ebx
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movl %ebx, 0xf0(%rax)
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x4a78b
leaq -0x140(%rbp), %rdi
callq 0x12580
testl %eax, %eax
je 0x23f0c
movq (%rbx), %r15
testq %r15, %r15
je 0x23f8c
leaq 0x4090a(%rip), %rdi     # 0x647be
callq 0x439c6
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4b366
leaq -0x60(%rbp), %rdi
movq %rax, (%rdi)
movq -0x78(%rbp), %rax
movq %rax, 0x8(%rdi)
pushq $0x2
popq %rbx
movl %ebx, %esi
callq 0x44692
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
movq %rax, %rdi
callq 0x4b366
movq %r15, %rdi
movq %rax, %rsi
movl %ebx, %edx
callq 0x21174
movq -0x40(%rbp), %r14
jmp 0x241e2
movq %r14, %r12
movabsq $-0x800000000000, %r14  # imm = 0xFFFF800000000000
movq %r15, %rdi
testb $0x2, %r13b
jne 0x23f76
xorl %eax, %eax
movl %eax, %ecx
leaq -0x30(%rbp), %r8
movq %r15, %rdi
movq %r12, %rsi
movl $0x20000, %edx          # imm = 0x20000
callq 0x2ee91
movq %rax, %rcx
andq %r14, %rcx
movabsq $-0x3800000000000, %r14 # imm = 0xFFFC800000000000
cmpq %r14, %rcx
jne 0x24230
movq %rax, %rdi
callq 0x4b1f1
movq %rax, 0x68(%rbx)
movq %rax, %rdi
movq %r14, %rsi
callq 0x4b3ab
movq %rax, %rdi
callq 0x28c2a
movq -0x30(%rbp), %rdi
movq %r15, %r14
addq %r12, %r14
btl $0x8, %r13d
movq %rbx, -0x38(%rbp)
jb 0x23fca
movq %rdi, %rbx
jmp 0x23ff3
testb $0x1, %r13b
movq -0x40(%rbp), %r14
jne 0x241a9
movq -0x78(%rbp), %rdi
movq %rdi, %rax
shrq $0x2f, %rax
cmpl $0x1fff4, %eax          # imm = 0x1FFF4
jne 0x241ce
movl $0x6, (%r12)
callq 0x4b1f1
movq %rax, %rdi
callq 0x12b30
jmp 0x241dd
movq %r14, %rsi
subq %rdi, %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x30(%rbp), %r15
movl $0x20000, %edx          # imm = 0x20000
movq %r15, %r8
callq 0x2ee91
movq %rax, %rdi
callq 0x4b1f1
movq %rax, (%rbx)
movq (%r15), %rbx
testb $0x4, %r13b
movl %r13d, -0x44(%rbp)
jne 0x2406e
movl (%rbx), %eax
movl %eax, %ecx
movq %r14, %rax
subq %rbx, %rax
addq $-0x4, %rax
xorl %edx, %edx
pushq $0x28
popq %rsi
divq %rsi
cmpq %rcx, %rax
jb 0x2423c
movq -0x38(%rbp), %r12
movq %rcx, 0x58(%r12)
movq %rcx, 0x50(%r12)
imulq $0x28, %rcx, %r15
movq %r15, %rdi
callq 0x12760
movq %rax, 0x48(%r12)
testq %rax, %rax
je 0x24259
movq %rax, %rdi
movl $0x1, 0x60(%r12)
leaq 0x4(%rbx), %rsi
movq %rsi, -0x30(%rbp)
movq %r15, %rdx
callq 0x12670
leaq (%rbx,%r15), %rdi
addq $0x4, %rdi
movq %rdi, -0x30(%rbp)
jmp 0x24071
movq %rbx, %rdi
movq %r14, %rsi
subq %rdi, %rsi
xorl %eax, %eax
movl %eax, %r12d
movl $0x20000, %edx          # imm = 0x20000
leaq -0x30(%rbp), %rbx
movq %r12, %rcx
movq %rbx, %r8
callq 0x2ee91
movq %rax, %r15
movq %rax, %r13
movq (%rbx), %rdi
subq %rdi, %r14
movq %r14, %rsi
movl $0x20000, %edx          # imm = 0x20000
movq %r12, %rcx
movq %rbx, %r8
callq 0x2ee91
movq %rax, -0x60(%rbp)
movabsq $-0x800000000000, %rax  # imm = 0xFFFF800000000000
andq %r15, %rax
movabsq $-0x6800000000000, %rcx # imm = 0xFFF9800000000000
cmpq %rcx, %rax
je 0x24121
movabsq $-0x2000000000000, %rcx # imm = 0xFFFE000000000000
cmpq %rcx, %rax
movq -0x40(%rbp), %r14
leaq 0x10(%rbp), %r12
movq -0x38(%rbp), %rbx
jne 0x24248
movq %r13, %rdi
callq 0x4b1f1
movq 0x10(%rax), %rcx
movl 0x64(%rcx), %edx
pushq $0x40
popq %rsi
leaq -0x60(%rbp), %rcx
movq %rax, %rdi
callq 0x271c9
testq %rax, %rax
je 0x24285
movq %rax, %r15
orl $0x1f2, 0x10(%rax)      # imm = 0x1F2
jmp 0x24138
movq %r13, %rdi
callq 0x4b1f1
movq %rax, %r15
movq -0x40(%rbp), %r14
leaq 0x10(%rbp), %r12
movq -0x38(%rbp), %rbx
movl -0x44(%rbp), %eax
testb $0x8, %al
je 0x2418a
movq 0x28(%r15), %rax
movq %rax, %rbx
testq %rax, %rax
jne 0x24159
xorl %edi, %edi
callq 0x43b8a
movq %rax, %rbx
movq %rax, 0x28(%r15)
leaq 0x32bb1(%rip), %rdi     # 0x56d11
callq 0x439c6
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4b366
movq -0x60(%rbp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x43e19
movq -0x38(%rbp), %rbx
movq (%rbx), %rax
movq %rax, 0x68(%r15)
movq -0x60(%rbp), %rsi
movq %r15, %rdi
callq 0x209ca
callq 0x22d74
andl $0x0, (%r12)
jmp 0x241e2
movq 0x5be40(%rip), %rax     # 0x7fff0
movq (%rax), %rsi
movq -0x78(%rbp), %rcx
leaq 0x32860(%rip), %rdi     # 0x56a1e
leaq 0x32c20(%rip), %rdx     # 0x56de5
xorl %eax, %eax
callq 0x2c621
jmp 0x241e2
movl $0x5, (%r12)
leaq 0x32c22(%rip), %rax     # 0x56dff
movq %rax, 0x8(%r12)
leaq -0x160(%rbp), %rdi
callq 0x4a7f7
movq -0x68(%rbp), %r15
movq %r15, %rdi
callq 0x15f62
movq %r15, %rdi
callq 0x120b0
callq 0x4af60
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
movq %r14, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x32b22(%rip), %rdi     # 0x56d59
callq 0x177b1
leaq 0x32b3b(%rip), %rdi     # 0x56d7e
callq 0x177b1
leaq 0x32b46(%rip), %rdi     # 0x56d95
xorl %eax, %eax
movq %r13, %rsi
callq 0x176c9
movq 0x5bd90(%rip), %rax     # 0x7fff0
movq (%rax), %rdi
leaq 0x28d9a(%rip), %rsi     # 0x4d004
leaq 0x325f7(%rip), %rdx     # 0x56868
xorl %eax, %eax
movl $0xbd9, %ecx            # imm = 0xBD9
callq 0x125e0
pushq $0x1
popq %rdi
callq 0x12a60
leaq 0x32b2c(%rip), %rdi     # 0x56db8
xorl %eax, %eax
callq 0x176c9