#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 */

/* Variables and functions */
int IFNAMSIZ;
scalar_t__ atoi(char *);
int bind_and_listen(int);
int /*<<< orphan*/ client_loop(int, int);
int connect_server(char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ server_loop(int, int);
int /*<<< orphan*/ sprintf(unsigned char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ system(unsigned char *);
int tun_open(char *);
int main(int argc, char *argv[]) {
  char name[IFNAMSIZ];
  unsigned char cmd[1024];
  int localfd, remotefd;
  if (argc < 2) {
    fprintf(stderr, "usage: ./step3 <0|1>\n");
    return 1;
  }
  memset(name, 0, IFNAMSIZ);
  localfd = tun_open(name);
  if (localfd == -1)
    return 1;
  fprintf(stdout, "%s opened\n", name);
  if (atoi(argv[1])) {
    sprintf(cmd, "ifconfig %s 10.0.1.1 up", name);
    system(cmd);
    sprintf(cmd, "route add 10.0.1.2 dev %s", name);
    system(cmd);
    remotefd = connect_server("127.0.0.1", 6687);
    if (remotefd == -1)
      return 1;
    client_loop(remotefd, localfd);
  } else {
    sprintf(cmd, "ifconfig %s 10.0.2.1 up", name);
    system(cmd);
    remotefd = bind_and_listen(6687);
    if (remotefd == -1)
      return 1;
    server_loop(remotefd, localfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x438(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    121f <main+0x7f>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    13b7 <main+0x217>
mov    -0x438(%rbp),%rdi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x438(%rbp),%rdi
call   1090 <tun_open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    126b <main+0xcb>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    13b7 <main+0x217>
mov    -0x438(%rbp),%rdx
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
cmp    $0x0,%rax
je     133c <main+0x19c>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd6c(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd5e(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
lea    0xd60(%rip),%rdi
mov    $0x1a1f,%esi
call   1040 <connect_server@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    1326 <main+0x186>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    13b7 <main+0x217>
mov    -0x428(%rbp),%edi
mov    -0x424(%rbp),%esi
call   1030 <client_loop@plt>
jmp    13a6 <main+0x206>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd0c(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    $0x1a1f,%edi
call   10b0 <bind_and_listen@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    1395 <main+0x1f5>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    13b7 <main+0x217>
mov    -0x428(%rbp),%edi
mov    -0x424(%rbp),%esi
call   1080 <server_loop@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x1,%edi
jg     11f3 <main+0x53>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%r15d
jmp    12dd <main+0x13d>
mov    %rsi,%r12
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1090 <tun_open@plt>
mov    $0x1,%r15d
cmp    $0xffffffff,%eax
je     12dd <main+0x13d>
mov    %eax,%r14d
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x8(%r12),%rdi
call   10a0 <atoi@plt>
test   %rax,%rax
je     12a0 <main+0x100>
lea    0xdd7(%rip),%rsi
lea    -0x420(%rbp),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
call   1050 <system@plt>
lea    0xdce(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
call   1050 <system@plt>
lea    0xdce(%rip),%rdi
mov    $0x1a1f,%esi
call   1040 <connect_server@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x13d>
mov    %eax,%edi
mov    %r14d,%esi
call   1030 <client_loop@plt>
jmp    12da <main+0x13a>
lea    0xdb6(%rip),%rsi
lea    -0x420(%rbp),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
call   1050 <system@plt>
mov    $0x1a1f,%edi
call   10b0 <bind_and_listen@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x13d>
mov    %eax,%edi
mov    %r14d,%esi
call   1080 <server_loop@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@IFNAMSIZ = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: ./step3 <0|1>\0A\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [24 x i8] c"ifconfig %s 10.0.1.1 up\00"
@.str.3 = [26 x i8] c"route add 10.0.1.2 dev %s\00"
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [24 x i8] c"ifconfig %s 10.0.2.1 up\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @IFNAMSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %11
br label %67
21: ; preds = %2
%22 = load i32, i32* @IFNAMSIZ
%23 = call i32 @memset(i8* %15, i32 0, i32 %22)
%24 = call i32 @tun_open(i8* %15)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 1, i32* %3
store i32 1, i32* %11
br label %67
28: ; preds = %21
%29 = load i32, i32* @stdout
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i64 @atoi(i8* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %28
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%38 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%40 = call i32 @system(i8* %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%42 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i32 @system(i8* %43)
%45 = call i32 @connect_server(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 6687)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %36
store i32 1, i32* %3
store i32 1, i32* %11
br label %67
49: ; preds = %36
%50 = load i32, i32* %10
%51 = load i32, i32* %9
%52 = call i32 @client_loop(i32 %50, i32 %51)
br label %66
53: ; preds = %28
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = call i32 @sprintf(i8* %54, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%57 = call i32 @system(i8* %56)
%58 = call i32 @bind_and_listen(i32 6687)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %53
store i32 1, i32* %3
store i32 1, i32* %11
br label %67
62: ; preds = %53
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = call i32 @server_loop(i32 %63, i32 %64)
br label %66
66: ; preds = %62, %49
store i32 0, i32* %3
store i32 1, i32* %11
br label %67
67: ; preds = %66, %61, %48, %27, %18
%68 = load i8*, i8** %6
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@IFNAMSIZ = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: ./step3 <0|1>\0A\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [24 x i8] c"ifconfig %s 10.0.1.1 up\00"
@.str.3 = [26 x i8] c"route add 10.0.1.2 dev %s\00"
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [24 x i8] c"ifconfig %s 10.0.2.1 up\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = load i32, i32* @IFNAMSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %39
12: ; preds = %2
%13 = call i32 @memset(i8* %6, i32 0, i32 %4)
%14 = call i32 @tun_open(i8* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %39, label %16
16: ; preds = %12
%17 = load i32, i32* @stdout
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @atoi(i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %16
%24 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%25 = call i32 @system(i8* %7)
%26 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%27 = call i32 @system(i8* %7)
%28 = call i32 @connect_server(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 6687)
%29 = icmp eq i32 %28, -1
br i1 %29, label %39, label %30
30: ; preds = %23
%31 = call i32 @client_loop(i32 %28, i32 %14)
br label %39
32: ; preds = %16
%33 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%34 = call i32 @system(i8* %7)
%35 = call i32 @bind_and_listen(i32 6687)
%36 = icmp eq i32 %35, -1
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = call i32 @server_loop(i32 %35, i32 %14)
br label %39
39: ; preds = %30, %37, %32, %23, %12, %9
%40 = phi i32 [ 1, %9 ], [ 1, %12 ], [ 1, %23 ], [ 1, %32 ], [ 0, %37 ], [ 0, %30 ]
ret i32 %40
}