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

/* Variables and functions */
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ ETH_ALEN;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCSIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned char *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ safe_strncpy(int /*<<< orphan*/, char const *,
                                int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tap_set_hwaddr(int fd, const char *dev, unsigned char *hwaddr) {
  struct ifreq ifr;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  memcpy(&ifr.ifr_hwaddr.sa_data, hwaddr, ETH_ALEN);
  if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
    perror("SIOCSIFHWADDR");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<tap_set_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1050 <safe_strncpy@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11fd <tap_set_hwaddr+0x9d>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <tap_set_hwaddr+0xac>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_set_hwaddr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1050 <safe_strncpy@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <memcpy@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f2 <tap_set_hwaddr+0x92>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe07(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11e4 <tap_set_hwaddr+0x84>

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

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