// File: tenup-coin[P]seeder/netbase.cpp
// Function: Socks4(CService const&, unsigned int&)
bool static Socks4(const CService &addrDest, SOCKET& hSocket)
{
    printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str());
    if (!addrDest.IsIPv4())
    {
        closesocket(hSocket);
        return error("Proxy destination is not IPv4");
    }
    char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
    struct sockaddr_in addr;
    socklen_t len = sizeof(addr);
    if (!addrDest.GetSockAddr((struct sockaddr*)&addr, &len) || addr.sin_family != AF_INET)
    {
        closesocket(hSocket);
        return error("Cannot get proxy destination address");
    }
    memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
    memcpy(pszSocks4IP + 4, &addr.sin_addr, 4);
    char* pszSocks4 = pszSocks4IP;
    int nSize = sizeof(pszSocks4IP);

    int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
    if (ret != nSize)
    {
        closesocket(hSocket);
        return error("Error sending to proxy");
    }
    char pchRet[8];
    if (recv(hSocket, pchRet, 8, 0) != 8)
    {
        closesocket(hSocket);
        return error("Error reading proxy response");
    }
    if (pchRet[1] != 0x5a)
    {
        closesocket(hSocket);
        if (pchRet[1] != 0x5b)
            printf("ERROR: Proxy returned error %d\n", pchRet[1]);
        return false;
    }
    printf("SOCKS4 connected %s\n", addrDest.ToString().c_str());
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x4800
movq -0x1c0(%rbp), %rdx
leaq 0x2286d(%rip), %rsi     # 0x3361e
leaq -0x38(%rbp), %rdi
callq 0x4690
jmp 0x10dbc
movq -0x10(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x15040
jmp 0x10dcb
leaq -0x70(%rbp), %rdi
callq 0x41e0
movq %rax, %rsi
xorl %eax, %eax
leaq -0x38(%rbp), %rdi
callq 0x14360
jmp 0x10de4
leaq -0x70(%rbp), %rdi
callq 0x42f0
leaq -0x38(%rbp), %rdi
callq 0x42f0
leaq -0x39(%rbp), %rdi
callq 0x4520
movq -0x10(%rbp), %rdi
callq 0x134f0
testb $0x1, %al
jne 0x10f05
movq -0x18(%rbp), %rdi
callq 0x16610
leaq -0x91(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x4800
movq -0x1c8(%rbp), %rdx
leaq 0x227fa(%rip), %rsi     # 0x33634
leaq -0x90(%rbp), %rdi
callq 0x4690
jmp 0x10e48
xorl %eax, %eax
leaq -0x90(%rbp), %rdi
callq 0x15160
movb %al, -0x1c9(%rbp)
jmp 0x10e5e
movb -0x1c9(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x42f0
leaq -0x91(%rbp), %rdi
callq 0x4520
jmp 0x11395
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x10ec0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x10eb7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x42f0
leaq -0x38(%rbp), %rdi
callq 0x42f0
leaq -0x39(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x10ef4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x42f0
leaq -0x91(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movq 0x22414(%rip), %rax     # 0x33320
movq %rax, -0x9e(%rbp)
movl 0x2240f(%rip), %eax     # 0x33328
movl %eax, -0x96(%rbp)
movb 0x22407(%rip), %al      # 0x3332c
movb %al, -0x92(%rbp)
movl $0x10, -0xb4(%rbp)
movq -0x10(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xb4(%rbp), %rdx
callq 0x13c30
testb $0x1, %al
jne 0x10f52
jmp 0x10f62
movzwl -0xb0(%rbp), %eax
cmpl $0x2, %eax
je 0x1100f
movq -0x18(%rbp), %rdi
callq 0x16610
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x4800
movq -0x1d8(%rbp), %rdx
leaq 0x226c6(%rip), %rsi     # 0x33652
leaq -0xd8(%rbp), %rdi
callq 0x4690
jmp 0x10f9a
xorl %eax, %eax
leaq -0xd8(%rbp), %rdi
callq 0x15160
movb %al, -0x1d9(%rbp)
jmp 0x10fb0
movb -0x1d9(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x42f0
leaq -0xd9(%rbp), %rdi
callq 0x4520
jmp 0x11395
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x10ffe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x42f0
leaq -0xd9(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movw -0xae(%rbp), %ax
movw %ax, -0x9c(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0x9a(%rbp)
leaq -0x9e(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl $0xd, -0xec(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %edi
movq -0xe8(%rbp), %rsi
movslq -0xec(%rbp), %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x4240
movl %eax, -0xf0(%rbp)
movl -0xf0(%rbp), %eax
cmpl -0xec(%rbp), %eax
je 0x11124
movq -0x18(%rbp), %rdi
callq 0x16610
leaq -0x111(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x4800
movq -0x1e8(%rbp), %rdx
leaq 0x225d6(%rip), %rsi     # 0x33677
leaq -0x110(%rbp), %rdi
callq 0x4690
jmp 0x110af
xorl %eax, %eax
leaq -0x110(%rbp), %rdi
callq 0x15160
movb %al, -0x1e9(%rbp)
jmp 0x110c5
movb -0x1e9(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x42f0
leaq -0x111(%rbp), %rdi
callq 0x4520
jmp 0x11395
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11113
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x42f0
leaq -0x111(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movq -0x18(%rbp), %rax
movl (%rax), %edi
leaq -0x119(%rbp), %rsi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x4180
cmpq $0x8, %rax
je 0x111f4
movq -0x18(%rbp), %rdi
callq 0x16610
leaq -0x141(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x4800
movq -0x1f8(%rbp), %rdx
leaq 0x2251d(%rip), %rsi     # 0x3368e
leaq -0x140(%rbp), %rdi
callq 0x4690
jmp 0x1117f
xorl %eax, %eax
leaq -0x140(%rbp), %rdi
callq 0x15160
movb %al, -0x1f9(%rbp)
jmp 0x11195
movb -0x1f9(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x42f0
leaq -0x141(%rbp), %rdi
callq 0x4520
jmp 0x11395
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x111e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x42f0
leaq -0x141(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movsbl -0x118(%rbp), %eax
cmpl $0x5a, %eax
je 0x112bd
movq -0x18(%rbp), %rdi
callq 0x16610
movsbl -0x118(%rbp), %eax
cmpl $0x5b, %eax
je 0x112b4
leaq -0x169(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x4800
movq -0x208(%rbp), %rdx
leaq 0x2246d(%rip), %rsi     # 0x336ab
leaq -0x168(%rbp), %rdi
callq 0x4690
jmp 0x1124c
movsbl -0x118(%rbp), %esi
xorl %eax, %eax
leaq -0x168(%rbp), %rdi
callq 0x14360
jmp 0x11263
leaq -0x168(%rbp), %rdi
callq 0x42f0
leaq -0x169(%rbp), %rdi
callq 0x4520
jmp 0x112b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x42f0
leaq -0x169(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movb $0x0, -0x1(%rbp)
jmp 0x11395
leaq -0x191(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x4800
movq -0x210(%rbp), %rdx
leaq 0x223ed(%rip), %rsi     # 0x336cb
leaq -0x190(%rbp), %rdi
callq 0x4690
jmp 0x112ec
movq -0x10(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x15040
jmp 0x112fe
leaq -0x1b8(%rbp), %rdi
callq 0x41e0
movq %rax, %rsi
xorl %eax, %eax
leaq -0x190(%rbp), %rdi
callq 0x14360
jmp 0x1131d
leaq -0x1b8(%rbp), %rdi
callq 0x42f0
leaq -0x190(%rbp), %rdi
callq 0x42f0
leaq -0x191(%rbp), %rdi
callq 0x4520
movb $0x1, -0x1(%rbp)
jmp 0x11395
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11387
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1137b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x42f0
leaq -0x190(%rbp), %rdi
callq 0x42f0
leaq -0x191(%rbp), %rdi
callq 0x4520
jmp 0x113a3
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x47f0
nopl (%rax)