// File: /workspace/github_cpp_makefile_star2_O1/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, 0x4(%rsp)
callq 0x6221
movq %rax, %r14
leaq 0xa9cf(%rip), %r15      # 0xd210
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl 0x4(%rsp), %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x6046
testq %rax, %rax
js 0x2986
movzwl 0xa9af(%rip), %edx      # 0xd220
cmpl $0x1, %edx
jne 0x2992
movq 0xa98f(%rip), %r12      # 0xd210
leaq 0xa988(%rip), %rsi      # 0xd210
movq %r14, 0xa989(%rip)      # 0xd218
movl $0x2, 0xa987(%rip)      # 0xd220
movl 0x4(%rsp), %edi
movl $0x1, %edx
callq 0x5ec0
testq %rax, %rax
js 0x29d7
leaq 0xa959(%rip), %r15      # 0xd210
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2120
movl 0x4(%rsp), %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x6046
testq %rax, %rax
jns 0x28ec
leaq 0x57e5(%rip), %rsi      # 0x80c8
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq 0xa925(%rip), %r13      # 0xd218
cmpq %r14, %r13
jne 0x29ab
movq 0xa90d(%rip), %rbp      # 0xd210
leaq 0x1(%r12), %rax
cmpq %rax, %rbp
jne 0x29ab
movzwl 0xa908(%rip), %edx      # 0xd220
cmpl $0x3, %edx
jne 0x29fc
movl $0x50, %edi
callq 0x21d0
movq %rax, %r14
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x2190
movq %rbp, (%r14)
movq %r13, 0x8(%r14)
movq %rax, 0x10(%r14)
leaq 0x48(%r14), %rax
movq %rax, 0x18(%r14)
movq $0x1, 0x20(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r14)
movl $0x3f800000, 0x38(%r14) # imm = 0x3F800000
movups %xmm0, 0x40(%r14)
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x4a0e
movq %r14, (%rax)
leaq 0x5834(%rip), %rsi      # 0x81b3
movl $0x3, %edi
jmp 0x29e3
movq %rax, %r15
leaq 0x5695(%rip), %rsi      # 0x8025
jmp 0x29e1
leaq 0x56c0(%rip), %rsi      # 0x8059
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x4, %r15
jmp 0x29ea
movq 0xa85e(%rip), %rdx      # 0xd210
incq %r12
leaq 0x5740(%rip), %rsi      # 0x80fc
xorl %edi, %edi
movq %r12, %rcx
movq %r13, %r8
movq %r14, %r9
xorl %eax, %eax
callq 0x273c
movq $-0x3, %r15
jmp 0x29ea
movq %rax, %r15
leaq 0x56ac(%rip), %rsi      # 0x808d
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 0x577c(%rip), %rsi      # 0x817f
xorl %edi, %edi
xorl %eax, %eax
callq 0x273c
movq $-0x5, %r15
jmp 0x29ea