// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rdx, %r14
movl %esi, %ebp
movl %edi, %r15d
movl %edi, 0xc(%rsp)
leaq 0x34a5(%rip), %rsi      # 0x6448
movl $0x3, %edi
movl %r15d, %edx
xorl %eax, %eax
callq 0x27f8
leaq 0x67247(%rip), %rbx     # 0x6a200
movl $0x106, %edx            # imm = 0x106
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
movl $0x2, %edx
movl %r15d, %edi
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3063
movzbl 0x67214(%rip), %edx     # 0x6a200
cmpl $0x5, %edx
jne 0x306f
movq %r13, 0x10(%rsp)
leaq 0x67203(%rip), %rbx     # 0x6a200
movzbl 0x671fc(%rip), %r13d    # 0x6a201
movw $0x0, 0x671f2(%rip)     # 0x6a200
movl %r15d, %edi
movq %rbx, %rsi
movq %r13, %rdx
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
setle %al
cmpl %r12d, %r13d
setne %cl
orb %al, %cl
jne 0x308d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %eax, %eax
cmpb $0x0, (%rax,%rbx)
je 0x3099
incq %rax
cmpq %rax, %r12
jne 0x303d
leaq 0x34b1(%rip), %rsi      # 0x6503
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movb $-0x1, %r12b
jmp 0x309c
leaq 0x340e(%rip), %rsi      # 0x6478
jmp 0x3370
leaq 0x343e(%rip), %rsi      # 0x64b4
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x337c
leaq 0x3444(%rip), %rsi      # 0x64d8
jmp 0x3370
xorl %r12d, %r12d
leaq 0x6715d(%rip), %rbx     # 0x6a200
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2130
movb $0x5, 0x67149(%rip)     # 0x6a200
movb %r12b, 0x67143(%rip)    # 0x6a201
movl $0x2, %edx
movl %r15d, %edi
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x494f
movq %rax, %r12
testl %r12d, %r12d
js 0x31d9
leaq 0x6711a(%rip), %rbx     # 0x6a200
movl $0x106, %edx            # imm = 0x106
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
movl $0x4, %edx
movl %r15d, %edi
movq %rbx, %rsi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x31e5
movb 0x670e4(%rip), %r13b    # 0x6a201
movzbl 0x670df(%rip), %edx     # 0x6a203
movl $0x0, 0x670d2(%rip)     # 0x6a200
movb %dl, 0x670cc(%rip)      # 0x6a200
cmpl $0x1, %edx
je 0x321e
cmpl $0x4, %edx
je 0x31f1
cmpl $0x3, %edx
jne 0x332e
leaq 0x670ab(%rip), %rsi     # 0x6a201
movl $0x1, %edx
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3360
movzbl 0x67086(%rip), %edx     # 0x6a201
leaq 0x67080(%rip), %rsi     # 0x6a202
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3369
movb %r13b, 0xb(%rsp)
movl %r12d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%rbx,%r13), %rsi
addq $0x2, %rsi
movl $0x2, %edx
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3343
addq $0x2, %r13
jmp 0x327a
leaq 0x333d(%rip), %rsi      # 0x651d
jmp 0x3370
leaq 0x3361(%rip), %rsi      # 0x654d
jmp 0x3370
leaq 0x67009(%rip), %rsi     # 0x6a201
movl $0x10, %edx
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
jns 0x3243
leaq 0x3439(%rip), %rsi      # 0x6652
jmp 0x3370
leaq 0x66fdc(%rip), %rsi     # 0x6a201
movl $0x4, %edx
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3357
movb %r13b, 0xb(%rsp)
movl %r12d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq (%rbx,%r13), %rsi
incq %rsi
movl $0x2, %edx
movl %r15d, %edi
movl $0x1, %ecx
callq 0x48af
movq %rax, %r12
testl %r12d, %r12d
js 0x3343
incq %r13
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (%r12,%r13), %rbx
leaq 0x67084(%rip), %rdi     # 0x6a310
movl $0x101, %edx            # imm = 0x101
xorl %esi, %esi
callq 0x2130
leaq 0x66f61(%rip), %rsi     # 0x6a200
leaq 0x6706a(%rip), %rdi     # 0x6a310
movq %rbx, %rdx
callq 0x2190
leaq 0x66f4b(%rip), %rdi     # 0x6a200
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2130
movb 0xb(%rsp), %al
movb %al, 0x66f37(%rip)      # 0x6a200
leaq 0x66f31(%rip), %rdi     # 0x6a201
leaq 0x67039(%rip), %rsi     # 0x6a310
movq %rbx, %rdx
callq 0x2190
bswapl %r15d
leaq 0x66f18(%rip), %rax     # 0x6a201
movl %r15d, (%rax,%rbx)
leaq (%r12,%r13), %rdx
addq $0x5, %rdx
movl %ebp, %edi
leaq 0x66f02(%rip), %rsi     # 0x6a200
movq %r14, %rcx
movl $0x6, %r8d
callq 0x2a5b
movq %rax, %r12
testl %r12d, %r12d
js 0x334c
leaq 0xc(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x405a
movl $0x1, (%rax)
xorl %r12d, %r12d
jmp 0x337c
leaq 0x3350(%rip), %rsi      # 0x6685
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
jmp 0x337c
leaq 0x32a3(%rip), %rsi      # 0x65ed
jmp 0x3370
leaq 0x336c(%rip), %rsi      # 0x66bf
xorl %edi, %edi
jmp 0x3375
leaq 0x32c1(%rip), %rsi      # 0x661f
jmp 0x3370
leaq 0x3210(%rip), %rsi      # 0x6577
jmp 0x3370
leaq 0x3243(%rip), %rsi      # 0x65b3
movl $0x1, %edi
xorl %eax, %eax
callq 0x27f8
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq