// File: renzibei[P]TSProxy/proxyclient.cpp
// Function: recvMessage(int, unsigned char*, unsigned long, ClientContext*, unsigned char*)
ssize_t recvMessage(int sockFd, uint8_t *dst, size_t bufferSize, ClientContext *ctx, uint8_t *msgTypePtr = NULL) {
    static InnerMsg innerMsg;
    memset(&innerMsg, 0, sizeof(innerMsg));

    ssize_t recvCode = recvTSPacket(sockFd, &innerMsg, constants::serverNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Error, "Fail to recv data");
        return recvCode;
    }
    if (innerMsg.sSeq != ctx->sSeq + 1) {
        LogHelper::log(Error, "Fail to recv data, seq error, sSeq: %llu, expected: %llu", innerMsg.sSeq, ctx->sSeq + 1);
        return -2;
    }
    if (innerMsg.msgType != 5 && innerMsg.msgType != 7 && innerMsg.msgType != 9) {
        LogHelper::log(Error, "Fail to recv data, msgType error, recv type :%d", innerMsg.msgType);
        return -3;
    }
    if (msgTypePtr != NULL)
        *msgTypePtr = innerMsg.msgType;
    if (innerMsg.dataLength > bufferSize) {
        LogHelper::log(Error, "Fail to recv data, bufferSize not enought, bufferSize :%lu, expected: %lu", bufferSize, innerMsg.dataLength);
        return -4;
    }
    memcpy(dst, innerMsg.data, innerMsg.dataLength);
    ctx->sSeq = innerMsg.sSeq;
    return innerMsg.dataLength;
}

// Assembly:
subq $0x38, %rsp
movl %edi, 0x2c(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %r8, 0x8(%rsp)
leaq 0x3e26d(%rip), %rdi     # 0x41240
xorl %esi, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x2150
movl 0x2c(%rsp), %edi
leaq 0x3e256(%rip), %rsi     # 0x41240
movl $0x1, %edx
callq 0x8a20
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jge 0x301d
xorl %edi, %edi
leaq 0x81d5(%rip), %rsi      # 0xb1dd
movb $0x0, %al
callq 0x2a50
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x3138
movq 0x3e224(%rip), %rax     # 0x41248
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
je 0x3068
movq 0x3e20b(%rip), %rdx     # 0x41248
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
xorl %edi, %edi
leaq 0x819c(%rip), %rsi      # 0xb1ef
movb $0x0, %al
callq 0x2a50
movq $-0x2, 0x30(%rsp)
jmp 0x3138
movzwl 0x3e1e1(%rip), %eax     # 0x41250
cmpl $0x5, %eax
je 0x30b1
movzwl 0x3e1d5(%rip), %eax     # 0x41250
cmpl $0x7, %eax
je 0x30b1
movzwl 0x3e1c9(%rip), %eax     # 0x41250
cmpl $0x9, %eax
je 0x30b1
movzwl 0x3e1bd(%rip), %edx     # 0x41250
xorl %edi, %edi
leaq 0x818c(%rip), %rsi      # 0xb228
movb $0x0, %al
callq 0x2a50
movq $-0x3, 0x30(%rsp)
jmp 0x3138
cmpq $0x0, 0x8(%rsp)
je 0x30c9
movw 0x3e190(%rip), %ax      # 0x41250
movb %al, %cl
movq 0x8(%rsp), %rax
movb %cl, (%rax)
movzwl 0x3e182(%rip), %eax     # 0x41252
cmpq 0x18(%rsp), %rax
jbe 0x30fe
movq 0x18(%rsp), %rdx
movzwl 0x3e16f(%rip), %ecx     # 0x41252
xorl %edi, %edi
leaq 0x816c(%rip), %rsi      # 0xb258
movb $0x0, %al
callq 0x2a50
movq $-0x4, 0x30(%rsp)
jmp 0x3138
movq 0x20(%rsp), %rdi
movzwl 0x3e148(%rip), %eax     # 0x41252
movl %eax, %edx
leaq 0x3e12d(%rip), %rsi     # 0x41240
addq $0x14, %rsi
callq 0x21c0
movq 0x3e125(%rip), %rcx     # 0x41248
movq 0x10(%rsp), %rax
movq %rcx, 0x8(%rax)
movzwl 0x3e11f(%rip), %eax     # 0x41252
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)