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

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int IFF_NO_PI;
int IFF_TAP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ TUNSETIFF;
int atol(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getopt(int, char **, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ loop(int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int c;
  int tun_fd = 0;
  struct ifreq ifr;
  while ((c = getopt(argc, argv, "t:")) != -1) {
    switch (c) {
    case 't':
      tun_fd = atol(optarg);
      break;
    default:
      usage(*argv);
      break;
    }
  }
  if (!tun_fd) {
    fprintf(stderr, "%s: Invalid arguments!\n", "ux_tun");
    usage(*argv);
    exit(1);
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ);
  if (ioctl(tun_fd, TUNSETIFF, (unsigned long)&ifr) != 0) {
    perror("ioctl on tun device");
    exit(1);
  }
  fcntl(tun_fd, F_SETFL, O_NONBLOCK);
  loop(tun_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe25(%rip),%rdx
call   10c0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1224 <main+0x74>
mov    -0x14(%rbp),%eax
sub    $0x74,%eax
jne    1213 <main+0x63>
jmp    11fd <main+0x4d>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   10a0 <atol@plt>
mov    %eax,-0x18(%rbp)
jmp    121f <main+0x6f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
jmp    11cd <main+0x1d>
cmpl   $0x0,-0x18(%rbp)
jne    1260 <main+0xb0>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
lea    0xdd6(%rip),%rdx
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    0x2d20(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
lea    0xd8a(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
je     12d2 <main+0x122>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
mov    0x2cfb(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x18(%rbp),%edi
call   1050 <loop@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
lea    0xe35(%rip),%r15
mov    0x2e26(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10c0 <getopt@plt>
cmp    $0xffffffff,%eax
je     120f <main+0x5f>
cmp    $0x74,%eax
je     1201 <main+0x51>
mov    (%rbx),%rdi
call   1090 <usage@plt>
jmp    11e0 <main+0x30>
mov    (%r12),%edi
call   10a0 <atol@plt>
mov    %eax,%r14d
jmp    11e0 <main+0x30>
test   %r14d,%r14d
je     12a5 <main+0xf5>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2d9d(%rip),%rax
mov    0x2d66(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0xc(%rsp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    0xdcc(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
jne    12d3 <main+0x123>
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
call   1040 <fcntl@plt>
mov    %r14d,%edi
call   1050 <loop@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
lea    0xd5f(%rip),%rdx
call   1080 <fprintf@plt>
mov    (%rbx),%rdi
call   1090 <usage@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd4e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [3 x i8] c"t:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s: Invalid arguments!\0A\00"
@.str.2 = [7 x i8] c"ux_tun\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@.str.3 = [6 x i8] c"tun%d\00"
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.4 = [20 x i8] c"ioctl on tun device\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
br label %9
9: ; preds = %23, %2
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @getopt(i32 %10, i8** %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %6
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i32, i32* %6
switch i32 %15, label %19 [
i32 116, label %16
]
16: ; preds = %14
%17 = load i32, i32* @optarg
%18 = call i32 @atol(i32 %17)
store i32 %18, i32* %7
br label %23
19: ; preds = %14
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
br label %23
23: ; preds = %19, %16
br label %9
24: ; preds = %9
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = call i32 @usage(i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = call i32 @memset(%struct.ifreq* %8, i32 0, i32 8)
%36 = load i32, i32* @IFF_TAP
%37 = load i32, i32* @IFF_NO_PI
%38 = or i32 %36, %37
%39 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%41 = load i32, i32* %40
%42 = load i32, i32* @IFNAMSIZ
%43 = call i32 @strncpy(i32 %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %7
%45 = load i32, i32* @TUNSETIFF
%46 = ptrtoint %struct.ifreq* %8 to i64
%47 = call i64 @ioctl(i32 %44, i32 %45, i64 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %34
%50 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %34
%53 = load i32, i32* %7
%54 = load i32, i32* @F_SETFL
%55 = load i32, i32* @O_NONBLOCK
%56 = call i32 @fcntl(i32 %53, i32 %54, i32 %55)
%57 = load i32, i32* %7
%58 = call i32 @loop(i32 %57)
ret i32 0
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [3 x i8] c"t:\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"%s: Invalid arguments!\0A\00"
@.str.2 = [7 x i8] c"ux_tun\00"
@IFF_TAP = global i32 0
@IFF_NO_PI = global i32 0
@.str.3 = [6 x i8] c"tun%d\00"
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.4 = [20 x i8] c"ioctl on tun device\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
br label %5
5: ; preds = %9, %2
%6 = phi i32 [ %11, %9 ], [ 0, %2 ]
br label %7
7: ; preds = %5, %12
%8 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %8, label %12 [
i32 -1, label %15
i32 116, label %9
]
9: ; preds = %7
%10 = load i32, i32* @optarg
%11 = tail call i32 @atol(i32 %10)
br label %5
12: ; preds = %7
%13 = load i8*, i8** %1
%14 = tail call i32 @usage(i8* %13)
br label %7
15: ; preds = %7
%16 = icmp eq i32 %6, 0
br i1 %16, label %17, label %23
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = load i8*, i8** %1
%21 = tail call i32 @usage(i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%25 = load i32, i32* @IFF_TAP
%26 = load i32, i32* @IFF_NO_PI
%27 = or i32 %26, %25
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* @IFNAMSIZ
%32 = call i32 @strncpy(i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @TUNSETIFF
%34 = ptrtoint %struct.ifreq* %3 to i64
%35 = call i64 @ioctl(i32 %6, i32 %33, i64 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %23
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %23
%41 = load i32, i32* @F_SETFL
%42 = load i32, i32* @O_NONBLOCK
%43 = call i32 @fcntl(i32 %6, i32 %41, i32 %42)
%44 = call i32 @loop(i32 %6)
ret i32 0
}