#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 */
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int IFF_NO_PI;
int IFF_TUN;
int IFNAMSIZ;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ print_bytes(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int tun_alloc(char *, int);
int old_main(int argc, char **argv) {
  char readbuf[(2048)];
  char tun_name[IFNAMSIZ];
  strcpy(tun_name, "tun0");
  printf("Creating tun...\n");
  int my_tun = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI);
  if (my_tun < 0) {
    printf("Could not create tun0. Permissions not correct?\n");
    _exit(-1);
  }
  printf("Listening on fd %d...\n", my_tun);
  for (;;) {
    ssize_t b = read(my_tun, readbuf, sizeof readbuf);
    print_bytes(readbuf, b);
  }
}


x86 assembly (clang, -O0):
<old_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x818(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x830(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x820(%rbp)
lea    0xe52(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x830(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
call   1060 <tun_alloc@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jge    120a <old_main+0xaa>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>
mov    -0x824(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x824(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1070 <read@plt>
mov    %eax,-0x828(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x828(%rbp),%esi
call   1080 <print_bytes@plt>
jmp    121e <old_main+0xbe>

x86 assembly (clang, -O2):
<old_main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe61(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
lea    0xe6e(%rip),%rdi
call   1050 <puts@plt>
mov    0x2e1e(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <tun_alloc@plt>
test   %eax,%eax
js     120c <old_main+0x9c>
mov    %eax,%r14d
lea    0xe2a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    -0x810(%rbp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1080 <read@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <print_bytes@plt>
jmp    11f0 <old_main+0x80>
lea    0xe19(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@IFNAMSIZ = global i32 0
@.str = [5 x i8] c"tun0\00"
@.str.1 = [17 x i8] c"Creating tun...\0A\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@.str.2 = [49 x i8] c"Could not create tun0. Permissions not correct?\0A\00"
@.str.3 = [23 x i8] c"Listening on fd %d...\0A\00"
define i32 @old_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [2048 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%10 = load i32, i32* @IFNAMSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = call i32 @strcpy(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @IFF_TUN
%17 = load i32, i32* @IFF_NO_PI
%18 = or i32 %16, %17
%19 = call i32 @tun_alloc(i8* %13, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @_exit(i32 -1)
unreachable
25: ; preds = %2
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %28, %25
%29 = load i32, i32* %8
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 2048)
store i32 %31, i32* %9
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%33 = load i32, i32* %9
%34 = call i32 @print_bytes(i8* %32, i32 %33)
br label %28
}

LLVM IR (-O2):
@IFNAMSIZ = global i32 0
@.str = [5 x i8] c"tun0\00"
@IFF_TUN = global i32 0
@IFF_NO_PI = global i32 0
@.str.3 = [23 x i8] c"Listening on fd %d...\0A\00"
@str = [16 x i8] c"Creating tun...\00"
@str.4 = [48 x i8] c"Could not create tun0. Permissions not correct?\00"
define i32 @old_main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @IFNAMSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @IFF_TUN
%11 = load i32, i32* @IFF_NO_PI
%12 = or i32 %11, %10
%13 = call i32 @tun_alloc(i8* %7, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.4, i64 0, i64 0))
%17 = call i32 @_exit(i32 -1)
unreachable
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %20
20: ; preds = %20, %18
%21 = call i32 @read(i32 %13, i8* %4, i32 2048)
%22 = call i32 @print_bytes(i8* %4, i32 %21)
br label %20
}