// File: /workspace/github_cpp_makefile_star2_O2/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 0x21e0
movl %eax, %edi
callq 0x2210
callq 0x4d77
movq %rax, %r14
leaq 0x8a4f(%rip), %rdi      # 0xb260
movl $0xfff0, %edx           # imm = 0xFFF0
xorl %esi, %esi
callq 0x2140
leaq 0x8a2c(%rip), %r15      # 0xb250
movq %r14, 0x8a25(%rip)      # 0xb250
andq $0x0, 0x8a25(%rip)      # 0xb258
movw $0x1, 0x8a24(%rip)      # 0xb260
callq 0x3b6c
movq %rax, %rdi
callq 0x4290
pushq $0x1
popq %rdx
movl %eax, %edi
movq %r15, %rsi
callq 0x4a43
testq %rax, %rax
js 0x2905
leaq 0x89ea(%rip), %r15      # 0xb250
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2140
callq 0x3b6c
movq %rax, %rdi
callq 0x4290
pushq $0x1
popq %rdx
movl %eax, %edi
movq %r15, %rsi
callq 0x4bc1
testq %rax, %rax
jns 0x28a4
leaq 0x37d5(%rip), %rsi      # 0x6070
xorl %edi, %edi
xorl %eax, %eax
callq 0x26e8
movq 0x89a5(%rip), %rdx      # 0xb250
cmpq %r14, %rdx
jne 0x291a
movzwl 0x89a9(%rip), %edx      # 0xb260
cmpl $0x2, %edx
jne 0x2931
incq %r14
movq %r14, 0x898a(%rip)      # 0xb250
leaq 0x8983(%rip), %r14      # 0xb250
movl $0x3, 0x8989(%rip)      # 0xb260
callq 0x3b6c
movq %rax, %rdi
callq 0x4290
pushq $0x1
popq %rdx
movl %eax, %edi
movq %r14, %rsi
callq 0x4a43
testq %rax, %rax
js 0x294e
movaps 0x8953(%rip), %xmm0     # 0xb250
movups %xmm0, (%rbx)
xorl %r15d, %r15d
jmp 0x2945
movq %rax, %r15
leaq 0x372b(%rip), %rsi      # 0x603a
xorl %edi, %edi
xorl %eax, %eax
callq 0x26e8
jmp 0x2945
leaq 0x3780(%rip), %rsi      # 0x60a1
xorl %edi, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x26e8
pushq $-0x3
jmp 0x2943
leaq 0x37a2(%rip), %rsi      # 0x60da
xorl %edi, %edi
xorl %eax, %eax
callq 0x26e8
pushq $-0x4
popq %r15
movq %r15, %rax
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %r15
leaq 0x37c3(%rip), %rsi      # 0x611b
jmp 0x290f