// File: /workspace/github_c_makefile_star2_O1/uhlin[P]enhanced-duc/source/network.c
// Function: net_check_for_ip_change
ip_chg_t
net_check_for_ip_change(void)
{
	bool			 address_resolved = false;
	bool			 connected = false;
	char			 buf[1000] = { '\0' };
	char			 srv[255] = { '\0' };
	const char		*backup_srv = setting("backup_ip_lookup_srv");
	const char		*port = "80";
	const char		*primary_srv = setting("primary_ip_lookup_srv");
	struct addrinfo		*res, *rp;
	unsigned char		 nw_addr[sizeof(struct in_addr)];

	if (setting_bool("force_update", true))
		return IP_HAS_CHANGED;

	if ((res = net_addr_resolve(primary_srv, port)) != NULL) {
		(void) strlcpy(srv, primary_srv, sizeof srv);
		address_resolved = true;
		goto done;
	}
	if ((res = net_addr_resolve(backup_srv, port)) != NULL) {
		(void) strlcpy(srv, backup_srv, sizeof srv);
		address_resolved = true;
	}

  done:
	if (!address_resolved)
		return IP_HAS_CHANGED; /* force update */

	for (rp = res; rp; rp = rp->ai_next) {
		g_socket =
		    socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

		if (g_socket == SOCKET_CREATION_FAILED) {
			continue;
		} else if (connect(g_socket, rp->ai_addr, rp->ai_addrlen) == 0) {
			connected = true;
			break;
		} else {
			(void) close(g_socket);
		}
	}

	freeaddrinfo(res);

	if (!connected) {
		if (g_socket >= 0)
			(void) close(g_socket);
		g_socket = -1;
		return IP_HAS_CHANGED;
	}

	(void) net_send_plain("GET /index.html HTTP/1.0\r\nHost: %s\r\n"
	    "User-Agent: %s/%s %s", srv, g_programName, g_programVersion,
	    g_maintainerEmail);
	(void) net_recv_plain(buf, sizeof buf);
	(void) close(g_socket);
	g_socket = -1;

	const char *cp = strrchr(trim(buf), '\n');

	if (!cp) {
		log_warn(0, "net_check_for_ip_change: warning: "
		    "cannot locate last occurrance of a newline");
		return IP_NO_CHANGE;
	} else if (inet_pton(AF_INET, ++cp, nw_addr) == 0) {
		log_warn(0, "net_check_for_ip_change: warning: "
		    "bogus ipv4 address");
		return IP_NO_CHANGE;
	} else if (strings_match(cp, g_last_ip_addr)) {
		log_debug("not updating (the external ip hasn't changed)");
		return IP_NO_CHANGE;
	}

	(void) strlcpy(g_last_ip_addr, cp, sizeof g_last_ip_addr);
	log_msg("ip has changed to %s", cp);
	return IP_HAS_CHANGED;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
leaq 0x110(%rsp), %rdi
xorl %ebx, %ebx
movl $0x3e8, %edx            # imm = 0x3E8
xorl %esi, %esi
callq 0x4220
leaq 0x10(%rsp), %rdi
movl $0xff, %edx
xorl %esi, %esi
callq 0x4220
leaq 0x3185(%rip), %rdi      # 0xa50b
callq 0x798d
movq %rax, %r15
leaq 0x318e(%rip), %rdi      # 0xa523
callq 0x798d
movq %rax, %r12
leaq 0x3195(%rip), %rdi      # 0xa539
movl $0x1, %esi
callq 0x75f8
testb %al, %al
jne 0x7471
leaq 0x3163(%rip), %rsi      # 0xa520
movq %r12, %rdi
callq 0x72a2
testq %rax, %rax
je 0x73cf
movq %rax, %r14
jmp 0x73ed
leaq 0x314a(%rip), %rsi      # 0xa520
movq %r15, %rdi
callq 0x72a2
testq %rax, %rax
je 0x7482
movq %rax, %r14
movq %r15, %r12
leaq 0x10(%rsp), %rdi
movl $0xff, %edx
movq %r12, %rsi
callq 0x83c0
movb $0x1, %al
testb %al, %al
je 0x7471
testq %r14, %r14
je 0x7450
movq %r14, %r15
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x40f0
movl %eax, 0x702c(%rip)      # 0xe450
cmpl $-0x1, %eax
je 0x7447
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x4360
testl %eax, %eax
je 0x748c
movl 0x700e(%rip), %edi      # 0xe450
callq 0x4800
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x740d
movq %r14, %rdi
callq 0x45c0
movl 0x6ff2(%rip), %edi      # 0xe450
testl %edi, %edi
js 0x7467
callq 0x4800
movl $0xffffffff, 0x6fdf(%rip) # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
xorl %eax, %eax
xorl %r14d, %r14d
jmp 0x7401
movq %r14, %rdi
callq 0x45c0
leaq 0x30ab(%rip), %rdi      # 0xa546
leaq 0x1f9e(%rip), %rdx      # 0x9440
leaq 0x1fa4(%rip), %rcx      # 0x944d
leaq 0x1fb0(%rip), %r8       # 0x9460
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x6e7c
leaq 0x110(%rsp), %rbx
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdi
callq 0x7026
movl 0x6f79(%rip), %edi      # 0xe450
callq 0x4800
movl $0xffffffff, 0x6f6a(%rip) # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x8619
movq %rax, %rdi
movl $0xa, %esi
callq 0x44b0
testq %rax, %rax
je 0x755b
movq %rax, %r14
incq %r14
leaq 0xc(%rsp), %rdx
movl $0x2, %edi
movq %r14, %rsi
callq 0x47e0
testl %eax, %eax
je 0x7564
leaq 0x71fd(%rip), %rsi      # 0xe720
movq %r14, %rdi
callq 0x4640
testl %eax, %eax
je 0x757e
leaq 0x71ea(%rip), %rdi      # 0xe720
movl $0x64, %edx
movq %r14, %rsi
callq 0x83c0
leaq 0x30e5(%rip), %rdi      # 0xa62f
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x588a
jmp 0x7471
leaq 0x301d(%rip), %rsi      # 0xa57f
jmp 0x756b
leaq 0x3061(%rip), %rsi      # 0xa5cc
xorl %edi, %edi
xorl %eax, %eax
callq 0x591f
movl $0x1, %ebx
jmp 0x7471
leaq 0x307c(%rip), %rdi      # 0xa601
xorl %eax, %eax
callq 0x57b1
jmp 0x7574