#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*/ *);
char *malloc(int const);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
char *execute_system_command(const char *command) {
  FILE *file = popen(command, "r");
  if (file == NULL)
    return NULL;
  const int result_length = 4096;
  char *result = malloc(result_length);
  if (result == NULL)
    return NULL;
  int offset = 0;
  while (offset != (result_length - 1)) {
    int c = fgetc(file);
    if (c == EOF) {
      result[offset] = '\0';
      break;
    }
    result[offset] = c;
    ++offset;
  }
  result[offset] = '\0';
  pclose(file);
  return result;
}


x86 assembly (clang, -O0):
<execute_system_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1178 <execute_system_command+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1223 <execute_system_command+0xe3>
movl   $0x1000,-0x1c(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a5 <execute_system_command+0x65>
movq   $0x0,-0x8(%rbp)
jmp    1223 <execute_system_command+0xe3>
movl   $0x0,-0x2c(%rbp)
cmpl   $0xfff,-0x2c(%rbp)
je     1206 <execute_system_command+0xc6>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%eax
jne    11e8 <execute_system_command+0xa8>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1206 <execute_system_command+0xc6>
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ac <execute_system_command+0x6c>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_system_command>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     11a6 <execute_system_command+0x66>
mov    %rax,%r14
mov    $0x1000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a6 <execute_system_command+0x66>
mov    %rax,%r15
xor    %ebx,%ebx
mov    0x2e71(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r12),%eax
je     11ab <execute_system_command+0x6b>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0xfff,%rbx
jne    1180 <execute_system_command+0x40>
mov    $0xfff,%ebx
jmp    11b0 <execute_system_command+0x70>
xor    %r15d,%r15d
jmp    11bd <execute_system_command+0x7d>
movb   $0x0,(%r15,%rbx,1)
movb   $0x0,(%r15,%rbx,1)
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @execute_system_command(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %51
14: ; preds = %1
store i32 4096, i32* %5
%15 = call i8* @malloc(i32 4096)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %2
br label %51
19: ; preds = %14
store i32 0, i32* %7
br label %20
20: ; preds = %34, %19
%21 = load i32, i32* %7
%22 = icmp ne i32 %21, 4095
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = load i32*, i32** %4
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %23
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
br label %43
34: ; preds = %23
%35 = load i32, i32* %8
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %20
43: ; preds = %29, %20
%44 = load i8*, i8** %6
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i32*, i32** %4
%49 = call i32 @pclose(i32* %48)
%50 = load i8*, i8** %6
store i8* %50, i8** %2
br label %51
51: ; preds = %43, %18, %13
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define noalias i8* @execute_system_command(i8* nocapture %0) {
%2 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%6 = icmp eq i8* %5, null
br i1 %6, label %25, label %7
7: ; preds = %4, %16
%8 = phi i64 [ %19, %16 ], [ 0, %4 ]
%9 = tail call i32 @fgetc(i32* %2)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = and i64 %8, 4294967295
%14 = getelementptr inbounds i8, i8* %5, i64 %13
store i8 0, i8* %14
%15 = and i64 %8, 4294967295
br label %21
16: ; preds = %7
%17 = trunc i32 %9 to i8
%18 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 %17, i8* %18
%19 = add nuw nsw i64 %8, 1
%20 = icmp eq i64 %19, 4095
br i1 %20, label %21, label %7
21: ; preds = %16, %12
%22 = phi i64 [ %15, %12 ], [ 4095, %16 ]
%23 = getelementptr inbounds i8, i8* %5, i64 %22
store i8 0, i8* %23
%24 = tail call i32 @pclose(i32* %2)
br label %25
25: ; preds = %21, %4, %1
%26 = phi i8* [ null, %1 ], [ %5, %21 ], [ null, %4 ]
ret i8* %26
}