// File: night-shift[P]Srl/src/lib/PMsgPack.cpp
// Function: Srl::PMsgPack::read(Srl::Lib::In&)
pair<MemBlock, Value> PMsgPack::read(In& source)
{
    if(this->scope) {

        if(this->scope->elements < 1) {
            if(this->scope_stack.size() < 1) {
                error();
            }
            this->scope_stack.pop();
            this->scope = this->scope_stack.size() > 0 ? &this->scope_stack.top() : nullptr;

            return { MemBlock(), Type::Scope_End };
        }

        this->scope->elements--;
    }

    MemBlock name;
    auto prefix = source.read_move<uint8_t>(error);



    if(this->scope && this->scope->type == Type::Object) {
        name = prefix_is_str(prefix) ? read_name(prefix, this->buffer, source) : name;
        prefix = source.read_move<uint8_t>(error);

    }

    if(prefix_is_scope(prefix)) {

        Type type; size_t size;
        tie(type, size) = get_scope_info(prefix, source);

        this->scope_stack.emplace(type, size);
        this->scope = &this->scope_stack.top();

        return { name, type };
    }

    if(prefix_is_str(prefix)) {
        return { name, read_str(prefix, source) };
    }

    if(prefix_is_scalar(prefix)) {
        return { name, read_scalar(prefix, source) };
    }


    if(prefix_is_bin(prefix)) {
        return { name, read_bin(prefix, source) };
    }

    /* shouldn't end here */
    error();
    return { MemBlock(), Type::Null };
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xf8(%rbp)
movq %rdi, -0xf0(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, 0x70(%rax)
je 0x580e4
movq -0xe8(%rbp), %rax
movq 0x70(%rax), %rax
cmpl $0x1, 0x4(%rax)
jae 0x580d0
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x33890
cmpq $0x1, %rax
jae 0x5804e
leaq 0x2ef97(%rip), %rdi     # 0x86fe0
callq 0x35bb0
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x382b0
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x33890
cmpq $0x0, %rax
jbe 0x5808d
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x343f0
movq %rax, -0x100(%rbp)
jmp 0x58098
xorl %eax, %eax
movq %rax, -0x100(%rbp)
jmp 0x58098
movq -0xe8(%rbp), %rax
movq -0x100(%rbp), %rcx
movq %rcx, 0x70(%rax)
leaq -0x20(%rbp), %rdi
callq 0x374c0
movq -0xf8(%rbp), %rdi
movb $0x10, -0x21(%rbp)
leaq -0x20(%rbp), %rsi
leaq -0x21(%rbp), %rdx
callq 0x35a90
jmp 0x58311
movq -0xe8(%rbp), %rax
movq 0x70(%rax), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
leaq -0x38(%rbp), %rdi
callq 0x374c0
movq -0x10(%rbp), %rdi
leaq 0x2eee8(%rip), %rsi     # 0x86fe0
callq 0x34dd0
movb %al, %cl
movq -0xe8(%rbp), %rax
movb %cl, -0x39(%rbp)
cmpq $0x0, 0x70(%rax)
je 0x58186
movq -0xe8(%rbp), %rax
movq 0x70(%rax), %rax
cmpb $0xe, (%rax)
jne 0x58186
movzbl -0x39(%rbp), %edi
callq 0x58330
testb $0x1, %al
jne 0x5812f
jmp 0x58153
movq -0xe8(%rbp), %rsi
movb -0x39(%rbp), %al
addq $0x8, %rsi
movq -0x10(%rbp), %rdx
movzbl %al, %edi
callq 0x58390
movq %rax, -0x50(%rbp)
movq %rdx, -0x48(%rbp)
jmp 0x58163
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x2ee62(%rip), %rsi     # 0x86fe0
callq 0x34dd0
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %edi
callq 0x58460
testb $0x1, %al
jne 0x58198
jmp 0x5821b
movb -0x39(%rbp), %al
movq -0x10(%rbp), %rsi
movzbl %al, %edi
callq 0x584c0
movb %al, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x51(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x36290
leaq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x37400
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x51(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x34620
movq -0xe8(%rbp), %rdi
addq $0x20, %rdi
callq 0x343f0
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movq -0xe8(%rbp), %rax
movq %rcx, 0x70(%rax)
leaq -0x38(%rbp), %rsi
leaq -0x51(%rbp), %rdx
callq 0x37660
jmp 0x58311
movzbl -0x39(%rbp), %edi
callq 0x58330
testb $0x1, %al
jne 0x5822a
jmp 0x5825c
movb -0x39(%rbp), %al
movq -0x10(%rbp), %rdx
leaq -0x98(%rbp), %rdi
movzbl %al, %esi
callq 0x585e0
movq -0xf8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0x35c10
jmp 0x58311
movzbl -0x39(%rbp), %edi
callq 0x58650
testb $0x1, %al
jne 0x5826b
jmp 0x5829a
movb -0x39(%rbp), %al
movq -0x10(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
movzbl %al, %esi
callq 0x586d0
movq -0xf8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x35c10
jmp 0x58311
movzbl -0x39(%rbp), %edi
callq 0x587b0
testb $0x1, %al
jne 0x582a9
jmp 0x582d8
movb -0x39(%rbp), %al
movq -0x10(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
movzbl %al, %esi
callq 0x587f0
movq -0xf8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0xc8(%rbp), %rdx
callq 0x35c10
jmp 0x58311
leaq 0x2ed01(%rip), %rdi     # 0x86fe0
callq 0x35bb0
leaq -0xd8(%rbp), %rdi
callq 0x374c0
movq -0xf8(%rbp), %rdi
movb $0xb, -0xd9(%rbp)
leaq -0xd8(%rbp), %rsi
leaq -0xd9(%rbp), %rdx
callq 0x35a90
movq -0xf0(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)