#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 */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind_socket(char *);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
size_t select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
              int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ send_fd(int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  fd_set fds, rfds, wfds;
  size_t n_fds, fds_handled;
  int i, client, new_fd;
  int upstream, downstream;
  if (argc != 3) {
    usage(argv[0]);
    exit(1);
  }
  if ((upstream = bind_socket(argv[1])) == 0) {
    fprintf(stderr, "Couldn't bind to %s\n", argv[1]);
    exit(1);
  }
  if ((downstream = bind_socket(argv[2])) == 0) {
    fprintf(stderr, "Couldn't bind to %s\n", argv[2]);
    exit(1);
  }
  FD_ZERO(&fds);
  FD_ZERO(&rfds);
  FD_ZERO(&wfds);
  FD_SET(downstream, &fds);
  new_fd = dup(upstream);
  while (1) {
    memcpy(&rfds, &fds, sizeof(fd_set));
    n_fds = select(FD_SETSIZE, &rfds, &wfds, NULL, NULL);
    for (i = 0; i < FD_SETSIZE; i++) {
      if (!FD_ISSET(i, &rfds) && !FD_ISSET(i, &wfds))
        continue;
      if (i == downstream) {
        client = accept(downstream, NULL, NULL);
        FD_SET(client, &wfds);
      } else {
        dup2(upstream, new_fd);
        send_fd(i, new_fd);
        close(i);
        FD_CLR(i, &fds);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1226 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <usage@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <bind_socket@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jne    1266 <main+0x76>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xda9(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <bind_socket@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jne    12a6 <main+0xb6>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd69(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    -0x14(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
lea    -0x18(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
lea    -0x1c(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x44(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x40(%rbp),%edi
call   1070 <dup@plt>
mov    %eax,-0x3c(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   10c0 <select@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2cbb(%rip),%rcx
cmp    (%rcx),%eax
jge    13c0 <main+0x1d0>
mov    -0x34(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1354 <main+0x164>
mov    -0x34(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1354 <main+0x164>
jmp    13b2 <main+0x1c2>
mov    -0x34(%rbp),%eax
cmp    -0x44(%rbp),%eax
jne    1383 <main+0x193>
mov    -0x44(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1110 <FD_SET@plt>
jmp    13ad <main+0x1bd>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
call   1050 <dup2@plt>
mov    -0x34(%rbp),%edi
mov    -0x3c(%rbp),%esi
call   1060 <send_fd@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x34(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10f0 <FD_CLR@plt>
jmp    13b2 <main+0x1c2>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1313 <main+0x123>
jmp    12d8 <main+0xe8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    131d <main+0x12d>
mov    0x8(%rbx),%rdi
call   1030 <bind_socket@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     132f <main+0x13f>
mov    0x10(%rbx),%rdi
call   1030 <bind_socket@plt>
test   %eax,%eax
je     133e <main+0x14e>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <FD_ZERO@plt>
lea    0x14(%rsp),%r13
mov    %r13,%rdi
call   10b0 <FD_ZERO@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   10b0 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    0x8(%rsp),%edi
call   1070 <dup@plt>
mov    %eax,%ebx
mov    0x2d63(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   10a0 <memcpy@plt>
mov    (%r15),%edi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
cmpl   $0x0,(%r15)
jle    1280 <main+0x90>
xor    %r14d,%r14d
jmp    12e3 <main+0xf3>
mov    0x8(%rsp),%edi
mov    %ebx,%esi
call   1050 <dup2@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1060 <send_fd@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
lea    0xc(%rsp),%rsi
call   10f0 <FD_CLR@plt>
add    $0x1,%r14d
cmp    (%r15),%r14d
jge    1280 <main+0x90>
mov    %r14d,%edi
mov    %r13,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
jne    1301 <main+0x111>
mov    %r14d,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     12da <main+0xea>
cmp    %r14d,%ebp
jne    12b0 <main+0xc0>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
mov    %eax,%edi
mov    %r12,%rsi
call   1110 <FD_SET@plt>
jmp    12da <main+0xea>
mov    (%rbx),%rdi
call   10d0 <usage@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
jmp    134b <main+0x15b>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xcae(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Couldn't bind to %s\0A\00"
@FD_SETSIZE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @bind_socket(i8* %27)
store i32 %28, i32* %14
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %24
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32 @bind_socket(i8* %40)
store i32 %41, i32* %15
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %37
%51 = call i32 @FD_ZERO(i32* %6)
%52 = call i32 @FD_ZERO(i32* %7)
%53 = call i32 @FD_ZERO(i32* %8)
%54 = load i32, i32* %15
%55 = call i32 @FD_SET(i32 %54, i32* %6)
%56 = load i32, i32* %14
%57 = call i32 @dup(i32 %56)
store i32 %57, i32* %13
br label %58
58: ; preds = %50, %99
%59 = call i32 @memcpy(i32* %7, i32* %6, i32 4)
%60 = load i32, i32* @FD_SETSIZE
%61 = call i64 @select(i32 %60, i32* %7, i32* %8, i32* null, i32* null)
store i64 %61, i64* %9
store i32 0, i32* %11
br label %62
62: ; preds = %96, %58
%63 = load i32, i32* %11
%64 = load i32, i32* @FD_SETSIZE
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %99
66: ; preds = %62
%67 = load i32, i32* %11
%68 = call i32 @FD_ISSET(i32 %67, i32* %7)
%69 = icmp ne i32 %68, 0
br i1 %69, label %75, label %70
70: ; preds = %66
%71 = load i32, i32* %11
%72 = call i32 @FD_ISSET(i32 %71, i32* %8)
%73 = icmp ne i32 %72, 0
br i1 %73, label %75, label %74
74: ; preds = %70
br label %96
75: ; preds = %70, %66
%76 = load i32, i32* %11
%77 = load i32, i32* %15
%78 = icmp eq i32 %76, %77
br i1 %78, label %79, label %84
79: ; preds = %75
%80 = load i32, i32* %15
%81 = call i32 @accept(i32 %80, i32* null, i32* null)
store i32 %81, i32* %12
%82 = load i32, i32* %12
%83 = call i32 @FD_SET(i32 %82, i32* %8)
br label %95
84: ; preds = %75
%85 = load i32, i32* %14
%86 = load i32, i32* %13
%87 = call i32 @dup2(i32 %85, i32 %86)
%88 = load i32, i32* %11
%89 = load i32, i32* %13
%90 = call i32 @send_fd(i32 %88, i32 %89)
%91 = load i32, i32* %11
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %11
%94 = call i32 @FD_CLR(i32 %93, i32* %6)
br label %95
95: ; preds = %84, %79
br label %96
96: ; preds = %95, %74
%97 = load i32, i32* %11
%98 = add nsw i32 %97, 1
store i32 %98, i32* %11
br label %62
99: ; preds = %62
br label %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Couldn't bind to %s\0A\00"
@FD_SETSIZE = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp eq i32 %0, 3
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @usage(i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @bind_socket(i8* %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %15
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32 @bind_socket(i8* %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %25
%32 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %31)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = call i32 @FD_ZERO(i32* %3)
%36 = call i32 @FD_ZERO(i32* %4)
%37 = call i32 @FD_ZERO(i32* %5)
%38 = call i32 @FD_SET(i32 %27, i32* %3)
%39 = call i32 @dup(i32 %17)
br label %40
40: ; preds = %46, %34
%41 = call i32 @memcpy(i32* %4, i32* %3, i32 4)
%42 = load i32, i32* @FD_SETSIZE
%43 = call i64 @select(i32 %42, i32* %4, i32* %5, i32* null, i32* null)
%44 = load i32, i32* @FD_SETSIZE
%45 = icmp sgt i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %64, %40
br label %40
47: ; preds = %40, %64
%48 = phi i32 [ %65, %64 ], [ 0, %40 ]
%49 = call i32 @FD_ISSET(i32 %48, i32* %4)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @FD_ISSET(i32 %48, i32* %5)
%53 = icmp eq i32 %52, 0
br i1 %53, label %64, label %54
54: ; preds = %51, %47
%55 = icmp eq i32 %48, %27
br i1 %55, label %56, label %59
56: ; preds = %54
%57 = call i32 @accept(i32 %27, i32* null, i32* null)
%58 = call i32 @FD_SET(i32 %57, i32* %5)
br label %64
59: ; preds = %54
%60 = call i32 @dup2(i32 %17, i32 %39)
%61 = call i32 @send_fd(i32 %48, i32 %39)
%62 = call i32 @close(i32 %48)
%63 = call i32 @FD_CLR(i32 %48, i32* %3)
br label %64
64: ; preds = %56, %59, %51
%65 = add nuw nsw i32 %48, 1
%66 = load i32, i32* @FD_SETSIZE
%67 = icmp slt i32 %65, %66
br i1 %67, label %47, label %46
}