// File: /workspace/github_c_makefile_star2_O1/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, -0x60(%rbp)
leaq 0x10(%rbp), %r13
movq 0x8(%r13), %rax
movq 0x18(%rax), %r14
movq %r14, -0x30(%rbp)
movq %rax, -0x58(%rbp)
movslq 0x10(%rax), %r12
movl (%r13), %ebx
movl $0x0, (%r13)
callq 0x4f47f
movl 0x4(%r13), %r15d
movq %fs:0x0, %rax
leaq -0x348(%rax), %rax
movl %r15d, 0xf0(%rax)
movq %rax, %r15
leaq -0x158(%rbp), %rdi
callq 0x4eda2
leaq -0x138(%rbp), %rdi
callq 0x12580
testl %eax, %eax
je 0x26522
movq (%r15), %r15
testq %r15, %r15
je 0x266f5
leaq 0x432f2(%rip), %rdi     # 0x697be
callq 0x47cd3
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4f9ad
leaq -0x50(%rbp), %rdi
movq %rax, (%rdi)
movq -0x70(%rbp), %rax
movq %rax, 0x8(%rdi)
movl $0x2, %ebx
movl %ebx, %esi
callq 0x48a9f
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
movq %rax, %rdi
callq 0x4f9ad
movq %r15, %rdi
movq %rax, %rsi
movl %ebx, %edx
callq 0x23526
jmp 0x267e9
addq %r12, %r14
movabsq $-0x800000000000, %r12  # imm = 0xFFFF800000000000
testb $0x2, %bl
movq %r15, -0x38(%rbp)
jne 0x26592
movabsq $-0x3800000000000, %r15 # imm = 0xFFFC800000000000
leaq -0x30(%rbp), %r8
movq (%r8), %rdi
movq %r14, %rsi
subq %rdi, %rsi
xorl %eax, %eax
movl %eax, %ecx
movl $0x20000, %edx          # imm = 0x20000
callq 0x3157c
movq %rax, %rcx
andq %r12, %rcx
cmpq %r15, %rcx
jne 0x2683a
movq %rax, %rdi
callq 0x4f838
movq -0x38(%rbp), %rcx
movq %rax, 0x68(%rcx)
movq %rax, %rdi
movq %r15, %rsi
movq %rcx, %r15
callq 0x4f9f2
movq %rax, %rdi
callq 0x2b5e5
btl $0x8, %ebx
jae 0x265be
leaq -0x30(%rbp), %r8
movq (%r8), %rdi
movq %r14, %rsi
subq %rdi, %rsi
xorl %eax, %eax
movl %eax, %ecx
movl $0x20000, %edx          # imm = 0x20000
callq 0x3157c
movq %rax, %rdi
callq 0x4f838
movq %rax, (%r15)
movl %ebx, -0x3c(%rbp)
testb $0x4, %bl
jne 0x26642
movq -0x30(%rbp), %rbx
movl (%rbx), %eax
movl %eax, %ecx
movq %r14, %rax
subq %rbx, %rax
addq $-0x4, %rax
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rdx
shrq $0x5, %rdx
cmpq %rcx, %rdx
movq -0x38(%rbp), %r13
jb 0x26846
movq %rcx, 0x58(%r13)
movq %rcx, 0x50(%r13)
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r15
movq %r15, %rdi
callq 0x12770
movq %rax, 0x48(%r13)
testq %rax, %rax
je 0x26863
movq %rax, %rdi
movl $0x1, 0x60(%r13)
leaq 0x4(%rbx), %rsi
movq %rsi, -0x30(%rbp)
movq %r15, %rdx
callq 0x12680
leaq (%rbx,%r15), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %r12
movq (%r12), %rdi
movq %r14, %rsi
subq %rdi, %rsi
xorl %eax, %eax
movl %eax, %r13d
movl $0x20000, %edx          # imm = 0x20000
movq %r13, %rcx
movq %r12, %r8
callq 0x3157c
movq %rax, %r15
movq %rax, %rbx
movq (%r12), %rdi
subq %rdi, %r14
movq %r14, %rsi
movl $0x20000, %edx          # imm = 0x20000
movq %r13, %rcx
movq %r12, %r8
callq 0x3157c
movq %rax, -0x50(%rbp)
movabsq $-0x800000000000, %rax  # imm = 0xFFFF800000000000
andq %r15, %rax
movabsq $-0x6800000000000, %rcx # imm = 0xFFF9800000000000
cmpq %rcx, %rax
je 0x2672e
movabsq $-0x2000000000000, %rcx # imm = 0xFFFE000000000000
cmpq %rcx, %rax
leaq 0x10(%rbp), %r13
jne 0x26852
movq %rbx, %rdi
callq 0x4f838
movq 0x10(%rax), %rcx
movl 0x64(%rcx), %edx
leaq -0x50(%rbp), %rcx
movq %rax, %rdi
movl $0x40, %esi
callq 0x29abc
testq %rax, %rax
je 0x26868
movq %rax, %r15
orl $0x1f2, 0x10(%rax)      # imm = 0x1F2
jmp 0x2673d
testb $0x1, %bl
jne 0x267b1
movq -0x70(%rbp), %rdi
movq %rdi, %rax
shrq $0x2f, %rax
cmpl $0x1fff4, %eax          # imm = 0x1FFF4
jne 0x267d6
movl $0x6, (%r13)
callq 0x4f838
movq %rax, %rdi
callq 0x12b40
jmp 0x267e5
movq %rbx, %rdi
callq 0x4f838
movq %rax, %r15
leaq 0x10(%rbp), %r13
movl -0x3c(%rbp), %eax
testb $0x8, %al
je 0x26787
cmpq $0x0, 0x28(%r15)
jne 0x26756
xorl %edi, %edi
callq 0x47e96
movq %rax, 0x28(%r15)
movq 0x28(%r15), %rbx
leaq 0x355b0(%rip), %rdi     # 0x5bd11
callq 0x47cd3
movabsq $-0x5000000000000, %rsi # imm = 0xFFFB000000000000
movq %rax, %rdi
callq 0x4f9ad
movq -0x50(%rbp), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x481b3
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, 0x68(%r15)
movq -0x50(%rbp), %rsi
xorl %ecx, %ecx
movq %r15, %rdi
movl %ecx, %edx
callq 0x22b9f
callq 0x25348
movl $0x0, (%r13)
jmp 0x267e9
movq 0x5f838(%rip), %rax     # 0x85ff0
movq (%rax), %rsi
movq -0x70(%rbp), %rcx
leaq 0x35258(%rip), %rdi     # 0x5ba1e
leaq 0x35618(%rip), %rdx     # 0x5bde5
xorl %eax, %eax
callq 0x2f340
jmp 0x267e9
movl $0x5, (%r13)
leaq 0x3561a(%rip), %rax     # 0x5bdff
movq %rax, 0x8(%r13)
movq -0x60(%rbp), %r15
leaq -0x158(%rbp), %rdi
callq 0x4ee11
movq -0x58(%rbp), %r14
movq %r14, %rdi
callq 0x17741
movq %r14, %rdi
callq 0x120b0
callq 0x4f5c8
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x10(%r15)
movups %xmm0, (%r15)
movq %r15, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x35518(%rip), %rdi     # 0x5bd59
callq 0x19263
leaq 0x35531(%rip), %rdi     # 0x5bd7e
callq 0x19263
leaq 0x3553c(%rip), %rdi     # 0x5bd95
xorl %eax, %eax
movq %rbx, %rsi
callq 0x1917b
callq 0x136a0
leaq 0x35549(%rip), %rdi     # 0x5bdb8
xorl %eax, %eax
callq 0x1917b