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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ARPHRD_ETHER;
int /*<<< orphan*/ ETH_ALEN;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int get_hwaddr(unsigned char *hwaddr) {
  int idx;
  int sock;
  struct ifreq ifr;
  char ifname[IFNAMSIZ + 1];
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    return (-1);
  for (idx = 1;; idx++) {
    memset(&ifr, 0, sizeof ifr);
    ifr.ifr_ifindex = idx;
    if (ioctl(sock, SIOCGIFNAME, &ifr) < 0)
      break;
    strncpy(ifname, ifr.ifr_name, IFNAMSIZ);
    ifname[IFNAMSIZ] = 0;
    memset(&ifr, 0, sizeof ifr);
    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
    if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
      break;
    if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
      continue;
    memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
    close(sock);
    return (0);
  }
  close(sock);
  return (-1);
}


x86 assembly (clang, -O0):
<get_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d3 <get_hwaddr+0x73>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    12f3 <get_hwaddr+0x193>
movl   $0x1,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1214 <get_hwaddr+0xb4>
jmp    12dd <get_hwaddr+0x17d>
mov    -0x58(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rax
mov    0x2d7b(%rip),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x58(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1286 <get_hwaddr+0x126>
jmp    12dd <get_hwaddr+0x17d>
mov    -0x28(%rbp),%rax
mov    0x2d3f(%rip),%rcx
cmp    (%rcx),%rax
je     129f <get_hwaddr+0x13f>
jmp    12cf <get_hwaddr+0x16f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2d33(%rip),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    12f3 <get_hwaddr+0x193>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11da <get_hwaddr+0x7a>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hwaddr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r13
mov    %rsp,%r12
mov    0x2e32(%rip),%r14
mov    (%r14),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     12ad <get_hwaddr+0x14d>
mov    %eax,%r15d
mov    %r13,-0x38(%rbp)
lea    -0x58(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x1,-0x58(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     12d1 <get_hwaddr+0x171>
mov    %r12,-0x30(%rbp)
mov    $0x2,%r12d
lea    -0x58(%rbp),%r13
nopl   0x0(%rax,%rax,1)
mov    -0x50(%rbp),%rsi
mov    (%r14),%edx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movslq (%r14),%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x50(%rbp),%rdi
mov    (%r14),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     12a1 <get_hwaddr+0x141>
mov    -0x48(%rbp),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
je     12b5 <get_hwaddr+0x155>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    %r12d,-0x58(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %r13,%rdx
call   1050 <ioctl@plt>
add    $0x1,%r12d
test   %rax,%rax
jns    1210 <get_hwaddr+0xb0>
mov    -0x30(%rbp),%r12
mov    $0xffffffff,%r13d
jmp    12d1 <get_hwaddr+0x171>
mov    $0xffffffff,%r13d
jmp    12d9 <get_hwaddr+0x179>
mov    -0x40(%rbp),%esi
mov    0x2d21(%rip),%rax
mov    (%rax),%edx
mov    -0x38(%rbp),%rdi
call   1070 <memcpy@plt>
xor    %r13d,%r13d
mov    -0x30(%rbp),%r12
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r12,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64, i32 }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@SIOCGIFHWADDR = global i32 0
@ARPHRD_ETHER = global i64 0
@ETH_ALEN = global i32 0
define i32 @get_hwaddr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @IFNAMSIZ
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %9
br label %70
20: ; preds = %1
store i32 1, i32* %4
br label %21
21: ; preds = %64, %20
%22 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 32)
%23 = load i32, i32* %4
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = load i32, i32* @SIOCGIFNAME
%27 = call i64 @ioctl(i32 %25, i32 %26, %struct.ifreq* %6)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
br label %67
30: ; preds = %21
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i8* %14, i8* %32, i32 %33)
%35 = load i32, i32* @IFNAMSIZ
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %14, i64 %36
store i8 0, i8* %37
%38 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 32)
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = load i32, i32* @IFNAMSIZ
%42 = call i32 @strncpy(i8* %40, i8* %14, i32 %41)
%43 = load i32, i32* %5
%44 = load i32, i32* @SIOCGIFHWADDR
%45 = call i64 @ioctl(i32 %43, i32 %44, %struct.ifreq* %6)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %30
br label %67
48: ; preds = %30
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = load i64, i64* @ARPHRD_ETHER
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %48
br label %64
55: ; preds = %48
%56 = load i8*, i8** %3
%57 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 2
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load i32, i32* @ETH_ALEN
%61 = call i32 @memcpy(i8* %56, i32 %59, i32 %60)
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
store i32 0, i32* %2
store i32 1, i32* %9
br label %70
64: ; preds = %54
%65 = load i32, i32* %4
%66 = add nsw i32 %65, 1
store i32 %66, i32* %4
br label %21
67: ; preds = %47, %29
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
store i32 1, i32* %9
br label %70
70: ; preds = %67, %55, %19
%71 = load i8*, i8** %7
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8*, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64, i32 }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@SIOCGIFHWADDR = global i32 0
@ARPHRD_ETHER = global i64 0
@ETH_ALEN = global i32 0
define i32 @get_hwaddr(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @IFNAMSIZ
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %55, label %13
13: ; preds = %1
%14 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 32)
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 1, i32* %15
%16 = load i32, i32* @SIOCGIFNAME
%17 = call i64 @ioctl(i32 %11, i32 %16, %struct.ifreq* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %52, label %19
19: ; preds = %13
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 2, i32 0
br label %27
22: ; preds = %42
%23 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 32)
store i32 %46, i32* %15
%24 = load i32, i32* @SIOCGIFNAME
%25 = call i64 @ioctl(i32 %11, i32 %24, %struct.ifreq* %2)
%26 = icmp slt i64 %25, 0
br i1 %26, label %52, label %27
27: ; preds = %19, %22
%28 = phi i32 [ 1, %19 ], [ %46, %22 ]
%29 = load i8*, i8** %20
%30 = load i32, i32* @IFNAMSIZ
%31 = call i32 @strncpy(i8* %8, i8* %29, i32 %30)
%32 = load i32, i32* @IFNAMSIZ
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %8, i64 %33
store i8 0, i8* %34
%35 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 32)
%36 = load i8*, i8** %20
%37 = load i32, i32* @IFNAMSIZ
%38 = call i32 @strncpy(i8* %36, i8* %8, i32 %37)
%39 = load i32, i32* @SIOCGIFHWADDR
%40 = call i64 @ioctl(i32 %11, i32 %39, %struct.ifreq* %2)
%41 = icmp slt i64 %40, 0
br i1 %41, label %52, label %42
42: ; preds = %27
%43 = load i64, i64* %21
%44 = load i64, i64* @ARPHRD_ETHER
%45 = icmp eq i64 %43, %44
%46 = add nuw nsw i32 %28, 1
br i1 %45, label %47, label %22
47: ; preds = %42
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 2, i32 1
%49 = load i32, i32* %48
%50 = load i32, i32* @ETH_ALEN
%51 = call i32 @memcpy(i8* %0, i32 %49, i32 %50)
br label %52
52: ; preds = %27, %22, %13, %47
%53 = phi i32 [ 0, %47 ], [ -1, %13 ], [ -1, %22 ], [ -1, %27 ]
%54 = call i32 @close(i32 %11)
br label %55
55: ; preds = %52, %1
%56 = phi i32 [ -1, %1 ], [ %53, %52 ]
ret i32 %56
}