#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: ./step5 <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])) {
    if (argc < 3) {
      fprintf(stderr, "usage: ./step5 1 ip\n");
      return 1;
    }
    sprintf(cmd, "ifconfig %s 10.0.1.2 up", name);
    system(cmd);
    sprintf(cmd, "route add 10.0.1.1 dev %s", name);
    system(cmd);
    sprintf(cmd, "route add 8.8.8.8 dev %s", name);
    system(cmd);
    remotefd = connect_server(argv[2], 6687);
    if (remotefd == -1)
      return 1;
    client_loop(remotefd, localfd);
  } else {
    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 = 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    143b <main+0x29b>
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    143b <main+0x29b>
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     139a <main+0x1fa>
cmpl   $0x3,-0x8(%rbp)
jge    12d7 <main+0x137>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    143b <main+0x29b>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd4a(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xd30(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x1a1f,%esi
call   1040 <connect_server@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    1384 <main+0x1e4>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    143b <main+0x29b>
mov    -0x428(%rbp),%edi
mov    -0x424(%rbp),%esi
call   1030 <client_loop@plt>
jmp    142a <main+0x28a>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xcd2(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
call   1050 <system@plt>
mov    -0x438(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    0xcc4(%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    1419 <main+0x279>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x42c(%rbp)
jmp    143b <main+0x29b>
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   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e1d(%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     11f5 <main+0x55>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%r15d
jmp    133a <main+0x19a>
mov    %rsi,%r12
mov    %edi,%r13d
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     133a <main+0x19a>
mov    %eax,%r14d
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x8(%r12),%rdi
call   10a0 <atoi@plt>
test   %rax,%rax
je     126a <main+0xca>
cmp    $0x2,%r13d
ja     12c4 <main+0x124>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    133a <main+0x19a>
lea    0xe10(%rip),%rsi
lea    -0x430(%rbp),%r12
mov    %r12,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r12,%rdi
call   1050 <system@plt>
lea    0xe07(%rip),%rsi
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     133a <main+0x19a>
mov    %eax,%edi
mov    %r14d,%esi
call   1080 <server_loop@plt>
jmp    1337 <main+0x197>
lea    0xd6b(%rip),%rsi
lea    -0x430(%rbp),%r13
mov    %r13,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r13,%rdi
call   1050 <system@plt>
lea    0xd62(%rip),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r13,%rdi
call   1050 <system@plt>
lea    0xd62(%rip),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %r13,%rdi
call   1050 <system@plt>
mov    0x10(%r12),%rdi
mov    $0x1a1f,%esi
call   1040 <connect_server@plt>
cmp    $0xffffffff,%eax
je     133a <main+0x19a>
mov    %eax,%edi
mov    %r14d,%esi
call   1030 <client_loop@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@IFNAMSIZ = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: ./step5 <0|1>\0A\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [21 x i8] c"usage: ./step5 1 ip\0A\00"
@.str.3 = [24 x i8] c"ifconfig %s 10.0.1.2 up\00"
@.str.4 = [26 x i8] c"route add 10.0.1.1 dev %s\00"
@.str.5 = [25 x i8] c"route add 8.8.8.8 dev %s\00"
@.str.6 = [24 x i8] c"ifconfig %s 10.0.1.1 up\00"
@.str.7 = [26 x i8] c"route add 10.0.1.2 dev %s\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 %84
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 %84
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 %66
36: ; preds = %28
%37 = load i32, i32* %4
%38 = icmp slt i32 %37, 3
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %11
br label %84
42: ; preds = %36
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = call i32 @system(i8* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%48 = call i32 @sprintf(i8* %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %15)
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%50 = call i32 @system(i8* %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %15)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%54 = call i32 @system(i8* %53)
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @connect_server(i8* %57, 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 = %42
store i32 1, i32* %3
store i32 1, i32* %11
br label %84
62: ; preds = %42
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = call i32 @client_loop(i32 %63, i32 %64)
br label %83
66: ; preds = %28
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%68 = call i32 @sprintf(i8* %67, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %15)
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%70 = call i32 @system(i8* %69)
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%72 = call i32 @sprintf(i8* %71, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %15)
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%74 = call i32 @system(i8* %73)
%75 = call i32 @bind_and_listen(i32 6687)
store i32 %75, i32* %10
%76 = load i32, i32* %10
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %66
store i32 1, i32* %3
store i32 1, i32* %11
br label %84
79: ; preds = %66
%80 = load i32, i32* %10
%81 = load i32, i32* %9
%82 = call i32 @server_loop(i32 %80, i32 %81)
br label %83
83: ; preds = %79, %62
store i32 0, i32* %3
store i32 1, i32* %11
br label %84
84: ; preds = %83, %78, %61, %39, %27, %18
%85 = load i8*, i8** %6
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@IFNAMSIZ = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"usage: ./step5 <0|1>\0A\00"
@stdout = global i32 0
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [21 x i8] c"usage: ./step5 1 ip\0A\00"
@.str.3 = [24 x i8] c"ifconfig %s 10.0.1.2 up\00"
@.str.4 = [26 x i8] c"route add 10.0.1.1 dev %s\00"
@.str.5 = [25 x i8] c"route add 8.8.8.8 dev %s\00"
@.str.6 = [24 x i8] c"ifconfig %s 10.0.1.1 up\00"
@.str.7 = [26 x i8] c"route add 10.0.1.2 dev %s\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 %50
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 %50, 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 %41, label %23
23: ; preds = %16
%24 = icmp ult i32 %0, 3
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %50
28: ; preds = %23
%29 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%30 = call i32 @system(i8* %7)
%31 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%32 = call i32 @system(i8* %7)
%33 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%34 = call i32 @system(i8* %7)
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @connect_server(i8* %36, i32 6687)
%38 = icmp eq i32 %37, -1
br i1 %38, label %50, label %39
39: ; preds = %28
%40 = call i32 @client_loop(i32 %37, i32 %14)
br label %50
41: ; preds = %16
%42 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%43 = call i32 @system(i8* %7)
%44 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%45 = call i32 @system(i8* %7)
%46 = call i32 @bind_and_listen(i32 6687)
%47 = icmp eq i32 %46, -1
br i1 %47, label %50, label %48
48: ; preds = %41
%49 = call i32 @server_loop(i32 %46, i32 %14)
br label %50
50: ; preds = %39, %48, %41, %28, %12, %25, %9
%51 = phi i32 [ 1, %9 ], [ 1, %25 ], [ 1, %12 ], [ 1, %28 ], [ 1, %41 ], [ 0, %48 ], [ 0, %39 ]
ret i32 %51
}