#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*/ IFNAMSIZ;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char,
                           unsigned char, unsigned char, unsigned char,
                           unsigned char);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int find_my_mac(const char *iface, char *mac) {
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, iface, IFNAMSIZ);
  int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (s == -1) {
    perror("socket");
    return -1;
  }
  if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
    perror("ioctl(SIOCGIFHWADDR)");
    close(s);
    return -1;
  }
  unsigned char macaddr[6];
  memcpy(macaddr, ifr.ifr_hwaddr.sa_data, 6);
  sprintf(mac, "%.2X%.2X%.2X%.2X%.2X%.2X", macaddr[0], macaddr[1], macaddr[2],
          macaddr[3], macaddr[4], macaddr[5]);
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<find_my_mac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11fa <find_my_mac+0x7a>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <find_my_mac+0x115>
mov    -0x24(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1238 <find_my_mac+0xb8>
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <find_my_mac+0x115>
lea    -0x2a(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x2a(%rbp),%dl
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%r8b
mov    -0x27(%rbp),%r9b
mov    -0x26(%rbp),%al
lea    0xdb7(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %al,%eax
mov    %eax,(%rsp)
movzbl -0x25(%rbp),%eax
mov    %eax,0x8(%rsp)
call   1090 <sprintf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_my_mac>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     124f <find_my_mac+0xcf>
mov    %eax,%ebp
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1262 <find_my_mac+0xe2>
mov    0x8(%rsp),%esi
lea    0x2(%rsp),%rdi
mov    $0x6,%edx
call   1070 <memcpy@plt>
movzbl 0x2(%rsp),%edx
movzbl 0x3(%rsp),%ecx
movzbl 0x4(%rsp),%r8d
movzbl 0x5(%rsp),%r9d
movzbl 0x6(%rsp),%eax
movzbl 0x7(%rsp),%ebx
lea    0xded(%rip),%rsi
mov    %r14,%rdi
push   %rbx
push   %rax
call   1090 <sprintf@plt>
add    $0x10,%rsp
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1246 <find_my_mac+0xc6>
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
jmp    1246 <find_my_mac+0xc6>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@IFNAMSIZ = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"ioctl(SIOCGIFHWADDR)\00"
@.str.2 = [25 x i8] c"%.2X%.2X%.2X%.2X%.2X%.2X\00"
define i32 @find_my_mac(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca [6 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i8*, i8** %4
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strncpy(i32 %11, i8* %12, i32 %13)
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = load i32, i32* @IPPROTO_IP
%18 = call i32 @socket(i32 %15, i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
23: ; preds = %2
%24 = load i32, i32* %7
%25 = load i32, i32* @SIOCGIFHWADDR
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %6)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %3
br label %54
32: ; preds = %23
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @memcpy(i8* %33, i32 %36, i32 6)
%38 = load i8*, i8** %5
%39 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%40 = load i8, i8* %39
%41 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 1
%42 = load i8, i8* %41
%43 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 2
%44 = load i8, i8* %43
%45 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 3
%46 = load i8, i8* %45
%47 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 4
%48 = load i8, i8* %47
%49 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 5
%50 = load i8, i8* %49
%51 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %40, i8 zeroext %42, i8 zeroext %44, i8 zeroext %46, i8 zeroext %48, i8 zeroext %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
store i32 0, i32* %3
br label %54
54: ; preds = %32, %28, %21
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@IFNAMSIZ = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"ioctl(SIOCGIFHWADDR)\00"
@.str.2 = [25 x i8] c"%.2X%.2X%.2X%.2X%.2X%.2X\00"
define i32 @find_my_mac(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = alloca [6 x i8]
%5 = bitcast %struct.ifreq* %3 to i8*
%6 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%8 = load i32, i32* %7
%9 = load i32, i32* @IFNAMSIZ
%10 = call i32 @strncpy(i32 %8, i8* %0, i32 %9)
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @IPPROTO_IP
%14 = call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %43
18: ; preds = %2
%19 = load i32, i32* @SIOCGIFHWADDR
%20 = call i32 @ioctl(i32 %14, i32 %19, %struct.ifreq* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @close(i32 %14)
br label %43
25: ; preds = %18
%26 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %26, i32 %28, i32 6)
%30 = load i8, i8* %26
%31 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 1
%32 = load i8, i8* %31
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 2
%34 = load i8, i8* %33
%35 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
%36 = load i8, i8* %35
%37 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 4
%38 = load i8, i8* %37
%39 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 5
%40 = load i8, i8* %39
%41 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %30, i8 zeroext %32, i8 zeroext %34, i8 zeroext %36, i8 zeroext %38, i8 zeroext %40)
%42 = call i32 @close(i32 %14)
br label %43
43: ; preds = %25, %22, %16
%44 = phi i32 [ -1, %16 ], [ -1, %22 ], [ 0, %25 ]
ret i32 %44
}