// File: /workspace/github_c_makefile_star2_O3/janet-lang[P]janet/src/core/marsh.c
// Function: unmarshal_one_fiber
static const uint8_t *unmarshal_one_fiber(
    UnmarshalState *st,
    const uint8_t *data,
    JanetFiber **out,
    int flags) {

    /* Initialize a new fiber with gc friendly defaults */
    JanetFiber *fiber = janet_gcalloc(JANET_MEMORY_FIBER, sizeof(JanetFiber));
    fiber->flags = 0;
    fiber->frame = 0;
    fiber->stackstart = 0;
    fiber->stacktop = 0;
    fiber->capacity = 0;
    fiber->maxstack = 0;
    fiber->data = NULL;
    fiber->child = NULL;
    fiber->env = NULL;
    fiber->last_value = janet_wrap_nil();
#ifdef JANET_EV
    fiber->sched_id = 0;
    fiber->supervisor_channel = NULL;
    fiber->ev_state = NULL;
    fiber->ev_callback = NULL;
    fiber->ev_stream = NULL;
#endif

    /* Push fiber to seen stack */
    janet_v_push(st->lookup, janet_wrap_fiber(fiber));

    /* Read ints */
    int32_t fiber_flags = readint(st, &data);
    int32_t frame = readnat(st, &data);
    int32_t fiber_stackstart = readnat(st, &data);
    int32_t fiber_stacktop = readnat(st, &data);
    int32_t fiber_maxstack = readnat(st, &data);
    JanetTable *fiber_env = NULL;

    /* Check for bad flags and ints */
    if ((int32_t)(frame + JANET_FRAME_SIZE) > fiber_stackstart ||
            fiber_stackstart > fiber_stacktop ||
            fiber_stacktop > fiber_maxstack) {
        janet_panic("fiber has incorrect stack setup");
    }

    /* Allocate stack memory */
    fiber->capacity = fiber_stacktop + 10;
    fiber->data = janet_malloc(sizeof(Janet) * fiber->capacity);
    if (!fiber->data) {
        JANET_OUT_OF_MEMORY;
    }
    for (int32_t i = 0; i < fiber->capacity; i++) {
        fiber->data[i] = janet_wrap_nil();
    }

    /* get frames */
    int32_t stack = frame;
    int32_t stacktop = fiber_stackstart - JANET_FRAME_SIZE;
    while (stack > 0) {
        JanetFunction *func = NULL;
        JanetFuncDef *def = NULL;
        JanetFuncEnv *env = NULL;
        int32_t frameflags = readint(st, &data);
        int32_t prevframe = readnat(st, &data);
        int32_t pcdiff = readnat(st, &data);

        /* Get frame items */
        Janet *framestack = fiber->data + stack;
        JanetStackFrame *framep = janet_stack_frame(framestack);

        /* Get function */
        Janet funcv;
        data = unmarshal_one(st, data, &funcv, flags + 1);
        janet_asserttype(funcv, JANET_FUNCTION, st);
        func = janet_unwrap_function(funcv);
        def = func->def;

        /* Check env */
        if (frameflags & JANET_STACKFRAME_HASENV) {
            frameflags &= ~JANET_STACKFRAME_HASENV;
            data = unmarshal_one_env(st, data, &env, flags + 1);
        }

        /* Error checking */
        int32_t expected_framesize = def->slotcount;
        if (expected_framesize != stacktop - stack) {
            janet_panic("fiber stackframe size mismatch");
        }
        if (pcdiff >= def->bytecode_length) {
            janet_panic("fiber stackframe has invalid pc");
        }
        if ((int32_t)(prevframe + JANET_FRAME_SIZE) > stack) {
            janet_panic("fiber stackframe does not align with previous frame");
        }

        /* Get stack items */
        for (int32_t i = stack; i < stacktop; i++)
            data = unmarshal_one(st, data, fiber->data + i, flags + 1);

        /* Set frame */
        framep->env = env;
        framep->pc = def->bytecode + pcdiff;
        framep->prevframe = prevframe;
        framep->flags = frameflags;
        framep->func = func;

        /* Goto previous frame */
        stacktop = stack - JANET_FRAME_SIZE;
        stack = prevframe;
    }
    if (stack < 0) {
        janet_panic("fiber has too many stackframes");
    }

    /* Check for fiber env */
    if (fiber_flags & JANET_FIBER_FLAG_HASENV) {
        Janet envv;
        fiber_flags &= ~JANET_FIBER_FLAG_HASENV;
        data = unmarshal_one(st, data, &envv, flags + 1);
        janet_asserttype(envv, JANET_TABLE, st);
        fiber_env = janet_unwrap_table(envv);
    }

    /* Check for child fiber */
    if (fiber_flags & JANET_FIBER_FLAG_HASCHILD) {
        Janet fiberv;
        fiber_flags &= ~JANET_FIBER_FLAG_HASCHILD;
        data = unmarshal_one(st, data, &fiberv, flags + 1);
        janet_asserttype(fiberv, JANET_FIBER, st);
        fiber->child = janet_unwrap_fiber(fiberv);
    }

    /* Get the fiber last value */
    data = unmarshal_one(st, data, &fiber->last_value, flags + 1);

    /* We have valid fiber, finally construct remaining fields. */
    fiber->frame = frame;
    fiber->flags = fiber_flags;
    fiber->stackstart = fiber_stackstart;
    fiber->stacktop = fiber_stacktop;
    fiber->maxstack = fiber_maxstack;
    fiber->env = fiber_env;

    int status = janet_fiber_status(fiber);
    if (status < 0 || status > JANET_STATUS_ALIVE) {
        janet_panic("invalid fiber status");
    }

    /* Return data */
    *out = fiber;
    return data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %r14d
movq %rdx, -0x90(%rbp)
movq %rdi, %r13
movabsq $-0x77fffffffffff, %rbx # imm = 0xFFF8800000000001
movq %rsi, -0x38(%rbp)
movl $0x70, %esi
movl $0x7, %edi
callq 0x2b686
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movq %rbx, %rdi
callq 0x4ece6
movq %rax, 0x40(%r15)
movl $0x0, 0x48(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r15)
movq %r15, -0x30(%rbp)
movups %xmm0, 0x60(%r15)
movq 0xc8(%r13), %r15
testq %r15, %r15
movq %r14, -0x60(%rbp)
je 0x31bec
movl -0x4(%r15), %eax
leal 0x1(%rax), %ecx
cmpl -0x8(%r15), %ecx
jl 0x31c0e
movq %r15, %rdi
movl $0x1, %esi
movl $0x8, %edx
callq 0x4b4e8
movq %rax, %r15
movq %rax, 0xc8(%r13)
movl -0x4(%rax), %eax
leal 0x1(%rax), %ecx
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movl %ecx, -0x4(%r15)
movslq %eax, %r14
movq -0x30(%rbp), %rdi
callq 0x4ed7e
movq %rax, (%r15,%r14,8)
leaq -0x38(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x3007a
movl %eax, -0x44(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x31b3b
movq %rax, -0x68(%rbp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x31b3b
movl %eax, %r15d
movq %r13, %rdi
movq %r12, %rsi
callq 0x31b3b
movl %eax, %r14d
movq %r13, %rdi
movq %r12, %rsi
callq 0x31b3b
movl %eax, %ecx
movq -0x68(%rbp), %rax
addl $0x4, %eax
movl %r15d, -0x3c(%rbp)
cmpl %r15d, %eax
ja 0x32059
cmpl %r14d, -0x3c(%rbp)
ja 0x32059
cmpl %ecx, %r14d
ja 0x32059
movl %ecx, -0x6c(%rbp)
leal 0xa(%r14), %eax
movq -0x30(%rbp), %r15
movl %eax, 0x20(%r15)
movq %r14, -0x88(%rbp)
movl %r14d, %eax
leaq 0x50(,%rax,8), %rdi
callq 0x12760
movq %rax, 0x30(%r15)
testq %rax, %rax
je 0x32065
movq -0x30(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x78(%rbp)
xorl %r14d, %r14d
movq -0x30(%rbp), %r12
movq 0x30(%r12), %r15
movq %rbx, %rdi
callq 0x4ece6
movq %rax, (%r15,%r14,8)
incq %r14
movslq 0x20(%r12), %rax
cmpq %rax, %r14
jl 0x31ce3
movq -0x68(%rbp), %rcx
testl %ecx, %ecx
movq %r13, -0x50(%rbp)
je 0x31eda
movq -0x60(%rbp), %rax
leal 0x1(%rax), %r15d
movl -0x3c(%rbp), %r14d
movl %ecx, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq %r13, %rdi
leaq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x3007a
movq %r13, %r12
movl %eax, %r13d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x31b3b
movq %rax, -0x80(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x31b3b
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rsi
movl %r15d, %ecx
movq %r12, %rdi
leaq -0xb8(%rbp), %rdx
callq 0x30284
movq %rax, %rbx
movq %rax, -0x38(%rbp)
movq -0xb8(%rbp), %rdx
movq %rdx, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x2000000000000, %rcx # imm = 0xFFFE000000000000
cmpq %rcx, %rax
jne 0x32046
movq %rdx, %rdi
callq 0x4ebc4
movq %rax, %r12
movq 0x10(%rax), %rcx
testl %r13d, %r13d
jns 0x31deb
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq -0x50(%rbp), %rdi
movq %rbx, %rsi
leaq -0x58(%rbp), %rdx
movq %rcx, %rbx
movl %r15d, %ecx
callq 0x3270d
movq %rbx, %rcx
movq %rax, %rbx
movq %rax, -0x38(%rbp)
addl $-0x4, %r14d
movl -0x40(%rbp), %edx
movl %r14d, %eax
subl %edx, %eax
cmpl %eax, 0x5c(%rcx)
jne 0x3203a
movl -0x48(%rbp), %eax
cmpl 0x70(%rcx), %eax
jge 0x3202e
movq %r12, -0xa8(%rbp)
movl %r13d, -0x70(%rbp)
movq -0x80(%rbp), %rax
addl $0x4, %eax
cmpl %edx, %eax
ja 0x32022
movq %rcx, -0xa0(%rbp)
movl %r15d, %r12d
cmpl %r14d, %edx
movq %rdx, -0x98(%rbp)
jge 0x31e75
movl %r14d, %r14d
leaq (,%rdx,8), %r15
subq %rdx, %r14
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdx
addq %r15, %rdx
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %ecx
callq 0x30284
movq %rax, %rbx
addq $0x8, %r15
decq %r14
jne 0x31e4e
jmp 0x31e79
movq -0x50(%rbp), %r13
movq %rbx, -0x38(%rbp)
movq -0x58(%rbp), %rax
movq -0xb0(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq %rax, -0x10(%rdx,%rsi,8)
movl -0x48(%rbp), %eax
shlq $0x2, %rax
movq -0xa0(%rbp), %rcx
addq 0x28(%rcx), %rax
movq %rax, -0x18(%rdx,%rsi,8)
movq -0x80(%rbp), %rcx
movl %ecx, -0x8(%rdx,%rsi,8)
movl -0x70(%rbp), %eax
movl %eax, -0x4(%rdx,%rsi,8)
movq -0xa8(%rbp), %rax
movq %rax, -0x20(%rdx,%rsi,8)
movl -0x40(%rbp), %r14d
movl %ecx, -0x40(%rbp)
testl %ecx, %ecx
movl %r12d, %r15d
jne 0x31d20
jmp 0x31ede
movq -0x38(%rbp), %rbx
movl -0x44(%rbp), %eax
btl $0x1e, %eax
jb 0x31ef0
xorl %r12d, %r12d
movq -0x60(%rbp), %r14
jmp 0x31f50
movq -0x60(%rbp), %r14
leal 0x1(%r14), %ecx
leaq -0x58(%rbp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x30284
movq %rax, %rbx
movq %rax, -0x38(%rbp)
movq (%r15), %rdx
movq %rdx, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x3800000000000, %rcx # imm = 0xFFFC800000000000
cmpq %rcx, %rax
jne 0x32076
movl -0x44(%rbp), %r15d
andl $0xbfffffff, %r15d      # imm = 0xBFFFFFFF
movq %rdx, %rdi
callq 0x4ebc4
movq %rax, %r12
movl %r15d, %eax
movl %eax, %r15d
incl %r14d
btl $0x1d, %eax
jae 0x31fb7
movq %r13, %rdi
leaq -0x58(%rbp), %r13
movq %rbx, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x30284
movq %rax, %rbx
movq %rax, -0x38(%rbp)
movq (%r13), %rdx
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rdx, %rcx
movabsq $-0x6800000000000, %rax # imm = 0xFFF9800000000000
cmpq %rax, %rcx
jne 0x32084
andl $0xdfffffff, %r15d      # imm = 0xDFFFFFFF
movq %rdx, %rdi
callq 0x4ebc4
movq -0x30(%rbp), %rcx
movq %rax, 0x38(%rcx)
movq -0x50(%rbp), %r13
movq -0x78(%rbp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
movl %r14d, %ecx
callq 0x30284
movq %rax, %rbx
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rdi
movl %eax, 0x14(%rdi)
movl %r15d, 0x10(%rdi)
movl -0x3c(%rbp), %eax
movl %eax, 0x18(%rdi)
movq -0x88(%rbp), %rax
movl %eax, 0x1c(%rdi)
movl -0x6c(%rbp), %eax
movl %eax, 0x24(%rdi)
movq %r12, 0x28(%rdi)
callq 0x2996f
cmpl $0x10, %eax
jae 0x3206a
movq -0x90(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2de82(%rip), %rdi     # 0x5feab
callq 0x191a3
leaq 0x2de56(%rip), %rdi     # 0x5fe8b
callq 0x191a3
leaq 0x2de2b(%rip), %rdi     # 0x5fe6c
callq 0x191a3
leaq 0x2dfd5(%rip), %rdi     # 0x60022
movl $0x1000, %esi           # imm = 0x1000
xorl %eax, %eax
callq 0x190bb
leaq 0x2ddec(%rip), %rdi     # 0x5fe4c
callq 0x191a3
callq 0x13a6c
leaq 0x2de6e(%rip), %rdi     # 0x5fedf
callq 0x191a3
leaq 0x2dfa5(%rip), %rdi     # 0x60022
movl $0x200, %esi            # imm = 0x200
jmp 0x32052
leaq 0x2df97(%rip), %rdi     # 0x60022
movl $0x8, %esi
jmp 0x32052