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

	setHints(1);
	if ((rv = getaddrinfo(NULL, port_str.str().c_str(), &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
		return 1;
	}

	// loop through all the results and bind 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("server: socket");
			continue;
		}
		if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
			perror("setsockopt");
		}
		if (::bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close();
			perror("server: bind");
			continue;
		}
		break;
	}

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

	return 0;//no error
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movl $0x1, -0x20(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x2170
movq -0x1e8(%rbp), %rax
leaq -0x198(%rbp), %rdi
movzwl 0x28(%rax), %esi
callq 0x2390
jmp 0x2ba6
movq -0x1e8(%rbp), %rdi
movl $0x1, %esi
callq 0x2720
leaq -0x1d8(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x21f0
jmp 0x2bcc
leaq -0x1d8(%rbp), %rdi
callq 0x20f0
movq -0x1e8(%rbp), %rcx
movq %rax, %rsi
movq %rcx, %rdx
addq $0x30, %rdx
addq $0x60, %rcx
xorl %eax, %eax
movl %eax, %edi
callq 0x22d0
movl %eax, -0x1ec(%rbp)
jmp 0x2bfe
movl -0x1ec(%rbp), %eax
movl %eax, -0x1c(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x2150
movl -0x1ec(%rbp), %eax
cmpl $0x0, %eax
je 0x2ca3
movq 0x63af(%rip), %rax      # 0x8fd8
movq (%rax), %rax
movq %rax, -0x1f8(%rbp)
movl -0x1c(%rbp), %edi
callq 0x2310
movq -0x1f8(%rbp), %rdi
movq %rax, %rdx
leaq 0x33b8(%rip), %rsi      # 0x6004
movb $0x0, %al
callq 0x2330
movl $0x1, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
jmp 0x2dc2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x2dda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x2150
jmp 0x2dda
movq -0x1e8(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x2d7f
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 0x2cf5
leaq 0x3344(%rip), %rdi      # 0x6030
callq 0x21c0
jmp 0x2cf3
jmp 0x2d6e
movq -0x1e8(%rbp), %rax
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x20(%rbp), %rcx
movl $0x4, %r8d
callq 0x23e0
cmpl $-0x1, %eax
jne 0x2d2c
leaq 0x331c(%rip), %rdi      # 0x603f
callq 0x21c0
jmp 0x2d2a
jmp 0x2d2c
movq -0x1e8(%rbp), %rax
movl (%rax), %edi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
callq 0x2070
cmpl $-0x1, %eax
jne 0x2d6c
movq -0x1e8(%rbp), %rdi
callq 0x2b40
jmp 0x2d5c
leaq 0x32e7(%rip), %rdi      # 0x604a
callq 0x21c0
jmp 0x2d6a
jmp 0x2d6e
jmp 0x2d7f
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2cb2
cmpq $0x0, -0x18(%rbp)
jne 0x2db1
movq 0x624b(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x32c0(%rip), %rsi      # 0x6057
movb $0x0, %al
callq 0x2330
movl $0x2, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
jmp 0x2dc2
movl $0x0, -0x4(%rbp)
movl $0x1, -0x1dc(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x2190
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x1a8(%rbp), %rdi
callq 0x2190
movq -0x1b0(%rbp), %rdi
callq 0x23c0
nopw %cs:(%rax,%rax)