#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*/ u_char;
struct TYPE_4__ {
  scalar_t__ sa_data;
};
struct TYPE_3__ {
  int /*<<< orphan*/ sa_family;
};
struct ifreq {
  TYPE_2__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
  TYPE_1__ ifr_addr;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int get_iface_mac(const char *iface, u_char buf[]) {
  int fd;
  int ret = 0;
  struct ifreq ifr;
  u_char *mac = NULL;
  memset(&ifr, 0, sizeof(ifr));
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
    ret = -1;
    goto failed;
  }
  mac = (u_char *)ifr.ifr_hwaddr.sa_data;
  memcpy(buf, mac, 6);
failed:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<get_iface_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11fc <get_iface_mac+0x9c>
movl   $0xffffffff,-0x18(%rbp)
jmp    1216 <get_iface_mac+0xb6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_iface_mac>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r13
lea    0x8(%rsp),%r15
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e56(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r13,%rsi
call   1030 <strncpy@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f2 <get_iface_mac+0x92>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x6,%edx
call   1070 <memcpy@plt>
jmp    11f8 <get_iface_mac+0x98>
mov    $0xffffffff,%r12d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, i32, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_iface_mac(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca i32*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %6
store i32* null, i32** %8
%9 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 16)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 2
%15 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = load i64, i64* @IFNAMSIZ
%20 = sub nsw i64 %19, 1
%21 = call i32 @strncpy(i32 %17, i8* %18, i64 %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i64 @ioctl(i32 %22, i32 %23, %struct.ifreq* %7)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %2
store i32 -1, i32* %6
br label %35
27: ; preds = %2
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %28, i32 0, i32 0
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to i32*
store i32* %31, i32** %8
%32 = load i32*, i32** %4
%33 = load i32*, i32** %8
%34 = call i32 @memcpy(i32* %32, i32* %33, i32 6)
br label %35
35: ; preds = %27, %26
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, i32, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i64 }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IFNAMSIZ = global i64 0
@SIOCGIFHWADDR = global i32 0
define i32 @get_iface_mac(i8* %0, i32* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 2, i32 0
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i64, i64* @IFNAMSIZ
%14 = add nsw i64 %13, -1
%15 = call i32 @strncpy(i32 %12, i8* %0, i64 %14)
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = call i64 @ioctl(i32 %8, i32 %16, %struct.ifreq* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %2
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i32*
%23 = call i32 @memcpy(i32* %1, i32* %22, i32 6)
br label %24
24: ; preds = %2, %19
%25 = phi i32 [ 0, %19 ], [ -1, %2 ]
%26 = call i32 @close(i32 %8)
ret i32 %25
}