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

/* Variables and functions */
int BUFSIZ;
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ info(char *, ...);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int runcommand(char *cmd) {
  FILE *fp;
  char buf[BUFSIZ];
  info("%s\n", cmd);
  if ((fp = popen(cmd, "r")) == NULL) {
    info("popen failed!\n");
    return -1;
  }
  while (fgets(buf, sizeof(buf), fp)) {
    info(buf);
  }
  return pclose(fp);
}


x86 assembly (clang, -O0):
<runcommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1060 <info@plt>
mov    -0x10(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cd <runcommand+0x8d>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1060 <info@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1212 <runcommand+0xd2>
jmp    11d2 <runcommand+0x92>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11ff <runcommand+0xbf>
mov    -0x38(%rbp),%rdi
mov    $0x0,%al
call   1060 <info@plt>
jmp    11d2 <runcommand+0x92>
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runcommand>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e8c(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe91(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <info@plt>
lea    0xe84(%rip),%rsi
mov    %r15,%rdi
call   1050 <popen@plt>
test   %rax,%rax
je     11b9 <runcommand+0x79>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11af <runcommand+0x6f>
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <info@plt>
jmp    1190 <runcommand+0x50>
mov    %r15,%rdi
call   1030 <pclose@plt>
jmp    11cc <runcommand+0x8c>
lea    0xe46(%rip),%rdi
xor    %eax,%eax
call   1060 <info@plt>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [15 x i8] c"popen failed!\0A\00"
define i32 @runcommand(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32 (i8*, ...) @info(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i64 @fgets(i8* %4, i32 %2, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %13
11: ; preds = %1
%12 = tail call i32 (i8*, ...) @info(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %19
13: ; preds = %8, %13
%14 = call i32 (i8*, ...) @info(i8* %4)
%15 = call i64 @fgets(i8* %4, i32 %2, i32* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %13
17: ; preds = %13, %8
%18 = call i32 @pclose(i32* %6)
br label %19
19: ; preds = %17, %11
%20 = phi i32 [ -1, %11 ], [ %18, %17 ]
ret i32 %20
}