#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 */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fwrite(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
size_t exec_and_redirect_output(char *command, FILE *output) {
  size_t r = 0, w = 0;
  FILE *pipe = NULL;
  if (!(pipe = popen(command, "r"))) {
    return 0;
  }
  size_t read = 0;
  char r_b[0x2000];
  while (!feof(pipe)) {
    if ((r = fread(r_b, 1, 0x2000, pipe)) <= 0) {
      break;
    }
    w += fwrite(r_b, r, 1, output);
    read += r;
  }
  pclose(pipe);
  return w;
}


x86 assembly (clang, -O0):
<exec_and_redirect_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a6 <exec_and_redirect_output+0x56>
movq   $0x0,-0x8(%rbp)
jmp    123c <exec_and_redirect_output+0xec>
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11cc <exec_and_redirect_output+0x7c>
jmp    122b <exec_and_redirect_output+0xdb>
lea    -0x2040(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
ja     11f9 <exec_and_redirect_output+0xa9>
jmp    122b <exec_and_redirect_output+0xdb>
lea    -0x2040(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11ae <exec_and_redirect_output+0x5e>
mov    -0x30(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_and_redirect_output>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%r14
lea    0xe98(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     11d6 <exec_and_redirect_output+0x86>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <feof@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    11cc <exec_and_redirect_output+0x7c>
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11cc <exec_and_redirect_output+0x7c>
mov    %r12,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
add    %rax,%rbx
mov    %r15,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1190 <exec_and_redirect_output+0x40>
mov    %r15,%rdi
call   1040 <pclose@plt>
jmp    11d8 <exec_and_redirect_output+0x88>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i64 @exec_and_redirect_output(i8* %0, i32* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca [8192 x i8]
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 0, i64* %6
store i64 0, i64* %7
store i32* null, i32** %8
%11 = load i8*, i8** %4
%12 = call i32* @popen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i64 0, i64* %3
br label %41
15: ; preds = %2
store i64 0, i64* %9
br label %16
16: ; preds = %27, %15
%17 = load i32*, i32** %8
%18 = call i32 @feof(i32* %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %37
21: ; preds = %16
%22 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%23 = load i32*, i32** %8
%24 = call i64 @fread(i8* %22, i32 1, i32 8192, i32* %23)
store i64 %24, i64* %6
%25 = icmp ule i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
br label %37
27: ; preds = %21
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%29 = load i64, i64* %6
%30 = load i32*, i32** %5
%31 = call i64 @fwrite(i8* %28, i64 %29, i32 1, i32* %30)
%32 = load i64, i64* %7
%33 = add i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %6
%35 = load i64, i64* %9
%36 = add i64 %35, %34
store i64 %36, i64* %9
br label %16
37: ; preds = %26, %16
%38 = load i32*, i32** %8
%39 = call i32 @pclose(i32* %38)
%40 = load i64, i64* %7
store i64 %40, i64* %3
br label %41
41: ; preds = %37, %14
%42 = load i64, i64* %3
ret i64 %42
}

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