#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);
char const *inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
const char *GetInterfaceIP(const char *Interface) {
  int fd, result;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1)
    return ("");
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, Interface, IFNAMSIZ - 1);
  result = ioctl(fd, SIOCGIFADDR, &ifr);
  if (result == -1)
    return ("");
  close(fd);
  return (inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}


x86 assembly (clang, -O0):
<GetInterfaceIP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1192 <GetInterfaceIP+0x42>
lea    0xe77(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11fe <GetInterfaceIP+0xae>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e1c(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ea <GetInterfaceIP+0x9a>
lea    0xe1f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11fe <GetInterfaceIP+0xae>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetInterfaceIP>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7f(%rip),%r15
mov    (%r15),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
lea    0xe85(%rip),%rbx
cmp    $0xffffffff,%eax
je     11c8 <GetInterfaceIP+0x78>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11c8 <GetInterfaceIP+0x78>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@SOCK_DGRAM = global i32 0
@.str = [1 x i8] zeroinitializer
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
define i8* @GetInterfaceIP(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %37
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = load i64, i64* @IFNAMSIZ
%21 = sub nsw i64 %20, 1
%22 = call i32 @strncpy(i32 %18, i8* %19, i64 %21)
%23 = load i32, i32* %4
%24 = load i32, i32* @SIOCGIFADDR
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %6)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %13
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %37
29: ; preds = %13
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%33 = bitcast %struct.TYPE_2__* %32 to %struct.sockaddr_in*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @inet_ntoa(i32 %35)
store i8* %36, i8** %2
br label %37
37: ; preds = %29, %28, %12
%38 = load i8*, i8** %2
ret i8* %38
}

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