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

/* Variables and functions */
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
scalar_t__ write(int, char *, size_t);
int shell_stream(int fd, char *cmd) {
  char buf[4096];
  int total = 0;
  FILE *pf;
  size_t ret;
  pf = popen(cmd, "r");
  if (pf == NULL) {
    perror("popen");
    return 0;
  }
  while ((ret = fread(buf, 1, 4096, pf)) > 0) {
    total += write(fd, buf, ret);
  }
  pclose(pf);
  return total;
}


x86 assembly (clang, -O0):
<shell_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1014(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jne    11a9 <shell_stream+0x59>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    121f <shell_stream+0xcf>
jmp    11ae <shell_stream+0x5e>
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jbe    120a <shell_stream+0xba>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,%rcx
movslq -0x1014(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1014(%rbp)
jmp    11ae <shell_stream+0x5e>
mov    -0x1020(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_stream>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
lea    0xe96(%rip),%rsi
mov    %rax,%rdi
call   1060 <popen@plt>
test   %rax,%rax
je     11e2 <shell_stream+0x92>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11ca <shell_stream+0x7a>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
add    %eax,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11a0 <shell_stream+0x50>
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
xor    %ebp,%ebp
jmp    11d2 <shell_stream+0x82>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"popen\00"
define i32 @shell_stream(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %35
16: ; preds = %2
br label %17
17: ; preds = %22, %16
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%19 = load i32*, i32** %8
%20 = call i64 @fread(i8* %18, i32 1, i32 4096, i32* %19)
store i64 %20, i64* %9
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i32, i32* %4
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%25 = load i64, i64* %9
%26 = call i64 @write(i32 %23, i8* %24, i64 %25)
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = add nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %7
br label %17
31: ; preds = %17
%32 = load i32*, i32** %8
%33 = call i32 @pclose(i32* %32)
%34 = load i32, i32* %7
store i32 %34, i32* %3
br label %35
35: ; preds = %31, %14
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"popen\00"
define i32 @shell_stream(i32 %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i64 @fread(i8* %4, i32 1, i32 4096, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %23
12: ; preds = %7, %12
%13 = phi i64 [ %18, %12 ], [ %8, %7 ]
%14 = phi i32 [ %17, %12 ], [ 0, %7 ]
%15 = call i64 @write(i32 %0, i8* %4, i64 %13)
%16 = trunc i64 %15 to i32
%17 = add i32 %14, %16
%18 = call i64 @fread(i8* %4, i32 1, i32 4096, i32* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %12
20: ; preds = %12, %7
%21 = phi i32 [ 0, %7 ], [ %17, %12 ]
%22 = tail call i32 @pclose(i32* %5)
br label %23
23: ; preds = %20, %10
%24 = phi i32 [ 0, %10 ], [ %21, %20 ]
ret i32 %24
}