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

/* Variables and functions */
int ETH_ALEN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int errno;
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int etherp_if_mac_addr(int s, const char *ifname,
                       unsigned char mac_addr[ETH_ALEN]) {
  struct ifreq ifr;
  int i;
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
    perror("Cannot get MAC address");
    return errno;
  }
  for (i = 0; i < ETH_ALEN; ++i)
    mac_addr[i] = (unsigned char)ifr.ifr_hwaddr.sa_data[i];
  return 0;
}


x86 assembly (clang, -O0):
<etherp_if_mac_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1193 <etherp_if_mac_addr+0x63>
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <etherp_if_mac_addr+0xaa>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%eax
jge    11d3 <etherp_if_mac_addr+0xa3>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119a <etherp_if_mac_addr+0x6a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<etherp_if_mac_addr>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %edi,%ebp
mov    0x2e8e(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     119e <etherp_if_mac_addr+0x6e>
mov    0x2e7e(%rip),%rdi
cmpl   $0x0,(%rdi)
jle    1195 <etherp_if_mac_addr+0x65>
mov    0x8(%rsp),%rdx
xor    %eax,%eax
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
movzbl (%rdx,%rsi,8),%ecx
mov    %cl,(%rbx,%rsi,1)
add    $0x1,%rsi
movslq (%rdi),%rcx
cmp    %rcx,%rsi
jl     1180 <etherp_if_mac_addr+0x50>
jmp    1197 <etherp_if_mac_addr+0x67>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
jmp    1197 <etherp_if_mac_addr+0x67>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@ETH_ALEN = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [23 x i8] c"Cannot get MAC address\00"
@errno = global i32 0
define i32 @etherp_if_mac_addr(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.ifreq
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i32, i32* @ETH_ALEN
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%13 = load i32, i32* %12
%14 = load i8*, i8** %6
%15 = load i32, i32* @IFNAMSIZ
%16 = call i32 @strncpy(i32 %13, i8* %14, i32 %15)
%17 = load i32, i32* %5
%18 = load i32, i32* @SIOCGIFHWADDR
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @errno
store i32 %23, i32* %4
br label %46
24: ; preds = %3
store i32 0, i32* %9
br label %25
25: ; preds = %42, %24
%26 = load i32, i32* %9
%27 = load i32, i32* @ETH_ALEN
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %45
29: ; preds = %25
%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 0
%32 = load i64*, i64** %31
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i64, i64* %32, i64 %34
%36 = load i64, i64* %35
%37 = trunc i64 %36 to i8
%38 = load i8*, i8** %7
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
br label %42
42: ; preds = %29
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %25
45: ; preds = %25
store i32 0, i32* %4
br label %46
46: ; preds = %45, %21
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64* }
@ETH_ALEN = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [23 x i8] c"Cannot get MAC address\00"
@errno = global i32 0
define i32 @etherp_if_mac_addr(i32 %0, i8* %1, i8* nocapture writeonly %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @IFNAMSIZ
%7 = tail call i32 @strncpy(i32 undef, i8* %1, i32 %6)
%8 = load i32, i32* @SIOCGIFHWADDR
%9 = call i32 @ioctl(i32 %0, i32 %8, %struct.ifreq* %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = load i32, i32* @ETH_ALEN
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
%16 = load i64*, i64** %15
br label %20
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @errno
br label %30
20: ; preds = %14, %20
%21 = phi i64 [ 0, %14 ], [ %26, %20 ]
%22 = getelementptr inbounds i64, i64* %16, i64 %21
%23 = load i64, i64* %22
%24 = trunc i64 %23 to i8
%25 = getelementptr inbounds i8, i8* %2, i64 %21
store i8 %24, i8* %25
%26 = add nuw nsw i64 %21, 1
%27 = load i32, i32* @ETH_ALEN
%28 = sext i32 %27 to i64
%29 = icmp slt i64 %26, %28
br i1 %29, label %20, label %30
30: ; preds = %20, %11, %17
%31 = phi i32 [ %19, %17 ], [ 0, %11 ], [ 0, %20 ]
ret i32 %31
}