// File: /workspace/github_cpp_makefile_star2_O1/occivink[P]pyct/pyct.cpp
// Function: encrypt(std::vector<unsigned char, std::allocator<unsigned char>>, std::optional<unsigned long> const&, std::array<unsigned char, 32ul> const&)
vector<u8> encrypt(vector<u8> input, const std::optional<u64>& pad_to, const Hash& hash) {
    if (pad_to and *pad_to < input.size())
        throw invalid_argument{"Padded length is smaller than input"};
    u64 real_length = input.size();
    u64 padded_length = pad_to.value_or(input.size());
    u64 message_total_length = padded_length + 8;

    // allocate space for the padded message, its size, the mac and the nonce
    input.resize(padded_length +                   8 +       16 +        24);
    // the padding is unitialized data but that's fine

    u8* message_data = input.data();
    u8* size_data = message_data + padded_length;
    u8* mac_data = size_data + 8;
    u8* nonce_data = mac_data + 16;

    size_data[7] = static_cast<u8>((real_length & 0x00000000000000FF));
    size_data[6] = static_cast<u8>((real_length & 0x000000000000FF00) >> 8);
    size_data[5] = static_cast<u8>((real_length & 0x0000000000FF0000) >> 16);
    size_data[4] = static_cast<u8>((real_length & 0x00000000FF000000) >> 24);
    size_data[3] = static_cast<u8>((real_length & 0x000000FF00000000) >> 32);
    size_data[2] = static_cast<u8>((real_length & 0x0000FF0000000000) >> 40);
    size_data[1] = static_cast<u8>((real_length & 0x00FF000000000000) >> 48);
    size_data[0] = static_cast<u8>((real_length & 0xFF00000000000000) >> 56);

    // get some random data as the nonce
    random_device device;
    for (int i = 0; i < 6; ++i) {
        auto rand = device();
        nonce_data[i * 4]     = static_cast<u8>((rand & 0x000000FF));
        nonce_data[i * 4 + 1] = static_cast<u8>((rand & 0x000FFF00) >> 8);
        nonce_data[i * 4 + 2] = static_cast<u8>((rand & 0x00FF0000) >> 16);
        nonce_data[i * 4 + 3] = static_cast<u8>((rand & 0xFF000000) >> 24);
    }

    // do in-place encryption
    crypto_lock(mac_data,
                message_data,
                hash.data(),
                nonce_data,
                message_data, message_total_length);
    return input;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x13a8, %rsp           # imm = 0x13A8
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
movb 0x8(%rdx), %al
cmpb $0x1, %al
jne 0xa872
movq 0x8(%r14), %rsi
subq (%r14), %rsi
cmpq %rsi, (%rdx)
jb 0xa93d
movq %rcx, 0x18(%rsp)
movq 0x8(%r14), %r12
subq (%r14), %r12
movq %r12, %rbx
testb %al, %al
je 0xa888
movq (%rdx), %rbx
leaq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x30(%rbx), %rsi
movq %r14, %rdi
callq 0xc0a2
movq (%r14), %r13
leaq (%rbx,%r13), %r15
addq $0x8, %r15
leaq 0x18(%r13,%rbx), %rbp
bswapq %r12
movq %r12, -0x18(%rbp)
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0xc0ce
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x22b0
movl %eax, 0x10(%r15,%rbx,4)
incq %rbx
cmpq $0x6, %rbx
jne 0xa8c3
movq %r15, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq 0x10(%rsp), %r9
callq 0x951a
movq (%r14), %rax
movq 0x8(%rsp), %rbx
movq %rax, (%rbx)
movq 0x8(%r14), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%r14), %rax
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq $0x0, 0x10(%r14)
leaq 0x20(%rsp), %rdi
callq 0x21a0
movq %rbx, %rax
addq $0x13a8, %rsp           # imm = 0x13A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x37bc(%rip), %rsi      # 0xe10d
movq %rax, %rdi
callq 0x23a0
movq 0x7670(%rip), %rsi      # 0x11fd0
movq 0x7659(%rip), %rdx      # 0x11fc0
movq %r14, %rdi
callq 0x2360
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0xa98d
jmp 0xa995
jmp 0xa980
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x2380
movq %rax, %rdi
callq 0xc388