// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x500, %rsp            # imm = 0x500
leaq 0x110(%rsp), %rdi
xorl %ebp, %ebp
movl $0x3e8, %edx            # imm = 0x3E8
xorl %esi, %esi
callq 0x4220
leaq 0x10(%rsp), %rdi
movl $0xff, %edx
xorl %esi, %esi
callq 0x4220
leaq 0x33b2(%rip), %rdi      # 0xa54b
callq 0x776a
movq %rax, %r15
leaq 0x33bb(%rip), %rdi      # 0xa563
callq 0x776a
movq %rax, %r12
leaq 0x33c2(%rip), %rdi      # 0xa579
pushq $0x1
popq %rbx
movl %ebx, %esi
callq 0x73b8
testb %al, %al
jne 0x735b
leaq 0x3390(%rip), %rsi      # 0xa560
movq %r12, %rdi
callq 0x708c
testq %rax, %rax
je 0x71e2
movq %rax, %r14
jmp 0x7200
leaq 0x3377(%rip), %rsi      # 0xa560
movq %r15, %rdi
callq 0x708c
testq %rax, %rax
je 0x735b
movq %rax, %r14
movq %r15, %r12
leaq 0x10(%rsp), %rdi
movl $0xff, %edx
movq %r12, %rsi
callq 0x8104
movq %r14, %r15
testq %r15, %r15
je 0x725a
movl 0x4(%r15), %edi
movl 0x8(%r15), %esi
movl 0xc(%r15), %edx
callq 0x40f0
movl %eax, 0x721f(%rip)      # 0xe450
cmpl $-0x1, %eax
je 0x7254
movq 0x18(%r15), %rsi
movl 0x10(%r15), %edx
movl %eax, %edi
callq 0x4360
testl %eax, %eax
je 0x727d
movl 0x7201(%rip), %edi      # 0xe450
callq 0x47f0
movq 0x28(%r15), %r15
jmp 0x7215
movq %r14, %rdi
callq 0x45b0
movl 0x71e8(%rip), %edi      # 0xe450
testl %edi, %edi
js 0x7271
callq 0x47f0
orl $-0x1, 0x71d8(%rip)     # 0xe450
jmp 0x735b
movq %r14, %rdi
callq 0x45b0
leaq 0x32fa(%rip), %rdi      # 0xa586
leaq 0x21cd(%rip), %rdx      # 0x9460
leaq 0x21d3(%rip), %rcx      # 0x946d
leaq 0x21df(%rip), %r8       # 0x9480
leaq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x6cdc
leaq 0x110(%rsp), %r14
movl $0x3e8, %esi            # imm = 0x3E8
movq %r14, %rdi
callq 0x6e66
movl 0x7188(%rip), %edi      # 0xe450
callq 0x47f0
orl $-0x1, 0x717c(%rip)     # 0xe450
movq %r14, %rdi
callq 0x8332
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x44b0
testq %rax, %rax
je 0x7340
movq %rax, %r14
incq %r14
pushq $0x2
popq %rdi
leaq 0xc(%rsp), %rdx
movq %r14, %rsi
callq 0x47d0
testl %eax, %eax
je 0x7349
leaq 0x7413(%rip), %rsi      # 0xe720
movq %r14, %rdi
callq 0x4630
testl %eax, %eax
je 0x736d
leaq 0x7400(%rip), %rdi      # 0xe720
pushq $0x64
popq %rdx
movq %r14, %rsi
callq 0x8104
leaq 0x333d(%rip), %rdi      # 0xa66f
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x5757
jmp 0x735b
leaq 0x3278(%rip), %rsi      # 0xa5bf
jmp 0x7350
leaq 0x32bc(%rip), %rsi      # 0xa60c
xorl %edi, %edi
xorl %eax, %eax
callq 0x57ea
movl %ebx, %ebp
movl %ebp, %eax
addq $0x500, %rsp            # imm = 0x500
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x32cd(%rip), %rdi      # 0xa641
xorl %eax, %eax
callq 0x5684
jmp 0x7359