// File: /workspace/github_c_makefile_star2_O3/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 0x3266(%rip), %rdi      # 0xa50b
callq 0x7889
movq %rax, %r15
leaq 0x326f(%rip), %rdi      # 0xa523
callq 0x7889
movq %rax, %r12
leaq 0x3276(%rip), %rdi      # 0xa539
movl $0x1, %esi
callq 0x7500
testb %al, %al
jne 0x7474
leaq 0x3244(%rip), %rsi      # 0xa520
movq %r12, %rdi
callq 0x71ce
testq %rax, %rax
je 0x72ee
movq %rax, %r14
jmp 0x730c
leaq 0x322b(%rip), %rsi      # 0xa520
movq %r15, %rdi
callq 0x71ce
testq %rax, %rax
je 0x7474
movq %rax, %r14
movq %r15, %r12
leaq 0x10(%rsp), %rdi
movl $0xff, %edx
movq %r12, %rsi
callq 0x8278
movq %r14, %r15
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x40f0
movl %eax, 0x7118(%rip)      # 0xe450
cmpl $-0x1, %eax
je 0x735b
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x4360
testl %eax, %eax
je 0x738a
movl 0x70fa(%rip), %edi      # 0xe450
callq 0x4800
movq 0x28(%r15), %r15
testq %r15, %r15
jne 0x7321
movq %r14, %rdi
callq 0x45c0
movl 0x70de(%rip), %edi      # 0xe450
testl %edi, %edi
js 0x737b
callq 0x4800
movl $0xffffffff, 0x70cb(%rip) # imm = 0xFFFFFFFF
jmp 0x7474
movq %r14, %rdi
callq 0x45c0
leaq 0x31ad(%rip), %rdi      # 0xa546
leaq 0x20a0(%rip), %rdx      # 0x9440
leaq 0x20a6(%rip), %rcx      # 0x944d
leaq 0x20b2(%rip), %r8       # 0x9460
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x6da8
leaq 0x110(%rsp), %rbx
movl $0x3e8, %esi            # imm = 0x3E8
movq %rbx, %rdi
callq 0x6f52
movl 0x707b(%rip), %edi      # 0xe450
callq 0x4800
movl $0xffffffff, 0x706c(%rip) # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x84bc
movq %rax, %rdi
movl $0xa, %esi
callq 0x44b0
testq %rax, %rax
je 0x7456
movq %rax, %r14
incq %r14
leaq 0xc(%rsp), %rdx
movl $0x2, %edi
movq %r14, %rsi
callq 0x47e0
testl %eax, %eax
je 0x745f
leaq 0x72ff(%rip), %rsi      # 0xe720
movq %r14, %rdi
callq 0x4640
testl %eax, %eax
je 0x7485
leaq 0x72ec(%rip), %rdi      # 0xe720
movl $0x64, %edx
movq %r14, %rsi
callq 0x8278
leaq 0x31e7(%rip), %rdi      # 0xa62f
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x57fa
jmp 0x7474
leaq 0x3122(%rip), %rsi      # 0xa57f
jmp 0x7466
leaq 0x3166(%rip), %rsi      # 0xa5cc
xorl %edi, %edi
xorl %eax, %eax
callq 0x588f
movl $0x1, %ebx
movl %ebx, %eax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x3175(%rip), %rdi      # 0xa601
xorl %eax, %eax
callq 0x5721
jmp 0x746f