#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 */
typedef scalar_t__ u_int32_t;
struct in_addr {
  scalar_t__ s_addr;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_PACKET;
int /*<<< orphan*/ _debug(char *, int, int /*<<< orphan*/, char *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
scalar_t__ inet_ntoa(struct in_addr);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(void *, void *, int);
char *safe_strdup(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *get_iface_ip(char *ifname) {
  struct ifreq if_data;
  struct in_addr in;
  char *ip_str;
  int sockd;
  u_int32_t ip;
  if ((sockd = socket(AF_INET, SOCK_PACKET, htons(0x8086))) < 0) {
    _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/"
           "util.c",
           160, LOG_ERR, "socket(): %s", strerror(errno));
    return NULL;
  }
  strcpy(if_data.ifr_name, ifname);
  if (ioctl(sockd, SIOCGIFADDR, &if_data) < 0) {
    _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/"
           "util.c",
           169, LOG_ERR, "ioctl(): SIOCGIFADDR %s", strerror(errno));
    return NULL;
  }
  memcpy((void *)&ip, (void *)&if_data.ifr_addr.sa_data + 2, 4);
  in.s_addr = ip;
  ip_str = (char *)inet_ntoa(in);
  close(sockd);
  return safe_strdup(ip_str);
}


x86 assembly (clang, -O0):
<get_iface_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x8086,%edi
call   1050 <htons@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    122c <get_iface_ip+0x8c>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edx
mov    %eax,%r8d
lea    0xdf2(%rip),%rdi
mov    $0xa0,%esi
lea    0xe70(%rip),%rcx
call   1090 <_debug@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <get_iface_ip+0x13c>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    129c <get_iface_ip+0xfc>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%edx
mov    %eax,%r8d
lea    0xd82(%rip),%rdi
mov    $0xa9,%esi
lea    0xe0d(%rip),%rcx
call   1090 <_debug@plt>
movq   $0x0,-0x8(%rbp)
jmp    12dc <get_iface_ip+0x13c>
lea    -0x38(%rbp),%rdi
lea    -0x18(%rbp),%rsi
add    $0x2,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <safe_strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_iface_ip>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e26(%rip),%rax
mov    (%rax),%ebx
mov    0x2e25(%rip),%rax
mov    (%rax),%ebp
mov    $0x8086,%edi
call   1050 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1229 <get_iface_ip+0x89>
mov    %eax,%ebx
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     1255 <get_iface_ip+0xb5>
lea    0xa(%rsp),%rsi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
mov    (%rsp),%rdi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %rbp,%rdi
call   10a0 <safe_strdup@plt>
jmp    128b <get_iface_ip+0xeb>
mov    0x2d88(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb9(%rip),%rdi
lea    0xe3c(%rip),%rcx
mov    $0xa0,%esi
jmp    127f <get_iface_ip+0xdf>
mov    0x2d5c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8d(%rip),%rdi
lea    0xe1d(%rip),%rcx
mov    $0xa9,%esi
mov    %ebx,%edx
mov    %eax,%r8d
call   1090 <_debug@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i64 }
@AF_INET = global i32 0
@SOCK_PACKET = global i32 0
@.str = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/util.c\00"
@LOG_ERR = global i32 0
@.str.1 = [13 x i8] c"socket(): %s\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.2 = [24 x i8] c"ioctl(): SIOCGIFADDR %s\00"
define i8* @get_iface_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca %struct.in_addr
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %3
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_PACKET
%11 = call i32 @htons(i32 32902)
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @LOG_ERR
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 160, i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i8* null, i8** %2
br label %50
19: ; preds = %1
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = load i32, i32* %7
%25 = load i32, i32* @SIOCGIFADDR
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifreq* %4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 169, i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %31)
store i8* null, i8** %2
br label %50
33: ; preds = %19
%34 = bitcast i64* %8 to i8*
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = bitcast i32* %36 to i8*
%38 = getelementptr i8, i8* %37, i64 2
%39 = call i32 @memcpy(i8* %34, i8* %38, i32 4)
%40 = load i64, i64* %8
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
store i64 %40, i64* %41
%42 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %5, i32 0, i32 0
%43 = load i64, i64* %42
%44 = call i64 @inet_ntoa(i64 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %6
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = load i8*, i8** %6
%49 = call i8* @safe_strdup(i8* %48)
store i8* %49, i8** %2
br label %50
50: ; preds = %33, %28, %14
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_PACKET = global i32 0
@.str = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/util.c\00"
@LOG_ERR = global i32 0
@.str.1 = [13 x i8] c"socket(): %s\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.2 = [24 x i8] c"ioctl(): SIOCGIFADDR %s\00"
define i8* @get_iface_ip(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca i64
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = bitcast i64* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_PACKET
%8 = tail call i32 @htons(i32 32902)
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @LOG_ERR
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 160, i32 %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %34
16: ; preds = %1
%17 = tail call i32 @strcpy(i32 undef, i8* %0)
%18 = load i32, i32* @SIOCGIFADDR
%19 = call i64 @ioctl(i32 %9, i32 %18, %struct.ifreq* %2)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @LOG_ERR
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 169, i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %34
26: ; preds = %16
%27 = getelementptr inbounds i8, i8* %4, i64 2
%28 = call i32 @memcpy(i8* %5, i8* %27, i32 4)
%29 = load i64, i64* %3
%30 = call i64 @inet_ntoa(i64 %29)
%31 = inttoptr i64 %30 to i8*
%32 = call i32 @close(i32 %9)
%33 = call i8* @safe_strdup(i8* %31)
br label %34
34: ; preds = %26, %21, %11
%35 = phi i8* [ null, %11 ], [ null, %21 ], [ %33, %26 ]
ret i8* %35
}