#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 ssize_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDIN_FILENO;
int STDOUT_FILENO;
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int interact(int sock) {
  fd_set fds;
  ssize_t ssize;
  char buffer[1024];
  write(sock,
        "uname -a ; id"
        "\n",
        sizeof("uname -a ; id"));
  while (1) {
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds);
    FD_SET(sock, &fds);
    select(sock + 1, &fds, NULL, NULL, NULL);
    if (FD_ISSET(STDIN_FILENO, &fds)) {
      ssize = read(STDIN_FILENO, buffer, sizeof(buffer));
      if (ssize < 0) {
        return (-1);
      }
      if (ssize == 0) {
        return (0);
      }
      write(sock, buffer, ssize);
    }
    if (FD_ISSET(sock, &fds)) {
      ssize = read(sock, buffer, sizeof(buffer));
      if (ssize < 0) {
        return (-1);
      }
      if (ssize == 0) {
        return (0);
      }
      write(STDOUT_FILENO, buffer, ssize);
    }
  }
  return (-1);
}


x86 assembly (clang, -O0):
<interact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe88(%rip),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     123a <interact+0xda>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1212 <interact+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <interact+0x150>
cmpl   $0x0,-0x10(%rbp)
jne    1228 <interact+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <interact+0x150>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ab <interact+0x14b>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    127d <interact+0x11d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b0 <interact+0x150>
cmpl   $0x0,-0x10(%rbp)
jne    1293 <interact+0x133>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <interact+0x150>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    1182 <interact+0x22>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<interact>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebx
lea    0xe86(%rip),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
lea    0x1(%rbx),%r14d
lea    0xc(%rsp),%rbp
mov    0x2e44(%rip),%r12
lea    0x10(%rsp),%r15
mov    0x2e50(%rip),%r13
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    (%r12),%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>
mov    (%r12),%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1204 <interact+0xa4>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     123b <interact+0xdb>
je     1242 <interact+0xe2>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11a0 <interact+0x40>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     123b <interact+0xdb>
je     1242 <interact+0xe2>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11a0 <interact+0x40>
mov    $0xffffffff,%eax
jmp    1244 <interact+0xe4>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"uname -a ; id\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @interact(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @write(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
br label %9
9: ; preds = %1, %57
%10 = call i32 @FD_ZERO(i32* %4)
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @FD_SET(i32 %11, i32* %4)
%13 = load i32, i32* %3
%14 = call i32 @FD_SET(i32 %13, i32* %4)
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i32 @select(i32 %16, i32* %4, i32* null, i32* null, i32* null)
%18 = load i32, i32* @STDIN_FILENO
%19 = call i64 @FD_ISSET(i32 %18, i32* %4)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %9
%22 = load i32, i32* @STDIN_FILENO
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1024)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %2
br label %58
28: ; preds = %21
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 0, i32* %2
br label %58
32: ; preds = %28
%33 = load i32, i32* %3
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = load i32, i32* %5
%36 = call i32 @write(i32 %33, i8* %34, i32 %35)
br label %37
37: ; preds = %32, %9
%38 = load i32, i32* %3
%39 = call i64 @FD_ISSET(i32 %38, i32* %4)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %57
41: ; preds = %37
%42 = load i32, i32* %3
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 1024)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 -1, i32* %2
br label %58
48: ; preds = %41
%49 = load i32, i32* %5
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %48
store i32 0, i32* %2
br label %58
52: ; preds = %48
%53 = load i32, i32* @STDOUT_FILENO
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = load i32, i32* %5
%56 = call i32 @write(i32 %53, i8* %54, i32 %55)
br label %57
57: ; preds = %52, %37
br label %9
58: ; preds = %51, %47, %31, %27
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@.str = [15 x i8] c"uname -a ; id\0A\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @interact(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 14)
%7 = add nsw i32 %0, 1
br label %8
8: ; preds = %36, %1
%9 = call i32 @FD_ZERO(i32* %2)
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @FD_SET(i32 %10, i32* %2)
%12 = call i32 @FD_SET(i32 %0, i32* %2)
%13 = call i32 @select(i32 %7, i32* %2, i32* null, i32* null, i32* null)
%14 = load i32, i32* @STDIN_FILENO
%15 = call i64 @FD_ISSET(i32 %14, i32* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %8
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @read(i32 %18, i8* %5, i32 1024)
%20 = icmp slt i32 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %17
%22 = icmp eq i32 %19, 0
br i1 %22, label %37, label %23
23: ; preds = %21
%24 = call i32 @write(i32 %0, i8* %5, i32 %19)
br label %25
25: ; preds = %23, %8
%26 = call i64 @FD_ISSET(i32 %0, i32* %2)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = call i32 @read(i32 %0, i8* %5, i32 1024)
%30 = icmp slt i32 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = icmp eq i32 %29, 0
br i1 %32, label %37, label %33
33: ; preds = %31
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @write(i32 %34, i8* %5, i32 %29)
br label %36
36: ; preds = %33, %25
br label %8
37: ; preds = %31, %28, %21, %17
%38 = phi i32 [ -1, %17 ], [ 0, %21 ], [ -1, %28 ], [ 0, %31 ]
ret i32 %38
}