#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
void shell(int sock) {
  int l;
  char buf[512];
  fd_set rfds;
  while (1) {
    FD_SET(0, &rfds);
    FD_SET(sock, &rfds);
    select(sock + 1, &rfds, NULL, NULL, NULL);
    if (FD_ISSET(0, &rfds)) {
      l = read(0, buf, sizeof(buf));
      if (l <= 0) {
        printf("\n - Connection closed by local user\n");
        exit(EXIT_FAILURE);
      }
      write(sock, buf, l);
    }
    if (FD_ISSET(sock, &rfds)) {
      l = read(sock, buf, sizeof(buf));
      if (l == 0) {
        printf("\n - Connection closed by remote host.\n");
        exit(EXIT_FAILURE);
      } else if (l < 0) {
        printf("\n - Read failure\n");
        exit(EXIT_FAILURE);
      }
      write(1, buf, l);
    }
  }
}


x86 assembly (clang, -O0):
<shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
xor    %edi,%edi
lea    -0x214(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x214(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x214(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
xor    %edi,%edi
lea    -0x214(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     121c <shell+0xac>
lea    -0x210(%rbp),%rsi
xor    %edi,%edi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     120a <shell+0x9a>
lea    0xe09(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x214(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ad <shell+0x13d>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1270 <shell+0x100>
lea    0xdc8(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jge    1294 <shell+0x124>
lea    0xdcb(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1299 <shell+0x129>
lea    -0x210(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    117e <shell+0xe>

x86 assembly (clang, -O2):
<shell>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%r14d
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r15
nopl   (%rax)
xor    %edi,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     11e4 <shell+0x74>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    121c <shell+0xac>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1190 <shell+0x20>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1225 <shell+0xb5>
js     122e <shell+0xbe>
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    1190 <shell+0x20>
lea    0xe14(%rip),%rdi
jmp    1235 <shell+0xc5>
lea    0xde5(%rip),%rdi
jmp    1235 <shell+0xc5>
lea    0xdcb(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"\0A - Connection closed by local user\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [39 x i8] c"\0A - Connection closed by remote host.\0A\00"
@.str.2 = [18 x i8] c"\0A - Read failure\0A\00"
define void @shell(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %55
%7 = call i32 @FD_SET(i32 0, i32* %5)
%8 = load i32, i32* %2
%9 = call i32 @FD_SET(i32 %8, i32* %5)
%10 = load i32, i32* %2
%11 = add nsw i32 %10, 1
%12 = call i32 @select(i32 %11, i32* %5, i32* null, i32* null, i32* null)
%13 = call i64 @FD_ISSET(i32 0, i32* %5)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %29
15: ; preds = %6
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i32 0, i8* %16, i32 512)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = load i32, i32* %2
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = load i32, i32* %3
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
br label %29
29: ; preds = %24, %6
%30 = load i32, i32* %2
%31 = call i64 @FD_ISSET(i32 %30, i32* %5)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %55
33: ; preds = %29
%34 = load i32, i32* %2
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 512)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %33
%44 = load i32, i32* %3
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %43
br label %51
51: ; preds = %50
%52 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%53 = load i32, i32* %3
%54 = call i32 @write(i32 1, i8* %52, i32 %53)
br label %55
55: ; preds = %51, %29
br label %6
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@str = [17 x i8] c"\0A - Read failure\00"
@str.3 = [38 x i8] c"\0A - Connection closed by remote host.\00"
@str.4 = [36 x i8] c"\0A - Connection closed by local user\00"
define void @shell(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = add nsw i32 %0, 1
br label %7
7: ; preds = %40, %1
%8 = call i32 @FD_SET(i32 0, i32* %3)
%9 = call i32 @FD_SET(i32 %0, i32* %3)
%10 = call i32 @select(i32 %6, i32* %3, i32* null, i32* null, i32* null)
%11 = call i64 @FD_ISSET(i32 0, i32* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %7
%14 = call i32 @read(i32 0, i8* %4, i32 512)
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.4, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = call i32 @write(i32 %0, i8* %4, i32 %14)
br label %22
22: ; preds = %20, %7
%23 = call i64 @FD_ISSET(i32 %0, i32* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %22
%26 = call i32 @read(i32 %0, i8* %4, i32 512)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.3, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %25
%33 = icmp slt i32 %26, 0
br i1 %33, label %34, label %38
34: ; preds = %32
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %32
%39 = call i32 @write(i32 1, i8* %4, i32 %26)
br label %40
40: ; preds = %38, %22
br label %7
}