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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SIOCSIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int tap_fix_macaddr(char *dev_name, unsigned char *dev_mac) {
  int rc;
  int s;
  struct ifreq ifr;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
  rc = ioctl(s, SIOCGIFHWADDR, &ifr);
  if (rc != 0) {
    perror("ioctl(SIOCGIFHWADDR)");
    goto out;
  }
  ifr.ifr_hwaddr.sa_data[0] = 0xFE;
  ifr.ifr_hwaddr.sa_data[1] = dev_mac[1];
  ifr.ifr_hwaddr.sa_data[2] = dev_mac[2];
  ifr.ifr_hwaddr.sa_data[3] = dev_mac[3];
  ifr.ifr_hwaddr.sa_data[4] = dev_mac[4];
  ifr.ifr_hwaddr.sa_data[5] = dev_mac[5];
  rc = ioctl(s, SIOCSIFHWADDR, &ifr);
  if (rc != 0)
    perror("ioctl(SIOCSIFHWADDR)");
out:
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<tap_fix_macaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c4 <tap_fix_macaddr+0x74>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
jmp    124c <tap_fix_macaddr+0xfc>
mov    -0x28(%rbp),%rax
movl   $0xfe,(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x4(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x5(%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    -0x18(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1247 <tap_fix_macaddr+0xf7>
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
jmp    124c <tap_fix_macaddr+0xfc>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_fix_macaddr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%r14d
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    11fb <tap_fix_macaddr+0xab>
mov    (%rsp),%rax
movl   $0xfe,(%rax)
movzbl 0x1(%rbx),%ecx
mov    %ecx,0x4(%rax)
movzbl 0x2(%rbx),%ecx
mov    %ecx,0x8(%rax)
movzbl 0x3(%rbx),%ecx
mov    %ecx,0xc(%rax)
movzbl 0x4(%rbx),%ecx
mov    %ecx,0x10(%rax)
movzbl 0x5(%rbx),%ecx
mov    %ecx,0x14(%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
jne    1204 <tap_fix_macaddr+0xb4>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
jmp    120b <tap_fix_macaddr+0xbb>
lea    0xe0a(%rip),%rdi
call   1060 <perror@plt>
jmp    11e8 <tap_fix_macaddr+0x98>

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
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [21 x i8] c"ioctl(SIOCGIFHWADDR)\00"
@SIOCSIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"ioctl(SIOCSIFHWADDR)\00"
define i32 @tap_fix_macaddr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
store i8* %0, i8** %3
store i8* %1, i8** %4
%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* %6
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %3
%14 = load i32, i32* @IFNAMSIZ
%15 = call i32 @strncpy(i32 %12, i8* %13, i32 %14)
%16 = load i32, i32* %6
%17 = load i32, i32* @SIOCGIFHWADDR
%18 = call i32 @ioctl(i32 %16, i32 %17, %struct.ifreq* %7)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %76
23: ; preds = %2
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i32*, i32** %25
%27 = getelementptr inbounds i32, i32* %26, i64 0
store i32 254, i32* %27
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
%34 = load i32*, i32** %33
%35 = getelementptr inbounds i32, i32* %34, i64 1
store i32 %31, i32* %35
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 2
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 2
store i32 %39, i32* %43
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i64 3
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
%50 = load i32*, i32** %49
%51 = getelementptr inbounds i32, i32* %50, i64 3
store i32 %47, i32* %51
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 4
%54 = load i8, i8* %53
%55 = zext i8 %54 to i32
%56 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32*, i32** %57
%59 = getelementptr inbounds i32, i32* %58, i64 4
store i32 %55, i32* %59
%60 = load i8*, i8** %4
%61 = getelementptr inbounds i8, i8* %60, i64 5
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%65 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %64, i32 0, i32 0
%66 = load i32*, i32** %65
%67 = getelementptr inbounds i32, i32* %66, i64 5
store i32 %63, i32* %67
%68 = load i32, i32* %6
%69 = load i32, i32* @SIOCSIFHWADDR
%70 = call i32 @ioctl(i32 %68, i32 %69, %struct.ifreq* %7)
store i32 %70, i32* %5
%71 = load i32, i32* %5
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %23
%74 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %75
75: ; preds = %73, %23
br label %76
76: ; preds = %75, %21
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
ret i32 0
}

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
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
@.str = [21 x i8] c"ioctl(SIOCGIFHWADDR)\00"
@SIOCSIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"ioctl(SIOCSIFHWADDR)\00"
define i32 @tap_fix_macaddr(i8* %0, i8* nocapture %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 = load i32, i32* @IFNAMSIZ
%9 = tail call i32 @strncpy(i32 undef, i8* %0, i32 %8)
%10 = load i32, i32* @SIOCGIFHWADDR
%11 = call i32 @ioctl(i32 %7, i32 %10, %struct.ifreq* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %2
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
%15 = load i32*, i32** %14
store i32 254, i32* %15
%16 = getelementptr inbounds i8, i8* %1, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = getelementptr inbounds i32, i32* %15, i64 1
store i32 %18, i32* %19
%20 = getelementptr inbounds i8, i8* %1, i64 2
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = getelementptr inbounds i32, i32* %15, i64 2
store i32 %22, i32* %23
%24 = getelementptr inbounds i8, i8* %1, i64 3
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = getelementptr inbounds i32, i32* %15, i64 3
store i32 %26, i32* %27
%28 = getelementptr inbounds i8, i8* %1, i64 4
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = getelementptr inbounds i32, i32* %15, i64 4
store i32 %30, i32* %31
%32 = getelementptr inbounds i8, i8* %1, i64 5
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = getelementptr inbounds i32, i32* %15, i64 5
store i32 %34, i32* %35
%36 = load i32, i32* @SIOCSIFHWADDR
%37 = call i32 @ioctl(i32 %7, i32 %36, %struct.ifreq* %3)
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %13, %2
%40 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %13 ]
%41 = call i32 @perror(i8* %40)
br label %42
42: ; preds = %39, %13
%43 = call i32 @close(i32 %7)
ret i32 0
}