#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 TYPE_2__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strdup(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
char *ifaceutil_getip(char *iface_name) {
  int fd;
  struct ifreq ifr;
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, iface_name, IFNAMSIZ - 1);
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    return NULL;
  }
  if (ioctl(fd, SIOCGIFADDR, &ifr) == -1) {
    return NULL;
  }
  close(fd);
  return strdup(inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}


x86 assembly (clang, -O0):
<ifaceutil_getip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e42(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c4 <ifaceutil_getip+0x64>
movq   $0x0,-0x8(%rbp)
jmp    120a <ifaceutil_getip+0xaa>
mov    -0x14(%rbp),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11ef <ifaceutil_getip+0x8f>
movq   $0x0,-0x8(%rbp)
jmp    120a <ifaceutil_getip+0xaa>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%edi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifaceutil_getip>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e76(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdi,%rsi
call   1030 <strncpy@plt>
mov    (%rbx),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d5 <ifaceutil_getip+0x75>
mov    %eax,%ebp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11d3 <ifaceutil_getip+0x73>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%edi
call   1070 <strdup@plt>
mov    %rax,%rbx
jmp    11d5 <ifaceutil_getip+0x75>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i64 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i8* @ifaceutil_getip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%8 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %7, i32 0, i32 0
store i32 %6, i32* %8
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %3
%12 = load i64, i64* @IFNAMSIZ
%13 = sub nsw i64 %12, 1
%14 = call i32 @strncpy(i32 %10, i8* %11, i64 %13)
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %1
store i8* null, i8** %2
br label %35
20: ; preds = %1
%21 = load i32, i32* %4
%22 = load i32, i32* @SIOCGIFADDR
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %5)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %20
store i8* null, i8** %2
br label %35
26: ; preds = %20
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%30 = bitcast %struct.TYPE_2__* %29 to %struct.sockaddr_in*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @inet_ntoa(i32 %32)
%34 = call i8* @strdup(i32 %33)
store i8* %34, i8** %2
br label %35
35: ; preds = %26, %25, %19
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@IFNAMSIZ = global i64 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i8* @ifaceutil_getip(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
store i32 %4, i32* %5
%6 = load i64, i64* @IFNAMSIZ
%7 = add nsw i64 %6, -1
%8 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %7)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %22, label %13
13: ; preds = %1
%14 = load i32, i32* @SIOCGIFADDR
%15 = call i32 @ioctl(i32 %11, i32 %14, %struct.ifreq* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = call i32 @close(i32 %11)
%19 = load i32, i32* %5
%20 = call i32 @inet_ntoa(i32 %19)
%21 = call i8* @strdup(i32 %20)
br label %22
22: ; preds = %13, %1, %17
%23 = phi i8* [ %21, %17 ], [ null, %1 ], [ null, %13 ]
ret i8* %23
}