#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 atoi(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *, char *, char *,
                          char *, char *, char *);
int memory_used_per_get(int per_total) {
  FILE *fp;
  char buff[1024];
  char mem_s[256];
  char total_s[256];
  char used_s[256];
  char free_s[256];
  char shared_s[256];
  char buffers_s[256];
  char cached_s[256];
  int total;
  int shared;
  int free;
  int cached;
  int used;
  int used_real;
  fp = popen("free | grep Mem:", "r");
  if (fp == NULL) {
    perror("erro : ");
    return 0;
  }
  while (fgets(buff, 1024, fp) != NULL)
    ;
  sscanf(buff, "%s %s %s %s %s %s %s", mem_s, total_s, used_s, free_s, shared_s,
         buffers_s, cached_s);
  total = atoi(total_s);
  shared = atoi(shared_s);
  cached = atoi(cached_s);
  used = atoi(used_s);
  used_real = used - shared - cached;
  used_real = total - used_real;
  int change_per = used_real / (total / per_total);
  printf("real used : %d\n", used_real);
  printf("real used per : %d\n", change_per);
  pclose(fp);
  return change_per;
}


x86 assembly (clang, -O0):
<memory_used_per_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb50,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe7b(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b8 <memory_used_per_get+0x48>
lea    0xe6c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1302 <memory_used_per_get+0x192>
jmp    11bd <memory_used_per_get+0x4d>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11e1 <memory_used_per_get+0x71>
jmp    11bd <memory_used_per_get+0x4d>
lea    -0x410(%rbp),%rdi
lea    -0x510(%rbp),%rdx
lea    -0x610(%rbp),%rcx
lea    -0x710(%rbp),%r8
lea    -0x810(%rbp),%r9
lea    -0x910(%rbp),%r11
lea    -0xa10(%rbp),%r10
lea    -0xb10(%rbp),%rax
lea    0xdfb(%rip),%rsi
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
call   1060 <sscanf@plt>
lea    -0x610(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xb14(%rbp)
lea    -0x910(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xb18(%rbp)
lea    -0xb10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xb20(%rbp)
lea    -0x710(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0xb24(%rbp)
mov    -0xb24(%rbp),%eax
sub    -0xb18(%rbp),%eax
sub    -0xb20(%rbp),%eax
mov    %eax,-0xb28(%rbp)
mov    -0xb14(%rbp),%eax
sub    -0xb28(%rbp),%eax
mov    %eax,-0xb28(%rbp)
mov    -0xb28(%rbp),%eax
mov    %eax,-0xb30(%rbp)
mov    -0xb14(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,%ecx
mov    -0xb30(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0xb2c(%rbp)
mov    -0xb28(%rbp),%esi
lea    0xd57(%rip),%rdi
call   1030 <printf@plt>
mov    -0xb2c(%rbp),%esi
lea    0xd55(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0xb2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_used_per_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb18,%rsp
mov    %edi,%ebp
lea    0xe76(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     129d <memory_used_per_get+0x12d>
mov    %rax,%rbx
mov    %ebp,0xc(%rsp)
lea    0x710(%rsp),%rbp
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <memory_used_per_get+0x40>
sub    $0x8,%rsp
lea    0x18(%rsp),%r15
lea    0x118(%rsp),%rax
lea    0x218(%rsp),%r13
lea    0xe36(%rip),%rsi
lea    0x718(%rsp),%rdi
lea    0x618(%rsp),%rdx
lea    0x518(%rsp),%rbp
lea    0x418(%rsp),%r12
lea    0x318(%rsp),%r9
mov    %rbp,%rcx
mov    %r12,%r8
push   %r15
push   %rax
push   %r13
call   1060 <sscanf@plt>
add    $0x20,%rsp
mov    %rbp,%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    %r13,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1090 <atoi@plt>
add    %r14d,%ebp
add    %r15d,%ebp
sub    %eax,%ebp
mov    %r14d,%eax
cltd
idivl  0xc(%rsp)
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%r14d
lea    0xdc6(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
lea    0xdc8(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %r14d,%eax
add    $0xb18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6f(%rip),%rdi
call   1080 <perror@plt>
xor    %r14d,%r14d
jmp    1288 <memory_used_per_get+0x118>

LLVM IR (-O0):
@.str = [17 x i8] c"free | grep Mem:\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"erro : \00"
@.str.3 = [21 x i8] c"%s %s %s %s %s %s %s\00"
@.str.4 = [16 x i8] c"real used : %d\0A\00"
@.str.5 = [20 x i8] c"real used per : %d\0A\00"
define i32 @memory_used_per_get(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca [256 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %3
%20 = call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %4
%21 = load i32*, i32** %4
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %70
25: ; preds = %1
br label %26
26: ; preds = %31, %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = load i32*, i32** %4
%29 = call i32* @fgets(i8* %27, i32 1024, i32* %28)
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %26
br label %26
32: ; preds = %26
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%41 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %34, i8* %35, i8* %36, i8* %37, i8* %38, i8* %39, i8* %40)
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %13
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %14
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %16
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%49 = call i32 @atoi(i8* %48)
store i32 %49, i32* %17
%50 = load i32, i32* %17
%51 = load i32, i32* %14
%52 = sub nsw i32 %50, %51
%53 = load i32, i32* %16
%54 = sub nsw i32 %52, %53
store i32 %54, i32* %18
%55 = load i32, i32* %13
%56 = load i32, i32* %18
%57 = sub nsw i32 %55, %56
store i32 %57, i32* %18
%58 = load i32, i32* %18
%59 = load i32, i32* %13
%60 = load i32, i32* %3
%61 = sdiv i32 %59, %60
%62 = sdiv i32 %58, %61
store i32 %62, i32* %19
%63 = load i32, i32* %18
%64 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %63)
%65 = load i32, i32* %19
%66 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %65)
%67 = load i32*, i32** %4
%68 = call i32 @pclose(i32* %67)
%69 = load i32, i32* %19
store i32 %69, i32* %2
br label %70
70: ; preds = %32, %23
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
@.str = [17 x i8] c"free | grep Mem:\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"erro : \00"
@.str.3 = [21 x i8] c"%s %s %s %s %s %s %s\00"
@.str.4 = [16 x i8] c"real used : %d\0A\00"
@.str.5 = [20 x i8] c"real used per : %d\0A\00"
define i32 @memory_used_per_get(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
%9 = alloca [256 x i8]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%18 = tail call i32* @popen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %39
22: ; preds = %1, %22
%23 = call i32* @fgets(i8* %10, i32 1024, i32* %18)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %22
25: ; preds = %22
%26 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %11, i8* %12, i8* %13, i8* %14, i8* %15, i8* %16, i8* %17)
%27 = call i32 @atoi(i8* %12)
%28 = call i32 @atoi(i8* %15)
%29 = call i32 @atoi(i8* %17)
%30 = call i32 @atoi(i8* %13)
%31 = add i32 %28, %27
%32 = add i32 %31, %29
%33 = sub i32 %32, %30
%34 = sdiv i32 %27, %0
%35 = sdiv i32 %33, %34
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%37 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%38 = call i32 @pclose(i32* %18)
br label %39
39: ; preds = %25, %20
%40 = phi i32 [ 0, %20 ], [ %35, %25 ]
ret i32 %40
}