#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int hwaddr_get(char *ifname, char *addr, int addrlen) {
  struct ifreq ifr;
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  int ret;
  if (fd < 0)
    return (-1);
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  ret = ioctl(fd, SIOCGIFHWADDR, (char *)&ifr);
  memcpy(addr, ifr.ifr_hwaddr.sa_data, addrlen);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<hwaddr_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1195 <hwaddr_get+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <hwaddr_get+0x8b>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hwaddr_get>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
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     11b8 <hwaddr_get+0x68>
mov    %eax,%ebp
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
mov    (%rsp),%esi
mov    %r15,%rdi
mov    %r14d,%edx
call   1060 <memcpy@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11bd <hwaddr_get+0x6d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
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 i32 @hwaddr_get(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.ifreq
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %35
17: ; preds = %3
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load i8*, i8** %5
%21 = call i32 @strncpy(i32 %19, i8* %20, i32 4)
%22 = load i32, i32* %9
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = bitcast %struct.ifreq* %8 to i8*
%25 = call i32 @ioctl(i32 %22, i32 %23, i8* %24)
store i32 %25, i32* %10
%26 = load i8*, i8** %6
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* %7
%31 = call i32 @memcpy(i8* %26, i32 %29, i32 %30)
%32 = load i32, i32* %9
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %10
store i32 %34, i32* %4
br label %35
35: ; preds = %17, %16
%36 = load i32, i32* %4
ret i32 %36
}

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 @hwaddr_get(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %3
%11 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%12 = load i32, i32* @SIOCGIFHWADDR
%13 = call i32 @ioctl(i32 %8, i32 %12, i8* %5)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @memcpy(i8* %1, i32 %15, i32 %2)
%17 = call i32 @close(i32 %8)
br label %18
18: ; preds = %3, %10
%19 = phi i32 [ %13, %10 ], [ -1, %3 ]
ret i32 %19
}