#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 {
  short ifr_flags;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int IFNAMSIZ;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
void tap_set_flags(int fd, short flags, const char *devname) {
  struct ifreq ifr;
  int ret, len;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = flags;
  len = strlen(devname);
  if (len >= IFNAMSIZ)
    error(1, 0, "device name too long");
  strcpy(ifr.ifr_name, devname);
  ret = ioctl(fd, TUNSETIFF, (void *)&ifr);
  if (ret == -1)
    error(1, errno, "ioctl tunsetiff");
}


x86 assembly (clang, -O0):
<tap_set_flags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x6(%rbp),%ax
mov    %ax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
jl     11af <tap_set_flags+0x5f>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe56(%rip),%rdx
call   1070 <error@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f7 <tap_set_flags+0xa7>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe23(%rip),%rdx
call   1070 <error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tap_set_flags>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %bp,0x8(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
jl     119c <tap_set_flags+0x4c>
lea    0xe70(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11dd <tap_set_flags+0x8d>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i16, i32 }
@IFNAMSIZ = global i32 0
@.str = [21 x i8] c"device name too long\00"
@TUNSETIFF = global i32 0
@errno = global i32 0
@.str.1 = [16 x i8] c"ioctl tunsetiff\00"
define void @tap_set_flags(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca %struct.ifreq
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%10 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%11 = load i16, i16* %5
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
store i16 %11, i16* %12
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = load i32, i32* @IFNAMSIZ
%17 = icmp sge i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %3
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i32 %22, i8* %23)
%25 = load i32, i32* %4
%26 = load i32, i32* @TUNSETIFF
%27 = bitcast %struct.ifreq* %7 to i8*
%28 = call i32 @ioctl(i32 %25, i32 %26, i8* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i32, i32* @errno
%33 = call i32 @error(i32 1, i32 %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %20
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { i16, i32 }
@IFNAMSIZ = global i32 0
@.str = [21 x i8] c"device name too long\00"
@TUNSETIFF = global i32 0
@errno = global i32 0
@.str.1 = [16 x i8] c"ioctl tunsetiff\00"
define void @tap_set_flags(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = bitcast %struct.ifreq* %4 to i8*
%6 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 0
store i16 %1, i16* %7
%8 = call i32 @strlen(i8* %2)
%9 = load i32, i32* @IFNAMSIZ
%10 = icmp slt i32 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* %2)
%17 = load i32, i32* @TUNSETIFF
%18 = call i32 @ioctl(i32 %0, i32 %17, i8* %5)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* @errno
%22 = call i32 @error(i32 1, i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %20, %13
ret void
}