// File: /workspace/github_cpp_makefile_star2_O2/renzibei[P]TSProxy/utils.cpp
// Function: sendTSPacket(int, InnerMsg const*, int)
ssize_t sendTSPacket(int fd, const InnerMsg *msg, int isNonBlocking) {
    static TSPacket packetStruct;
    // static InnerMsg msgStruct;
    memset(&packetStruct, 0, sizeof(TSPacket));
    // memset(&msgStruct, 0, sizeof(msgStruct));
    
    
    // msgStruct.cSeq = cSeq;
    // msgStruct.sSeq = sSeq;
    // msgStruct.dataLength = nbyte;
    // memcpy(msgStruct.data, src, nbyte);
    static uint8_t packetBuffer[PACKET_BUFFER_SIZE];
    memset(packetBuffer, 0, PACKET_BUFFER_SIZE);

    ssize_t dataLen = assembleInnerMsg(packetBuffer, sizeof(packetBuffer), msg);
    if (dataLen < 0) {
        LogHelper::log(Warn, "Fail to assemble InnerMsg, retCode: %d", dataLen);
        return dataLen;
    }
    char key[16], random[16], mac[16];
    int keyRet = constants::getKey(key);
    if (keyRet != 0) {
        LogHelper::log(Warn, "Fail to get key");
        return keyRet;
    } 
    int chiperLen = encrypt((char *)(packetStruct.msgData), sizeof(packetStruct.msgData), (const char*)packetBuffer, dataLen, key, random, mac);
    if (chiperLen < 0) {
        LogHelper::log(Warn, "Fail to encrypt when send TSPacket");
        return chiperLen;
    }
    
    packetStruct.contentType = TSPacketContentType;
    packetStruct.tsVersion = TSPacketVersion;
    packetStruct.length = chiperLen + sizeof(random) + sizeof(mac);
    memcpy(packetStruct.random, random, sizeof(random));
    memcpy(packetStruct.mac, mac, sizeof(mac));

    // fprintf(stderr, "when send, key: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)key[i]);
    // }
    // fprintf(stderr, "\nrandom: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)packetStruct.random[i]);
    // }
    // fprintf(stderr, "\nmac: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%u ", (uint8_t)packetStruct.mac[i]);
    // }
    // fprintf(stderr, "\n");

    dataLen = assembleTSPacket(packetBuffer, sizeof(packetBuffer), &packetStruct);
    if (dataLen < 0) {
        LogHelper::log(Warn, "Fail to assemble TSPacket, retCode: %d", dataLen);
        return dataLen;
    }
    // size_t dataLen = assembleTSPacket(packetBuffer, &packetStruct);
    LogHelper::log(Debug, "sockFd: %d start to write %d bytes", fd, dataLen);
    return writeNBytes(fd, packetBuffer, dataLen, isNonBlocking);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebx
movq %rsi, %r14
movl %edi, %ebp
leaq 0x76645(%rip), %rdi     # 0x7c3f0
movl $0x10026, %edx          # imm = 0x10026
xorl %esi, %esi
callq 0x2130
leaq 0x86662(%rip), %r15     # 0x8c420
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdi
movq %r14, %rdx
callq 0x5c29
movq %rax, %r14
testq %rax, %rax
js 0x5e0c
leaq 0x30(%rsp), %rdi
callq 0x60f4
testl %eax, %eax
je 0x5e2e
movl %eax, %r15d
leaq 0x1f7d(%rip), %rsi      # 0x7d7a
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
movslq %r15d, %r14
jmp 0x5e20
leaq 0x1f40(%rip), %rsi      # 0x7d53
pushq $0x1
popq %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x262c
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
leaq 0x765b8(%rip), %rdi     # 0x7c3f6
leaq 0x865db(%rip), %rdx     # 0x8c420
leaq 0x30(%rsp), %r8
leaq 0x20(%rsp), %r9
movl $0x10000, %esi          # imm = 0x10000
movl %r14d, %ecx
callq 0x6998
movl %eax, %r14d
testl %eax, %eax
js 0x5eec
movb $0x17, 0x76582(%rip)    # 0x7c3f0
leaq 0x7657b(%rip), %rdx     # 0x7c3f0
movw $0x303, 0x76574(%rip)   # imm = 0x303
addl $0x20, %r14d
movw %r14w, 0x7656a(%rip)    # 0x7c3f4
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x86560(%rip)    # 0x8c3f6
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x86564(%rip)    # 0x8c406
leaq 0x86577(%rip), %rdi     # 0x8c420
movl $0x10000, %esi          # imm = 0x10000
callq 0x5cb4
movq %rax, %r14
testq %rax, %rax
js 0x5f05
leaq 0x1f12(%rip), %rsi      # 0x7dd4
pushq $0x3
popq %rdi
movl %ebp, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x262c
leaq 0x86548(%rip), %rsi     # 0x8c420
movl %ebp, %edi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x5a08
movq %rax, %r14
jmp 0x5e20
leaq 0x1e97(%rip), %rsi      # 0x7d8a
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
movslq %r14d, %r14
jmp 0x5e20
leaq 0x1ea1(%rip), %rsi      # 0x7dad
jmp 0x5e13