#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__ {
  unsigned char *sa_data;
};
struct ifreq {
  TYPE_1__ ifr_addr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int hw_address(char *interface, unsigned char *hw_address) {
  struct ifreq s;
  strncpy(s.ifr_name, interface, sizeof(s.ifr_name));
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  if (fd == -1)
    return -1;
  if (ioctl(fd, SIOCGIFHWADDR, &s) == -1)
    return -1;
  int i;
  for (i = 0; i < 6; i++) {
    hw_address[i] = s.ifr_addr.sa_data[i];
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<hw_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    119a <hw_address+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <hw_address+0xc8>
mov    -0x2c(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11c4 <hw_address+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <hw_address+0xc8>
movl   $0x0,-0x30(%rbp)
cmpl   $0x6,-0x30(%rbp)
jge    11f9 <hw_address+0xb9>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11cb <hw_address+0x8b>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hw_address>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11d1 <hw_address+0x91>
mov    %eax,%ebp
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11d1 <hw_address+0x91>
mov    (%rsp),%rax
mov    (%rax),%cl
mov    %cl,(%r14)
mov    0x1(%rax),%cl
mov    %cl,0x1(%r14)
mov    0x2(%rax),%cl
mov    %cl,0x2(%r14)
mov    0x3(%rax),%cl
mov    %cl,0x3(%r14)
mov    0x4(%rax),%cl
mov    %cl,0x4(%r14)
mov    0x5(%rax),%al
mov    %al,0x5(%r14)
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @hw_address(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%10 = load i32, i32* %9
%11 = load i8*, i8** %4
%12 = call i32 @strncpy(i32 %10, i8* %11, i32 4)
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_IP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %48
20: ; preds = %2
%21 = load i32, i32* %7
%22 = load i32, i32* @SIOCGIFHWADDR
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %6)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 -1, i32* %3
br label %48
26: ; preds = %20
store i32 0, i32* %8
br label %27
27: ; preds = %42, %26
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 6
br i1 %29, label %30, label %45
30: ; preds = %27
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %5
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
br label %42
42: ; preds = %30
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %27
45: ; preds = %27
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %25, %19
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_IP = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @hw_address(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_IP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %35, label %11
11: ; preds = %2
%12 = load i32, i32* @SIOCGIFHWADDR
%13 = call i32 @ioctl(i32 %9, i32 %12, %struct.ifreq* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %35, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8, i8* %17
store i8 %18, i8* %1
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = load i8, i8* %19
%21 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %20, i8* %21
%22 = getelementptr inbounds i8, i8* %17, i64 2
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %23, i8* %24
%25 = getelementptr inbounds i8, i8* %17, i64 3
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %26, i8* %27
%28 = getelementptr inbounds i8, i8* %17, i64 4
%29 = load i8, i8* %28
%30 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %29, i8* %30
%31 = getelementptr inbounds i8, i8* %17, i64 5
%32 = load i8, i8* %31
%33 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %32, i8* %33
%34 = call i32 @close(i32 %9)
br label %35
35: ; preds = %11, %2, %15
%36 = phi i32 [ 0, %15 ], [ -1, %2 ], [ -1, %11 ]
ret i32 %36
}