#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 passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};
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 O_APPEND;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ asprintf(char **, char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwnam(char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct ifreq ifr;
  struct passwd *p;
  unsigned int i;
  char *newargs[argc + 1];
  int fd;
  if (argc < 4) {
    fprintf(stderr, "Usage: tundev user logfile qemu <qemu options>...\n");
    exit(1);
  }
  fd = open("/dev/net/tun", O_RDWR);
  if (fd < 0) {
    perror("Could not open /dev/net/tun");
    exit(1);
  }
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  strncpy(ifr.ifr_name, "tun%d", IFNAMSIZ);
  if (ioctl(fd, TUNSETIFF, (void *)&ifr) != 0) {
    perror("Could not get tun device");
    exit(1);
  }
  p = getpwnam(argv[1]);
  if (!p) {
    fprintf(stderr, "No user '%s'\n", argv[1]);
    exit(1);
  }
  setgroups(0, NULL);
  setgid(p->pw_gid);
  if (setuid(p->pw_uid) != 0) {
    perror("setting uid");
    exit(1);
  }
  newargs[0] = argv[3];
  newargs[1] = "-tun-fd";
  asprintf(&newargs[2], "%d", fd);
  for (i = 4; i <= argc; i++)
    newargs[i - 1] = argv[i];
  if (strcmp(argv[2], "-") == 0) {
    execvp(newargs[0], newargs);
    exit(1);
  }
  switch (fork()) {
  case 0: {
    close(1);
    close(2);
    open(argv[2], O_WRONLY | O_APPEND);
    open(argv[2], O_WRONLY | O_APPEND);
    close(0);
    execvp(newargs[0], newargs);
    exit(1);
  }
  case -1:
    perror("fork failed");
    exit(1);
  }
  printf("%s\n", ifr.ifr_name);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    127b <main+0x6b>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   10e0 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    12b3 <main+0xa3>
lea    0xd8f(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    0x2cd5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
lea    0xd63(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x44(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     1326 <main+0x116>
lea    0xd3e(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    136b <main+0x15b>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd14(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <setgroups@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   10d0 <setgid@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1120 <setuid@plt>
cmp    $0x0,%rax
je     13ad <main+0x19d>
lea    0xcde(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,(%rdi)
lea    0xcc5(%rip),%rax
mov    %rax,0x8(%rdi)
add    $0x10,%rdi
mov    -0x44(%rbp),%edx
lea    0xc7d(%rip),%rsi
call   10c0 <asprintf@plt>
movl   $0x4,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
ja     1416 <main+0x206>
mov    -0x50(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x2c(%rbp),%edx
mov    (%rcx,%rdx,8),%rdx
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    13e1 <main+0x1d1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xc6b(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    144a <main+0x23a>
mov    -0x50(%rbp),%rsi
mov    (%rsi),%rdi
call   1100 <execvp@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
xor    %eax,%eax
call   1130 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x54(%rbp)
sub    $0xffffffff,%eax
je     14e3 <main+0x2d3>
jmp    1464 <main+0x254>
mov    -0x54(%rbp),%eax
test   %eax,%eax
jne    14f9 <main+0x2e9>
jmp    1474 <main+0x264>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2b19(%rip),%rax
mov    (%rax),%esi
mov    0x2b00(%rip),%rax
or     (%rax),%esi
call   10e0 <open@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2afa(%rip),%rax
mov    (%rax),%esi
mov    0x2ae1(%rip),%rax
or     (%rax),%esi
call   10e0 <open@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    -0x50(%rbp),%rsi
mov    (%rsi),%rdi
call   1100 <execvp@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xba8(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xb9a(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%eax
mov    %rsp,%r15
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    $0x3,%edi
jg     1271 <main+0x51>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    %rsi,%r14
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xdbe(%rip),%rdi
call   10f0 <open@plt>
test   %eax,%eax
jns    1299 <main+0x79>
lea    0xd9f(%rip),%rdi
jmp    1420 <main+0x200>
mov    %eax,%r12d
lea    -0x38(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2d22(%rip),%rax
mov    0x2ceb(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
lea    0xd77(%rip),%rsi
call   1030 <strncpy@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
jne    1329 <main+0x109>
mov    0x8(%r14),%rdi
call   10a0 <getpwnam@plt>
test   %rax,%rax
jne    1335 <main+0x115>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd56(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xd25(%rip),%rdi
jmp    1420 <main+0x200>
mov    %rax,%r13
xor    %edi,%edi
xor    %esi,%esi
call   1080 <setgroups@plt>
mov    0x4(%r13),%edi
call   10e0 <setgid@plt>
mov    0x0(%r13),%edi
call   1130 <setuid@plt>
test   %rax,%rax
jne    1419 <main+0x1f9>
mov    0x18(%r14),%rax
mov    %rax,(%r15)
lea    0xd1e(%rip),%rax
mov    %rax,0x8(%r15)
lea    0x10(%r15),%rdi
lea    0xcd9(%rip),%rsi
mov    %r12d,%edx
call   10d0 <asprintf@plt>
lea    0x18(%r15),%rdi
lea    0x20(%r14),%rsi
add    $0xfffffffd,%ebx
shl    $0x3,%rbx
mov    %rbx,%rdx
call   10c0 <memcpy@plt>
mov    0x10(%r14),%rdi
lea    0xced(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1404 <main+0x1e4>
xor    %eax,%eax
call   1140 <fork@plt>
cmp    $0xffffffff,%eax
je     142f <main+0x20f>
test   %eax,%eax
jne    1438 <main+0x218>
mov    $0x1,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   1070 <close@plt>
mov    0x10(%r14),%rdi
mov    0x2bd4(%rip),%r12
mov    0x2bbd(%rip),%rbx
mov    (%rbx),%esi
or     (%r12),%esi
call   10f0 <open@plt>
mov    0x10(%r14),%rdi
mov    (%rbx),%esi
or     (%r12),%esi
call   10f0 <open@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1110 <execvp@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xc5c(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    0xc5c(%rip),%rdi
jmp    1420 <main+0x200>
mov    -0x30(%rbp),%rsi
lea    0xc5b(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [51 x i8] c"Usage: tundev user logfile qemu <qemu options>...\0A\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [28 x i8] c"Could not open /dev/net/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 = [25 x i8] c"Could not get tun device\00"
@.str.5 = [14 x i8] c"No user '%s'\0A\00"
@.str.6 = [12 x i8] c"setting uid\00"
@.str.7 = [8 x i8] c"-tun-fd\00"
@.str.8 = [3 x i8] c"%d\00"
@.str.9 = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.10 = [12 x i8] c"fork failed\00"
@.str.11 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.ifreq
%7 = alloca %struct.passwd*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8*, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 4
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 16)
%33 = load i32, i32* @IFF_TAP
%34 = load i32, i32* @IFF_NO_PI
%35 = or i32 %33, %34
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%38 = load i8*, i8** %37
%39 = load i32, i32* @IFNAMSIZ
%40 = call i32 @strncpy(i8* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %11
%42 = load i32, i32* @TUNSETIFF
%43 = bitcast %struct.ifreq* %6 to i8*
%44 = call i64 @ioctl(i32 %41, i32 %42, i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %31
%47 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %31
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call %struct.passwd* @getpwnam(i8* %52)
store %struct.passwd* %53, %struct.passwd** %7
%54 = load %struct.passwd*, %struct.passwd** %7
%55 = icmp ne %struct.passwd* %54, null
br i1 %55, label %63, label %56
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %49
%64 = call i32 @setgroups(i32 0, i32* null)
%65 = load %struct.passwd*, %struct.passwd** %7
%66 = getelementptr inbounds %struct.passwd, %struct.passwd* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = call i32 @setgid(i32 %67)
%69 = load %struct.passwd*, %struct.passwd** %7
%70 = getelementptr inbounds %struct.passwd, %struct.passwd* %69, i32 0, i32 0
%71 = load i32, i32* %70
%72 = call i64 @setuid(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %63
%75 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %63
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 3
%80 = load i8*, i8** %79
%81 = getelementptr inbounds i8*, i8** %16, i64 0
store i8* %80, i8** %81
%82 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8** %82
%83 = getelementptr inbounds i8*, i8** %16, i64 2
%84 = load i32, i32* %11
%85 = call i32 @asprintf(i8** %83, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %84)
store i32 4, i32* %8
br label %86
86: ; preds = %100, %77
%87 = load i32, i32* %8
%88 = load i32, i32* %4
%89 = icmp ule i32 %87, %88
br i1 %89, label %90, label %103
90: ; preds = %86
%91 = load i8**, i8*** %5
%92 = load i32, i32* %8
%93 = zext i32 %92 to i64
%94 = getelementptr inbounds i8*, i8** %91, i64 %93
%95 = load i8*, i8** %94
%96 = load i32, i32* %8
%97 = sub i32 %96, 1
%98 = zext i32 %97 to i64
%99 = getelementptr inbounds i8*, i8** %16, i64 %98
store i8* %95, i8** %99
br label %100
100: ; preds = %90
%101 = load i32, i32* %8
%102 = add i32 %101, 1
store i32 %102, i32* %8
br label %86
103: ; preds = %86
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 2
%106 = load i8*, i8** %105
%107 = call i64 @strcmp(i8* %106, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%108 = icmp eq i64 %107, 0
br i1 %108, label %109, label %114
109: ; preds = %103
%110 = getelementptr inbounds i8*, i8** %16, i64 0
%111 = load i8*, i8** %110
%112 = call i32 @execvp(i8* %111, i8** %16)
%113 = call i32 @exit(i32 1)
unreachable
114: ; preds = %103
%115 = call i32 (...) @fork()
switch i32 %115, label %141 [
i32 0, label %116
i32 -1, label %138
]
116: ; preds = %114
%117 = call i32 @close(i32 1)
%118 = call i32 @close(i32 2)
%119 = load i8**, i8*** %5
%120 = getelementptr inbounds i8*, i8** %119, i64 2
%121 = load i8*, i8** %120
%122 = load i32, i32* @O_WRONLY
%123 = load i32, i32* @O_APPEND
%124 = or i32 %122, %123
%125 = call i32 @open(i8* %121, i32 %124)
%126 = load i8**, i8*** %5
%127 = getelementptr inbounds i8*, i8** %126, i64 2
%128 = load i8*, i8** %127
%129 = load i32, i32* @O_WRONLY
%130 = load i32, i32* @O_APPEND
%131 = or i32 %129, %130
%132 = call i32 @open(i8* %128, i32 %131)
%133 = call i32 @close(i32 0)
%134 = getelementptr inbounds i8*, i8** %16, i64 0
%135 = load i8*, i8** %134
%136 = call i32 @execvp(i8* %135, i8** %16)
%137 = call i32 @exit(i32 1)
unreachable
138: ; preds = %114
%139 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
%140 = call i32 @exit(i32 1)
unreachable
141: ; preds = %114
%142 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%143 = load i8*, i8** %142
%144 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %143)
%145 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
%struct.passwd = type { i32, i32 }
@stderr = global i32 0
@.str = [51 x i8] c"Usage: tundev user logfile qemu <qemu options>...\0A\00"
@.str.1 = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.2 = [28 x i8] c"Could not open /dev/net/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 = [25 x i8] c"Could not get tun device\00"
@.str.5 = [14 x i8] c"No user '%s'\0A\00"
@.str.6 = [12 x i8] c"setting uid\00"
@.str.7 = [8 x i8] c"-tun-fd\00"
@.str.8 = [3 x i8] c"%d\00"
@.str.9 = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.10 = [12 x i8] c"fork failed\00"
@.str.11 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = add i32 %0, 1
%6 = zext i32 %5 to i64
%7 = alloca i8*, i64 %6
%8 = icmp slt i32 %0, 4
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 16)
%22 = load i32, i32* @IFF_TAP
%23 = load i32, i32* @IFF_NO_PI
%24 = or i32 %23, %22
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @IFNAMSIZ
%29 = call i32 @strncpy(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @TUNSETIFF
%31 = call i64 @ioctl(i32 %15, i32 %30, i8* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %20
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = call %struct.passwd* @getpwnam(i8* %38)
%40 = icmp eq %struct.passwd* %39, null
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %37
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = call i32 @setgroups(i32 0, i32* null)
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i64 0, i32 1
%49 = load i32, i32* %48
%50 = call i32 @setgid(i32 %49)
%51 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i64 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @setuid(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %46
%56 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %46
%59 = getelementptr inbounds i8*, i8** %1, i64 3
%60 = load i8*, i8** %59
store i8* %60, i8** %7
%61 = getelementptr inbounds i8*, i8** %7, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8** %61
%62 = getelementptr inbounds i8*, i8** %7, i64 2
%63 = call i32 @asprintf(i8** %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %15)
%64 = getelementptr i8*, i8** %7, i64 3
%65 = bitcast i8** %64 to i8*
%66 = getelementptr i8*, i8** %1, i64 4
%67 = bitcast i8** %66 to i8*
%68 = add i32 %0, -3
%69 = zext i32 %68 to i64
%70 = shl nuw nsw i64 %69, 3
%71 = getelementptr inbounds i8*, i8** %1, i64 2
%72 = load i8*, i8** %71
%73 = call i64 @strcmp(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %58
%76 = load i8*, i8** %7
%77 = call i32 @execvp(i8* %76, i8** %7)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %58
%80 = call i32 (...) @fork()
switch i32 %80, label %101 [
i32 0, label %81
i32 -1, label %98
]
81: ; preds = %79
%82 = call i32 @close(i32 1)
%83 = call i32 @close(i32 2)
%84 = load i8*, i8** %71
%85 = load i32, i32* @O_WRONLY
%86 = load i32, i32* @O_APPEND
%87 = or i32 %86, %85
%88 = call i32 @open(i8* %84, i32 %87)
%89 = load i8*, i8** %71
%90 = load i32, i32* @O_WRONLY
%91 = load i32, i32* @O_APPEND
%92 = or i32 %91, %90
%93 = call i32 @open(i8* %89, i32 %92)
%94 = call i32 @close(i32 0)
%95 = load i8*, i8** %7
%96 = call i32 @execvp(i8* %95, i8** %7)
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %79
%99 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0))
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %79
%102 = load i8*, i8** %26
%103 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.11, i64 0, i64 0), i8* %102)
%104 = call i32 @exit(i32 0)
unreachable
}