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

/* Variables and functions */
int /*<<< orphan*/ ARPHRD_ETHER;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCSIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fatal(char *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
void set_macaddr(const char *dev, uint8_t *mac) {
  struct ifreq ifq;
  int fd;
  fd = socket(PF_INET, SOCK_DGRAM, 0);
  if (fd == -1)
    fatal("Could not get socket to set MAC address");
  strcpy(ifq.ifr_name, dev);
  memcpy(&ifq.ifr_hwaddr.sa_data[0], mac, 6);
  ifq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
  if (ioctl(fd, SIOCSIFHWADDR, &ifq) == -1)
    fatal("SIOCSIFHWADDR failed");
  close(fd);
}


x86 assembly (clang, -O0):
<set_macaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a2 <set_macaddr+0x42>
lea    0xe63(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f6 <set_macaddr+0x96>
lea    0xe37(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_macaddr>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    119b <set_macaddr+0x3b>
lea    0xe6a(%rip),%rdi
call   1070 <fatal@plt>
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rsi
mov    $0x6,%edx
call   1060 <memcpy@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11e3 <set_macaddr+0x83>
lea    0xe4a(%rip),%rdi
call   1070 <fatal@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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
@.str = [40 x i8] c"Could not get socket to set MAC address\00"
@ARPHRD_ETHER = global i32 0
@SIOCSIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"SIOCSIFHWADDR failed\00"
define void @set_macaddr(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.ifreq
%6 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i32 %16, i8* %17)
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 1
%21 = load i32*, i32** %20
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32*, i32** %4
%24 = call i32 @memcpy(i32* %22, i32* %23, i32 6)
%25 = load i32, i32* @ARPHRD_ETHER
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %6
%29 = load i32, i32* @SIOCSIFHWADDR
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.ifreq* %5)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %14
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
ret void
}

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
@.str = [40 x i8] c"Could not get socket to set MAC address\00"
@ARPHRD_ETHER = global i32 0
@SIOCSIFHWADDR = global i32 0
@.str.1 = [21 x i8] c"SIOCSIFHWADDR failed\00"
define void @set_macaddr(i8* %0, i32* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%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 eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @fatal(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i32 @strcpy(i32 undef, i8* %0)
%13 = tail call i32 @memcpy(i32* undef, i32* %1, i32 6)
%14 = load i32, i32* @ARPHRD_ETHER
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @SIOCSIFHWADDR
%17 = call i32 @ioctl(i32 %7, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = call i32 @close(i32 %7)
ret void
}