#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 */
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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr const *, int);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int getLocalIp(char *buffer) {
  const char *kGoogleDnsIp = "8.8.8.8";
  struct sockaddr_in serv;
  struct sockaddr_in name;
  int dns_port = 53;
  socklen_t namelen;
  const char *p = NULL;
  int sock = 0;
  int err = 0;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    printf("snetool: cannot create socket\n");
    return EXIT_FAILURE;
  }
  memset(&serv, 0, sizeof(serv));
  serv.sin_family = AF_INET;
  serv.sin_addr.s_addr = inet_addr(kGoogleDnsIp);
  serv.sin_port = htons(dns_port);
  err = connect(sock, (const struct sockaddr *)&serv, sizeof(serv));
  namelen = sizeof(name);
  err = getsockname(sock, (struct sockaddr *)&name, &namelen);
  p = inet_ntop(AF_INET, &name.sin_addr, buffer, 100);
  if (p == NULL) {
    printf("snetool: cannot get local ip\n");
    return EXIT_FAILURE;
  }
  close(sock);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<getLocalIp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x35,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1207 <getLocalIp+0x77>
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c3 <getLocalIp+0x133>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,-0x50(%rbp)
movl   $0xc,-0x40(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1090 <getsockname@plt>
mov    %eax,-0x50(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12af <getLocalIp+0x11f>
lea    0xd8e(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c3 <getLocalIp+0x133>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getLocalIp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r15
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1252 <getLocalIp+0xc2>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
lea    0xe19(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    $0x35,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rbx
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <getsockname@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
test   %rax,%rax
je     125b <getLocalIp+0xcb>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d78(%rip),%rax
jmp    126e <getLocalIp+0xde>
lea    0xdcc(%rip),%rdi
jmp    1262 <getLocalIp+0xd2>
lea    0xda6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%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"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [31 x i8] c"snetool: cannot create socket\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [30 x i8] c"snetool: cannot get local ip\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @getLocalIp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 53, i32* %7
store i8* null, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %2
br label %50
20: ; preds = %1
%21 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = call i32 @inet_addr(i8* %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %7
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* %10
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %11
store i32 12, i32* %8
%34 = load i32, i32* %10
%35 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%36 = call i32 @getsockname(i32 %34, %struct.sockaddr* %35, i32* %8)
store i32 %36, i32* %11
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%39 = load i8*, i8** %3
%40 = call i8* @inet_ntop(i32 %37, %struct.TYPE_2__* %38, i8* %39, i32 100)
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %20
%44 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
store i32 %45, i32* %2
br label %50
46: ; preds = %20
%47 = load i32, i32* %10
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* @EXIT_SUCCESS
store i32 %49, i32* %2
br label %50
50: ; preds = %46, %43, %17
%51 = load i32, i32* %2
ret i32 %51
}

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
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [29 x i8] c"snetool: cannot get local ip\00"
@str.3 = [30 x i8] c"snetool: cannot create socket\00"
define i32 @getLocalIp(i8* %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 %12, label %14
12: ; preds = %1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.3, i64 0, i64 0))
br label %34
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = call i32 @htons(i32 53)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @connect(i32 %10, %struct.sockaddr* %22, i32 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 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i8* @inet_ntop(i32 %26, %struct.TYPE_2__* %27, i8* %0, i32 100)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %14
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %34
32: ; preds = %14
%33 = call i32 @close(i32 %10)
br label %34
34: ; preds = %32, %30, %12
%35 = phi i32* [ @EXIT_FAILURE, %12 ], [ @EXIT_FAILURE, %30 ], [ @EXIT_SUCCESS, %32 ]
%36 = load i32, i32* %35
ret i32 %36
}