#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 getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
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 ssgl_get_primary_host_ip_addr(uint32_t *ipaddr) {
  const char *kGoogleDnsIp = "8.8.8.8";
  uint16_t kDnsPort = 53;
  struct sockaddr_in serv;
  struct sockaddr_in name;
  socklen_t namelen;
  int sock, err;
  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(kGoogleDnsIp);
  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    0xe6d(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x35,-0x1c(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    11cf <ssgl_get_primary_host_ip_addr+0x4f>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <ssgl_get_primary_host_ip_addr+0x103>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x40(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1225 <ssgl_get_primary_host_ip_addr+0xa5>
jmp    1274 <ssgl_get_primary_host_ip_addr+0xf4>
movl   $0xc,-0x3c(%rbp)
mov    -0x40(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1080 <getsockname@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     124e <ssgl_get_primary_host_ip_addr+0xce>
jmp    1274 <ssgl_get_primary_host_ip_addr+0xf4>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1283 <ssgl_get_primary_host_ip_addr+0x103>
mov    -0x40(%rbp),%edi
call   1050 <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   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e54(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     123c <ssgl_get_primary_host_ip_addr+0xbc>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
lea    0xe2d(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    $0x35,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1233 <ssgl_get_primary_host_ip_addr+0xb3>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1080 <getsockname@plt>
test   %eax,%eax
jne    1233 <ssgl_get_primary_host_ip_addr+0xb3>
lea    0x18(%rsp),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
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"
@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 i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = 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* %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* %9
store i32 %17, i32* %2
br label %51
18: ; preds = %1
%19 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i8*, i8** %4
%23 = call i32 @inet_addr(i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %5
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %9
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %18
br label %48
35: ; preds = %18
store i32 12, i32* %8
%36 = load i32, i32* %9
%37 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%38 = call i32 @getsockname(i32 %36, %struct.sockaddr* %37, i32* %8)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
br label %48
42: ; preds = %35
%43 = load i32*, i32** %3
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%45 = call i32 @memcpy(i32* %43, %struct.TYPE_2__* %44, i32 4)
%46 = load i32, i32* %9
%47 = call i32 @close(i32 %46)
store i32 0, i32* %2
br label %51
48: ; preds = %41, %34
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %51
51: ; preds = %48, %42, %16
%52 = load i32, i32* %2
ret i32 %52
}

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"
@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 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = call i32 @htons(i32 53)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = call i32 @connect(i32 %10, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %12
store i32 12, i32* %4
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @getsockname(i32 %10, %struct.sockaddr* %24, i32* %4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%29 = call i32 @memcpy(i32* %0, %struct.TYPE_2__* %28, i32 4)
br label %30
30: ; preds = %12, %23, %27
%31 = phi i32 [ 0, %27 ], [ -1, %23 ], [ -1, %12 ]
%32 = call i32 @close(i32 %10)
br label %33
33: ; preds = %30, %1
%34 = phi i32 [ %10, %1 ], [ %31, %30 ]
ret i32 %34
}