// File: /workspace/github_cpp_makefile_star2_O1/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 0x5271
movq %rax, %r14
leaq 0x893f(%rip), %r15      # 0xb240
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
movq %r14, 0x8929(%rip)      # 0xb240
movq $0x0, 0x8926(%rip) # 0xb248
movl $0x1, 0x8924(%rip)      # 0xb250
callq 0x408e
movq %rax, %rdi
callq 0x4672
movl %eax, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x4f10
testq %rax, %rax
js 0x29fb
leaq 0x88e8(%rip), %r15      # 0xb240
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
callq 0x408e
movq %rax, %rdi
callq 0x4672
movl %eax, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x5096
testq %rax, %rax
jns 0x2998
leaq 0x46e1(%rip), %rsi      # 0x7070
xorl %edi, %edi
xorl %eax, %eax
callq 0x27f8
movq 0x88a1(%rip), %rdx      # 0xb240
cmpq %r14, %rdx
jne 0x2a10
movzwl 0x88a5(%rip), %eax      # 0xb250
cmpl $0x2, %eax
jne 0x2a2c
incq %rdx
movq %rdx, 0x8886(%rip)      # 0xb240
leaq 0x887f(%rip), %r14      # 0xb240
movl $0x3, 0x8885(%rip)      # 0xb250
callq 0x408e
movq %rax, %rdi
callq 0x4672
movl %eax, %edi
movq %r14, %rsi
movl $0x1, %edx
callq 0x4f10
testq %rax, %rax
js 0x2a4e
movaps 0x884d(%rip), %xmm0     # 0xb240
movups %xmm0, (%rbx)
xorl %r15d, %r15d
jmp 0x2a45
movq %rax, %r15
leaq 0x4635(%rip), %rsi      # 0x703a
xorl %edi, %edi
xorl %eax, %eax
callq 0x27f8
jmp 0x2a45
leaq 0x468a(%rip), %rsi      # 0x70a1
xorl %edi, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27f8
movq $-0x3, %r15
jmp 0x2a45
leaq 0x46a7(%rip), %rsi      # 0x70da
xorl %edi, %edi
movl %eax, %edx
xorl %eax, %eax
callq 0x27f8
movq $-0x4, %r15
movq %r15, %rax
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %r15
leaq 0x46c3(%rip), %rsi      # 0x711b
jmp 0x2a05