// File: night-shift[P]Srl/src/lib/PJson.cpp
// Function: Srl::PJson::write(Srl::Value const&, Srl::Lib::MemBlock const&, Srl::Lib::Out&)
void PJson::write(const Value& value, const MemBlock& name, Out& out)
{
    auto type = value.type();
    this->insert_spacing(type, out);

    this->scope_closed = false;

    if(this->scope_type != Type::Null &&
       this->scope_type != Type::Array &&
       type != Type::Scope_End) {

        insert_in_quotes(out, name);
        out.write(':');

        if(!this->compact) {
            out.write(' ');
        }
    }

    if(TpTools::is_scope(type)) {

        auto c = type == Type::Array ? '[' : '{';

        out.write(c);
        this->scope_stack.push(type);
        this->scope_closed = true;
        this->scope_type = type;

        return;
    }

    if(type == Type::Scope_End) {

        assert(!this->scope_stack.empty());

        auto c = this->scope_type == Type::Array ? ']' : '}';

        out.write(c);
        this->scope_stack.pop();

        this->scope_type = !this->scope_stack.empty()
            ? this->scope_stack.top()
            : Type::Null;

        return;
    }

    if(TpTools::is_scalar(type)) {
        out.write(value.data(), value.size());

    } else {
        bool escape = type != Type::Binary;
        insert_in_quotes(out, { value.data(), value.size() }, escape);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x35b80
movq -0x40(%rbp), %rdi
movb %al, -0x21(%rbp)
movb -0x21(%rbp), %al
movq -0x20(%rbp), %rdx
movzbl %al, %esi
callq 0x341d0
movq -0x40(%rbp), %rax
movb $0x0, 0x61(%rax)
cmpb $0xb, 0x60(%rax)
je 0x4ec0a
movq -0x40(%rbp), %rax
cmpb $0xf, 0x60(%rax)
je 0x4ec0a
cmpb $0x10, -0x21(%rbp)
je 0x4ec0a
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x4ee70
movq -0x20(%rbp), %rdi
movb $0x3a, -0x22(%rbp)
leaq -0x22(%rbp), %rsi
callq 0x34740
movq -0x40(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0x4ec08
movq -0x20(%rbp), %rdi
movb $0x20, -0x23(%rbp)
leaq -0x23(%rbp), %rsi
callq 0x34740
jmp 0x4ec0a
movzbl -0x21(%rbp), %edi
callq 0x35db0
testb $0x1, %al
jne 0x4ec19
jmp 0x4ec68
movb -0x21(%rbp), %cl
movb $0x5b, %al
movb $0x7b, %dl
movb %dl, -0x42(%rbp)
cmpb $0xf, %cl
movb %al, -0x41(%rbp)
je 0x4ec31
movb -0x42(%rbp), %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
movb %al, -0x24(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x34740
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
leaq -0x21(%rbp), %rsi
callq 0x372f0
movq -0x40(%rbp), %rax
movb $0x1, 0x61(%rax)
movb -0x21(%rbp), %cl
movb %cl, 0x60(%rax)
jmp 0x4edad
cmpb $0x10, -0x21(%rbp)
jne 0x4ed1f
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x36a30
xorb $-0x1, %al
testb $0x1, %al
jne 0x4ec87
jmp 0x4ec89
jmp 0x4eca8
leaq 0x1abc3(%rip), %rdi     # 0x69853
leaq 0x1abd7(%rip), %rsi     # 0x6986e
movl $0x84, %edx
leaq 0x1abdd(%rip), %rcx     # 0x69880
callq 0x349a0
movq -0x40(%rbp), %rax
movb 0x60(%rax), %cl
movb $0x5d, %al
movb $0x7d, %dl
movb %dl, -0x44(%rbp)
cmpb $0xf, %cl
movb %al, -0x43(%rbp)
je 0x4ecc4
movb -0x44(%rbp), %al
movb %al, -0x43(%rbp)
movb -0x43(%rbp), %al
movb %al, -0x25(%rbp)
movq -0x20(%rbp), %rdi
leaq -0x25(%rbp), %rsi
callq 0x34740
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x35800
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x36a30
testb $0x1, %al
jne 0x4ed09
movq -0x40(%rbp), %rdi
addq $0x10, %rdi
callq 0x37f90
movb (%rax), %al
movb %al, -0x45(%rbp)
jmp 0x4ed10
movb $0xb, %al
movb %al, -0x45(%rbp)
jmp 0x4ed10
movq -0x40(%rbp), %rax
movb -0x45(%rbp), %cl
movb %cl, 0x60(%rax)
jmp 0x4edad
movzbl -0x21(%rbp), %edi
callq 0x345d0
testb $0x1, %al
jne 0x4ed2e
jmp 0x4ed5e
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x36770
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x34510
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq %rax, %rdx
callq 0x364e0
jmp 0x4edad
cmpb $0xd, -0x21(%rbp)
setne %al
andb $0x1, %al
movb %al, -0x26(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x36770
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x34510
movq -0x68(%rbp), %rsi
movq %rax, %rdx
leaq -0x38(%rbp), %rdi
callq 0x33280
movq -0x60(%rbp), %rdi
movb -0x26(%rbp), %al
leaq -0x38(%rbp), %rsi
andb $0x1, %al
movzbl %al, %edx
callq 0x4ee70
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)