#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 */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct ifreq {
  int /*<<< orphan*/ ifr_name;
  int /*<<< orphan*/ ifr_addr;
};

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
char *inet_ntoa(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ log_msg(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
char *get_ip_from_dev(const char *dev) {
  int sfd;
  struct ifreq ifr;
  struct sockaddr_in *saddr = (struct sockaddr_in *)&ifr.ifr_addr;
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  sfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sfd == -1) {
    log_msg(LOG_ERR, "Error opening socket: %s\n", strerror(errno));
    return NULL;
  }
  if (ioctl(sfd, SIOCGIFADDR, &ifr) == -1) {
    log_msg(LOG_ERR, "Error opening if device: %s\n", strerror(errno));
    close(sfd);
    return NULL;
  }
  close(sfd);
  return inet_ntoa(saddr->sin_addr);
}


x86 assembly (clang, -O0):
<get_ip_from_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fb <get_ip_from_dev+0x8b>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe17(%rip),%rsi
call   1070 <log_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1270 <get_ip_from_dev+0x100>
mov    -0x14(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1259 <get_ip_from_dev+0xe9>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xddb(%rip),%rsi
call   1070 <log_msg@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1270 <get_ip_from_dev+0x100>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_from_dev>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %rdi,%rsi
call   1030 <strncpy@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11ce <get_ip_from_dev+0x5e>
mov    %eax,%ebx
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11f7 <get_ip_from_dev+0x87>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1040 <inet_ntoa@plt>
jmp    1227 <get_ip_from_dev+0xb7>
mov    0x2ddb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1070 <log_msg@plt>
jmp    1225 <get_ip_from_dev+0xb5>
mov    0x2db2(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe05(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1070 <log_msg@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_in = type { i32 }
@IFNAMSIZ = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [26 x i8] c"Error opening socket: %s\0A\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.1 = [29 x i8] c"Error opening if device: %s\0A\00"
define i8* @get_ip_from_dev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
%6 = alloca %struct.sockaddr_in*
store i8* %0, i8** %3
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%8 = bitcast i32* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %8, %struct.sockaddr_in** %6
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%10 = load i32, i32* %9
%11 = load i8*, i8** %3
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i32 %10, i8* %11, i32 %12)
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @log_msg(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %22)
store i8* null, i8** %2
br label %43
24: ; preds = %1
%25 = load i32, i32* %4
%26 = load i32, i32* @SIOCGIFADDR
%27 = call i32 @ioctl(i32 %25, i32 %26, %struct.ifreq* %5)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* @LOG_ERR
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @log_msg(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
store i8* null, i8** %2
br label %43
36: ; preds = %24
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%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)
store i8* %42, i8** %2
br label %43
43: ; preds = %36, %29, %19
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@IFNAMSIZ = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@LOG_ERR = global i32 0
@.str = [26 x i8] c"Error opening socket: %s\0A\00"
@errno = global i32 0
@SIOCGIFADDR = global i32 0
@.str.1 = [29 x i8] c"Error opening if device: %s\0A\00"
define i8* @get_ip_from_dev(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%5 = load i32, i32* @IFNAMSIZ
%6 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %5)
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
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 @log_msg(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %30
16: ; preds = %1
%17 = load i32, i32* @SIOCGIFADDR
%18 = call i32 @ioctl(i32 %9, i32 %17, %struct.ifreq* %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* @LOG_ERR
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 @log_msg(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @close(i32 %9)
br label %30
26: ; preds = %16
%27 = call i32 @close(i32 %9)
%28 = load i32, i32* %4
%29 = call i8* @inet_ntoa(i32 %28)
br label %30
30: ; preds = %26, %20, %11
%31 = phi i8* [ null, %11 ], [ null, %20 ], [ %29, %26 ]
ret i8* %31
}