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

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SOCK_DGRAM;
int ioctl(int, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int netdriver_setmacaddr(int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int up_setmacaddr(void) {
  int sockfd;
  int ret = -1;
  sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sockfd >= 0) {
    struct ifreq req;
    memset(&req, 0, sizeof(struct ifreq));
    strncpy(req.ifr_name, "tap0", IFNAMSIZ);
    ret = ioctl(sockfd, SIOCGIFHWADDR, (unsigned long)&req);
    if (!ret) {
      ret = netdriver_setmacaddr(&req.ifr_hwaddr.sa_data);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<up_setmacaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     11e0 <up_setmacaddr+0x90>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0xe58(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11db <up_setmacaddr+0x8b>
lea    -0x10(%rbp),%rdi
call   1030 <netdriver_setmacaddr@plt>
mov    %eax,-0x8(%rbp)
jmp    11e0 <up_setmacaddr+0x90>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<up_setmacaddr>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11bf <up_setmacaddr+0x6f>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
lea    0xe67(%rip),%rsi
call   1040 <strncpy@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
jne    11c4 <up_setmacaddr+0x74>
mov    %rsp,%rdi
call   1030 <netdriver_setmacaddr@plt>
jmp    11c4 <up_setmacaddr+0x74>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"tap0\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @up_setmacaddr() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.ifreq
store i32 -1, i32* %2
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %0
%10 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strncpy(i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %1
%16 = load i32, i32* @SIOCGIFHWADDR
%17 = ptrtoint %struct.ifreq* %3 to i64
%18 = call i32 @ioctl(i32 %15, i32 %16, i64 %17)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %9
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = call i32 @netdriver_setmacaddr(i32* %23)
store i32 %24, i32* %2
br label %25
25: ; preds = %21, %9
br label %26
26: ; preds = %25, %0
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"tap0\00"
@IFNAMSIZ = global i32 0
@SIOCGIFHWADDR = global i32 0
define i32 @up_setmacaddr() {
%1 = alloca %struct.ifreq
%2 = load i32, i32* @PF_INET
%3 = load i32, i32* @SOCK_DGRAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %22
6: ; preds = %0
%7 = bitcast %struct.ifreq* %1 to i8*
%8 = call i32 @memset(%struct.ifreq* %1, i32 0, i32 8)
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 1
%10 = load i32, i32* %9
%11 = load i32, i32* @IFNAMSIZ
%12 = call i32 @strncpy(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @SIOCGIFHWADDR
%14 = ptrtoint %struct.ifreq* %1 to i64
%15 = call i32 @ioctl(i32 %4, i32 %13, i64 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %6
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0, i32 0
%19 = call i32 @netdriver_setmacaddr(i32* %18)
br label %20
20: ; preds = %17, %6
%21 = phi i32 [ %15, %6 ], [ %19, %17 ]
br label %22
22: ; preds = %20, %0
%23 = phi i32 [ %21, %20 ], [ -1, %0 ]
ret i32 %23
}