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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int connect_addr(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pushdata(int, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void dotunneling(int sock, int outport) {
  int localsock;
  int maxfd;
  fd_set read_fds;
  if ((localsock = connect_addr("localhost", outport)) == -1) {
    fprintf(stderr, "Unable to connect to thingy... %s\n", strerror(errno));
    return;
  }
  FD_ZERO(&read_fds);
  FD_SET(sock, &read_fds);
  FD_SET(localsock, &read_fds);
  maxfd = sock > localsock ? sock : localsock;
  for (;;) {
    fd_set tmpread_fds = read_fds;
    if (select(maxfd + 1, &tmpread_fds, NULL, NULL, NULL) == -1) {
      perror("select");
      exit(1);
    }
    if (FD_ISSET(sock, &tmpread_fds)) {
      printf("Sending data to client\n");
      pushdata(sock, localsock);
    }
    if (FD_ISSET(localsock, &tmpread_fds)) {
      printf("Sending data to server\n");
      pushdata(localsock, sock);
    }
  }
}


x86 assembly (clang, -O0):
<dotunneling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe38(%rip),%rdi
call   10a0 <connect_addr@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    120b <dotunneling+0x5b>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe0a(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret
lea    -0x14(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1243 <dotunneling+0x93>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1249 <dotunneling+0x99>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0xffffffff,%eax
jne    128e <dotunneling+0xde>
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12bb <dotunneling+0x10b>
lea    0xd89(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <pushdata@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12e8 <dotunneling+0x138>
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <pushdata@plt>
jmp    124f <dotunneling+0x9f>

x86 assembly (clang, -O2):
<dotunneling>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xe3c(%rip),%rdi
call   10a0 <connect_addr@plt>
cmp    $0xffffffff,%eax
jne    1204 <dotunneling+0x54>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe1e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10d0 <FD_SET@plt>
cmp    %ebx,%ebp
mov    %ebp,%r14d
cmovl  %ebx,%r14d
add    $0x1,%r14d
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
cmp    $0xffffffff,%eax
jne    1268 <dotunneling+0xb8>
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rsp,%r15
lea    0xdc2(%rip),%r12
lea    0xdd2(%rip),%r13
jmp    129e <dotunneling+0xee>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     1252 <dotunneling+0xa2>
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12be <dotunneling+0x10e>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <pushdata@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1280 <dotunneling+0xd0>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <pushdata@plt>
jmp    1280 <dotunneling+0xd0>

LLVM IR (-O0):
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Unable to connect to thingy... %s\0A\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"select\00"
@.str.3 = [24 x i8] c"Sending data to client\0A\00"
@.str.4 = [24 x i8] c"Sending data to server\0A\00"
define void @dotunneling(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = call i32 @connect_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %15)
ret void
17: ; preds = %2
%18 = call i32 @FD_ZERO(i32* %7)
%19 = load i32, i32* %3
%20 = call i32 @FD_SET(i32 %19, i32* %7)
%21 = load i32, i32* %5
%22 = call i32 @FD_SET(i32 %21, i32* %7)
%23 = load i32, i32* %3
%24 = load i32, i32* %5
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = load i32, i32* %3
br label %30
28: ; preds = %17
%29 = load i32, i32* %5
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
store i32 %31, i32* %6
br label %32
32: ; preds = %59, %30
%33 = load i32, i32* %7
store i32 %33, i32* %8
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
%36 = call i32 @select(i32 %35, i32* %8, i32* null, i32* null, i32* null)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i32, i32* %3
%43 = call i64 @FD_ISSET(i32 %42, i32* %8)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %3
%48 = load i32, i32* %5
%49 = call i32 @pushdata(i32 %47, i32 %48)
br label %50
50: ; preds = %45, %41
%51 = load i32, i32* %5
%52 = call i64 @FD_ISSET(i32 %51, i32* %8)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* %5
%57 = load i32, i32* %3
%58 = call i32 @pushdata(i32 %56, i32 %57)
br label %59
59: ; preds = %54, %50
br label %32
}

LLVM IR (-O2):
@.str = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Unable to connect to thingy... %s\0A\00"
@errno = global i32 0
@.str.2 = [7 x i8] c"select\00"
@str = [23 x i8] c"Sending data to server\00"
@str.5 = [23 x i8] c"Sending data to client\00"
define void @dotunneling(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @connect_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %11)
ret void
13: ; preds = %2
%14 = call i32 @FD_ZERO(i32* %3)
%15 = call i32 @FD_SET(i32 %0, i32* %3)
%16 = call i32 @FD_SET(i32 %6, i32* %3)
%17 = icmp slt i32 %6, %0
%18 = select i1 %17, i32 %0, i32 %6
%19 = bitcast i32* %4 to i8*
%20 = add nsw i32 %18, 1
%21 = load i32, i32* %3
store i32 %21, i32* %4
%22 = call i32 @select(i32 %20, i32* %4, i32* null, i32* null, i32* null)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %39, %13
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13, %39
%28 = call i64 @FD_ISSET(i32 %0, i32* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
%32 = call i32 @pushdata(i32 %0, i32 %6)
br label %33
33: ; preds = %30, %27
%34 = call i64 @FD_ISSET(i32 %6, i32* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%38 = call i32 @pushdata(i32 %6, i32 %0)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %3
store i32 %40, i32* %4
%41 = call i32 @select(i32 %20, i32* %4, i32* null, i32* null, i32* null)
%42 = icmp eq i32 %41, -1
br i1 %42, label %24, label %27
}