#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 */
struct timeval {
  int tv_usec;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main() {
  FILE *fp = popen("bash test.sh", "r");
  int fd = fileno(fp);
  int opts = fcntl(fd, F_GETFL, 0);
  if (opts < 0)
    return 0;
  opts &= ~O_NONBLOCK;
  opts |= O_NONBLOCK;
  if (fcntl(fd, F_SETFL, opts) < 0)
    return 0;
  char buf[1024] = {'\0'};
  struct timeval tv = {0};
  tv.tv_usec = 1 * 1000 * 1000;
  fd_set fdset;
  FD_ZERO(&fdset);
  FD_SET(fd, &fdset);
  while (1) {
    int num = select(fd + 1, &fdset, NULL, NULL, NULL);
    if (0 > num) {
      if (EINTR == errno || EAGAIN == errno || EWOULDBLOCK == errno) {
        continue;
      }
    }
    int nbytes = read(fd, buf, 1024);
    fprintf(stderr, "%d:%s\n", nbytes, buf);
  }
  pclose(fp);
  return fd;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f1 <main+0x61>
movl   $0x0,-0x4(%rbp)
jmp    1351 <main+0x1c1>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    123c <main+0xac>
movl   $0x0,-0x4(%rbp)
jmp    1351 <main+0x1c1>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movl   $0xf4240,-0x428(%rbp)
lea    -0x42c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x42c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x430(%rbp)
xor    %eax,%eax
cmp    -0x430(%rbp),%eax
jle    1310 <main+0x180>
mov    0x2cf1(%rip),%rax
mov    (%rax),%rax
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
je     1306 <main+0x176>
mov    0x2cc7(%rip),%rax
mov    (%rax),%rax
mov    0x2ce5(%rip),%rcx
cmp    (%rcx),%rax
je     1306 <main+0x176>
mov    0x2ccd(%rip),%rax
mov    (%rax),%rax
mov    0x2ccb(%rip),%rcx
cmp    (%rcx),%rax
jne    130b <main+0x17b>
jmp    1287 <main+0xf7>
jmp    1310 <main+0x180>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x434(%rbp)
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
mov    -0x434(%rbp),%edx
lea    -0x420(%rbp),%rcx
lea    0xcc8(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1287 <main+0xf7>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xe58(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,%rdi
call   1070 <fileno@plt>
mov    %eax,%r15d
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     12b6 <main+0x126>
mov    0x2e07(%rip),%rcx
or     (%rcx),%eax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%esi
mov    %r15d,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     12b6 <main+0x126>
lea    0x10(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1080 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
mov    %r15d,%ebp
add    $0x1,%ebp
mov    0x2da0(%rip),%r13
lea    0xdd0(%rip),%r12
jmp    1271 <main+0xe1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <fprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
jns    1250 <main+0xc0>
mov    0x2d3b(%rip),%rax
mov    (%rax),%rax
mov    0x2d19(%rip),%rcx
cmp    %rax,(%rcx)
je     1271 <main+0xe1>
mov    0x2cfd(%rip),%rcx
cmp    %rax,(%rcx)
je     1271 <main+0xe1>
mov    0x2d11(%rip),%rcx
cmp    %rax,(%rcx)
jne    1250 <main+0xc0>
jmp    1271 <main+0xe1>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"bash test.sh\00"
@.str.1 = [2 x i8] c"r\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str.2 = [7 x i8] c"%d:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32* @popen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = call i32 @fileno(i32* %11)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = load i32, i32* @F_GETFL
%15 = call i64 @fcntl(i32 %13, i32 %14, i32 0)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %0
store i32 0, i32* %1
br label %69
20: ; preds = %0
%21 = load i32, i32* @O_NONBLOCK
%22 = xor i32 %21, -1
%23 = load i32, i32* %4
%24 = and i32 %23, %22
store i32 %24, i32* %4
%25 = load i32, i32* @O_NONBLOCK
%26 = load i32, i32* %4
%27 = or i32 %26, %25
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %4
%31 = call i64 @fcntl(i32 %28, i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %20
store i32 0, i32* %1
br label %69
34: ; preds = %20
%35 = bitcast [1024 x i8]* %5 to i8*
%36 = bitcast %struct.timeval* %6 to i8*
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 1000000, i32* %37
%38 = call i32 @FD_ZERO(i32* %7)
%39 = load i32, i32* %3
%40 = call i32 @FD_SET(i32 %39, i32* %7)
br label %41
41: ; preds = %34, %59, %61
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
%44 = call i32 @select(i32 %43, i32* %7, i32* null, i32* null, i32* null)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp sgt i32 0, %45
br i1 %46, label %47, label %61
47: ; preds = %41
%48 = load i64, i64* @EINTR
%49 = load i64, i64* @errno
%50 = icmp eq i64 %48, %49
br i1 %50, label %59, label %51
51: ; preds = %47
%52 = load i64, i64* @EAGAIN
%53 = load i64, i64* @errno
%54 = icmp eq i64 %52, %53
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i64, i64* @EWOULDBLOCK
%57 = load i64, i64* @errno
%58 = icmp eq i64 %56, %57
br i1 %58, label %59, label %60
59: ; preds = %55, %51, %47
br label %41
60: ; preds = %55
br label %61
61: ; preds = %60, %41
%62 = load i32, i32* %3
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 1024)
store i32 %64, i32* %9
%65 = load i32, i32* @stderr
%66 = load i32, i32* %9
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%68 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %66, i8* %67)
br label %41
69: ; preds = %33, %19
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@.str = [13 x i8] c"bash test.sh\00"
@.str.1 = [2 x i8] c"r\00"
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str.2 = [7 x i8] c"%d:%s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = tail call i32* @popen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @fileno(i32* %3)
%5 = load i32, i32* @F_GETFL
%6 = tail call i64 @fcntl(i32 %4, i32 %5, i32 0)
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
br i1 %8, label %39, label %9
9: ; preds = %0
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %7
%12 = load i32, i32* @F_SETFL
%13 = tail call i64 @fcntl(i32 %4, i32 %12, i32 %11)
%14 = icmp slt i64 %13, 0
br i1 %14, label %39, label %15
15: ; preds = %9
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%17 = bitcast i32* %2 to i8*
%18 = call i32 @FD_ZERO(i32* %2)
%19 = call i32 @FD_SET(i32 %4, i32* %2)
%20 = add nsw i32 %4, 1
br label %21
21: ; preds = %34, %15
%22 = call i32 @select(i32 %20, i32* %2, i32* null, i32* null, i32* null)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i64, i64* @EINTR
%26 = load i64, i64* @errno
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %28, %26
%30 = select i1 %27, i1 true, i1 %29
%31 = load i64, i64* @EWOULDBLOCK
%32 = icmp eq i64 %31, %26
%33 = select i1 %30, i1 true, i1 %32
br i1 %33, label %34, label %35
34: ; preds = %24, %35
br label %21
35: ; preds = %24, %21
%36 = call i32 @read(i32 %4, i8* %16, i32 1024)
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %36, i8* %16)
br label %34
39: ; preds = %9, %0
ret i32 0
}