#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct TYPE_3__ {
  int /*<<< orphan*/ sa_data;
};
struct TYPE_4__ {
  TYPE_1__ ifru_hwaddr;
};
struct ifreq {
  char *ifr_name;
  TYPE_2__ ifr_ifru;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFHWADDRLEN;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int get_mac_by_ifname(const char *ifname, uint8_t *hwaddr) {
  struct ifreq ifr;
  int sock;
  if ((NULL == ifname) || (NULL == hwaddr)) {
    return -1;
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
  ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
  if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
    perror("ioctl(SIOCGIFHWADDR)");
    return -1;
  }
  memcpy(hwaddr, ifr.ifr_ifru.ifru_hwaddr.sa_data, IFHWADDRLEN);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<get_mac_by_ifname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1188 <get_mac_by_ifname+0x28>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1194 <get_mac_by_ifname+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <get_mac_by_ifname+0xda>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11c6 <get_mac_by_ifname+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <get_mac_by_ifname+0xda>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,0x7(%rax)
mov    -0x2c(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1216 <get_mac_by_ifname+0xb6>
lea    0xdfb(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <get_mac_by_ifname+0xda>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac_by_ifname>:
push   %rbx
test   %rdi,%rdi
je     114b <get_mac_by_ifname+0x2b>
test   %rsi,%rsi
je     114b <get_mac_by_ifname+0x2b>
mov    %rdi,%rbx
mov    0x2eb3(%rip),%rax
mov    (%rax),%edi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <socket@plt>
test   %eax,%eax
jns    1152 <get_mac_by_ifname+0x32>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.ifreq = type { i8*, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [21 x i8] c"ioctl(SIOCGIFHWADDR)\00"
@IFHWADDRLEN = global i32 0
define i32 @get_mac_by_ifname(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* null, %8
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %45
14: ; preds = %10
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %3
br label %45
21: ; preds = %14
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8*, i8** %4
%25 = call i32 @strncpy(i8* %23, i8* %24, i32 7)
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 7
store i8 0, i8* %28
%29 = load i32, i32* %7
%30 = load i32, i32* @SIOCGIFHWADDR
%31 = call i32 @ioctl(i32 %29, i32 %30, %struct.ifreq* %6)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
35: ; preds = %21
%36 = load i32*, i32** %5
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %37, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i32, i32* @IFHWADDRLEN
%42 = call i32 @memcpy(i32* %36, i32 %40, i32 %41)
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %35, %33, %20, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFHWADDR = global i32 0
@IFHWADDRLEN = global i32 0
define i32 @get_mac_by_ifname(i8* %0, i32* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i32* %1, null
%5 = or i1 %3, %4
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @strncpy(i8* undef, i8* %0, i32 7)
unreachable
13: ; preds = %6, %2
ret i32 -1
}