// File: /workspace/github_cpp_makefile_star2_O3/renzibei[P]TSProxy/proxyserver.cpp
// Function: serverHandShake(int, std::unordered_map<int, ClientNode*, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, ClientNode*>>>&)
ssize_t serverHandShake(int sockFd, ClientMap &cMap) {
    uint64_t sSeq = getRand63();
    static InnerMsg innerMsg;
    memset(&innerMsg, 0, sizeof(innerMsg));

    ssize_t recvCode = recvTSPacket(sockFd, &innerMsg, constants::serverNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Error, "server hand shake failed, fail to recv first packet");
        return recvCode;
    }
    if (innerMsg.msgType != 1) {
        LogHelper::log(Error, "server hand shake failed, first packet type get: %d", innerMsg.msgType);
        return -4;
    }

    uint64_t cSeq = innerMsg.cSeq;

    innerMsg.sSeq = sSeq;
    innerMsg.msgType = 2;
    innerMsg.dataLength = 0;
    ssize_t sendCode = sendTSPacket(sockFd, &innerMsg, constants::serverNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Error, "server hand shake shake failed, fail to send second packet");
        return sendCode;
    }

    memset(&innerMsg, 0, sizeof(innerMsg));
    recvCode = recvTSPacket(sockFd, &innerMsg, constants::serverNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Error, "server hand shake failed, fail to recv third packet");
    }
    if (innerMsg.sSeq != sSeq || innerMsg.cSeq != cSeq + 1) {
        LogHelper::log(Error, "server hand shake failed, third packet receive seq not inc, receive cSeq: %llu, expected: %llu, receive sSeq: %llu, expected: %llu", innerMsg.cSeq, cSeq + 1, innerMsg.sSeq, sSeq);
        return -3;
    }
    if (innerMsg.msgType != 3) {
        LogHelper::log(Error, "server hand shake failed, third packet type get: %d", innerMsg.msgType);
        return -5;
    }
    
    cMap[sockFd] = new ClientNode(innerMsg.cSeq, innerMsg.sSeq, time(NULL));
    LogHelper::log(Debug, "Server handshake completed");
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl %edi, %ebp
movl %edi, 0x4(%rsp)
callq 0x6038
movq %rax, %r14
leaq 0x99cd(%rip), %r15      # 0xc210
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl %ebp, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x5e5d
testq %rax, %rax
js 0x298c
movzwl 0x99af(%rip), %edx      # 0xc220
cmpl $0x1, %edx
jne 0x2998
movq 0x998f(%rip), %r12      # 0xc210
leaq 0x9988(%rip), %rsi      # 0xc210
movq %r14, 0x9989(%rip)      # 0xc218
movl $0x2, 0x9987(%rip)      # 0xc220
movl %ebp, %edi
movl $0x1, %edx
callq 0x5cd7
testq %rax, %rax
js 0x29b1
leaq 0x995b(%rip), %r15      # 0xc210
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl %ebp, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x5e5d
testq %rax, %rax
jns 0x28e8
leaq 0x47e9(%rip), %rsi      # 0x70c8
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq 0x9929(%rip), %r8       # 0xc218
movq %r14, %rax
xorq %r8, %rax
movq 0x9914(%rip), %r13      # 0xc210
incq %r12
movq %r13, %rcx
xorq %r12, %rcx
orq %rax, %rcx
jne 0x29d6
movzwl 0x990b(%rip), %edx      # 0xc220
cmpl $0x3, %edx
jne 0x29f8
movl $0x50, %edi
callq 0x21d0
movq %rax, %r12
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x2190
movq %r13, (%r12)
movq %r14, 0x8(%r12)
movq %rax, 0x10(%r12)
leaq 0x48(%r12), %rax
movq %rax, 0x18(%r12)
movq $0x1, 0x20(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r12)
movl $0x3f800000, 0x38(%r12) # imm = 0x3F800000
movups %xmm0, 0x40(%r12)
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x48c8
movq %r12, (%rax)
leaq 0x482e(%rip), %rsi      # 0x71b3
movl $0x3, %edi
jmp 0x29bd
movq %rax, %r15
leaq 0x468f(%rip), %rsi      # 0x7025
jmp 0x29bb
leaq 0x46ba(%rip), %rsi      # 0x7059
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x4, %r15
jmp 0x29c4
movq %rax, %r15
leaq 0x46d2(%rip), %rsi      # 0x708d
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x471f(%rip), %rsi      # 0x70fc
xorl %edi, %edi
movq %r13, %rdx
movq %r12, %rcx
movq %r14, %r9
xorl %eax, %eax
callq 0x273c
movq $-0x3, %r15
jmp 0x29c4
leaq 0x4780(%rip), %rsi      # 0x717f
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x5, %r15
jmp 0x29c4