#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;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
void GetMacAddress(char *ifacename, char *pMacAddr) {
  struct ifreq ifr;
  int s;
  int ok = 0;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1)
    return;
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, ifacename);
  if (ioctl(s, SIOCGIFHWADDR, &ifr) >= 0) {
    memcpy(pMacAddr, ifr.ifr_hwaddr.sa_data, 6);
  }
  close(s);
}


x86 assembly (clang, -O0):
<GetMacAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a2 <GetMacAddress+0x42>
jmp    11f6 <GetMacAddress+0x96>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jl     11ee <GetMacAddress+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetMacAddress>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11da <GetMacAddress+0x7a>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11d3 <GetMacAddress+0x73>
mov    (%rsp),%esi
mov    %r14,%rdi
mov    $0x6,%edx
call   1070 <memcpy@plt>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
define void @GetMacAddress(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.ifreq
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %7
%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
br label %33
14: ; preds = %2
%15 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* %6
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %5)
%23 = icmp sge i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %14
%25 = load i8*, i8** %4
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %25, i32 %28, i32 6)
br label %30
30: ; preds = %24, %14
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %30, %13
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFHWADDR = global i32 0
define void @GetMacAddress(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 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @strcpy(i32 %12, i8* %0)
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i64 @ioctl(i32 %7, i32 %14, %struct.ifreq* %3)
%16 = icmp sgt i64 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i8* %1, i32 %19, i32 6)
br label %21
21: ; preds = %17, %9
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %2, %21
ret void
}