#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 /*<<< orphan*/ close(int);
int do_pipe(int, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void telnet(int s) {
  fd_set fds;
  while (1) {
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    FD_SET(s, &fds);
    if (select(s + 1, &fds, NULL, NULL, NULL) == -1)
      break;
    if (FD_ISSET(0, &fds)) {
      if (do_pipe(0, s) == -1)
        break;
    }
    if (FD_ISSET(s, &fds)) {
      if (do_pipe(s, 0) == -1)
        break;
    }
  }
  close(s);
}


x86 assembly (clang, -O0):
<telnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
cmp    $0xffffffff,%eax
jne    11b3 <telnet+0x53>
jmp    121d <telnet+0xbd>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11e5 <telnet+0x85>
mov    -0x4(%rbp),%esi
xor    %edi,%edi
call   1030 <do_pipe@plt>
cmp    $0xffffffff,%eax
jne    11e0 <telnet+0x80>
jmp    121d <telnet+0xbd>
jmp    11e5 <telnet+0x85>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1218 <telnet+0xb8>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   1030 <do_pipe@plt>
cmp    $0xffffffff,%eax
jne    1213 <telnet+0xb3>
jmp    121d <telnet+0xbd>
jmp    1218 <telnet+0xb8>
jmp    116b <telnet+0xb>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<telnet>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%r14d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     121d <telnet+0xbd>
lea    0xc(%rsp),%rbp
jmp    11e3 <telnet+0x83>
nopl   (%rax)
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     121d <telnet+0xbd>
xor    %edi,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1200 <telnet+0xa0>
xor    %edi,%edi
mov    %ebx,%esi
call   1030 <do_pipe@plt>
cmp    $0xffffffff,%eax
je     121d <telnet+0xbd>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11b0 <telnet+0x50>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <do_pipe@plt>
cmp    $0xffffffff,%eax
jne    11b0 <telnet+0x50>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @telnet(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
br label %4
4: ; preds = %1, %33
%5 = call i32 @FD_ZERO(i32* %3)
%6 = call i32 @FD_SET(i32 0, i32* %3)
%7 = load i32, i32* %2
%8 = call i32 @FD_SET(i32 %7, i32* %3)
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
%11 = call i32 @select(i32 %10, i32* %3, i32* null, i32* null, i32* null)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %4
br label %34
14: ; preds = %4
%15 = call i64 @FD_ISSET(i32 0, i32* %3)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %2
%19 = call i32 @do_pipe(i32 0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %17
br label %34
22: ; preds = %17
br label %23
23: ; preds = %22, %14
%24 = load i32, i32* %2
%25 = call i64 @FD_ISSET(i32 %24, i32* %3)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* %2
%29 = call i32 @do_pipe(i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
br label %34
32: ; preds = %27
br label %33
33: ; preds = %32, %23
br label %4
34: ; preds = %31, %21, %13
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
ret void
}

LLVM IR (-O2):
define void @telnet(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = add nsw i32 %0, 1
%5 = call i32 @FD_ZERO(i32* %2)
%6 = call i32 @FD_SET(i32 0, i32* %2)
%7 = call i32 @FD_SET(i32 %0, i32* %2)
%8 = call i32 @select(i32 %4, i32* %2, i32* null, i32* null, i32* null)
%9 = icmp eq i32 %8, -1
br i1 %9, label %28, label %10
10: ; preds = %1, %22
%11 = call i64 @FD_ISSET(i32 0, i32* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @do_pipe(i32 0, i32 %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %28, label %16
16: ; preds = %13, %10
%17 = call i64 @FD_ISSET(i32 %0, i32* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @do_pipe(i32 %0, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %28, label %22
22: ; preds = %19, %16
%23 = call i32 @FD_ZERO(i32* %2)
%24 = call i32 @FD_SET(i32 0, i32* %2)
%25 = call i32 @FD_SET(i32 %0, i32* %2)
%26 = call i32 @select(i32 %4, i32* %2, i32* null, i32* null, i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %10
28: ; preds = %22, %13, %19, %1
%29 = call i32 @close(i32 %0)
ret void
}