#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 {
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};
struct addr {
  int dummy;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ARPHRD_ETHER;
int IF_NAMESIZE;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
struct addr *addr_create(int, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *if_indextoname(int, char *);
scalar_t__ 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);
struct addr *iface_hw_addr(int if_index) {
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1)
    return NULL;
  char if_name[IF_NAMESIZE];
  if (if_indextoname(if_index, if_name) == NULL)
    goto exit;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, if_name, IF_NAMESIZE - 1);
  if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
    goto exit;
  if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
    goto exit;
  struct addr *addr = addr_create(3, ifr.ifr_hwaddr.sa_data);
  close(fd);
  return addr;
exit:
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<iface_hw_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ae <iface_hw_addr+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    12a1 <iface_hw_addr+0x131>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x50(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <if_indextoname@plt>
cmp    $0x0,%rax
jne    11f1 <iface_hw_addr+0x81>
jmp    1283 <iface_hw_addr+0x113>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x50(%rbp),%rsi
mov    -0x28(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    123d <iface_hw_addr+0xcd>
jmp    1283 <iface_hw_addr+0x113>
mov    -0x38(%rbp),%rax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%rax
je     1256 <iface_hw_addr+0xe6>
jmp    1283 <iface_hw_addr+0x113>
mov    -0x30(%rbp),%esi
mov    $0x3,%edi
call   1070 <addr_create@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    129a <iface_hw_addr+0x12a>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iface_hw_addr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%r15d
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1251 <iface_hw_addr+0xe1>
mov    %eax,%r14d
mov    %rsp,-0x30(%rbp)
mov    0x2e29(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <if_indextoname@plt>
test   %rax,%rax
je     123f <iface_hw_addr+0xcf>
lea    -0x48(%rbp),%r15
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x38(%rbp),%edi
mov    0x0(%r13),%edx
add    $0xffffffff,%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1242 <iface_hw_addr+0xd2>
mov    -0x48(%rbp),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    123f <iface_hw_addr+0xcf>
mov    -0x40(%rbp),%esi
mov    $0x3,%edi
call   1070 <addr_create@plt>
mov    %rax,%r12
jmp    1242 <iface_hw_addr+0xd2>
xor    %r12d,%r12d
mov    -0x30(%rbp),%rbx
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbx,%rsp
mov    %r12,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addr = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IF_NAMESIZE = global i32 0
@SIOCGIFHWADDR = global i32 0
@ARPHRD_ETHER = global i64 0
define %struct.addr* @iface_hw_addr(i32 %0) {
%2 = alloca %struct.addr*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.ifreq
%8 = alloca %struct.addr*
%9 = alloca i32
store i32 %0, i32* %3
%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* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store %struct.addr* null, %struct.addr** %2
br label %57
16: ; preds = %1
%17 = load i32, i32* @IF_NAMESIZE
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %5
%20 = alloca i8, i64 %18
store i64 %18, i64* %6
%21 = load i32, i32* %3
%22 = call i32* @if_indextoname(i32 %21, i8* %20)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %16
br label %52
25: ; preds = %16
%26 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 24)
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i32, i32* @IF_NAMESIZE
%30 = sub nsw i32 %29, 1
%31 = call i32 @strncpy(i32 %28, i8* %20, i32 %30)
%32 = load i32, i32* %4
%33 = load i32, i32* @SIOCGIFHWADDR
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.ifreq* %7)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %25
br label %52
37: ; preds = %25
%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 = load i64, i64* @ARPHRD_ETHER
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %37
br label %52
44: ; preds = %37
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = call %struct.addr* @addr_create(i32 3, i32 %47)
store %struct.addr* %48, %struct.addr** %8
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = load %struct.addr*, %struct.addr** %8
store %struct.addr* %51, %struct.addr** %2
store i32 1, i32* %9
br label %55
52: ; preds = %43, %36, %24
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
store %struct.addr* null, %struct.addr** %2
store i32 1, i32* %9
br label %55
55: ; preds = %52, %44
%56 = load i8*, i8** %5
br label %57
57: ; preds = %55, %15
%58 = load %struct.addr*, %struct.addr** %2
ret %struct.addr* %58
}

LLVM IR (-O2):
%struct.addr = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IF_NAMESIZE = global i32 0
@SIOCGIFHWADDR = global i32 0
@ARPHRD_ETHER = global i64 0
define %struct.addr* @iface_hw_addr(i32 %0) {
%2 = alloca %struct.ifreq
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %36, label %7
7: ; preds = %1
%8 = load i32, i32* @IF_NAMESIZE
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = call i32* @if_indextoname(i32 %0, i8* %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %33, label %14
14: ; preds = %7
%15 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 24)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%17 = load i32, i32* %16
%18 = load i32, i32* @IF_NAMESIZE
%19 = add nsw i32 %18, -1
%20 = call i32 @strncpy(i32 %17, i8* %11, i32 %19)
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i64 @ioctl(i32 %5, i32 %21, %struct.ifreq* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %14
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i64, i64* @ARPHRD_ETHER
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call %struct.addr* @addr_create(i32 3, i32 %31)
br label %33
33: ; preds = %7, %14, %24, %29
%34 = phi %struct.addr* [ %32, %29 ], [ null, %24 ], [ null, %14 ], [ null, %7 ]
%35 = call i32 @close(i32 %5)
br label %36
36: ; preds = %1, %33
%37 = phi %struct.addr* [ %34, %33 ], [ null, %1 ]
ret %struct.addr* %37
}