#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 */

/* Type definitions */
struct ifreq {
  int ifr_flags;
  char *ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ TUNSETIFF;
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int tap_set_name(int fd, char *dev) {
  struct ifreq ifr;
  int err;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0)
    return err;
  strncpy(dev, ifr.ifr_name, IFNAMSIZ);
  return 0;
}


x86 assembly (clang, -O0):
<tap_set_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    0x2e61(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11a6 <tap_set_name+0x76>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <tap_set_name+0x93>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_set_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e83(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x8(%rsp),%rdi
mov    0x2e61(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11ad <tap_set_name+0x7d>
mov    0x8(%rsp),%rsi
mov    (%r12),%edx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
jmp    11b0 <tap_set_name+0x80>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tap_set_name(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%9 = load i32, i32* @IFF_TAP
%10 = load i32, i32* @IFF_NO_PI
%11 = or i32 %9, %10
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%14 = load i8*, i8** %13
%15 = load i8*, i8** %5
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i8* %14, i8* %15, i32 %16)
%18 = load i32, i32* %4
%19 = load i32, i32* @TUNSETIFF
%20 = bitcast %struct.ifreq* %6 to i8*
%21 = call i32 @ioctl(i32 %18, i32 %19, i8* %20)
store i32 %21, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %31
25: ; preds = %2
%26 = load i8*, i8** %5
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @IFNAMSIZ
%30 = call i32 @strncpy(i8* %26, i8* %28, i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %25, %23
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
define i32 @tap_set_name(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%6 = load i32, i32* @IFF_TAP
%7 = load i32, i32* @IFF_NO_PI
%8 = or i32 %7, %6
%9 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i8* %11, i8* %1, i32 %12)
%14 = load i32, i32* @TUNSETIFF
%15 = call i32 @ioctl(i32 %0, i32 %14, i8* %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i8*, i8** %10
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i8* %1, i8* %18, i32 %19)
br label %21
21: ; preds = %2, %17
%22 = phi i32 [ 0, %17 ], [ %15, %2 ]
ret i32 %22
}