#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*/ die(char *);
int /*<<< orphan*/ die_sans_errno(char *);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned long *, unsigned long *,
           unsigned long *, unsigned long *);
int /*<<< orphan*/ multithread_write(char *, int *);
int snprintf(char *, int, char *, double);
void *mem_mon(void *thread_id) {
  FILE *procmem = fopen("/proc/meminfo", "r");
  if (procmem == NULL) {
    die("couldn't open /proc/meminfo in mem_mon");
  }
  unsigned long int mem_stats[4];
  int values_read =
      fscanf(procmem,
             "MemTotal: %lu kB\n"
             "MemFree: %lu kB\n"
             "Buffers: %lu kB\n"
             "Cached: %lu kB\n",
             mem_stats + 0, mem_stats + 1, mem_stats + 2, mem_stats + 3);
  if (values_read != 4) {
    if (ferror(procmem)) {
      die("fscanf on /proc/meminfo in mem_mon failed");
    } else {
      die_sans_errno("matching error for fscanf on /proc/meminfo in mem_mon");
    }
  }
  if (fclose(procmem) != 0) {
    die("couldn't close /proc/meminfo in mem_mon");
  }
  int i;
  unsigned long int total_free_memory = 0;
  for (i = 1; i < 4; i++) {
    total_free_memory += mem_stats[i];
  }
  double percent_mem =
      ((double)(mem_stats[0] - total_free_memory) / (double)mem_stats[0]) * 100;
  char out_buffer[20];
  int bytes_written = snprintf(out_buffer, 20, "mem:%3.0f%%", percent_mem);
  if (bytes_written < 0) {
    die_sans_errno("snprintf in mem_mon failed");
  }
  multithread_write(out_buffer, (int *)thread_id);
  return NULL;
}


x86 assembly (clang, -O0):
<mem_mon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <mem_mon+0x3a>
lea    0xe8b(%rip),%rdi
call   1090 <die@plt>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rdx
lea    -0x30(%rbp),%rcx
add    $0x8,%rcx
lea    -0x30(%rbp),%r8
add    $0x10,%r8
lea    -0x30(%rbp),%r9
add    $0x18,%r9
lea    0xe86(%rip),%rsi
call   1060 <fscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
je     1228 <mem_mon+0xa8>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1217 <mem_mon+0x97>
lea    0xe9b(%rip),%rdi
call   1090 <die@plt>
jmp    1223 <mem_mon+0xa3>
lea    0xeb4(%rip),%rdi
call   10a0 <die_sans_errno@plt>
jmp    1228 <mem_mon+0xa8>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1247 <mem_mon+0xc7>
lea    0xec6(%rip),%rdi
call   1090 <die@plt>
movq   $0x0,-0x40(%rbp)
movl   $0x1,-0x38(%rbp)
cmpl   $0x4,-0x38(%rbp)
jge    127f <mem_mon+0xff>
movslq -0x38(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1256 <mem_mon+0xd6>
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
sub    %rdx,%rcx
movq   %rcx,%xmm0
movaps 0xd77(%rip),%xmm3
punpckldq %xmm3,%xmm0
movapd 0xd7b(%rip),%xmm2
subpd  %xmm2,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movq   %rax,%xmm1
punpckldq %xmm3,%xmm1
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  0xd28(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
lea    -0x60(%rbp),%rdi
movsd  -0x48(%rbp),%xmm0
mov    $0x14,%esi
lea    0xe3a(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    1314 <mem_mon+0x194>
lea    0xe2d(%rip),%rdi
call   10a0 <die_sans_errno@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <multithread_write@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_mon>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
lea    0xe97(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b1 <mem_mon+0x31>
lea    0xe8c(%rip),%rdi
call   1090 <die@plt>
lea    0x8(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0x18(%rsp),%r9
lea    0xe98(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   1060 <fscanf@plt>
cmp    $0x4,%eax
je     11fe <mem_mon+0x7e>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     11f2 <mem_mon+0x72>
lea    0xeb5(%rip),%rdi
call   1090 <die@plt>
jmp    11fe <mem_mon+0x7e>
lea    0xed1(%rip),%rdi
call   10a0 <die_sans_errno@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
je     1217 <mem_mon+0x97>
lea    0xeee(%rip),%rdi
call   1090 <die@plt>
mov    (%rsp),%rax
mov    0x10(%rsp),%rcx
add    0x8(%rsp),%rcx
add    0x18(%rsp),%rcx
movq   %rax,%xmm1
sub    %rcx,%rax
movq   %rax,%xmm2
movdqa 0xdc1(%rip),%xmm3
punpckldq %xmm3,%xmm2
movapd 0xdc5(%rip),%xmm4
subpd  %xmm4,%xmm2
movapd %xmm2,%xmm0
unpckhpd %xmm2,%xmm0
addsd  %xmm2,%xmm0
punpckldq %xmm3,%xmm1
subpd  %xmm4,%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
mulsd  0xda5(%rip),%xmm0
lea    0xea6(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x14,%esi
call   1050 <snprintf@plt>
test   %eax,%eax
jns    12a1 <mem_mon+0x121>
lea    0xe98(%rip),%rdi
call   10a0 <die_sans_errno@plt>
lea    0x20(%rsp),%rdi
mov    %r14,%rsi
call   1070 <multithread_write@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"couldn't open /proc/meminfo in mem_mon\00"
@.str.3 = [65 x i8] c"MemTotal: %lu kB\0AMemFree: %lu kB\0ABuffers: %lu kB\0ACached: %lu kB\0A\00"
@.str.4 = [42 x i8] c"fscanf on /proc/meminfo in mem_mon failed\00"
@.str.5 = [54 x i8] c"matching error for fscanf on /proc/meminfo in mem_mon\00"
@.str.6 = [40 x i8] c"couldn't close /proc/meminfo in mem_mon\00"
@.str.7 = [12 x i8] c"mem:%3.0f%%\00"
@.str.8 = [27 x i8] c"snprintf in mem_mon failed\00"
define i8* @mem_mon(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [4 x i64]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca double
%9 = alloca [20 x i8]
%10 = alloca i32
store i8* %0, i8** %2
%11 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = load i32*, i32** %3
%18 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%19 = getelementptr inbounds i64, i64* %18, i64 0
%20 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%21 = getelementptr inbounds i64, i64* %20, i64 1
%22 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%23 = getelementptr inbounds i64, i64* %22, i64 2
%24 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%25 = getelementptr inbounds i64, i64* %24, i64 3
%26 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i64* %19, i64* %21, i64* %23, i64* %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 4
br i1 %28, label %29, label %38
29: ; preds = %16
%30 = load i32*, i32** %3
%31 = call i64 @ferror(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %37
35: ; preds = %29
%36 = call i32 @die_sans_errno(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
br label %38
38: ; preds = %37, %16
%39 = load i32*, i32** %3
%40 = call i64 @fclose(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
store i64 0, i64* %7
store i32 1, i32* %6
br label %45
45: ; preds = %55, %44
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 4
br i1 %47, label %48, label %58
48: ; preds = %45
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 %50
%52 = load i64, i64* %51
%53 = load i64, i64* %7
%54 = add i64 %53, %52
store i64 %54, i64* %7
br label %55
55: ; preds = %48
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %45
58: ; preds = %45
%59 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%60 = load i64, i64* %59
%61 = load i64, i64* %7
%62 = sub i64 %60, %61
%63 = uitofp i64 %62 to double
%64 = getelementptr inbounds [4 x i64], [4 x i64]* %4, i64 0, i64 0
%65 = load i64, i64* %64
%66 = uitofp i64 %65 to double
%67 = fdiv double %63, %66
%68 = fmul double %67, 1.000000e+02
store double %68, double* %8
%69 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%70 = load double, double* %8
%71 = call i32 @snprintf(i8* %69, i32 20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), double %70)
store i32 %71, i32* %10
%72 = load i32, i32* %10
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %58
%75 = call i32 @die_sans_errno(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
br label %76
76: ; preds = %74, %58
%77 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%78 = load i8*, i8** %2
%79 = bitcast i8* %78 to i32*
%80 = call i32 @multithread_write(i8* %77, i32* %79)
ret i8* null
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/meminfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [39 x i8] c"couldn't open /proc/meminfo in mem_mon\00"
@.str.3 = [65 x i8] c"MemTotal: %lu kB\0AMemFree: %lu kB\0ABuffers: %lu kB\0ACached: %lu kB\0A\00"
@.str.4 = [42 x i8] c"fscanf on /proc/meminfo in mem_mon failed\00"
@.str.5 = [54 x i8] c"matching error for fscanf on /proc/meminfo in mem_mon\00"
@.str.6 = [40 x i8] c"couldn't close /proc/meminfo in mem_mon\00"
@.str.7 = [12 x i8] c"mem:%3.0f%%\00"
@.str.8 = [27 x i8] c"snprintf in mem_mon failed\00"
define noalias i8* @mem_mon(i8* %0) {
%2 = alloca [4 x i64]
%3 = alloca [20 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = bitcast [4 x i64]* %2 to i8*
%10 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 0
%11 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 1
%12 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 2
%13 = getelementptr inbounds [4 x i64], [4 x i64]* %2, i64 0, i64 3
%14 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i64* %10, i64* %11, i64* %12, i64* %13)
%15 = icmp eq i32 %14, 4
br i1 %15, label %23, label %16
16: ; preds = %8
%17 = call i64 @ferror(i32* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %23
21: ; preds = %16
%22 = call i32 @die_sans_errno(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %19, %21, %8
%24 = call i64 @fclose(i32* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i64, i64* %11
%30 = load i64, i64* %12
%31 = add i64 %30, %29
%32 = load i64, i64* %13
%33 = add i64 %32, %31
%34 = load i64, i64* %10
%35 = sub i64 %34, %33
%36 = uitofp i64 %35 to double
%37 = uitofp i64 %34 to double
%38 = fdiv double %36, %37
%39 = fmul double %38, 1.000000e+02
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%41 = call i32 @snprintf(i8* %40, i32 20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), double %39)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %28
%44 = call i32 @die_sans_errno(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
br label %45
45: ; preds = %43, %28
%46 = bitcast i8* %0 to i32*
%47 = call i32 @multithread_write(i8* %40, i32* %46)
ret i8* null
}