// File: amit-mittal[P]FTP/./include/../tcpSocket.cpp
// Function: tcpSocket::connect()
int tcpSocket::connect(){
	struct addrinfo *p;
	int rv;
	stringstream port_str;
	port_str<<port;

	setHints(0);
	if ((rv = getaddrinfo(ip.c_str(), port_str.str().c_str(), &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}
	
	// loop through all the results and connect to the first we can
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
			perror("socket");
			continue;
		}
		if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close();
			perror("connect");
			continue;
		}
		break;
	}

	if (p == NULL) {
		fprintf(stderr, "failed to connect\n");
		return 2;
	}

	return 0;//no error
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x2170
movq -0x1e8(%rbp), %rax
leaq -0x198(%rbp), %rdi
movzwl 0x28(%rax), %esi
callq 0x2390
jmp 0x28ff
movq -0x1e8(%rbp), %rdi
xorl %esi, %esi
callq 0x2720
movq -0x1e8(%rbp), %rdi
addq $0x8, %rdi
callq 0x20f0
movq %rax, -0x1f0(%rbp)
leaq -0x1d8(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x21f0
jmp 0x2939
leaq -0x1d8(%rbp), %rdi
callq 0x20f0
movq -0x1e8(%rbp), %rcx
movq -0x1f0(%rbp), %rdi
movq %rax, %rsi
movq %rcx, %rdx
addq $0x30, %rdx
addq $0x60, %rcx
callq 0x22d0
movl %eax, -0x1f4(%rbp)
jmp 0x296e
movl -0x1f4(%rbp), %eax
movl %eax, -0x1c(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x2150
movl -0x1f4(%rbp), %eax
cmpl $0x0, %eax
je 0x2a13
movq 0x663f(%rip), %rax      # 0x8fd8
movq (%rax), %rax
movq %rax, -0x200(%rbp)
movl -0x1c(%rbp), %edi
callq 0x2310
movq -0x200(%rbp), %rdi
movq %rax, %rdx
leaq 0x3648(%rip), %rsi      # 0x6004
movb $0x0, %al
callq 0x2330
movl $0x1, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
jmp 0x2b05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x2b1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x2150
jmp 0x2b1d
movq -0x1e8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2ac2
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x2080
movq -0x1e8(%rbp), %rcx
movl %eax, (%rcx)
cmpl $-0x1, %eax
jne 0x2a65
leaq 0x35dc(%rip), %rdi      # 0x6038
callq 0x21c0
jmp 0x2a63
jmp 0x2ab1
movq -0x1e8(%rbp), %rax
movl (%rax), %edi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x21b0
movl %eax, -0x204(%rbp)
jmp 0x2a86
movl -0x204(%rbp), %eax
cmpl $-0x1, %eax
jne 0x2aaf
movq -0x1e8(%rbp), %rdi
callq 0x2b40
jmp 0x2a9f
leaq 0x356f(%rip), %rdi      # 0x6015
callq 0x21c0
jmp 0x2aad
jmp 0x2ab1
jmp 0x2ac2
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2a22
cmpq $0x0, -0x18(%rbp)
jne 0x2af4
movq 0x6508(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x3543(%rip), %rsi      # 0x601d
movb $0x0, %al
callq 0x2330
movl $0x2, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
jmp 0x2b05
movl $0x0, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x2190
movl -0x4(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
leaq -0x1a8(%rbp), %rdi
callq 0x2190
movq -0x1b0(%rbp), %rdi
callq 0x23c0
nopw %cs:(%rax,%rax)