#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int dummy;
};
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 accept(int, struct sockaddr *, size_t *);
int bind_addr(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ get_port_foo();
int /*<<< orphan*/ gotdata(int, int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void dotunneling(int clientsock) {
  int serversock;
  struct hostent *he;
  struct sockaddr_in remote_addr;
  int maxfd = 0;
  size_t sin_size;
  int new_fd;
  fd_set read_fds;
  if ((serversock = bind_addr(NULL, get_port_foo(), 5)) == -1) {
    perror("bind_addr");
    return;
  }
  sin_size = sizeof(struct sockaddr_in);
  if ((new_fd = accept(serversock, (struct sockaddr *)&remote_addr,
                       &sin_size)) == -1) {
    perror("accept");
    return;
  }
  printf("commserver: got connection from %s\n",
         inet_ntoa(remote_addr.sin_addr));
  FD_ZERO(&read_fds);
  FD_SET(serversock, &read_fds);
  FD_SET(clientsock, &read_fds);
  maxfd = serversock > clientsock ? serversock : clientsock;
  for (;;) {
    fd_set tmpread_fds = read_fds;
    if (select(maxfd + 1, &tmpread_fds, NULL, NULL, NULL) == -1) {
      perror("select");
      exit(1);
    }
    if (FD_ISSET(serversock, &tmpread_fds)) {
      gotdata(serversock, clientsock);
    }
    if (FD_ISSET(clientsock, &tmpread_fds)) {
      gotdata(clientsock, serversock);
    }
  }
}


x86 assembly (clang, -O0):
<dotunneling>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <get_port_foo@plt>
mov    %eax,%esi
xor    %eax,%eax
mov    %eax,%edi
mov    $0x5,%edx
call   1070 <bind_addr@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1206 <dotunneling+0x46>
lea    0xe04(%rip),%rdi
call   10a0 <perror@plt>
jmp    131c <dotunneling+0x15c>
movq   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    123b <dotunneling+0x7b>
lea    0xdd9(%rip),%rdi
call   10a0 <perror@plt>
jmp    131c <dotunneling+0x15c>
mov    -0x18(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    128a <dotunneling+0xca>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1290 <dotunneling+0xd0>
mov    -0x4(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    12d5 <dotunneling+0x115>
lea    0xd6f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12f6 <dotunneling+0x136>
mov    -0x8(%rbp),%edi
mov    -0x4(%rbp),%esi
call   10d0 <gotdata@plt>
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1317 <dotunneling+0x157>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10d0 <gotdata@plt>
jmp    1296 <dotunneling+0xd6>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dotunneling>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
xor    %eax,%eax
call   1060 <get_port_foo@plt>
xor    %edi,%edi
mov    %eax,%esi
mov    $0x5,%edx
call   1070 <bind_addr@plt>
cmp    $0xffffffff,%eax
jne    11fd <dotunneling+0x3d>
lea    0xe12(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <perror@plt>
mov    %eax,%ebp
movq   $0x4,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %eax,%edi
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
jne    1235 <dotunneling+0x75>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xdcc(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
cmp    %r15d,%ebp
mov    %ebp,%r14d
cmovle %r15d,%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   1090 <select@plt>
cmp    $0xffffffff,%eax
jne    12b1 <dotunneling+0xf1>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %rsp,%rbx
jmp    12de <dotunneling+0x11e>
cs nopw 0x0(%rax,%rax,1)
mov    0x4(%rsp),%eax
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
cmp    $0xffffffff,%eax
je     129b <dotunneling+0xdb>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12f7 <dotunneling+0x137>
mov    %ebp,%edi
mov    %r15d,%esi
call   10d0 <gotdata@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12c0 <dotunneling+0x100>
mov    %r15d,%edi
mov    %ebp,%esi
call   10d0 <gotdata@plt>
jmp    12c0 <dotunneling+0x100>

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"bind_addr\00"
@.str.1 = [7 x i8] c"accept\00"
@.str.2 = [36 x i8] c"commserver: got connection from %s\0A\00"
@.str.3 = [7 x i8] c"select\00"
define void @dotunneling(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.hostent*
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
%11 = call i32 (...) @get_port_foo()
%12 = call i32 @bind_addr(i32* null, i32 %11, i32 5)
store i32 %12, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %68
16: ; preds = %1
store i64 4, i64* %7
%17 = load i32, i32* %3
%18 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%19 = call i32 @accept(i32 %17, %struct.sockaddr* %18, i64* %7)
store i32 %19, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %68
23: ; preds = %16
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @inet_ntoa(i32 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = call i32 @FD_ZERO(i32* %9)
%29 = load i32, i32* %3
%30 = call i32 @FD_SET(i32 %29, i32* %9)
%31 = load i32, i32* %2
%32 = call i32 @FD_SET(i32 %31, i32* %9)
%33 = load i32, i32* %3
%34 = load i32, i32* %2
%35 = icmp sgt i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = load i32, i32* %3
br label %40
38: ; preds = %23
%39 = load i32, i32* %2
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
store i32 %41, i32* %6
br label %42
42: ; preds = %67, %40
%43 = load i32, i32* %9
store i32 %43, i32* %10
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
%46 = call i32 @select(i32 %45, i32* %10, i32* null, i32* null, i32* null)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = load i32, i32* %3
%53 = call i64 @FD_ISSET(i32 %52, i32* %10)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* %3
%57 = load i32, i32* %2
%58 = call i32 @gotdata(i32 %56, i32 %57)
br label %59
59: ; preds = %55, %51
%60 = load i32, i32* %2
%61 = call i64 @FD_ISSET(i32 %60, i32* %10)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i32, i32* %2
%65 = load i32, i32* %3
%66 = call i32 @gotdata(i32 %64, i32 %65)
br label %67
67: ; preds = %63, %59
br label %42
68: ; preds = %21, %14
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"bind_addr\00"
@.str.1 = [7 x i8] c"accept\00"
@.str.2 = [36 x i8] c"commserver: got connection from %s\0A\00"
@.str.3 = [7 x i8] c"select\00"
define void @dotunneling(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i64* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 (...) @get_port_foo()
%10 = tail call i32 @bind_addr(i32* null, i32 %9, i32 5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %52
14: ; preds = %1
store i64 4, i64* %3
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i32 @accept(i32 %10, %struct.sockaddr* %15, i64* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %52
20: ; preds = %14
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i8* @inet_ntoa(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @FD_ZERO(i32* %4)
%26 = call i32 @FD_SET(i32 %10, i32* %4)
%27 = call i32 @FD_SET(i32 %0, i32* %4)
%28 = icmp sgt i32 %10, %0
%29 = select i1 %28, i32 %10, i32 %0
%30 = bitcast i32* %5 to i8*
%31 = add nsw i32 %29, 1
%32 = load i32, i32* %4
store i32 %32, i32* %5
%33 = call i32 @select(i32 %31, i32* %5, i32* null, i32* null, i32* null)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %48, %20
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20, %48
%39 = call i64 @FD_ISSET(i32 %10, i32* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @gotdata(i32 %10, i32 %0)
br label %43
43: ; preds = %41, %38
%44 = call i64 @FD_ISSET(i32 %0, i32* %5)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @gotdata(i32 %0, i32 %10)
br label %48
48: ; preds = %46, %43
%49 = load i32, i32* %4
store i32 %49, i32* %5
%50 = call i32 @select(i32 %31, i32* %5, i32* null, i32* null, i32* null)
%51 = icmp eq i32 %50, -1
br i1 %51, label %35, label %38
52: ; preds = %18, %12
ret void
}