#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void shell(int s) {
  char buff[4096];
  int retval;
  fd_set fds;
  printf("[+] connected!\n\n");
  for (;;) {
    FD_ZERO(&fds);
    FD_SET(0, &fds);
    FD_SET(s, &fds);
    if (select(s + 1, &fds, NULL, NULL, NULL) < 0) {
      perror("[-] shell.select()");
      exit(1);
    };
    if (FD_ISSET(0, &fds)) {
      if ((retval = read(1, buff, 4096)) < 1) {
        perror("[-] shell.recv(stdin)");
        exit(1);
      };
      send(s, buff, retval, 0);
    }
    if (FD_ISSET(s, &fds)) {
      if ((retval = recv(s, buff, 4096, 0)) < 1) {
        perror("[-] shell.recv(socket)");
        exit(1);
      };
      write(1, buff, retval);
    }
  }
}


x86 assembly (clang, -O0):
<shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe3b(%rip),%rdi
call   1070 <printf@plt>
lea    -0x1018(%rbp),%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x1018(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x1018(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1018(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
cmp    $0x0,%rax
jge    1230 <shell+0x80>
lea    0xdf0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
lea    -0x1018(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     129a <shell+0xea>
lea    -0x1010(%rbp),%rsi
mov    $0x1,%edi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x1,%eax
jge    1283 <shell+0xd3>
lea    0xdb0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x4(%rbp),%edi
lea    -0x1018(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1305 <shell+0x155>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x1,%eax
jge    12ee <shell+0x13e>
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    11ca <shell+0x1a>

x86 assembly (clang, -O2):
<shell>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebx
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
lea    0x1(%rbx),%r14d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %rax,%rax
jns    121d <shell+0x6d>
lea    0xdf2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r15
jmp    1263 <shell+0xb3>
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %rax,%rax
js     1207 <shell+0x57>
xor    %edi,%edi
mov    %rbp,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1296 <shell+0xe6>
mov    $0x1,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12ce <shell+0x11e>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1230 <shell+0x80>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    12da <shell+0x12a>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    1230 <shell+0x80>
lea    0xd3e(%rip),%rdi
jmp    120e <shell+0x5e>
lea    0xd48(%rip),%rdi
jmp    120e <shell+0x5e>

LLVM IR (-O0):
@.str = [17 x i8] c"[+] connected!\0A\0A\00"
@.str.1 = [19 x i8] c"[-] shell.select()\00"
@.str.2 = [22 x i8] c"[-] shell.recv(stdin)\00"
@.str.3 = [23 x i8] c"[-] shell.recv(socket)\00"
define void @shell(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %50, %1
%8 = call i32 @FD_ZERO(i32* %5)
%9 = call i32 @FD_SET(i32 0, i32* %5)
%10 = load i32, i32* %2
%11 = call i32 @FD_SET(i32 %10, i32* %5)
%12 = load i32, i32* %2
%13 = add nsw i32 %12, 1
%14 = call i64 @select(i32 %13, i32* %5, i32* null, i32* null, i32* null)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %7
%20 = call i64 @FD_ISSET(i32 0, i32* %5)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 1, i8* %23, i32 4096)
store i32 %24, i32* %4
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i32, i32* %2
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* %4
%33 = call i32 @send(i32 %30, i8* %31, i32 %32, i32 0)
br label %34
34: ; preds = %29, %19
%35 = load i32, i32* %2
%36 = call i64 @FD_ISSET(i32 %35, i32* %5)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %34
%39 = load i32, i32* %2
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%41 = call i32 @recv(i32 %39, i8* %40, i32 4096, i32 0)
store i32 %41, i32* %4
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %38
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%48 = load i32, i32* %4
%49 = call i32 @write(i32 1, i8* %47, i32 %48)
br label %50
50: ; preds = %46, %34
br label %7
}

LLVM IR (-O2):
@.str.1 = [19 x i8] c"[-] shell.select()\00"
@.str.2 = [22 x i8] c"[-] shell.recv(stdin)\00"
@.str.3 = [23 x i8] c"[-] shell.recv(socket)\00"
@str = [16 x i8] c"[+] connected!\0A\00"
define void @shell(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%7 = add nsw i32 %0, 1
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 0, i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = call i64 @select(i32 %7, i32* %3, i32* null, i32* null, i32* null)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %38, %1
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1, %38
%17 = call i64 @FD_ISSET(i32 0, i32* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = call i32 @read(i32 1, i8* %4, i32 4096)
%21 = icmp slt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = call i32 @send(i32 %0, i8* %4, i32 %20, i32 0)
br label %27
27: ; preds = %25, %16
%28 = call i64 @FD_ISSET(i32 %0, i32* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %27
%31 = call i32 @recv(i32 %0, i8* %4, i32 4096, i32 0)
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = call i32 @write(i32 1, i8* %4, i32 %31)
br label %38
38: ; preds = %36, %27
%39 = call i32 @FD_ZERO(i32* %3)
%40 = call i32 @FD_SET(i32 0, i32* %3)
%41 = call i32 @FD_SET(i32 %0, i32* %3)
%42 = call i64 @select(i32 %7, i32* %3, i32* null, i32* null, i32* null)
%43 = icmp slt i64 %42, 0
br i1 %43, label %13, label %16
}