// File: /workspace/github_cpp_makefile_star2_O1/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 0x2d98(%rip), %rsi      # 0x8df7
movl $0x3, %edi
movl %r14d, %edx
xorl %eax, %eax
callq 0x273c
leaq 0x9739b(%rip), %rsi     # 0x9d410
movl $0x5, %edx
movl %r14d, %edi
movl %ebp, %ecx
callq 0x59fa
testq %rax, %rax
js 0x617b
cmpb $0x17, 0x9737c(%rip)    # 0x9d410
jne 0x6145
cmpw $0x303, 0x9736e(%rip)   # imm = 0x303
jne 0x6161
movzwl 0x97363(%rip), %eax     # 0x9d413
rolw $0x8, %ax
movzwl %ax, %r15d
leaq 0x2d96(%rip), %rsi      # 0x8e55
movl $0x3, %edi
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x273c
leaq 0x9733d(%rip), %rsi     # 0x9d415
movl %r14d, %edi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x59fa
testq %rax, %rax
js 0x617b
leaq 0xa731b(%rip), %r14     # 0xad410
movl $0x10026, %edx          # imm = 0x10026
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
addl $0x5, %r15d
leaq 0x97301(%rip), %rsi     # 0x9d410
movq %r14, %rdi
movq %r15, %rdx
callq 0x5bfe
testl %eax, %eax
js 0x6186
leaq 0x10(%rsp), %rdi
callq 0x624c
testl %eax, %eax
je 0x61a0
leaq 0x2c47(%rip), %rsi      # 0x8d7a
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x3, %rax
jmp 0x617b
leaq 0x2ccb(%rip), %rsi      # 0x8e17
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x1, %rax
jmp 0x617b
leaq 0x2cc6(%rip), %rsi      # 0x8e2e
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 0x2ce8(%rip), %rsi      # 0x8e77
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movslq %ebp, %rax
jmp 0x617b
movzwl 0xa726d(%rip), %ecx     # 0xad414
addl $-0x20, %ecx
leaq 0xb7275(%rip), %rax     # 0xbd426
movq %rax, (%rsp)
leaq 0x97254(%rip), %rdi     # 0x9d410
leaq 0xa7253(%rip), %rdx     # 0xad416
leaq 0xb724c(%rip), %r9      # 0xbd416
leaq 0x10(%rsp), %r8
movl $0x10000, %esi          # imm = 0x10000
callq 0x6c6c
testl %eax, %eax
js 0x61f6
movl %eax, %edx
leaq 0x9722a(%rip), %rsi     # 0x9d410
movq %rbx, %rdi
callq 0x5b9d
testl %eax, %eax
js 0x6215
xorl %eax, %eax
jmp 0x617b
leaq 0x2c9f(%rip), %rsi      # 0x8e9c
movl $0x1, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x4, %rax
jmp 0x617b
leaq 0x2ca4(%rip), %rsi      # 0x8ec0
jmp 0x614c