// File: /workspace/github_cpp_makefile_star2_O2/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 0x60c7
movq %rax, %r14
leaq 0x9acd(%rip), %r15      # 0xc220
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
pushq $0x1
popq %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x5f11
testq %rax, %rax
js 0x2892
movzwl 0x9ab1(%rip), %edx      # 0xc230
cmpl $0x1, %edx
jne 0x289e
movq 0x9a91(%rip), %r12      # 0xc220
leaq 0x9a8a(%rip), %rsi      # 0xc220
movq %r14, 0x9a8b(%rip)      # 0xc228
movl $0x2, 0x9a89(%rip)      # 0xc230
pushq $0x1
popq %rdx
movl %ebp, %edi
callq 0x5d93
testq %rax, %rax
js 0x28b2
leaq 0x9a5f(%rip), %r15      # 0xc220
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
pushq $0x1
popq %rdx
movl %ebp, %edi
movq %r15, %rsi
callq 0x5f11
testq %rax, %rax
jns 0x27f2
leaq 0x48df(%rip), %rsi      # 0x70c8
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
movq 0x9a2f(%rip), %r8       # 0xc228
movq %r14, %rax
xorq %r8, %rax
movq 0x9a1a(%rip), %r13      # 0xc220
incq %r12
movq %r13, %rcx
xorq %r12, %rcx
orq %rax, %rcx
jne 0x28c7
movzwl 0x9a11(%rip), %edx      # 0xc230
cmpl $0x3, %edx
jne 0x28e4
pushq $0x50
popq %rdi
callq 0x21e0
movq %rax, %r12
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x21a0
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 0x473a
movq %r12, (%rax)
leaq 0x4926(%rip), %rsi      # 0x71b3
pushq $0x3
popq %rdi
jmp 0x28be
movq %rax, %r15
leaq 0x4789(%rip), %rsi      # 0x7025
jmp 0x28bc
leaq 0x47b4(%rip), %rsi      # 0x7059
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
pushq $-0x4
jmp 0x28f6
movq %rax, %r15
leaq 0x47d1(%rip), %rsi      # 0x708d
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
jmp 0x28f8
leaq 0x482e(%rip), %rsi      # 0x70fc
xorl %edi, %edi
movq %r13, %rdx
movq %r12, %rcx
movq %r14, %r9
xorl %eax, %eax
callq 0x262c
pushq $-0x3
jmp 0x28f6
leaq 0x4894(%rip), %rsi      # 0x717f
xorl %edi, %edi
xorl %eax, %eax
callq 0x262c
pushq $-0x5
popq %r15
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq