// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, 0x18(%rsp)
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
cmpb $0x0, 0x8(%rdx)
je 0xaacf
movq (%rdx), %r12
movq 0x8(%r14), %rbx
subq (%r14), %rbx
cmpq %rbx, %r12
jae 0xaad9
movl $0x10, %edi
callq 0x20f0
movq %rax, %r14
leaq 0x366c(%rip), %rsi      # 0xe11d
movq %rax, %rdi
callq 0x23a0
movq 0x7510(%rip), %rsi      # 0x11fd0
movq 0x74f9(%rip), %rdx      # 0x11fc0
movq %r14, %rdi
callq 0x2360
movq 0x8(%r14), %r12
subq (%r14), %r12
movq %r12, %rbx
leaq 0x8(%r12), %rax
movq %rax, 0x10(%rsp)
leaq 0x30(%r12), %rsi
movq %r14, %rdi
callq 0xc238
movq (%r14), %r13
leaq (%r12,%r13), %r15
addq $0x8, %r15
leaq 0x18(%r13,%r12), %rbp
bswapq %rbx
movq %rbx, -0x18(%rbp)
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0xc264
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x22b0
movl %eax, 0x10(%r15,%rbx,4)
incq %rbx
cmpq $0x6, %rbx
jne 0xab16
movq %r15, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movq 0x10(%rsp), %r9
callq 0x96f2
movups (%r14), %xmm0
movq 0x8(%rsp), %rbx
movups %xmm0, (%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
movq %rax, %rbx
movq %r14, %rdi
callq 0x2160
jmp 0xaba7
jmp 0xabaf
jmp 0xab9a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x21a0
movq %rbx, %rdi
callq 0x2380
movq %rax, %rdi
callq 0xc4bb