#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int main() {
  FILE *fp;
  int fd;
  int flags = 1;
  char buf[64];
  fd_set rfds;
  printf("popen...\n");
  fp = popen("sleep 10;echo hello", "r");
  fd = fileno(fp);
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  if (0) {
    flags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  }
  printf("select...\n");
  select(fd + 1, &rfds, NULL, NULL, NULL);
  printf("fread...\n");
  fread(buf, sizeof(char), 64, fp);
  printf("pclose...\n");
  pclose(fp);
  printf("return...\n");
  sleep(100);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
lea    0xe51(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x14(%rbp)
lea    -0x64(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x64(%rbp),%rsi
call   10a0 <FD_SET@plt>
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x64(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x40,%edx
call   1030 <fread@plt>
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
lea    0xdf1(%rip),%rdi
call   1040 <printf@plt>
mov    $0x64,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
lea    0xe55(%rip),%rdi
lea    0xe62(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
add    $0x1,%ebp
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x40,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
lea    0xe03(%rip),%rdi
call   1030 <puts@plt>
mov    $0x64,%edi
call   10b0 <sleep@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"popen...\0A\00"
@.str.1 = [20 x i8] c"sleep 10;echo hello\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"select...\0A\00"
@.str.4 = [10 x i8] c"fread...\0A\00"
@.str.5 = [11 x i8] c"pclose...\0A\00"
@.str.6 = [11 x i8] c"return...\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%7 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = call i32 @fileno(i32* %9)
store i32 %10, i32* %3
%11 = call i32 @FD_ZERO(i32* %6)
%12 = load i32, i32* %3
%13 = call i32 @FD_SET(i32 %12, i32* %6)
%14 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
%17 = call i32 @select(i32 %16, i32* %6, i32* null, i32* null, i32* null)
%18 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %2
%21 = call i32 @fread(i8* %19, i32 1, i32 64, i32* %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%23 = load i32*, i32** %2
%24 = call i32 @pclose(i32* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%26 = call i32 @sleep(i32 100)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"sleep 10;echo hello\00"
@.str.2 = [2 x i8] c"r\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@str = [9 x i8] c"popen...\00"
@str.7 = [10 x i8] c"select...\00"
@str.8 = [9 x i8] c"fread...\00"
@str.9 = [10 x i8] c"pclose...\00"
@str.10 = [10 x i8] c"return...\00"
define i32 @main() {
%1 = alloca [64 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%6 = tail call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @fileno(i32* %6)
%8 = call i32 @FD_ZERO(i32* %2)
%9 = call i32 @FD_SET(i32 %7, i32* %2)
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
%11 = add nsw i32 %7, 1
%12 = call i32 @select(i32 %11, i32* %2, i32* null, i32* null, i32* null)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.8, i64 0, i64 0))
%14 = call i32 @fread(i8* %3, i32 1, i32 64, i32* %6)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.9, i64 0, i64 0))
%16 = call i32 @pclose(i32* %6)
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%18 = call i32 @sleep(i32 100)
ret i32 0
}