#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *exec_command(char *command) {
  char buffer[1024], buffer2[1024];
  char *ptr = buffer2;
  FILE *file = popen(command, "r");
  while ((fgets(buffer, sizeof buffer, file)) != NULL) {
    strcat(buffer2, buffer);
  }
  buffer2[strlen(buffer2) - 1] = '\0';
  pclose(file);
  return ptr;
}


x86 assembly (clang, -O0):
<exec_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x810(%rbp),%rax
mov    %rax,-0x818(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x820(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x820(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11be <exec_command+0x6e>
lea    -0x810(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1070 <strcat@plt>
jmp    1184 <exec_command+0x34>
lea    -0x810(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x810(%rbp,%rax,1)
mov    -0x820(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x818(%rbp),%rax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_command>:
push   %r15
push   %r14
push   %rbx
sub    $0x800,%rsp
lea    0xe9d(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
lea    0x400(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b0 <exec_command+0x60>
mov    %rsp,%r14
lea    0x400(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <exec_command+0x40>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r15,%rdi
call   1040 <pclose@plt>
mov    %rbx,%rax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @exec_command(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %7, i8** %5
%8 = load i8*, i8** %2
%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
br label %10
10: ; preds = %15, %1
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i32* @fgets(i8* %11, i32 1024, i32* %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @strcat(i8* %16, i8* %17)
br label %10
19: ; preds = %10
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i32*, i32** %6
%26 = call i32 @pclose(i32* %25)
%27 = load i8*, i8** %5
ret i8* %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @exec_command(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = call i32* @fgets(i8* %4, i32 1024, i32* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1, %9
%10 = call i32 @strcat(i8* %5, i8* %4)
%11 = call i32* @fgets(i8* %4, i32 1024, i32* %6)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %9
13: ; preds = %9, %1
%14 = call i32 @strlen(i8* %5)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i32 @pclose(i32* %6)
ret i8* %5
}