#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__ {
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ SIOCGIFHWADDR;
scalar_t__ ioctl(int const, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int get_device_mac(const int sockfd, const char *device_name,
                   unsigned char *mac) {
  struct ifreq ifr;
  strncpy(ifr.ifr_name, device_name, 16);
  if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) != 0) {
    return -1;
  }
  memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  return 1;
}


x86 assembly (clang, -O0):
<get_device_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     117f <get_device_mac+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    119b <get_device_mac+0x6b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_device_mac>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %edi,%ebp
mov    $0x10,%edx
call   1030 <strncpy@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    117b <get_device_mac+0x4b>
mov    (%rsp),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <memcpy@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@SIOCGIFHWADDR = global i32 0
@ETH_ALEN = global i32 0
define i32 @get_device_mac(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 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %6
%12 = call i32 @strncpy(i32 %10, i8* %11, i32 16)
%13 = load i32, i32* %5
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i64 @ioctl(i32 %13, i32 %14, %struct.ifreq* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -1, i32* %4
br label %25
18: ; preds = %3
%19 = load i8*, i8** %7
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @ETH_ALEN
%24 = call i32 @memcpy(i8* %19, i32 %22, i32 %23)
store i32 1, i32* %4
br label %25
25: ; preds = %18, %17
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@SIOCGIFHWADDR = global i32 0
@ETH_ALEN = global i32 0
define i32 @get_device_mac(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = tail call i32 @strncpy(i32 undef, i8* %1, i32 16)
%7 = load i32, i32* @SIOCGIFHWADDR
%8 = call i64 @ioctl(i32 %0, i32 %7, %struct.ifreq* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %3
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @ETH_ALEN
%14 = call i32 @memcpy(i8* %2, i32 %12, i32 %13)
br label %15
15: ; preds = %3, %10
%16 = phi i32 [ 1, %10 ], [ -1, %3 ]
ret i32 %16
}