#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_family;
  int /*<<< orphan*/ sa_data;
};
struct ifreq {
  unsigned long ifr_flags;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
unsigned long IFF_UP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ SIOCSIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, size_t const);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int set_hwaddr(const char *ifname, const char *hwaddr, const size_t addrlen) {
  int fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    return -1;
  struct ifreq req;
  strncpy(req.ifr_name, ifname, sizeof(req.ifr_name));
  if (ioctl(fd, SIOCGIFFLAGS, (char *)&req) < 0)
    goto err;
  unsigned long flags = req.ifr_flags;
  req.ifr_flags &= ~IFF_UP;
  if (ioctl(fd, SIOCSIFFLAGS, (char *)&req) < 0)
    goto err;
  memcpy(req.ifr_hwaddr.sa_data, hwaddr, addrlen);
  req.ifr_hwaddr.sa_family = PF_UNIX;
  if (ioctl(fd, SIOCSIFHWADDR, (char *)&req) < 0)
    goto err;
  req.ifr_flags = flags;
  if (ioctl(fd, SIOCSIFFLAGS, (char *)&req) < 0)
    goto err;
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<set_hwaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1196 <set_hwaddr+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    129c <set_hwaddr+0x14c>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11cb <set_hwaddr+0x7b>
jmp    128d <set_hwaddr+0x13d>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
xor    $0xffffffffffffffff,%rax
and    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    120d <set_hwaddr+0xbd>
jmp    128d <set_hwaddr+0x13d>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    124d <set_hwaddr+0xfd>
jmp    128d <set_hwaddr+0x13d>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1279 <set_hwaddr+0x129>
jmp    128d <set_hwaddr+0x13d>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    129c <set_hwaddr+0x14c>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_hwaddr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1249 <set_hwaddr+0xf9>
mov    %eax,%ebp
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     123b <set_hwaddr+0xeb>
mov    0x8(%rsp),%rbx
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
not    %rax
and    %rbx,%rax
mov    %rax,0x8(%rsp)
mov    0x2e24(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     123b <set_hwaddr+0xeb>
mov    0x14(%rsp),%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     123b <set_hwaddr+0xeb>
mov    %rbx,0x8(%rsp)
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    1240 <set_hwaddr+0xf0>
mov    $0xffffffff,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    124e <set_hwaddr+0xfe>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i64 0
@SIOCSIFFLAGS = global i32 0
@PF_UNIX = global i32 0
@SIOCSIFHWADDR = global i32 0
define i32 @set_hwaddr(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.ifreq
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %73
17: ; preds = %3
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 2
%19 = load i32, i32* %18
%20 = load i8*, i8** %5
%21 = call i32 @strncpy(i32 %19, i8* %20, i32 4)
%22 = load i32, i32* %8
%23 = load i32, i32* @SIOCGIFFLAGS
%24 = bitcast %struct.ifreq* %9 to i8*
%25 = call i64 @ioctl(i32 %22, i32 %23, i8* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %17
br label %70
28: ; preds = %17
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* %10
%31 = load i64, i64* @IFF_UP
%32 = xor i64 %31, -1
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
%34 = load i64, i64* %33
%35 = and i64 %34, %32
store i64 %35, i64* %33
%36 = load i32, i32* %8
%37 = load i32, i32* @SIOCSIFFLAGS
%38 = bitcast %struct.ifreq* %9 to i8*
%39 = call i64 @ioctl(i32 %36, i32 %37, i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %28
br label %70
42: ; preds = %28
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load i8*, i8** %6
%47 = load i64, i64* %7
%48 = call i32 @memcpy(i32 %45, i8* %46, i64 %47)
%49 = load i32, i32* @PF_UNIX
%50 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
store i32 %49, i32* %51
%52 = load i32, i32* %8
%53 = load i32, i32* @SIOCSIFHWADDR
%54 = bitcast %struct.ifreq* %9 to i8*
%55 = call i64 @ioctl(i32 %52, i32 %53, i8* %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %42
br label %70
58: ; preds = %42
%59 = load i64, i64* %10
%60 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 0
store i64 %59, i64* %60
%61 = load i32, i32* %8
%62 = load i32, i32* @SIOCSIFFLAGS
%63 = bitcast %struct.ifreq* %9 to i8*
%64 = call i64 @ioctl(i32 %61, i32 %62, i8* %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %58
br label %70
67: ; preds = %58
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
store i32 0, i32* %4
br label %73
70: ; preds = %66, %57, %41, %27
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
store i32 -1, i32* %4
br label %73
73: ; preds = %70, %67, %16
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
%struct.ifreq = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32, i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFFLAGS = global i32 0
@IFF_UP = global i64 0
@SIOCSIFFLAGS = global i32 0
@PF_UNIX = global i32 0
@SIOCSIFHWADDR = global i32 0
define i32 @set_hwaddr(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.ifreq
%5 = load i32, i32* @PF_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 %41, label %9
9: ; preds = %3
%10 = bitcast %struct.ifreq* %4 to i8*
%11 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%12 = load i32, i32* @SIOCGIFFLAGS
%13 = call i64 @ioctl(i32 %7, i32 %12, i8* %10)
%14 = icmp slt i64 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %9
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @IFF_UP
%19 = xor i64 %18, -1
%20 = and i64 %17, %19
store i64 %20, i64* %16
%21 = load i32, i32* @SIOCSIFFLAGS
%22 = call i64 @ioctl(i32 %7, i32 %21, i8* %10)
%23 = icmp slt i64 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %15
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1, i32 1
%26 = load i32, i32* %25
%27 = call i32 @memcpy(i32 %26, i8* %1, i64 %2)
%28 = load i32, i32* @PF_UNIX
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @SIOCSIFHWADDR
%31 = call i64 @ioctl(i32 %7, i32 %30, i8* %10)
%32 = icmp slt i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %24
store i64 %17, i64* %16
%34 = load i32, i32* @SIOCSIFFLAGS
%35 = call i64 @ioctl(i32 %7, i32 %34, i8* %10)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33, %24, %15, %9
br label %38
38: ; preds = %33, %37
%39 = phi i32 [ -1, %37 ], [ 0, %33 ]
%40 = call i32 @close(i32 %7)
br label %41
41: ; preds = %3, %38
%42 = phi i32 [ %39, %38 ], [ -1, %3 ]
ret i32 %42
}