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

/* Variables and functions */
int /*<<< orphan*/ IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(struct ifreq *, int, int, int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int main(int argc, const char **argv) {
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, "tunnel0", IFNAMSIZ);
  ifr.ifr_flags = IFF_TAP;
  FILE *f = fopen("test", "wb");
  fwrite(&ifr, sizeof(ifr), 1, f);
  fclose(f);
  int fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  printf("%ld\n", TUNSETIFF);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
    fprintf(stderr, "Unable to configure tuntap device: ");
    perror("ioctl");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xe27(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe17(%rip),%rdi
lea    0xe15(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    0xddf(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125b <main+0xbb>
lea    0xdd3(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x123>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    12bc <main+0x11c>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xda0(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x123>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
lea    0xe2d(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe1c(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1269 <main+0xc9>
mov    %eax,%ebx
mov    0x2d86(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xdde(%rip),%rdi
call   1050 <printf@plt>
mov    0x0(%rbp),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
test   %rax,%rax
js     1272 <main+0xd2>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdad(%rip),%rdi
jmp    128e <main+0xee>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xdbd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    125d <main+0xbd>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [8 x i8] c"tunnel0\00"
@IFNAMSIZ = global i32 0
@IFF_TAP = global i32 0
@.str.1 = [5 x i8] c"test\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
@TUNSETIFF = global i32 0
@stderr = global i32 0
@.str.6 = [36 x i8] c"Unable to configure tuntap device: \00"
@.str.7 = [6 x i8] c"ioctl\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.ifreq
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%11 = load i32, i32* %10
%12 = load i32, i32* @IFNAMSIZ
%13 = call i32 @strncpy(i32 %11, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @IFF_TAP
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %14, i32* %15
%16 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = call i32 @fwrite(%struct.ifreq* %6, i32 8, i32 1, i32* %17)
%19 = load i32*, i32** %7
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
27: ; preds = %2
%28 = load i32, i32* @TUNSETIFF
%29 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %8
%31 = load i32, i32* @TUNSETIFF
%32 = bitcast %struct.ifreq* %6 to i8*
%33 = call i64 @ioctl(i32 %30, i32 %31, i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
39: ; preds = %27
store i32 0, i32* %3
br label %40
40: ; preds = %39, %35, %25
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [8 x i8] c"tunnel0\00"
@IFNAMSIZ = global i32 0
@IFF_TAP = global i32 0
@.str.1 = [5 x i8] c"test\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [5 x i8] c"%ld\0A\00"
@TUNSETIFF = global i32 0
@stderr = global i32 0
@.str.6 = [36 x i8] c"Unable to configure tuntap device: \00"
@.str.7 = [6 x i8] c"ioctl\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%7 = load i32, i32* %6
%8 = load i32, i32* @IFNAMSIZ
%9 = call i32 @strncpy(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @IFF_TAP
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @fwrite(%struct.ifreq* %3, i32 8, i32 1, i32* %12)
%14 = call i32 @fclose(i32* %12)
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %2
%19 = load i32, i32* @TUNSETIFF
%20 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @TUNSETIFF
%22 = call i64 @ioctl(i32 %16, i32 %21, i8* %4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %2, %24
%28 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), %24 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %2 ]
%29 = call i32 @perror(i8* %28)
br label %30
30: ; preds = %27, %18
%31 = phi i32 [ 0, %18 ], [ -1, %27 ]
ret i32 %31
}