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

/* 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(unsigned char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int get_mac_address(const char *ifname, unsigned char macaddress[6]) {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    return -1;
  }
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) {
    close(fd);
    return -1;
  }
  close(fd);
  memcpy(macaddress, ifr.ifr_hwaddr.sa_data, 6);
  return 0;
}


x86 assembly (clang, -O0):
<get_mac_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1192 <get_mac_address+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <get_mac_address+0xbb>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e1c(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11eb <get_mac_address+0x9b>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <get_mac_address+0xbb>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
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   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e7f(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     11db <get_mac_address+0x8b>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1050 <close@plt>
test   %rbp,%rbp
jne    11db <get_mac_address+0x8b>
mov    (%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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