#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 WEXITSTATUS(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ stderr;
int invoke_capture_stdout(const char *cmd, char *buf, size_t buf_size) {
  FILE *handle = popen(cmd, "r");
  if (!handle) {
    fprintf(stderr, "invoke_capture_stdout: popen failed\n");
    return -1;
  }
  size_t n = fread(buf, 1, buf_size - 1, handle);
  int status = pclose(handle);
  if (status == -1 || WEXITSTATUS(status) != 0) {
    fprintf(stderr, "invoke_capture_stdout: pclose failed with %d/%d\n", status,
            WEXITSTATUS(status));
    return -1;
  }
  buf[n] = '\0';
  return n;
}


x86 assembly (clang, -O0):
<invoke_capture_stdout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <invoke_capture_stdout+0x56>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <invoke_capture_stdout+0xea>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
je     11eb <invoke_capture_stdout+0x9b>
mov    -0x34(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     1227 <invoke_capture_stdout+0xd7>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%edx
mov    %eax,%ecx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <invoke_capture_stdout+0xea>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invoke_capture_stdout>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
lea    0xe9f(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11cd <invoke_capture_stdout+0x7d>
mov    %rax,%rbp
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <pclose@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11a2 <invoke_capture_stdout+0x52>
mov    %ebp,%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     11eb <invoke_capture_stdout+0x9b>
mov    0x2e37(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe6e(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11f0 <invoke_capture_stdout+0xa0>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11f0 <invoke_capture_stdout+0xa0>
movb   $0x0,(%r14,%rbx,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"invoke_capture_stdout: popen failed\0A\00"
@.str.2 = [49 x i8] c"invoke_capture_stdout: pclose failed with %d/%d\0A\00"
define i32 @invoke_capture_stdout(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%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 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = sub i64 %20, 1
%22 = load i32*, i32** %8
%23 = call i64 @fread(i8* %19, i32 1, i64 %21, i32* %22)
store i64 %23, i64* %9
%24 = load i32*, i32** %8
%25 = call i32 @pclose(i32* %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %18
%29 = load i32, i32* %10
%30 = call i32 @WEXITSTATUS(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28, %18
%33 = load i32, i32* @stderr
%34 = load i32, i32* %10
%35 = load i32, i32* %10
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %36)
store i32 -1, i32* %4
br label %44
38: ; preds = %28
%39 = load i8*, i8** %6
%40 = load i64, i64* %9
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i64, i64* %9
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
br label %44
44: ; preds = %38, %32, %15
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"invoke_capture_stdout: popen failed\0A\00"
@.str.2 = [49 x i8] c"invoke_capture_stdout: pclose failed with %d/%d\0A\00"
define i32 @invoke_capture_stdout(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%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 %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %24
9: ; preds = %3
%10 = add i64 %2, -1
%11 = tail call i64 @fread(i8* %1, i32 1, i64 %10, i32* %4)
%12 = tail call i32 @pclose(i32* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @WEXITSTATUS(i32 %12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14, %9
%18 = load i32, i32* @stderr
%19 = tail call i32 @WEXITSTATUS(i32 %12)
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %19)
br label %24
21: ; preds = %14
%22 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %22
%23 = trunc i64 %11 to i32
br label %24
24: ; preds = %17, %21, %6
%25 = phi i32 [ -1, %6 ], [ -1, %17 ], [ %23, %21 ]
ret i32 %25
}