#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__ EIO;
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 /*<<< orphan*/ destroy_socket(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
int read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ write(int, char *, int);
void shell_loop(int sock, int pty) {
  ((void)0);
  fd_set fds;
  char buf[(4094 + 31)];
  int res;
  int nfds;
  nfds = (sock > pty) ? sock : pty;
  setsid();
  while (1) {
    FD_ZERO(&fds);
    FD_SET(sock, &fds);
    FD_SET(pty, &fds);
    if (select(nfds + 1, &fds, NULL, NULL, NULL) < 0) {
      ((void)0);
      exit(1);
    }
    if (FD_ISSET(sock, &fds)) {
      memset(&buf, 0, (4094 + 31));
      if ((res = read(sock, buf, (4094 + 31))) <= 0) {
        if (res == -1 && errno != EIO)
          ((void)0);
        close(pty);
        destroy_socket(sock);
        exit(1);
      } else
        write(pty, buf, res);
    }
    if (FD_ISSET(pty, &fds)) {
      memset(&buf, 0, (4094 + 31));
      if ((res = read(pty, buf, (4094 + 31) - 31)) <= 0) {
        if (res == -1 && errno != EIO)
          ((void)0);
        close(pty);
        destroy_socket(sock);
        exit(1);
      } else
        write(sock, buf, res);
    }
  }
}


x86 assembly (clang, -O0):
<shell_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11db <shell_loop+0x2b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x103c(%rbp)
jmp    11e4 <shell_loop+0x34>
mov    -0x8(%rbp),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x103c(%rbp),%eax
mov    %eax,-0x1038(%rbp)
mov    $0x0,%al
call   1080 <setsid@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x1038(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0x0,%rax
jge    1249 <shell_loop+0x99>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12f5 <shell_loop+0x145>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x101d,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x101d,%edx
call   1090 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jg     12db <shell_loop+0x12b>
cmpl   $0xffffffff,-0x1034(%rbp)
jne    12c1 <shell_loop+0x111>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rax
mov    0x2d2d(%rip),%rcx
cmp    (%rcx),%rax
je     12c1 <shell_loop+0x111>
jmp    12c1 <shell_loop+0x111>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
call   1050 <destroy_socket@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1030 <write@plt>
jmp    12f5 <shell_loop+0x145>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13a1 <shell_loop+0x1f1>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x101d,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xffe,%edx
call   1090 <read@plt>
mov    %eax,-0x1034(%rbp)
cmp    $0x0,%eax
jg     1387 <shell_loop+0x1d7>
cmpl   $0xffffffff,-0x1034(%rbp)
jne    136d <shell_loop+0x1bd>
mov    0x2c83(%rip),%rax
mov    (%rax),%rax
mov    0x2c81(%rip),%rcx
cmp    (%rcx),%rax
je     136d <shell_loop+0x1bd>
jmp    136d <shell_loop+0x1bd>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%edi
call   1050 <destroy_socket@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1030 <write@plt>
jmp    13a1 <shell_loop+0x1f1>
jmp    11f7 <shell_loop+0x47>

x86 assembly (clang, -O2):
<shell_loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1030,%rsp
mov    %esi,%ebp
mov    %edi,%r15d
cmp    %esi,%edi
mov    %esi,%r14d
cmovg  %edi,%r14d
xor    %eax,%eax
call   1080 <setsid@plt>
add    $0x1,%r14d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
jns    121b <shell_loop+0x6b>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r12
jmp    1264 <shell_loop+0xb4>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
js     1211 <shell_loop+0x61>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a3 <shell_loop+0xf3>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x101d,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x101d,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12ea <shell_loop+0x13a>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1230 <shell_loop+0x80>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x101d,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xffe,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12ea <shell_loop+0x13a>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1230 <shell_loop+0x80>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1050 <destroy_socket@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EIO = global i64 0
define void @shell_loop(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4125 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = load i32, i32* %4
%11 = icmp sgt i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* %3
br label %16
14: ; preds = %2
%15 = load i32, i32* %4
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
store i32 %17, i32* %8
%18 = call i32 (...) @setsid()
br label %19
19: ; preds = %16, %93
%20 = call i32 @FD_ZERO(i32* %5)
%21 = load i32, i32* %3
%22 = call i32 @FD_SET(i32 %21, i32* %5)
%23 = load i32, i32* %4
%24 = call i32 @FD_SET(i32 %23, i32* %5)
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
%27 = call i64 @select(i32 %26, i32* %5, i32* null, i32* null, i32* null)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32, i32* %3
%33 = call i64 @FD_ISSET(i32 %32, i32* %5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %62
35: ; preds = %31
%36 = bitcast [4125 x i8]* %6 to i8**
%37 = call i32 @memset(i8** %36, i32 0, i32 4125)
%38 = load i32, i32* %3
%39 = getelementptr inbounds [4125 x i8], [4125 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 4125)
store i32 %40, i32* %7
%41 = icmp sle i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %35
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i64, i64* @errno
%47 = load i64, i64* @EIO
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
br label %50
50: ; preds = %49, %45, %42
%51 = load i32, i32* %4
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %3
%54 = call i32 @destroy_socket(i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %35
%57 = load i32, i32* %4
%58 = getelementptr inbounds [4125 x i8], [4125 x i8]* %6, i64 0, i64 0
%59 = load i32, i32* %7
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
br label %61
61: ; preds = %56
br label %62
62: ; preds = %61, %31
%63 = load i32, i32* %4
%64 = call i64 @FD_ISSET(i32 %63, i32* %5)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %93
66: ; preds = %62
%67 = bitcast [4125 x i8]* %6 to i8**
%68 = call i32 @memset(i8** %67, i32 0, i32 4125)
%69 = load i32, i32* %4
%70 = getelementptr inbounds [4125 x i8], [4125 x i8]* %6, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 4094)
store i32 %71, i32* %7
%72 = icmp sle i32 %71, 0
br i1 %72, label %73, label %87
73: ; preds = %66
%74 = load i32, i32* %7
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %81
76: ; preds = %73
%77 = load i64, i64* @errno
%78 = load i64, i64* @EIO
%79 = icmp ne i64 %77, %78
br i1 %79, label %80, label %81
80: ; preds = %76
br label %81
81: ; preds = %80, %76, %73
%82 = load i32, i32* %4
%83 = call i32 @close(i32 %82)
%84 = load i32, i32* %3
%85 = call i32 @destroy_socket(i32 %84)
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %66
%88 = load i32, i32* %3
%89 = getelementptr inbounds [4125 x i8], [4125 x i8]* %6, i64 0, i64 0
%90 = load i32, i32* %7
%91 = call i32 @write(i32 %88, i8* %89, i32 %90)
br label %92
92: ; preds = %87
br label %93
93: ; preds = %92, %62
br label %19
}

LLVM IR (-O2):
@errno = global i64 0
@EIO = global i64 0
define void @shell_loop(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [4125 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [4125 x i8], [4125 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, %1
%8 = select i1 %7, i32 %0, i32 %1
%9 = tail call i32 (...) @setsid()
%10 = add nsw i32 %8, 1
%11 = bitcast [4125 x i8]* %4 to i8**
%12 = call i32 @FD_ZERO(i32* %3)
%13 = call i32 @FD_SET(i32 %0, i32* %3)
%14 = call i32 @FD_SET(i32 %1, i32* %3)
%15 = call i64 @select(i32 %10, i32* %3, i32* null, i32* null, i32* null)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %45, %2
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2, %45
%20 = call i64 @FD_ISSET(i32 %0, i32* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %32, label %22
22: ; preds = %19
%23 = call i32 @memset(i8** %11, i32 0, i32 4125)
%24 = call i32 @read(i32 %0, i8* %6, i32 4125)
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = call i32 @close(i32 %1)
%28 = call i32 @destroy_socket(i32 %0)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @write(i32 %1, i8* %6, i32 %24)
br label %32
32: ; preds = %30, %19
%33 = call i64 @FD_ISSET(i32 %1, i32* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %45, label %35
35: ; preds = %32
%36 = call i32 @memset(i8** %11, i32 0, i32 4125)
%37 = call i32 @read(i32 %1, i8* %6, i32 4094)
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = call i32 @close(i32 %1)
%41 = call i32 @destroy_socket(i32 %0)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = call i32 @write(i32 %0, i8* %6, i32 %37)
br label %45
45: ; preds = %43, %32
%46 = call i32 @FD_ZERO(i32* %3)
%47 = call i32 @FD_SET(i32 %0, i32* %3)
%48 = call i32 @FD_SET(i32 %1, i32* %3)
%49 = call i64 @select(i32 %10, i32* %3, i32* null, i32* null, i32* null)
%50 = icmp slt i64 %49, 0
br i1 %50, label %17, label %19
}