#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 */
typedef int /*<<< orphan*/ uint8_t;
struct TYPE_2__ {
  int /*<<< orphan*/ *sa_data;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int sysapi_get_macaddr(char *ifname, uint8_t *macaddr) {
  int ret;
  int fd;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  int i;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
  if (ret > 0)
    for (i = 0; i < 6; i++)
      macaddr[i] = ifr.ifr_addr.sa_data[i];
  close(fd);
  return ret > 0 ? 0 : -1;
}


x86 assembly (clang, -O0):
<sysapi_get_macaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1192 <sysapi_get_macaddr+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <sysapi_get_macaddr+0xda>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    120f <sysapi_get_macaddr+0xbf>
movl   $0x0,-0x24(%rbp)
cmpl   $0x6,-0x24(%rbp)
jge    120a <sysapi_get_macaddr+0xba>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11dc <sysapi_get_macaddr+0x8c>
jmp    120f <sysapi_get_macaddr+0xbf>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysapi_get_macaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11f4 <sysapi_get_macaddr+0xa4>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
jle    11e2 <sysapi_get_macaddr+0x92>
mov    0x8(%rsp),%rcx
mov    (%rcx),%edx
mov    %edx,(%rbx)
mov    0x4(%rcx),%edx
mov    %edx,0x4(%rbx)
mov    0x8(%rcx),%edx
mov    %edx,0x8(%rbx)
mov    0xc(%rcx),%edx
mov    %edx,0xc(%rbx)
mov    0x10(%rcx),%edx
mov    %edx,0x10(%rbx)
mov    0x14(%rcx),%ecx
mov    %ecx,0x14(%rbx)
xor    %ebx,%ebx
test   %eax,%eax
setle  %bl
mov    %ebp,%edi
call   1060 <close@plt>
neg    %ebx
jmp    11f9 <sysapi_get_macaddr+0xa9>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%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 i32 @sysapi_get_macaddr(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.ifreq
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %54
16: ; preds = %2
%17 = call i32 @memset(%struct.ifreq* %9, i32 0, i32 16)
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %4
%21 = call i32 @strncpy(i32 %19, i8* %20, i32 4)
%22 = load i32, i32* %7
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %9)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %47
27: ; preds = %16
store i32 0, i32* %8
br label %28
28: ; preds = %43, %27
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 6
br i1 %30, label %31, label %46
31: ; preds = %28
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
%34 = load i32*, i32** %33
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = load i32*, i32** %5
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
store i32 %38, i32* %42
br label %43
43: ; preds = %31
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %28
46: ; preds = %28
br label %47
47: ; preds = %46, %16
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %6
%51 = icmp sgt i32 %50, 0
%52 = zext i1 %51 to i64
%53 = select i1 %51, i32 0, i32 -1
store i32 %53, i32* %3
br label %54
54: ; preds = %47, %15
%55 = load i32, i32* %3
ret i32 %55
}

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 i32 @sysapi_get_macaddr(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %40, label %8
8: ; preds = %2
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @strncpy(i32 %12, i8* %0, i32 4)
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i32 @ioctl(i32 %6, i32 %14, %struct.ifreq* %3)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %36
17: ; preds = %8
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%19 = load i32*, i32** %18
%20 = load i32, i32* %19
store i32 %20, i32* %1
%21 = getelementptr inbounds i32, i32* %19, i64 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %22, i32* %23
%24 = getelementptr inbounds i32, i32* %19, i64 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %25, i32* %26
%27 = getelementptr inbounds i32, i32* %19, i64 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds i32, i32* %1, i64 3
store i32 %28, i32* %29
%30 = getelementptr inbounds i32, i32* %19, i64 4
%31 = load i32, i32* %30
%32 = getelementptr inbounds i32, i32* %1, i64 4
store i32 %31, i32* %32
%33 = getelementptr inbounds i32, i32* %19, i64 5
%34 = load i32, i32* %33
%35 = getelementptr inbounds i32, i32* %1, i64 5
store i32 %34, i32* %35
br label %36
36: ; preds = %17, %8
%37 = call i32 @close(i32 %6)
%38 = xor i1 %16, true
%39 = sext i1 %38 to i32
br label %40
40: ; preds = %2, %36
%41 = phi i32 [ %39, %36 ], [ -1, %2 ]
ret i32 %41
}