#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*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, scalar_t__);
int getMAC(const char *interface, char *mac) {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
  if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) {
    return -1;
  }
  close(fd);
  unsigned char mac_address[6] = "";
  memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
  int j = 0;
  int i = 0;
  for (i = 0; i < 6; i++) {
    sprintf(&(mac[j]), "%02X:", mac_address[i]);
    j += 3;
  }
  mac[j - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<getMAC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e12(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11ed <getMAC+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1284 <getMAC+0x114>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    -0x2e(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1040 <memset@plt>
lea    -0x2e(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x6,%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x6,-0x38(%rbp)
jge    126c <getMAC+0xfc>
mov    -0x18(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
movslq -0x38(%rbp),%rax
lea    0xdb5(%rip),%rsi
movzbl -0x2e(%rbp,%rax,1),%edx
call   1080 <sprintf@plt>
mov    -0x34(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    122b <getMAC+0xbb>
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMAC>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e71(%rip),%r14
mov    (%r14),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1267 <getMAC+0x107>
mov    %ebp,%edi
call   1050 <close@plt>
movw   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
mov    0x8(%rsp),%esi
mov    %rsp,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
movzbl (%rsp),%edx
lea    0xe00(%rip),%r14
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <sprintf@plt>
lea    0x3(%r15),%rdi
movzbl 0x1(%rsp),%edx
mov    %r14,%rsi
call   1070 <sprintf@plt>
lea    0x6(%r15),%rdi
movzbl 0x2(%rsp),%edx
mov    %r14,%rsi
call   1070 <sprintf@plt>
lea    0x9(%r15),%rdi
movzbl 0x3(%rsp),%edx
mov    %r14,%rsi
call   1070 <sprintf@plt>
lea    0xc(%r15),%rdi
movzbl 0x4(%rsp),%edx
mov    %r14,%rsi
call   1070 <sprintf@plt>
lea    0xf(%r15),%rdi
movzbl 0x5(%rsp),%edx
mov    %r14,%rsi
call   1070 <sprintf@plt>
movb   $0x0,0x11(%r15)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
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
@.str = [6 x i8] c"%02X:\00"
define i32 @getMAC(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca [6 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %4
%20 = load i64, i64* @IFNAMSIZ
%21 = sub nsw i64 %20, 1
%22 = call i32 @strncpy(i32 %18, i8* %19, i64 %21)
%23 = load i32, i32* %6
%24 = load i32, i32* @SIOCGIFHWADDR
%25 = call i64 @ioctl(i32 %23, i32 %24, %struct.ifreq* %7)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 -1, i32* %3
br label %61
28: ; preds = %2
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = bitcast [6 x i8]* %8 to i8*
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i8* %32, i32 %35, i32 6)
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %10
br label %37
37: ; preds = %52, %28
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 6
br i1 %39, label %40, label %55
40: ; preds = %37
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %48)
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 3
store i32 %51, i32* %9
br label %52
52: ; preds = %40
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %37
55: ; preds = %37
%56 = load i8*, i8** %5
%57 = load i32, i32* %9
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
store i8 0, i8* %60
store i32 0, i32* %3
br label %61
61: ; preds = %55, %27
%62 = load i32, i32* %3
ret i32 %62
}

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
@.str = [6 x i8] c"%02X:\00"
define i32 @getMAC(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = alloca [6 x i8]
%5 = bitcast %struct.ifreq* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail 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 = load i64, i64* @IFNAMSIZ
%12 = add nsw i64 %11, -1
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i64 %12)
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i64 @ioctl(i32 %8, i32 %14, %struct.ifreq* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %46
17: ; preds = %2
%18 = call i32 @close(i32 %8)
%19 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i8* %19, i32 %21, i32 6)
%23 = load i8, i8* %19
%24 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %23)
%25 = getelementptr inbounds i8, i8* %1, i64 3
%26 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 1
%27 = load i8, i8* %26
%28 = call i32 @sprintf(i8* dereferenceable(1) %25, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %27)
%29 = getelementptr inbounds i8, i8* %1, i64 6
%30 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 2
%31 = load i8, i8* %30
%32 = call i32 @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %31)
%33 = getelementptr inbounds i8, i8* %1, i64 9
%34 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* dereferenceable(1) %33, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %35)
%37 = getelementptr inbounds i8, i8* %1, i64 12
%38 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 4
%39 = load i8, i8* %38
%40 = call i32 @sprintf(i8* dereferenceable(1) %37, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %39)
%41 = getelementptr inbounds i8, i8* %1, i64 15
%42 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 5
%43 = load i8, i8* %42
%44 = call i32 @sprintf(i8* dereferenceable(1) %41, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %43)
%45 = getelementptr inbounds i8, i8* %1, i64 17
store i8 0, i8* %45
br label %46
46: ; preds = %2, %17
%47 = phi i32 [ 0, %17 ], [ -1, %2 ]
ret i32 %47
}