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

/* Variables and functions */
int BUFSIZ;
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int write(int, char *, int /*<<< orphan*/);
void shovel_tty(int master_fd, int in_fd) {
  fd_set read_fds[1];
  int max_fd;
  char buf[BUFSIZ];
  ssize_t bytes;
  int n;
  while (master_fd != -1) {
    FD_ZERO(read_fds);
    if (in_fd != -1)
      FD_SET(in_fd, read_fds);
    if (master_fd != -1)
      FD_SET(master_fd, read_fds);
    max_fd = (master_fd > in_fd) ? master_fd : in_fd;
    if ((n = select(max_fd + 1, read_fds, NULL, NULL, NULL)) == -1 &&
        errno != EINTR)
      break;
    if (n == -1 && errno == EINTR)
      continue;
    if (in_fd != -1 && FD_ISSET(in_fd, read_fds)) {
      if ((bytes = read(in_fd, buf, BUFSIZ)) > 0) {
        if (master_fd != -1 && write(master_fd, buf, bytes) == -1)
          break;
      } else if (n == -1 && errno == EINTR)
        continue;
      else {
        in_fd = -1;
        continue;
      }
    }
    if (master_fd != -1 && FD_ISSET(master_fd, read_fds)) {
      if ((bytes = read(master_fd, buf, BUFSIZ)) > 0) {
        if (write(STDOUT_FILENO, buf, bytes) == -1)
          break;
      } else if (n == -1 && errno == EINTR)
        continue;
      else {
        close(master_fd);
        master_fd = -1;
        continue;
      }
    }
  }
}


x86 assembly (clang, -O0):
<shovel_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     13d1 <shovel_tty+0x261>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
cmpl   $0xffffffff,-0x8(%rbp)
je     11d3 <shovel_tty+0x63>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
cmpl   $0xffffffff,-0x4(%rbp)
je     11e9 <shovel_tty+0x79>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    1200 <shovel_tty+0x90>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    1206 <shovel_tty+0x96>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1251 <shovel_tty+0xe1>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     1251 <shovel_tty+0xe1>
jmp    13d1 <shovel_tty+0x261>
cmpl   $0xffffffff,-0x28(%rbp)
jne    127a <shovel_tty+0x10a>
mov    0x2d6e(%rip),%rax
mov    (%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    127a <shovel_tty+0x10a>
jmp    11aa <shovel_tty+0x3a>
cmpl   $0xffffffff,-0x8(%rbp)
je     1321 <shovel_tty+0x1b1>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1321 <shovel_tty+0x1b1>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    12e7 <shovel_tty+0x177>
cmpl   $0xffffffff,-0x4(%rbp)
je     12e2 <shovel_tty+0x172>
mov    -0x30(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12e2 <shovel_tty+0x172>
jmp    13d1 <shovel_tty+0x261>
jmp    131c <shovel_tty+0x1ac>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1310 <shovel_tty+0x1a0>
mov    0x2cd8(%rip),%rax
mov    (%rax),%rax
mov    0x2cc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1310 <shovel_tty+0x1a0>
jmp    11aa <shovel_tty+0x3a>
movl   $0xffffffff,-0x8(%rbp)
jmp    11aa <shovel_tty+0x3a>
jmp    1321 <shovel_tty+0x1b1>
cmpl   $0xffffffff,-0x4(%rbp)
je     13cc <shovel_tty+0x25c>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13cc <shovel_tty+0x25c>
mov    -0x30(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2c89(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    138a <shovel_tty+0x21a>
mov    -0x30(%rbp),%rsi
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1385 <shovel_tty+0x215>
jmp    13d1 <shovel_tty+0x261>
jmp    13c7 <shovel_tty+0x257>
cmpl   $0xffffffff,-0x28(%rbp)
jne    13b3 <shovel_tty+0x243>
mov    0x2c35(%rip),%rax
mov    (%rax),%rax
mov    0x2c23(%rip),%rcx
cmp    (%rcx),%rax
jne    13b3 <shovel_tty+0x243>
jmp    11aa <shovel_tty+0x3a>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <shovel_tty+0x3a>
jmp    13cc <shovel_tty+0x25c>
jmp    11aa <shovel_tty+0x3a>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shovel_tty>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,-0x30(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    $0xffffffff,%edi
je     12bc <shovel_tty+0x14c>
mov    %esi,%ebx
mov    %edi,%r14d
lea    -0x34(%rbp),%r12
cmp    %r14d,%ebx
mov    %ebx,%r13d
cmovl  %r14d,%r13d
add    $0x1,%r13d
jmp    11ea <shovel_tty+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    12bc <shovel_tty+0x14c>
mov    %r12,%rdi
call   1070 <FD_ZERO@plt>
cmp    $0xffffffff,%ebx
je     1201 <shovel_tty+0x91>
mov    %ebx,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    %r14d,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    %r13d,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     11d0 <shovel_tty+0x60>
cmp    $0xffffffff,%ebx
je     1260 <shovel_tty+0xf0>
mov    %ebx,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1260 <shovel_tty+0xf0>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <read@plt>
test   %eax,%eax
jle    12aa <shovel_tty+0x13a>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     12bc <shovel_tty+0x14c>
mov    %r14d,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11ea <shovel_tty+0x7a>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r15,%rsi
call   1060 <read@plt>
test   %eax,%eax
jle    12b4 <shovel_tty+0x144>
mov    0x2d5d(%rip),%rcx
mov    (%rcx),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11ea <shovel_tty+0x7a>
jmp    12bc <shovel_tty+0x14c>
mov    $0xffffffff,%ebx
jmp    11b1 <shovel_tty+0x41>
mov    %r14d,%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@STDOUT_FILENO = global i32 0
define void @shovel_tty(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i32]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
br label %15
15: ; preds = %127, %123, %122, %93, %92, %61, %2
%16 = load i32, i32* %3
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %128
18: ; preds = %15
%19 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%20 = call i32 @FD_ZERO(i32* %19)
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* %4
%25 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%26 = call i32 @FD_SET(i32 %24, i32* %25)
br label %27
27: ; preds = %23, %18
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %3
%32 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%33 = call i32 @FD_SET(i32 %31, i32* %32)
br label %34
34: ; preds = %30, %27
%35 = load i32, i32* %3
%36 = load i32, i32* %4
%37 = icmp sgt i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* %3
br label %42
40: ; preds = %34
%41 = load i32, i32* %4
br label %42
42: ; preds = %40, %38
%43 = phi i32 [ %39, %38 ], [ %41, %40 ]
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
%46 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%47 = call i32 @select(i32 %45, i32* %46, i32* null, i32* null, i32* null)
store i32 %47, i32* %10
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %128
54: ; preds = %49, %42
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %57
br label %15
62: ; preds = %57, %54
%63 = load i32, i32* %4
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %95
65: ; preds = %62
%66 = load i32, i32* %4
%67 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%68 = call i64 @FD_ISSET(i32 %66, i32* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %95
70: ; preds = %65
%71 = load i32, i32* %4
%72 = load i32, i32* @BUFSIZ
%73 = call i32 @read(i32 %71, i8* %14, i32 %72)
store i32 %73, i32* %9
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %85
75: ; preds = %70
%76 = load i32, i32* %3
%77 = icmp ne i32 %76, -1
br i1 %77, label %78, label %84
78: ; preds = %75
%79 = load i32, i32* %3
%80 = load i32, i32* %9
%81 = call i32 @write(i32 %79, i8* %14, i32 %80)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %84
83: ; preds = %78
br label %128
84: ; preds = %78, %75
br label %94
85: ; preds = %70
%86 = load i32, i32* %10
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %93
88: ; preds = %85
%89 = load i64, i64* @errno
%90 = load i64, i64* @EINTR
%91 = icmp eq i64 %89, %90
br i1 %91, label %92, label %93
92: ; preds = %88
br label %15
93: ; preds = %88, %85
store i32 -1, i32* %4
br label %15
94: ; preds = %84
br label %95
95: ; preds = %94, %65, %62
%96 = load i32, i32* %3
%97 = icmp ne i32 %96, -1
br i1 %97, label %98, label %127
98: ; preds = %95
%99 = load i32, i32* %3
%100 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%101 = call i64 @FD_ISSET(i32 %99, i32* %100)
%102 = icmp ne i64 %101, 0
br i1 %102, label %103, label %127
103: ; preds = %98
%104 = load i32, i32* %3
%105 = load i32, i32* @BUFSIZ
%106 = call i32 @read(i32 %104, i8* %14, i32 %105)
store i32 %106, i32* %9
%107 = icmp sgt i32 %106, 0
br i1 %107, label %108, label %115
108: ; preds = %103
%109 = load i32, i32* @STDOUT_FILENO
%110 = load i32, i32* %9
%111 = call i32 @write(i32 %109, i8* %14, i32 %110)
%112 = icmp eq i32 %111, -1
br i1 %112, label %113, label %114
113: ; preds = %108
br label %128
114: ; preds = %108
br label %126
115: ; preds = %103
%116 = load i32, i32* %10
%117 = icmp eq i32 %116, -1
br i1 %117, label %118, label %123
118: ; preds = %115
%119 = load i64, i64* @errno
%120 = load i64, i64* @EINTR
%121 = icmp eq i64 %119, %120
br i1 %121, label %122, label %123
122: ; preds = %118
br label %15
123: ; preds = %118, %115
%124 = load i32, i32* %3
%125 = call i32 @close(i32 %124)
store i32 -1, i32* %3
br label %15
126: ; preds = %114
br label %127
127: ; preds = %126, %98, %95
br label %15
128: ; preds = %113, %83, %53, %15
%129 = load i8*, i8** %7
ret void
}

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