// File: /workspace/github_cpp_makefile_star2_O3/renzibei[P]TSProxy/proxyclient.cpp
// Function: clientHandleShake(ClientContext*)
ssize_t clientHandleShake(ClientContext *ctx) {
    srand(time(NULL));
    uint64_t cSeq = getRand63();
    static InnerMsg innerMsg;
    memset(&innerMsg, 0, sizeof(innerMsg));
    innerMsg.cSeq = cSeq;
    innerMsg.sSeq = 0;
    innerMsg.msgType = 1;
    innerMsg.dataLength = 0;
    ssize_t sendCode = sendTSPacket(SocketPlugin::getInstance()->getSockFd(), &innerMsg, constants::clientNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Error, "client handle shake failed, fail to send first packet");
        return sendCode;
    }
    memset(&innerMsg, 0, sizeof(innerMsg));
    ssize_t recvCode = recvTSPacket(SocketPlugin::getInstance()->getSockFd(), &innerMsg, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Error, "client handle failed, fail to recv second packet");
    }
    if (innerMsg.cSeq != cSeq) {
        LogHelper::log(Error, "receive cSeq not inc, receive cSeq: %llu, expected: %llu", innerMsg.cSeq, cSeq);
        return -3;
    }
    if (innerMsg.msgType != 2) {
        LogHelper::log(Error, "client hand shake failed, msgType not equals to 2, recv Type: %d", innerMsg.msgType);
        return -4;
    }
    innerMsg.cSeq++;
    // innerMsg.sSeq;
    innerMsg.msgType = 3;
    innerMsg.dataLength = 0;
    sendCode = sendTSPacket(SocketPlugin::getInstance()->getSockFd(), &innerMsg, constants::clientNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Error, "client handle shake failed, fail to send third packet");
        return sendCode;
    }

    *ctx = ClientContext(innerMsg.cSeq, innerMsg.sSeq);
    return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
xorl %edi, %edi
callq 0x21d0
movl %eax, %edi
callq 0x2200
callq 0x5070
movq %rax, %r14
leaq 0x794f(%rip), %rdi      # 0xa250
movl $0xfff0, %edx           # imm = 0xFFF0
xorl %esi, %esi
callq 0x2130
leaq 0x792c(%rip), %r15      # 0xa240
movq %r14, 0x7925(%rip)      # 0xa240
movq $0x0, 0x7922(%rip) # 0xa248
movw $0x1, 0x7921(%rip)      # 0xa250
callq 0x3ee8
movq %rax, %rdi
callq 0x44ee
movl %eax, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x4d0f
testq %rax, %rax
js 0x29fe
leaq 0x78e5(%rip), %r15      # 0xa240
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
callq 0x3ee8
movq %rax, %rdi
callq 0x44ee
movl %eax, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x4e95
testq %rax, %rax
jns 0x299b
leaq 0x36de(%rip), %rsi      # 0x6070
xorl %edi, %edi
xorl %eax, %eax
callq 0x27f8
movq 0x789e(%rip), %rdx      # 0xa240
cmpq %r14, %rdx
jne 0x2a13
movzwl 0x78a2(%rip), %edx      # 0xa250
cmpl $0x2, %edx
jne 0x2a2f
incq %r14
movq %r14, 0x7883(%rip)      # 0xa240
leaq 0x787c(%rip), %r14      # 0xa240
movl $0x3, 0x7882(%rip)      # 0xa250
callq 0x3ee8
movq %rax, %rdi
callq 0x44ee
movl %eax, %edi
movq %r14, %rsi
movl $0x1, %edx
callq 0x4d0f
testq %rax, %rax
js 0x2a4f
movaps 0x784a(%rip), %xmm0     # 0xa240
movups %xmm0, (%rbx)
xorl %r15d, %r15d
jmp 0x2a46
movq %rax, %r15
leaq 0x3632(%rip), %rsi      # 0x603a
xorl %edi, %edi
xorl %eax, %eax
callq 0x27f8
jmp 0x2a46
leaq 0x3687(%rip), %rsi      # 0x60a1
xorl %edi, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27f8
movq $-0x3, %r15
jmp 0x2a46
leaq 0x36a4(%rip), %rsi      # 0x60da
xorl %edi, %edi
xorl %eax, %eax
callq 0x27f8
movq $-0x4, %r15
movq %r15, %rax
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %r15
leaq 0x36c2(%rip), %rsi      # 0x611b
jmp 0x2a08