#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ sa_data;
};
struct TYPE_4__ {
  TYPE_1__ ifru_hwaddr;
};
struct ifreq {
  TYPE_2__ ifr_ifru;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFHWADDRLEN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
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*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int net_get_hwaddr(char *ifname, unsigned char *mac) {
  struct ifreq ifr;
  int skfd;
  if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket error");
    return -1;
  }
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) {
    perror("net_get_hwaddr: ioctl SIOCGIFHWADDR");
    close(skfd);
    return -1;
  }
  close(skfd);
  memcpy(mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, IFHWADDRLEN);
  return 0;
}


x86 assembly (clang, -O0):
<net_get_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11ad <net_get_hwaddr+0x4d>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <net_get_hwaddr+0xc5>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1201 <net_get_hwaddr+0xa1>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <net_get_hwaddr+0xc5>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_get_hwaddr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11df <net_get_hwaddr+0x7f>
mov    %eax,%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11f2 <net_get_hwaddr+0x92>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%esi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1060 <memcpy@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11d6 <net_get_hwaddr+0x76>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
jmp    11d6 <net_get_hwaddr+0x76>

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.1 = [36 x i8] c"net_get_hwaddr: ioctl SIOCGIFHWADDR\00"
@IFHWADDRLEN = global i32 0
define i32 @net_get_hwaddr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
14: ; preds = %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %4
%18 = load i32, i32* @IFNAMSIZ
%19 = call i32 @strncpy(i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %7
%21 = load i32, i32* @SIOCGIFHWADDR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %3
br label %38
28: ; preds = %14
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %5
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @IFHWADDRLEN
%37 = call i32 @memcpy(i8* %31, i32 %35, i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %28, %24, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str.1 = [36 x i8] c"net_get_hwaddr: ioctl SIOCGIFHWADDR\00"
@IFHWADDRLEN = global i32 0
define i32 @net_get_hwaddr(i8* %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %26
11: ; preds = %2
%12 = load i32, i32* @IFNAMSIZ
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %12)
%14 = load i32, i32* @SIOCGIFHWADDR
%15 = call i64 @ioctl(i32 %7, i32 %14, %struct.ifreq* %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @close(i32 %7)
br label %26
20: ; preds = %11
%21 = call i32 @close(i32 %7)
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @IFHWADDRLEN
%25 = call i32 @memcpy(i8* %1, i32 %23, i32 %24)
br label %26
26: ; preds = %20, %17, %9
%27 = phi i32 [ -1, %9 ], [ -1, %17 ], [ 0, %20 ]
ret i32 %27
}