#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct ifaddrs {
  char *ifa_name;
  TYPE_1__ *ifa_addr;
  struct ifaddrs *ifa_next;
};
struct TYPE_2__ {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ getifaddrs(struct ifaddrs **);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void pi_get_interface_ip(char *buf) {
  struct ifaddrs *ifap, *ifa;
  struct sockaddr_in *sa;
  char *addr;
  char intstr[6] = "";
  char ipstr[16];
  getifaddrs(&ifap);
  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr->sa_family == AF_INET) {
      sa = (struct sockaddr_in *)ifa->ifa_addr;
      addr = inet_ntoa(sa->sin_addr);
      sprintf(intstr, "%s", ifa->ifa_name);
      sprintf(ipstr, "%s", addr);
    }
  }
  strncpy(buf, ipstr, 16);
}


x86 assembly (clang, -O0):
<pi_get_interface_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x2e(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1060 <getifaddrs@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ff <pi_get_interface_ip+0xaf>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e9 <pi_get_interface_ip+0x99>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
lea    -0x2e(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xe30(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe1c(%rip),%rsi
call   1070 <sprintf@plt>
jmp    11ee <pi_get_interface_ip+0x9e>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    117d <pi_get_interface_ip+0x2d>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pi_get_interface_ip>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1060 <getifaddrs@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11ab <pi_get_interface_ip+0x6b>
mov    0x2e7f(%rip),%r12
mov    (%r12),%rax
lea    0x10(%rsp),%r15
jmp    1189 <pi_get_interface_ip+0x49>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     11ab <pi_get_interface_ip+0x6b>
mov    0x8(%rbx),%rcx
mov    (%rcx),%rdi
cmp    %rax,%rdi
jne    1180 <pi_get_interface_ip+0x40>
call   1050 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    (%r12),%rax
jmp    1180 <pi_get_interface_ip+0x40>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    $0x10,%edx
call   1030 <strncpy@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ifaddrs = type { i8*, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i64 0
@.str = [3 x i8] c"%s\00"
define void @pi_get_interface_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifaddrs*
%4 = alloca %struct.ifaddrs*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8*
%7 = alloca [6 x i8]
%8 = alloca [16 x i8]
store i8* %0, i8** %2
%9 = bitcast [6 x i8]* %7 to i8*
%10 = call i32 @getifaddrs(%struct.ifaddrs** %3)
%11 = load %struct.ifaddrs*, %struct.ifaddrs** %3
store %struct.ifaddrs* %11, %struct.ifaddrs** %4
br label %12
12: ; preds = %41, %1
%13 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%14 = icmp ne %struct.ifaddrs* %13, null
br i1 %14, label %15, label %45
15: ; preds = %12
%16 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%17 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %16, i32 0, i32 1
%18 = load %struct.TYPE_2__*, %struct.TYPE_2__** %17
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = load i64, i64* @AF_INET
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %40
23: ; preds = %15
%24 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%25 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %24, i32 0, i32 1
%26 = load %struct.TYPE_2__*, %struct.TYPE_2__** %25
%27 = bitcast %struct.TYPE_2__* %26 to %struct.sockaddr_in*
store %struct.sockaddr_in* %27, %struct.sockaddr_in** %5
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i8* @inet_ntoa(i32 %30)
store i8* %31, i8** %6
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%33 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%34 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%38 = load i8*, i8** %6
%39 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %23, %15
br label %41
41: ; preds = %40
%42 = load %struct.ifaddrs*, %struct.ifaddrs** %4
%43 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %42, i32 0, i32 2
%44 = load %struct.ifaddrs*, %struct.ifaddrs** %43
store %struct.ifaddrs* %44, %struct.ifaddrs** %4
br label %12
45: ; preds = %12
%46 = load i8*, i8** %2
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%48 = call i32 @strncpy(i8* %46, i8* %47, i32 16)
ret void
}

LLVM IR (-O2):
%struct.ifaddrs = type { i8*, %struct.TYPE_2__*, %struct.ifaddrs* }
%struct.TYPE_2__ = type { i64 }
@AF_INET = global i64 0
define void @pi_get_interface_ip(i8* %0) {
%2 = alloca %struct.ifaddrs*
%3 = alloca [16 x i8]
%4 = bitcast %struct.ifaddrs** %2 to i8*
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%6 = call i32 @getifaddrs(%struct.ifaddrs** %2)
%7 = load %struct.ifaddrs*, %struct.ifaddrs** %2
%8 = icmp eq %struct.ifaddrs* %7, null
br i1 %8, label %29, label %9
9: ; preds = %1
%10 = load i64, i64* @AF_INET
br label %11
11: ; preds = %9, %24
%12 = phi i64 [ %25, %24 ], [ %10, %9 ]
%13 = phi %struct.ifaddrs* [ %27, %24 ], [ %7, %9 ]
%14 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %13, i64 0, i32 1
%15 = load %struct.TYPE_2__*, %struct.TYPE_2__** %14
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, %12
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = trunc i64 %17 to i32
%21 = call i8* @inet_ntoa(i32 %20)
%22 = call i8* @strcpy(i8* %5, i8* dereferenceable(1) %21)
%23 = load i64, i64* @AF_INET
br label %24
24: ; preds = %11, %19
%25 = phi i64 [ %12, %11 ], [ %23, %19 ]
%26 = getelementptr inbounds %struct.ifaddrs, %struct.ifaddrs* %13, i64 0, i32 2
%27 = load %struct.ifaddrs*, %struct.ifaddrs** %26
%28 = icmp eq %struct.ifaddrs* %27, null
br i1 %28, label %29, label %11
29: ; preds = %24, %1
%30 = call i32 @strncpy(i8* %0, i8* %5, i32 16)
ret void
}