#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
typedef int uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, TYPE_1__ *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int ssgl_get_primary_host_ip_addr(uint32_t *ipaddr) {
  char *GoogleDnsIp = "8.8.8.8";
  char *primary_ip_probe_addr;
  uint16_t kDnsPort = 53;
  struct sockaddr_in serv;
  struct sockaddr_in name;
  socklen_t namelen;
  int sock, err;
  primary_ip_probe_addr = getenv("SSGL_PRIMARY_IP_PROBE_ADDR");
  if (!primary_ip_probe_addr)
    primary_ip_probe_addr = GoogleDnsIp;
  fprintf(stderr, "Probing for primary host IP addr via %s\n",
          primary_ip_probe_addr);
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    return sock;
  memset(&serv, 0, sizeof(serv));
  serv.sin_family = AF_INET;
  serv.sin_addr.s_addr = inet_addr(primary_ip_probe_addr);
  serv.sin_port = htons(kDnsPort);
  err = connect(sock, (struct sockaddr *)&serv, sizeof(serv));
  if (err < 0)
    goto close_and_err;
  namelen = sizeof(name);
  err = getsockname(sock, (struct sockaddr *)&name, &namelen);
  if (err)
    goto close_and_err;
  memcpy(ipaddr, &name.sin_addr, sizeof(*ipaddr));
  close(sock);
  return 0;
close_and_err:
  close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<ssgl_get_primary_host_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe4d(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x35,-0x24(%rbp)
lea    0xe43(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e1 <ssgl_get_primary_host_ip_addr+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe2e(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    122b <ssgl_get_primary_host_ip_addr+0x8b>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12df <ssgl_get_primary_host_ip_addr+0x13f>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x48(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1281 <ssgl_get_primary_host_ip_addr+0xe1>
jmp    12d0 <ssgl_get_primary_host_ip_addr+0x130>
movl   $0xc,-0x44(%rbp)
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10a0 <getsockname@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     12aa <ssgl_get_primary_host_ip_addr+0x10a>
jmp    12d0 <ssgl_get_primary_host_ip_addr+0x130>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12df <ssgl_get_primary_host_ip_addr+0x13f>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_get_primary_host_ip_addr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r14
lea    0xe52(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe3b(%rip),%rbx
cmovne %rax,%rbx
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    0x2e00(%rip),%r12
mov    (%r12),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     128e <ssgl_get_primary_host_ip_addr+0xee>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1285 <ssgl_get_primary_host_ip_addr+0xe5>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <getsockname@plt>
test   %eax,%eax
jne    1285 <ssgl_get_primary_host_ip_addr+0xe5>
lea    0x20(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"8.8.8.8\00"
@.str.1 = [27 x i8] c"SSGL_PRIMARY_IP_PROBE_ADDR\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"Probing for primary host IP addr via %s\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @ssgl_get_primary_host_ip_addr(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 53, i32* %6
%12 = call i8* @getenv(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = load i8*, i8** %4
store i8* %16, i8** %5
br label %17
17: ; preds = %15, %1
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %5
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = load i32, i32* %10
store i32 %27, i32* %2
br label %61
28: ; preds = %17
%29 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i8*, i8** %5
%33 = call i32 @inet_addr(i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* %6
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* %10
%40 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%41 = call i32 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %28
br label %58
45: ; preds = %28
store i32 12, i32* %9
%46 = load i32, i32* %10
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i32 @getsockname(i32 %46, %struct.sockaddr* %47, i32* %9)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
br label %58
52: ; preds = %45
%53 = load i32*, i32** %3
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%55 = call i32 @memcpy(i32* %53, %struct.TYPE_2__* %54, i32 4)
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
store i32 0, i32* %2
br label %61
58: ; preds = %51, %44
%59 = load i32, i32* %10
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %2
br label %61
61: ; preds = %58, %52, %26
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"8.8.8.8\00"
@.str.1 = [27 x i8] c"SSGL_PRIMARY_IP_PROBE_ADDR\00"
@stderr = global i32 0
@.str.2 = [41 x i8] c"Probing for primary host IP addr via %s\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @ssgl_get_primary_host_ip_addr(i32* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
%10 = select i1 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %8
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %38, label %17
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* %10)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = call i32 @htons(i32 53)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @connect(i32 %15, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %17
store i32 12, i32* %4
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @getsockname(i32 %15, %struct.sockaddr* %29, i32* %4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = call i32 @memcpy(i32* %0, %struct.TYPE_2__* %33, i32 4)
br label %35
35: ; preds = %17, %28, %32
%36 = phi i32 [ 0, %32 ], [ -1, %28 ], [ -1, %17 ]
%37 = call i32 @close(i32 %15)
br label %38
38: ; preds = %35, %1
%39 = phi i32 [ %15, %1 ], [ %36, %35 ]
ret i32 %39
}