// File: /workspace/github_cpp_makefile_star2_O1/renzibei[P]TSProxy/proxyclient.cpp
// Function: socksHandShake(int, int, ClientContext*, std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<int const, int>>>&)
int socksHandShake(int localFd, int serverFd, ClientContext *ctx, LocalFdMap &localFdMap) {
    LogHelper::log(Debug, "client start to socks handshake for localFd: %d", localFd);
    static uint8_t socksBuffer[sizeof(SocksTcpReply)];
    memset(socksBuffer, 0, sizeof(socksBuffer));
    int recvCode = readNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to socks handshake in reading header in the first step");
        return recvCode;
    }
    uint8_t socksVersion = socksBuffer[0];
    uint8_t methodsNum = socksBuffer[1];
    if (socksVersion != constants::SocksVersion) {
        LogHelper::log(Warn, "socks version mismatch, version: %d", socksVersion);
        return -1;
    }
    memset(socksBuffer, 0, 2);
    recvCode = readNBytes(localFd, socksBuffer, methodsNum, constants::clientNonBlocking);
    if (recvCode <= 0 || methodsNum != recvCode) {
        LogHelper::log(Warn, "Fail to socks handshake in reading methods");
        return recvCode;
    }
    uint8_t supportMethod = constants::SocksNoSupportMethod;
    for (int i = 0; i < methodsNum; ++i) {
        if (socksBuffer[i] == constants::SocksNoAuthMethod) {
            supportMethod = socksBuffer[i];
            break;
        }
    }
    if (supportMethod == constants::SocksNoSupportMethod) {
        LogHelper::log(Warn, "No supportMethod in socks");
        // return -2;
    }
    memset(socksBuffer, 0, methodsNum);
    socksBuffer[0] = constants::SocksVersion;
    socksBuffer[1] = supportMethod;
    int sendCode = writeNBytes(localFd, socksBuffer, 2, constants::clientNonBlocking);
    if (sendCode < 0) {
        LogHelper::log(Warn, "Failed to send second packet in socks handshake");
        return sendCode;
    }

    memset(socksBuffer, 0, sizeof(socksBuffer));
    recvCode = readNBytes(localFd, socksBuffer, 4, constants::clientNonBlocking);
    if (recvCode < 0) {
        LogHelper::log(Warn, "Fail to recv thrid socks handshake header");
        return recvCode;
    }
    socksVersion = socksBuffer[0];
    uint8_t requestCmd = socksBuffer[1];
    uint8_t reserved = socksBuffer[2];
    uint8_t dstAddrType = socksBuffer[3];
    
    memset(socksBuffer, 0, 4);
    size_t totalLen = 1;
    socksBuffer[0] = dstAddrType;
    if (dstAddrType == constants::SocksAddrDomainType) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 1, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv domain addr length in the third handshake recv");
            return recvCode;
        }
        uint8_t domainLen = socksBuffer[1];
        totalLen += 1;
        // socksBuffer[0] = 0;
        recvCode = readNBytes(localFd, socksBuffer + 2, domainLen, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv full domain addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 2 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv4Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 4, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv4 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else if (dstAddrType == constants::SocksAddrIpv6Type) {
        recvCode = readNBytes(localFd, socksBuffer + 1, 16, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv ipv6 addr in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
        recvCode = readNBytes(localFd, socksBuffer + 1 + recvCode, 2, constants::clientNonBlocking);
        if (recvCode < 0) {
            LogHelper::log(Warn, "Fail to recv dst port in the third handshake recv");
            return recvCode;
        }
        totalLen += recvCode;
    }
    else {
        LogHelper::log(Warn, "Not valid addr type in the third handshake recv, type: %d", dstAddrType);
        return recvCode;
    }

    static uint8_t addrBuffer[constants::SocksDomainMaxLength + 2];
    memset(addrBuffer, 0, sizeof(addrBuffer));
    memcpy(addrBuffer, socksBuffer, totalLen);
    memset(socksBuffer, 0, totalLen);
    socksBuffer[0] = requestCmd;
    memcpy(socksBuffer + 1, addrBuffer, totalLen);
    hton_copy4bytes(socksBuffer + 1 + totalLen, &localFd);
    // memcpy(addrBuffer, socksBuffer, recvCode);
    // memset(socksBuffer, 0, recvCode);
    sendCode = sendMessage(serverFd, socksBuffer, totalLen + 1 + sizeof(localFd), ctx, 6);
    if (sendCode < 0) {
        LogHelper::log(Error, "Fail to send socks handshake third request to server");
        return sendCode;
    }
    localFdMap[localFd] = LocalFdWaiting;
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %ebp
movl %edi, %edx
movl %edi, 0xc(%rsp)
leaq 0x44cf(%rip), %rsi      # 0x7448
movl $0x3, %edi
xorl %eax, %eax
callq 0x27f8
leaq 0x68274(%rip), %r15     # 0x6b200
movl $0x106, %edx            # imm = 0x106
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
movl 0xc(%rsp), %edi
movl $0x2, %edx
movq %r15, %rsi
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x319a
movzbl 0x6823c(%rip), %edx     # 0x6b200
cmpl $0x5, %edx
jne 0x31a3
movzbl 0x6822c(%rip), %r12d    # 0x6b201
movw $0x0, 0x68222(%rip)     # 0x6b200
leaq 0x6821b(%rip), %r13     # 0x6b200
movl 0xc(%rsp), %edi
movq %r13, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
setg %al
movzbl %r12b, %ecx
cmpl %r15d, %ecx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x31be
movq %rbx, 0x10(%rsp)
testq %r12, %r12
je 0x3033
xorl %eax, %eax
movb (%rax,%r13), %bl
testb %bl, %bl
je 0x3035
incq %rax
cmpq %rax, %r12
jne 0x3023
movb $-0x1, %bl
cmpb $-0x1, %bl
jne 0x304d
leaq 0x44c2(%rip), %rsi      # 0x7503
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
leaq 0x681ac(%rip), %r15     # 0x6b200
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2130
movb $0x5, 0x68198(%rip)     # 0x6b200
movb %bl, 0x68193(%rip)      # 0x6b201
movl 0xc(%rsp), %edi
movl $0x2, %edx
movq %r15, %rsi
movl $0x1, %ecx
callq 0x4b1e
movq %rax, %r15
testl %r15d, %r15d
js 0x31c7
leaq 0x68169(%rip), %r12     # 0x6b200
movl $0x106, %edx            # imm = 0x106
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
movl 0xc(%rsp), %edi
movl $0x4, %edx
movq %r12, %rsi
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x31d0
movb 0x68132(%rip), %r13b    # 0x6b201
movzbl 0x6812d(%rip), %edx     # 0x6b203
movl $0x0, 0x68120(%rip)     # 0x6b200
movb %dl, 0x6811a(%rip)      # 0x6b200
cmpl $0x1, %edx
je 0x3220
cmpl $0x4, %edx
je 0x31f5
cmpl $0x3, %edx
jne 0x3282
movl 0xc(%rsp), %edi
leaq 0x680f5(%rip), %rsi     # 0x6b201
movl $0x1, %edx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x32b2
movb %r13b, 0xb(%rsp)
movzbl 0x680ce(%rip), %edx     # 0x6b201
movl 0xc(%rsp), %edi
leaq 0x680c4(%rip), %rsi     # 0x6b202
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x32be
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl 0xc(%rsp), %edi
leaq (%r15,%r12), %rsi
addq $0x2, %rsi
movq %r15, %r13
addq $0x2, %r13
movl $0x2, %edx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x32db
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
addq %r15, %r13
movb $0x1, %bl
jmp 0x32f0
leaq 0x42d7(%rip), %rsi      # 0x7478
jmp 0x31d7
leaq 0x430a(%rip), %rsi      # 0x74b4
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x31e3
leaq 0x4313(%rip), %rsi      # 0x74d8
jmp 0x31d7
leaq 0x434f(%rip), %rsi      # 0x751d
jmp 0x31d7
leaq 0x4376(%rip), %rsi      # 0x754d
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %edi
leaq 0x68001(%rip), %rsi     # 0x6b201
movl $0x10, %edx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
jns 0x3242
leaq 0x4434(%rip), %rsi      # 0x7652
jmp 0x31d7
movl 0xc(%rsp), %edi
leaq 0x67fd6(%rip), %rsi     # 0x6b201
movl $0x4, %edx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x32a6
movl %r15d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl 0xc(%rsp), %edi
leaq (%rbx,%r12), %rsi
incq %rsi
movl $0x2, %edx
movl $0x1, %ecx
callq 0x4a4a
movq %rax, %r15
testl %r15d, %r15d
js 0x329a
movb %r13b, 0xb(%rsp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%rbx,%r15), %r13
incq %r13
jmp 0x32f8
leaq 0x43fc(%rip), %rsi      # 0x7685
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
jmp 0x31e3
leaq 0x434c(%rip), %rsi      # 0x75ed
jmp 0x31d7
leaq 0x4372(%rip), %rsi      # 0x761f
jmp 0x31d7
leaq 0x42be(%rip), %rsi      # 0x7577
jmp 0x31d7
leaq 0x42ee(%rip), %rsi      # 0x75b3
xorl %ebx, %ebx
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movl $0x2, %r13d
jmp 0x32f0
leaq 0x430b(%rip), %rsi      # 0x75ed
xorl %ebx, %ebx
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
testb %bl, %bl
je 0x31e3
leaq 0x68011(%rip), %r15     # 0x6b310
movl $0x101, %edx            # imm = 0x101
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2190
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2130
movb 0xb(%rsp), %al
movb %al, 0x67ecd(%rip)      # 0x6b200
leaq 0x67ec7(%rip), %rdi     # 0x6b201
movq %r15, %rsi
movq %r13, %rdx
callq 0x2190
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0x1(%r13,%r12)
addq $0x5, %r13
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
movl $0x6, %r8d
callq 0x2a5a
movq %rax, %r15
testl %r15d, %r15d
js 0x338f
leaq 0xc(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x420e
movl $0x1, (%rax)
xorl %r15d, %r15d
jmp 0x31e3
leaq 0x4329(%rip), %rsi      # 0x76bf
xorl %edi, %edi
jmp 0x31dc