// File: /workspace/github_cpp_makefile_star2_O3/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 0x766f1(%rip), %rdi     # 0x7c3e0
movl $0x10026, %edx          # imm = 0x10026
xorl %esi, %esi
callq 0x2120
leaq 0x8670e(%rip), %r15     # 0x8c410
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdi
movq %r14, %rdx
callq 0x5b4a
movq %rax, %r14
testq %rax, %rax
js 0x5d52
leaq 0x30(%rsp), %rdi
callq 0x6064
testl %eax, %eax
je 0x5d76
movl %eax, %r15d
leaq 0x2039(%rip), %rsi      # 0x7d7a
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %r15d, %r14
jmp 0x5d68
leaq 0x1ffa(%rip), %rsi      # 0x7d53
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x273c
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
leaq 0x76660(%rip), %rdi     # 0x7c3e6
leaq 0x86683(%rip), %rdx     # 0x8c410
leaq 0x30(%rsp), %r8
leaq 0x20(%rsp), %r9
movl $0x10000, %esi          # imm = 0x10000
movl %r14d, %ecx
callq 0x6942
movl %eax, %r14d
testl %eax, %eax
js 0x5e36
movb $0x17, 0x7662a(%rip)    # 0x7c3e0
leaq 0x76623(%rip), %rdx     # 0x7c3e0
movw $0x303, 0x7661c(%rip)   # imm = 0x303
addl $0x20, %r14d
movw %r14w, 0x76612(%rip)    # 0x7c3e4
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x86608(%rip)    # 0x8c3e6
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x8660c(%rip)    # 0x8c3f6
leaq 0x8661f(%rip), %rdi     # 0x8c410
movl $0x10000, %esi          # imm = 0x10000
callq 0x5be2
movq %rax, %r14
testq %rax, %rax
js 0x5e51
leaq 0x1fca(%rip), %rsi      # 0x7dd4
movl $0x3, %edi
movl %ebp, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x273c
leaq 0x865ee(%rip), %rsi     # 0x8c410
movl %ebp, %edi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x5917
movq %rax, %r14
jmp 0x5d68
leaq 0x1f4d(%rip), %rsi      # 0x7d8a
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %r14d, %r14
jmp 0x5d68
leaq 0x1f55(%rip), %rsi      # 0x7dad
jmp 0x5d59