// File: /workspace/github_cpp_makefile_star2_O1/renzibei[P]TSProxy/utils.cpp
// Function: assembleTSPacket(unsigned char*, unsigned long, TSPacket const*)
ssize_t assembleTSPacket(uint8_t *dst, size_t buffer_size, const TSPacket *src_packet) {
    uint8_t *old_dst = dst;
    if (buffer_size < sizeof(src_packet->contentType) + sizeof(src_packet->tsVersion) + sizeof(src_packet->length) ) {
        LogHelper::log(Warn, "buffer size less than TSPacket header, buffer_size: %lu", buffer_size);
        return -1;
    }
    *dst++ = src_packet->contentType; 
    hton_copy2bytes(dst, &(src_packet->tsVersion)); dst += sizeof(src_packet->tsVersion);
    hton_copy2bytes(dst, &(src_packet->length)); dst += sizeof(src_packet->length);
    buffer_size -= sizeof(src_packet->contentType) + sizeof(src_packet->tsVersion) + sizeof(src_packet->length);

    size_t realMsgLen = src_packet->length - sizeof(src_packet->random) - sizeof(src_packet->mac);
    if (buffer_size < realMsgLen) {
        LogHelper::log(Warn, "buffer size less than TSPacket payload length, buffer_size: %lu, payload length: %lu", buffer_size, realMsgLen);
        return -2;
    }
    // size_t delta_dst = assembleInnerMsg(dst, &(src_packet->msgData)); dst += delta_dst;
    
    memcpy(dst, src_packet->msgData, realMsgLen); dst += realMsgLen;
    buffer_size -= realMsgLen;
    if (buffer_size < sizeof(src_packet->random) + sizeof(src_packet->mac)) {
        LogHelper::log(Warn, "buffer size less than TSPacket random, mac");
        return -3;
    }
    // uint8_t *tempDst = dst;
    memcpy(dst, src_packet->random, sizeof(src_packet->random));
    dst += sizeof(src_packet->random);
    memcpy(dst, src_packet->mac, sizeof(src_packet->mac));
    dst += sizeof(src_packet->mac);

    // fprintf(stderr, "when assemble random: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempDst[i]);
    // }
    // fprintf(stderr, "\nmac: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)tempDst[16 + i]);
    // }
    // fprintf(stderr, "\n");

    return dst - old_dst;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %rdx
cmpq $0x4, %rsi
ja 0x5e01
leaq 0x2eb2(%rip), %rsi      # 0x8c9b
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x1, %r15
jmp 0x5eae
movq %rdi, %rbx
movb (%r14), %al
movb %al, (%rdi)
movzwl 0x2(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x1(%rdi)
movzwl 0x4(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x3(%rdi)
addq $-0x5, %rdx
movzwl 0x4(%r14), %r15d
leaq -0x20(%r15), %r12
movq %rdx, %rbp
subq %r12, %rbp
jae 0x5e57
leaq 0x2e94(%rip), %rsi      # 0x8cd3
movl $0x1, %edi
movq %r12, %rcx
xorl %eax, %eax
callq 0x273c
movq $-0x2, %r15
jmp 0x5eae
leaq 0x5(%rbx), %r13
leaq 0x6(%r14), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x2150
cmpq $0x1f, %rbp
ja 0x5e8c
leaq 0x2eb1(%rip), %rsi      # 0x8d28
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x3, %r15
jmp 0x5eae
movups 0x10006(%r14), %xmm0
movups %xmm0, (%r13,%r12)
movups 0x10016(%r14), %xmm0
movups %xmm0, 0x10(%r13,%r12)
addq %r13, %r15
subq %rbx, %r15
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq