#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;
char *calloc(scalar_t__, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ memset(struct ifreq *, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, scalar_t__);
int utGetIpFromNetDev(char *netdev, char **szIpPtr) {
  int fd;
  struct ifreq ifr;
  int ret = 1;
  memset(&ifr, '\0', sizeof(struct ifreq));
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, netdev, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFADDR, &ifr) != -1) {
    *szIpPtr = calloc(IFNAMSIZ, sizeof(char));
    memcpy(*szIpPtr, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr),
           IFNAMSIZ - 1);
    ret = 0;
  }
  if (fd >= 0)
    close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<utGetIpFromNetDev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1258 <utGetIpFromNetDev+0xd8>
mov    0x2dba(%rip),%rax
mov    (%rax),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    0x2d83(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1090 <memcpy@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     126a <utGetIpFromNetDev+0xea>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utGetIpFromNetDev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r14
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e2c(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2def(%rip),%r13
mov    0x0(%r13),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1252 <utGetIpFromNetDev+0xd2>
mov    0x0(%r13),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rax
mov    %rbx,(%rax)
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    0x0(%r13),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <memcpy@plt>
test   %ebp,%ebp
js     1240 <utGetIpFromNetDev+0xc0>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r15d
test   %ebp,%ebp
jns    1239 <utGetIpFromNetDev+0xb9>
jmp    1240 <utGetIpFromNetDev+0xc0>

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
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
define i32 @utGetIpFromNetDev(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 1, i32* %7
%8 = call i32 @memset(%struct.ifreq* %6, i8 signext 0, i32 8)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = load i64, i64* @IFNAMSIZ
%19 = sub nsw i64 %18, 1
%20 = call i32 @strncpy(i32 %16, i8* %17, i64 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFADDR
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %6)
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %39
25: ; preds = %2
%26 = load i64, i64* @IFNAMSIZ
%27 = call i8* @calloc(i64 %26, i32 1)
%28 = load i8**, i8*** %4
store i8* %27, i8** %28
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%32 = bitcast %struct.TYPE_2__* %31 to %struct.sockaddr_in*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @inet_ntoa(i32 %34)
%36 = load i64, i64* @IFNAMSIZ
%37 = sub nsw i64 %36, 1
%38 = call i32 @memcpy(i8* %30, i32 %35, i64 %37)
store i32 0, i32* %7
br label %39
39: ; preds = %25, %2
%40 = load i32, i32* %5
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %7
ret i32 %46
}

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
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
define i32 @utGetIpFromNetDev(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i8 signext 0, i32 8)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i64, i64* @IFNAMSIZ
%14 = add nsw i64 %13, -1
%15 = call i32 @strncpy(i32 %12, i8* %0, i64 %14)
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i32 @ioctl(i32 %8, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, label %19
19: ; preds = %2
%20 = load i64, i64* @IFNAMSIZ
%21 = call i8* @calloc(i64 %20, i32 1)
store i8* %21, i8** %1
%22 = load i32, i32* %10
%23 = call i32 @inet_ntoa(i32 %22)
%24 = load i64, i64* @IFNAMSIZ
%25 = add nsw i64 %24, -1
%26 = call i32 @memcpy(i8* %21, i32 %23, i64 %25)
br label %27
27: ; preds = %19, %2
%28 = phi i32 [ 0, %19 ], [ 1, %2 ]
%29 = icmp sgt i32 %8, -1
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @close(i32 %8)
br label %32
32: ; preds = %30, %27
ret i32 %28
}