// File: /workspace/github_cpp_makefile_star2_O3/renzibei[P]TSProxy/utils.cpp
// Function: recvTSPacket(int, InnerMsg*, int)
ssize_t recvTSPacket(int fd, InnerMsg *msg, int isNonBlocking) {
    static uint8_t packetBuffer[PACKET_BUFFER_SIZE];
    LogHelper::log(Debug, "sockFd: %d start to read 5bytes", fd);
    ssize_t readRet = readNBytes(fd, packetBuffer, 5, isNonBlocking);
    if (readRet < 0) {
        return readRet;
    }
    uint8_t tsContentType = *packetBuffer;
    if (tsContentType != TSPacketContentType) {
        LogHelper::log(Warn, "content type not right");
        return -1;
    }
    uint16_t msgLen = 0, tsVersion = 0;
    ntoh_copy2bytes(&tsVersion, packetBuffer + 1);
    
    if (tsVersion != TSPacketVersion) {
        LogHelper::log(Warn, "Packet version not equals to tsversion");
        return -2;
    }

    ntoh_copy2bytes(&(msgLen), packetBuffer + 3);
    LogHelper::log(Debug, "sockFd: %d start to read %d bytes", fd, msgLen);
    readRet = readNBytes(fd, packetBuffer + 5, msgLen, isNonBlocking);
    if (readRet < 0) {
        return readRet;
    }
    static TSPacket packetStruct;
    memset(&packetStruct, 0, sizeof(packetStruct));
    int code = disAssembleTSPacket(&packetStruct, packetBuffer, msgLen + 5);
    if (code < 0) {
        LogHelper::log(Warn, "Fail to disassemble TSPacket in recv");
        return code;
    }
    char key[16];
    int keyRet = constants::getKey(key);
    if(keyRet != 0) {
        LogHelper::log(Error, "Fail to get key");
        return -3;
    }

    // fprintf(stderr, "when recv key: ");
    // for(int i = 0; i < 16; ++i) {
    //     fprintf(stderr, "%d ", (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");

    int textLen = decrypt((char*)packetBuffer, sizeof(packetBuffer), (const char*)packetStruct.msgData, packetStruct.length - 32, key, packetStruct.random, packetStruct.mac);
    if (textLen < 0) {
        LogHelper::log(Warn, "Fail to decrypt when recv ts packet");
        return -4;
    }


    code = disAssembleInnerMsg(msg, packetBuffer, textLen);
    if (code < 0) {
        LogHelper::log(Warn, "Fail to disassemble inner msg in recv");
        return code;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x1f81(%rip), %rsi      # 0x7df7
movl $0x3, %edi
movl %r14d, %edx
xorl %eax, %eax
callq 0x273c
leaq 0x96584(%rip), %rsi     # 0x9c410
movl $0x5, %edx
movl %r14d, %edi
movl %ebp, %ecx
callq 0x5877
testq %rax, %rax
js 0x5f92
cmpb $0x17, 0x96565(%rip)    # 0x9c410
jne 0x5f5c
cmpw $0x303, 0x96557(%rip)   # imm = 0x303
jne 0x5f78
movzwl 0x9654c(%rip), %eax     # 0x9c413
rolw $0x8, %ax
movzwl %ax, %r15d
leaq 0x1f7f(%rip), %rsi      # 0x7e55
movl $0x3, %edi
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x273c
leaq 0x96526(%rip), %rsi     # 0x9c415
movl %r14d, %edi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x5877
testq %rax, %rax
js 0x5f92
leaq 0xa6504(%rip), %r14     # 0xac410
movl $0x10026, %edx          # imm = 0x10026
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
addl $0x5, %r15d
leaq 0x964ea(%rip), %rsi     # 0x9c410
movq %r14, %rdi
movq %r15, %rdx
callq 0x5a15
testl %eax, %eax
js 0x5f9d
leaq 0x10(%rsp), %rdi
callq 0x6064
testl %eax, %eax
je 0x5fb7
leaq 0x1e30(%rip), %rsi      # 0x7d7a
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x3, %rax
jmp 0x5f92
leaq 0x1eb4(%rip), %rsi      # 0x7e17
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x1, %rax
jmp 0x5f92
leaq 0x1eaf(%rip), %rsi      # 0x7e2e
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x2, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
leaq 0x1ed1(%rip), %rsi      # 0x7e77
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %ebp, %rax
jmp 0x5f92
movzwl 0xa6456(%rip), %ecx     # 0xac414
addl $-0x20, %ecx
leaq 0xb645e(%rip), %rax     # 0xbc426
movq %rax, (%rsp)
leaq 0x9643d(%rip), %rdi     # 0x9c410
leaq 0xa643c(%rip), %rdx     # 0xac416
leaq 0xb6435(%rip), %r9      # 0xbc416
leaq 0x10(%rsp), %r8
movl $0x10000, %esi          # imm = 0x10000
callq 0x6a51
testl %eax, %eax
js 0x600d
movl %eax, %edx
leaq 0x96413(%rip), %rsi     # 0x9c410
movq %rbx, %rdi
callq 0x59b4
testl %eax, %eax
js 0x602c
xorl %eax, %eax
jmp 0x5f92
leaq 0x1e88(%rip), %rsi      # 0x7e9c
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x4, %rax
jmp 0x5f92
leaq 0x1e8d(%rip), %rsi      # 0x7ec0
jmp 0x5f63