// File: /workspace/github_cpp_makefile_star2_O1/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 0x77508(%rip), %rdi     # 0x7d3e0
movl $0x10026, %edx          # imm = 0x10026
xorl %esi, %esi
callq 0x2120
leaq 0x87525(%rip), %r15     # 0x8d410
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 0x5d33
movq %rax, %r14
testq %rax, %rax
js 0x5f3b
leaq 0x30(%rsp), %rdi
callq 0x624c
testl %eax, %eax
je 0x5f5f
movl %eax, %r15d
leaq 0x2e50(%rip), %rsi      # 0x8d7a
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %r15d, %r14
jmp 0x5f51
leaq 0x2e11(%rip), %rsi      # 0x8d53
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 0x77477(%rip), %rdi     # 0x7d3e6
leaq 0x8749a(%rip), %rdx     # 0x8d410
leaq 0x30(%rsp), %r8
leaq 0x20(%rsp), %r9
movl $0x10000, %esi          # imm = 0x10000
movl %r14d, %ecx
callq 0x6b5d
movl %eax, %r14d
testl %eax, %eax
js 0x601f
movb $0x17, 0x77441(%rip)    # 0x7d3e0
leaq 0x7743a(%rip), %rdx     # 0x7d3e0
movw $0x303, 0x77433(%rip)   # imm = 0x303
addl $0x20, %r14d
movw %r14w, 0x77429(%rip)    # 0x7d3e4
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x8741f(%rip)    # 0x8d3e6
movaps 0x10(%rsp), %xmm0
movups %xmm0, 0x87423(%rip)    # 0x8d3f6
leaq 0x87436(%rip), %rdi     # 0x8d410
movl $0x10000, %esi          # imm = 0x10000
callq 0x5dcb
movq %rax, %r14
testq %rax, %rax
js 0x603a
leaq 0x2de1(%rip), %rsi      # 0x8dd4
movl $0x3, %edi
movl %ebp, %edx
movq %r14, %rcx
xorl %eax, %eax
callq 0x273c
leaq 0x87405(%rip), %rsi     # 0x8d410
movl %ebp, %edi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x5ace
movq %rax, %r14
jmp 0x5f51
leaq 0x2d64(%rip), %rsi      # 0x8d8a
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %r14d, %r14
jmp 0x5f51
leaq 0x2d6c(%rip), %rsi      # 0x8dad
jmp 0x5f42