#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*/ assert(int);
int close(int);
char *inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int getIPv4addr(const char *dev, char *ipaddr) {
  int sockfd = -1;
  struct ifreq ifr;
  int ret = -1;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  assert(sockfd >= 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
  ret = ioctl(sockfd, SIOCGIFADDR, &ifr);
  assert(ret == 0);
  ret = close(sockfd);
  assert(ret == 0);
  ret = snprintf(ipaddr, sizeof("000.000.000.000"), "%s",
                 inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
  assert(ret <= (int)(sizeof("000.000.000.000") - 1));
  return 0;
}


x86 assembly (clang, -O0):
<getIPv4addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2df3(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rcx
mov    $0x10,%esi
lea    0xdb7(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xf,-0x24(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getIPv4addr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e5c(%rip),%r15
mov    (%r15),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1080 <assert@plt>
mov    (%r15),%eax
mov    %eax,(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    (%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xdfc(%rip),%rdx
mov    %r14,%rdi
mov    $0x10,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
xor    %edi,%edi
cmp    $0x10,%eax
setl   %dil
call   1080 <assert@plt>
xor    %eax,%eax
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
@IFNAMSIZ = global i64 0
@SIOCGIFADDR = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @getIPv4addr(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* %5
store i32 -1, i32* %7
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %3
%21 = load i64, i64* @IFNAMSIZ
%22 = sub nsw i64 %21, 1
%23 = call i32 @strncpy(i32 %19, i8* %20, i64 %22)
%24 = load i32, i32* %5
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %6)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 0
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
%37 = load i8*, i8** %4
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%39 = bitcast %struct.TYPE_2__* %38 to %struct.sockaddr_in*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i8* @inet_ntoa(i32 %41)
%43 = call i32 @snprintf(i8* %37, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %42)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp sle i32 %44, 15
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
ret i32 0
}

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
@.str = [3 x i8] c"%s\00"
define i32 @getIPv4addr(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = xor i32 %7, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i64, i64* @IFNAMSIZ
%14 = add nsw i64 %13, -1
%15 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %14)
%16 = load i32, i32* @SIOCGIFADDR
%17 = call i32 @ioctl(i32 %7, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = call i32 @close(i32 %7)
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %12
%26 = call i8* @inet_ntoa(i32 %25)
%27 = call i32 @snprintf(i8* %1, i32 16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = icmp slt i32 %27, 16
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
ret i32 0
}