#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 EOF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ printf(char *);
int shcmd(const char *command, char *out) {
  FILE *ret = NULL;
  int c = 0;
  ret = popen(command, "r");
  if (ret) {
    c = fgetc(ret);
    *out = c;
    while (c != EOF) {
      *out++ = c;
      c = fgetc(ret);
    }
    pclose(ret);
  } else {
    printf("failed to pope\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<shcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11dc <shcmd+0x9c>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x24(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
je     11ce <shcmd+0x8e>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
jmp    1195 <shcmd+0x55>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
jmp    11f4 <shcmd+0xb4>
lea    0xe1f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fb <shcmd+0xbb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shcmd>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
lea    0xeb1(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     119f <shcmd+0x5f>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    %al,(%rbx)
mov    0x2e7b(%rip),%r15
cmp    (%r15),%eax
je     1193 <shcmd+0x53>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %al,(%rbx)
add    $0x1,%rbx
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
jne    1180 <shcmd+0x40>
mov    %r14,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
jmp    11b0 <shcmd+0x70>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@str = [15 x i8] c"failed to pope\00"
define i32 @shcmd(i8* nocapture %0, i8* nocapture writeonly %1) {
%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 %20, label %5
5: ; preds = %2
%6 = tail call i32 @fgetc(i32* %3)
%7 = trunc i32 %6 to i8
store i8 %7, i8* %1
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %6, %8
br i1 %9, label %18, label %10
10: ; preds = %5, %10
%11 = phi i32 [ %15, %10 ], [ %6, %5 ]
%12 = phi i8* [ %14, %10 ], [ %1, %5 ]
%13 = trunc i32 %11 to i8
%14 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %13, i8* %12
%15 = tail call i32 @fgetc(i32* %3)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %10
18: ; preds = %10, %5
%19 = tail call i32 @pclose(i32* %3)
br label %22
20: ; preds = %2
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %18, %20
%23 = phi i32 [ 0, %18 ], [ 1, %20 ]
ret i32 %23
}