// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %rcx, -0x48(%rbp)
movq %rdx, -0x88(%rbp)
movq %rdi, %r12
movabsq $-0x77fffffffffff, %rbx # imm = 0xFFF8800000000001
movq %rsi, -0x30(%rbp)
movl $0x70, %esi
movl $0x7, %edi
callq 0x2bb18
movq %rax, %r13
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movq %rbx, %rdi
callq 0x4f95a
movq %rax, 0x40(%r13)
movl $0x0, 0x48(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r13)
movups %xmm0, 0x60(%r13)
movq 0xc8(%r12), %rdi
testq %rdi, %rdi
je 0x32176
movl -0x4(%rdi), %eax
incl %eax
cmpl -0x8(%rdi), %eax
jl 0x3218d
movl $0x1, %esi
movl $0x8, %edx
callq 0x4c0b4
movq %rax, 0xc8(%r12)
movabsq $-0x6800000000000, %rsi # imm = 0xFFF9800000000000
movq 0xc8(%r12), %r14
movslq -0x4(%r14), %r15
leal 0x1(%r15), %eax
movl %eax, -0x4(%r14)
movq %r13, %rdi
callq 0x4f9f2
movq %rax, (%r14,%r15,8)
leaq -0x30(%rbp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x30618
movl %eax, -0x3c(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x320ce
movl %eax, %r14d
movq %r12, %rdi
movq %r15, %rsi
callq 0x320ce
movl %eax, %r15d
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x320ce
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %rax, %r12
leaq -0x30(%rbp), %rsi
callq 0x320ce
movl %eax, %ecx
movq %r14, -0x58(%rbp)
leal 0x4(%r14), %eax
movl %r15d, -0x34(%rbp)
cmpl %r15d, %eax
ja 0x325d3
cmpl %r12d, -0x34(%rbp)
ja 0x325d3
cmpl %ecx, %r12d
ja 0x325d3
movl %ecx, -0x64(%rbp)
leal 0xa(%r12), %eax
movl %eax, 0x20(%r13)
movl %r12d, %eax
leaq 0x50(,%rax,8), %rdi
callq 0x12770
movq %rax, 0x30(%r13)
testq %rax, %rax
je 0x325df
movq %r12, -0x80(%rbp)
cmpl $0x0, 0x20(%r13)
jle 0x32281
xorl %r14d, %r14d
movq 0x30(%r13), %r15
movq %rbx, %rdi
callq 0x4f95a
movq %rax, (%r15,%r14,8)
incq %r14
movslq 0x20(%r13), %rax
cmpq %rax, %r14
jl 0x32265
cmpl $0x0, -0x58(%rbp)
movq -0x60(%rbp), %r12
je 0x32463
movq -0x48(%rbp), %rax
leal 0x1(%rax), %r15d
movl -0x34(%rbp), %r14d
movq -0x58(%rbp), %rax
movl %eax, -0x38(%rbp)
movq %r13, -0x70(%rbp)
movq $0x0, -0x50(%rbp)
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x30618
movl %r15d, %ebx
movl %eax, %r15d
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x320ce
movq %rax, -0x78(%rbp)
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x320ce
movl %eax, -0x40(%rbp)
movq 0x30(%r13), %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rsi
movq %r12, %r13
movq %r12, %rdi
leaq -0xb0(%rbp), %rdx
movl %ebx, %r12d
movl %ebx, %ecx
callq 0x30822
movq %rax, %rbx
movq %rax, -0x30(%rbp)
movq -0xb0(%rbp), %rdx
movq %rdx, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x2000000000000, %rcx # imm = 0xFFFE000000000000
cmpq %rcx, %rax
jne 0x325c0
movq %rdx, %rdi
callq 0x4f838
movq %rax, -0xa0(%rbp)
movq 0x10(%rax), %rcx
testl %r15d, %r15d
movl %r15d, %esi
jns 0x32379
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r13, %rdi
movl %esi, %r13d
movq %rbx, %rsi
leaq -0x50(%rbp), %rdx
movq %rcx, %rbx
movl %r12d, %ecx
callq 0x32c55
movl %r13d, %esi
movq %rbx, %rcx
movq %rax, -0x30(%rbp)
addl $-0x4, %r14d
movl -0x38(%rbp), %edx
movl %r14d, %eax
subl %edx, %eax
cmpl %eax, 0x5c(%rcx)
jne 0x325b4
movl -0x40(%rbp), %eax
cmpl 0x70(%rcx), %eax
jge 0x325a8
movq -0x78(%rbp), %rax
addl $0x4, %eax
cmpl %edx, %eax
ja 0x3259c
movq %rcx, -0x98(%rbp)
movl %esi, -0x68(%rbp)
movl %r12d, %r15d
movq -0x30(%rbp), %rax
cmpl %edx, %r14d
movq %rdx, -0x90(%rbp)
jle 0x323fc
movl %r14d, %r14d
leaq (,%rdx,8), %r12
subq %rdx, %r14
movq -0x70(%rbp), %r13
movq -0x60(%rbp), %rbx
movq 0x30(%r13), %rdx
addq %r12, %rdx
movq %rbx, %rdi
movq %rax, %rsi
movl %r15d, %ecx
callq 0x30822
addq $0x8, %r12
decq %r14
jne 0x323dc
jmp 0x32404
movq -0x70(%rbp), %r13
movq -0x60(%rbp), %rbx
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq -0xa8(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq %rax, -0x10(%rdx,%rsi,8)
movl -0x40(%rbp), %eax
shlq $0x2, %rax
movq -0x98(%rbp), %rcx
addq 0x28(%rcx), %rax
movq %rax, -0x18(%rdx,%rsi,8)
movq -0x78(%rbp), %rcx
movl %ecx, -0x8(%rdx,%rsi,8)
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rdx,%rsi,8)
movq -0xa0(%rbp), %rax
movq %rax, -0x20(%rdx,%rsi,8)
movl -0x38(%rbp), %r14d
movl %ecx, -0x38(%rbp)
testl %ecx, %ecx
movq %rbx, %r12
jne 0x322a6
movl -0x3c(%rbp), %eax
btl $0x1e, %eax
jb 0x32470
xorl %ebx, %ebx
jmp 0x324cd
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rax
leal 0x1(%rax), %ecx
leaq -0x50(%rbp), %rbx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x30822
movq %rax, -0x30(%rbp)
movq (%rbx), %rdx
movq %rdx, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x3800000000000, %rcx # imm = 0xFFFC800000000000
cmpq %rcx, %rax
jne 0x325f0
movl -0x3c(%rbp), %r14d
andl $0xbfffffff, %r14d      # imm = 0xBFFFFFFF
movq %rdx, %rdi
callq 0x4f838
movq %rax, %rbx
movl %r14d, %eax
movl %eax, %r14d
btl $0x1d, %eax
jae 0x3252a
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rax
leal 0x1(%rax), %ecx
leaq -0x50(%rbp), %r15
movq %r12, %rdi
movq %r15, %rdx
callq 0x30822
movq %rax, -0x30(%rbp)
movq (%r15), %rdx
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rdx, %rcx
movabsq $-0x6800000000000, %rax # imm = 0xFFF9800000000000
cmpq %rax, %rcx
jne 0x325fe
andl $0xdfffffff, %r14d      # imm = 0xDFFFFFFF
movq %rdx, %rdi
callq 0x4f838
movq %rax, 0x38(%r13)
movq %r13, %rdx
addq $0x40, %rdx
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rcx
incl %ecx
movq %r12, %rdi
callq 0x30822
movq %rax, %r15
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rax
movl %eax, 0x14(%r13)
movl %r14d, 0x10(%r13)
movl -0x34(%rbp), %eax
movl %eax, 0x18(%r13)
movq -0x80(%rbp), %rax
movl %eax, 0x1c(%r13)
movl -0x64(%rbp), %eax
movl %eax, 0x24(%r13)
movq %rbx, 0x28(%r13)
movq %r13, %rdi
callq 0x29e47
cmpl $0x10, %eax
jae 0x325e4
movq -0x88(%rbp), %rax
movq %r13, (%rax)
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2e908(%rip), %rdi     # 0x60eab
callq 0x19263
leaq 0x2e8dc(%rip), %rdi     # 0x60e8b
callq 0x19263
leaq 0x2e8b1(%rip), %rdi     # 0x60e6c
callq 0x19263
leaq 0x2ea5b(%rip), %rdi     # 0x61022
movl $0x1000, %esi           # imm = 0x1000
xorl %eax, %eax
callq 0x1917b
leaq 0x2e872(%rip), %rdi     # 0x60e4c
callq 0x19263
callq 0x13a7c
leaq 0x2e8f4(%rip), %rdi     # 0x60edf
callq 0x19263
leaq 0x2ea2b(%rip), %rdi     # 0x61022
movl $0x200, %esi            # imm = 0x200
jmp 0x325cc
leaq 0x2ea1d(%rip), %rdi     # 0x61022
movl $0x8, %esi
jmp 0x325cc