#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fatalf(char *, char const *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ printf(char *, char *);
int exec_with_output(const char *command) {
  FILE *fp = popen(command, "r");
  if (fp == NULL) {
    fatalf("Error: failed to run command: %s", command);
  }
  char buffer[1024];
  while (fgets(buffer, sizeof(buffer), fp) != NULL) {
    printf("%s", buffer);
  }
  return pclose(fp);
}


x86 assembly (clang, -O0):
<exec_with_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118e <exec_with_output+0x3e>
mov    -0x8(%rbp),%rsi
lea    0xe79(%rip),%rdi
call   1030 <fatalf@plt>
jmp    1193 <exec_with_output+0x43>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11ca <exec_with_output+0x7a>
lea    -0x410(%rbp),%rsi
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
jmp    1193 <exec_with_output+0x43>
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_with_output>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
lea    0xe9a(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1182 <exec_with_output+0x32>
lea    0xe88(%rip),%rdi
mov    %rbx,%rsi
call   1030 <fatalf@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11d0 <exec_with_output+0x80>
lea    0xe82(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11b0 <exec_with_output+0x60>
mov    %r15,%rdi
call   1050 <pclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"Error: failed to run command: %s\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @exec_with_output(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1024 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @fatalf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
br label %13
13: ; preds = %18, %12
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = load i32*, i32** %3
%16 = call i32* @fgets(i8* %14, i32 1024, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %13
21: ; preds = %13
%22 = load i32*, i32** %3
%23 = call i32 @pclose(i32* %22)
ret i32 %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [33 x i8] c"Error: failed to run command: %s\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @exec_with_output(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @fatalf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %7
7: ; preds = %5, %1
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = call i32* @fgets(i8* %8, i32 1024, i32* %3)
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %7, %11
%12 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%13 = call i32* @fgets(i8* %8, i32 1024, i32* %3)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %11
15: ; preds = %11, %7
%16 = call i32 @pclose(i32* %3)
ret i32 %16
}