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

/* Variables and functions */
scalar_t__ ARPHRD_ETHER;
scalar_t__ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int get_mac_address(int fd, const char *dname, unsigned char *buffer) {
  struct ifreq dconf;
  memset(&dconf, 0, sizeof(dconf));
  strncpy(dconf.ifr_name, dname, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFHWADDR, &dconf) != 0)
    return -1;
  if (dconf.ifr_hwaddr.sa_family != ARPHRD_ETHER)
    return -2;
  memcpy(buffer, dconf.ifr_hwaddr.sa_data, 6);
  return 0;
}


x86 assembly (clang, -O0):
<get_mac_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e5f(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11a8 <get_mac_address+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e0 <get_mac_address+0xa0>
mov    -0x30(%rbp),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
je     11c8 <get_mac_address+0x88>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e0 <get_mac_address+0xa0>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x6,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mac_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x10(%rsp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11c6 <get_mac_address+0x86>
mov    (%rsp),%rcx
mov    0x2e3f(%rip),%rdx
mov    $0xfffffffe,%eax
cmp    (%rdx),%rcx
jne    11c6 <get_mac_address+0x86>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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