#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 {
  char *ifr_name;
  TYPE_1__ ifr_addr;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int libnet_get_if_ipv4(void *priv, char *ipaddr, char *ifname) {
  int sk;
  char *ip;
  struct ifreq ifr;
  sk = socket(AF_INET, SOCK_DGRAM, 0);
  if (sk < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_addr.sa_family = AF_INET;
  strcpy(ifr.ifr_name, ifname);
  if (ioctl(sk, SIOCGIFADDR, &ifr) < 0) {
    close(sk);
    return -1;
  }
  ip = inet_ntoa((((struct sockaddr_in *)&ifr.ifr_addr))->sin_addr);
  strcpy(ipaddr, ip);
  close(sk);
  return 0;
}


x86 assembly (clang, -O0):
<libnet_get_if_ipv4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11a6 <libnet_get_if_ipv4+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <libnet_get_if_ipv4+0xca>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1202 <libnet_get_if_ipv4+0xa2>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <libnet_get_if_ipv4+0xca>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libnet_get_if_ipv4>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2e65(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11ef <libnet_get_if_ipv4+0x8f>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     11f7 <libnet_get_if_ipv4+0x97>
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
jmp    11fd <libnet_get_if_ipv4+0x9d>
mov    $0xffffffff,%r15d
jmp    1204 <libnet_get_if_ipv4+0xa4>
mov    $0xffffffff,%r15d
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

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

LLVM IR (-O2):
%struct.ifreq = type { i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFADDR = global i32 0
define i32 @libnet_get_if_ipv4(i8* nocapture readnone %0, i8* %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %3
%11 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 16)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = call i32 @strcpy(i8* %15, i8* %2)
%17 = load i32, i32* @SIOCGIFADDR
%18 = call i64 @ioctl(i32 %8, i32 %17, %struct.ifreq* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %10
%21 = load i32, i32* %13
%22 = call i8* @inet_ntoa(i32 %21)
%23 = call i32 @strcpy(i8* %1, i8* %22)
br label %24
24: ; preds = %10, %20
%25 = phi i32 [ 0, %20 ], [ -1, %10 ]
%26 = call i32 @close(i32 %8)
br label %27
27: ; preds = %24, %3
%28 = phi i32 [ -1, %3 ], [ %25, %24 ]
ret i32 %28
}