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

/* Variables and functions */
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFADDR;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ err_sys(char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void GetMacAddress(unsigned char *addr, const char *networkname) {
  int i, sockfd;
  struct ifreq ifr;
  struct arpreq arpr;
  strncpy(ifr.ifr_name, networkname, sizeof(ifr.ifr_name));
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    err_sys("socket");
  }
  if (ioctl(sockfd, SIOCGIFADDR, &ifr) == -1) {
    err_sys("1-ioctl");
  }
  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1) {
    err_sys("2-ioctl");
  }
  unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
  memcpy(addr, mac, 6);
}


x86 assembly (clang, -O0):
<GetMacAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11a2 <GetMacAddress+0x52>
lea    0xe63(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x18(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11cc <GetMacAddress+0x7c>
lea    0xe40(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1202 <GetMacAddress+0xb2>
lea    0xe12(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetMacAddress>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    118f <GetMacAddress+0x3f>
lea    0xe76(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11b3 <GetMacAddress+0x63>
lea    0xe59(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11e4 <GetMacAddress+0x94>
lea    0xe30(%rip),%rdi
call   1040 <err_sys@plt>
mov    (%rsp),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
call   1060 <memcpy@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64, i32 }
%struct.arpreq = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [8 x i8] c"1-ioctl\00"
@ARPHRD_ETHER = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.2 = [8 x i8] c"2-ioctl\00"
define void @GetMacAddress(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.arpreq
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i8*, i8** %4
%13 = call i32 @strncpy(i32 %11, i8* %12, i32 4)
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load i32, i32* %6
%22 = load i32, i32* @SIOCGIFADDR
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %7)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* @ARPHRD_ETHER
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 1
store i32 %28, i32* %30
%31 = load i32, i32* %6
%32 = load i32, i32* @SIOCGIFHWADDR
%33 = call i32 @ioctl(i32 %31, i32 %32, %struct.ifreq* %7)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %27
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %9
%42 = load i8*, i8** %3
%43 = load i8*, i8** %9
%44 = call i32 @memcpy(i8* %42, i8* %43, i32 6)
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64, i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFADDR = global i32 0
@.str.1 = [8 x i8] c"1-ioctl\00"
@ARPHRD_ETHER = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.2 = [8 x i8] c"2-ioctl\00"
define void @GetMacAddress(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 @strncpy(i32 undef, i8* %1, i32 4)
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @SIOCGIFADDR
%14 = call i32 @ioctl(i32 %8, i32 %13, %struct.ifreq* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* @ARPHRD_ETHER
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i32 @ioctl(i32 %8, i32 %21, %struct.ifreq* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i8*
%30 = call i32 @memcpy(i8* %0, i8* %29, i32 6)
ret void
}