#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*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int strlen(char *);
int process_info_get(char *buf) {
  FILE *stream;
  char *command =
      "(sleep 1; echo q) | top | grep ipc | sort | awk 'BEGIN{CPU=0, MEM=0, "
      "MEMP=0}{print \"PID=\"$1,\"MEM=\"$4,\"%MEM=\"$7,\"%CPU=\"$6; CPU += $6; "
      "MEM = $4; MEMP=$7}END{print \"TOTAL:MEM=\"MEM, "
      "\"%MEM=\"MEMP,\"%CPU=\"CPU}'";
  stream = popen(command, "r");
  if (stream == NULL) {
    return -1;
  }
  fread(buf, sizeof(char), 1024, stream);
  pclose(stream);
  return strlen(buf);
}


x86 assembly (clang, -O0):
<process_info_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xf69(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1182 <process_info_get+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ae <process_info_get+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_info_get>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xeb2(%rip),%rdi
lea    0xf76(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     118e <process_info_get+0x4e>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1050 <pclose@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <strlen@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [203 x i8] c"(sleep 1; echo q) | top | grep ipc | sort | awk 'BEGIN{CPU=0, MEM=0, MEMP=0}{print \22PID=\22$1,\22MEM=\22$4,\22%MEM=\22$7,\22%CPU=\22$6; CPU += $6; MEM = $4; MEMP=$7}END{print \22TOTAL:MEM=\22MEM, \22%MEM=\22MEMP,\22%CPU=\22CPU}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @process_info_get(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([203 x i8], [203 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %19
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32*, i32** %4
%14 = call i32 @fread(i8* %12, i32 1, i32 1024, i32* %13)
%15 = load i32*, i32** %4
%16 = call i32 @pclose(i32* %15)
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %2
br label %19
19: ; preds = %11, %10
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
@.str = [203 x i8] c"(sleep 1; echo q) | top | grep ipc | sort | awk 'BEGIN{CPU=0, MEM=0, MEMP=0}{print \22PID=\22$1,\22MEM=\22$4,\22%MEM=\22$7,\22%CPU=\22$6; CPU += $6; MEM = $4; MEMP=$7}END{print \22TOTAL:MEM=\22MEM, \22%MEM=\22MEMP,\22%CPU=\22CPU}'\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @process_info_get(i8* %0) {
%2 = tail call i32* @popen(i8* getelementptr inbounds ([203 x i8], [203 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = tail call i32 @fread(i8* %0, i32 1, i32 1024, i32* %2)
%6 = tail call i32 @pclose(i32* %2)
%7 = tail call i32 @strlen(i8* %0)
br label %8
8: ; preds = %1, %4
%9 = phi i32 [ %7, %4 ], [ -1, %1 ]
ret i32 %9
}