// File: /workspace/github_cpp_makefile_star2_O2/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 0x1ecd(%rip), %rsi      # 0x7df7
pushq $0x3
popq %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x262c
leaq 0x964e2(%rip), %rsi     # 0x9c420
pushq $0x5
popq %rdx
movl %r14d, %edi
movl %ebp, %ecx
callq 0x595b
testq %rax, %rax
js 0x602a
cmpb $0x17, 0x964c5(%rip)    # 0x9c420
jne 0x6001
cmpw $0x303, 0x964b7(%rip)   # imm = 0x303
jne 0x6016
movzwl 0x964ac(%rip), %eax     # 0x9c423
rolw $0x8, %ax
movzwl %ax, %r15d
leaq 0x1ecf(%rip), %rsi      # 0x7e55
pushq $0x3
popq %rdi
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x262c
leaq 0x96488(%rip), %rsi     # 0x9c425
movl %r14d, %edi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x595b
testq %rax, %rax
js 0x602a
leaq 0xa646a(%rip), %r14     # 0xac420
movl $0x10026, %edx          # imm = 0x10026
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
addl $0x5, %r15d
leaq 0x96450(%rip), %rsi     # 0x9c420
movq %r14, %rdi
movq %r15, %rdx
callq 0x5b0f
testl %eax, %eax
js 0x6035
leaq 0x10(%rsp), %rdi
callq 0x60f4
testl %eax, %eax
je 0x604d
leaq 0x1d86(%rip), %rsi      # 0x7d7a
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
pushq $-0x3
jmp 0x6029
leaq 0x1e0f(%rip), %rsi      # 0x7e17
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
pushq $-0x1
jmp 0x6029
leaq 0x1e11(%rip), %rsi      # 0x7e2e
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
pushq $-0x2
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
leaq 0x1e39(%rip), %rsi      # 0x7e77
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
movslq %ebp, %rax
jmp 0x602a
movzwl 0xa63d0(%rip), %ecx     # 0xac424
addl $-0x20, %ecx
leaq 0xb63d8(%rip), %rax     # 0xbc436
movq %rax, (%rsp)
leaq 0x963b7(%rip), %rdi     # 0x9c420
leaq 0xa63b6(%rip), %rdx     # 0xac426
leaq 0xb63af(%rip), %r9      # 0xbc426
leaq 0x10(%rsp), %r8
movl $0x10000, %esi          # imm = 0x10000
callq 0x6aa5
testl %eax, %eax
js 0x60a3
movl %eax, %edx
leaq 0x9638d(%rip), %rsi     # 0x9c420
movq %rbx, %rdi
callq 0x5ab2
testl %eax, %eax
js 0x60bb
xorl %eax, %eax
jmp 0x602a
leaq 0x1df2(%rip), %rsi      # 0x7e9c
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x262c
pushq $-0x4
jmp 0x6029
leaq 0x1dfe(%rip), %rsi      # 0x7ec0
jmp 0x6008