#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*/ time_t;
struct tm {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
float cpu_load_percent();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
struct tm *localtime(int /*<<< orphan*/ *);
int mem_usage_percent();
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int swap_active();
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int start_logger(long seconds, char *file) {
  FILE *dest = NULL;
  dest = fopen(file, "a+");
  if (dest == NULL)
    return 1;
  char timestr[25];
  time_t now_raw;
  struct tm *now;
  time(&now_raw);
  now = localtime(&now_raw);
  strftime(timestr, 25, "%a. %x  %X", now);
  fprintf(dest, "<Systemmonitor Resource Logger>\n");
  fprintf(dest, "[%s] logger Started for %ld seconds\n", timestr, seconds);
  int done = 0;
  long long allc = 0;
  long long allm = 0;
  float avgc = 0.0;
  float avgm = 0.0;
  float c;
  int m;
  while (done < seconds) {
    time(&now_raw);
    now = localtime(&now_raw);
    strftime(timestr, 25, "%a. %x  %X", now);
    m = mem_usage_percent();
    c = cpu_load_percent();
    fprintf(dest, "[%s] CPU: %7.4f %%\t\t|\tMEM: %3d %%\t\t|\tSWAP: %s\n",
            timestr, c, m, swap_active() == true ? "TRUE" : "FALSE");
    allm += m;
    allc += (int)c;
    done++;
  }
  avgm = (float)allm / seconds;
  avgc = (float)allc / seconds;
  fprintf(dest,
          "\n\n---Log Results---\n\naverage memory usage: %.2f %%\naverage CPU "
          "usage: %.2f %%\n\n-----------------\n",
          avgm, avgc);
  fprintf(dest, "[%s], logger stopped.\n", timestr);
  fclose(dest);
  return 0;
}


x86 assembly (clang, -O0):
<start_logger>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <start_logger+0x46>
movl   $0x1,-0x4(%rbp)
jmp    13ba <start_logger+0x22a>
lea    -0x44(%rbp),%rdi
call   1060 <time@plt>
lea    -0x44(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x19,%esi
lea    0xe03(%rip),%rdx
call   1070 <strftime@plt>
mov    -0x20(%rbp),%rdi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x54(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x6c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x70(%rbp)
movslq -0x54(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    1346 <start_logger+0x1b6>
lea    -0x44(%rbp),%rdi
call   1060 <time@plt>
lea    -0x44(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x19,%esi
lea    0xd73(%rip),%rdx
call   1070 <strftime@plt>
mov    $0x0,%al
call   1090 <mem_usage_percent@plt>
mov    %eax,-0x78(%rbp)
mov    $0x0,%al
call   10b0 <cpu_load_percent@plt>
movss  %xmm0,-0x74(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x98(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x90(%rbp)
movss  -0x74(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
mov    -0x78(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    $0x0,%al
call   10a0 <swap_active@plt>
mov    -0x98(%rbp),%rdi
mov    -0x90(%rbp),%rdx
movsd  -0x88(%rbp),%xmm0
ff
mov    -0x7c(%rbp),%ecx
mov    %eax,%esi
lea    0xd87(%rip),%r8
lea    0xd7b(%rip),%rax
cmp    $0x1,%esi
cmove  %rax,%r8
lea    0xd3e(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
movslq -0x78(%rbp),%rax
add    -0x68(%rbp),%rax
mov    %rax,-0x68(%rbp)
cvttss2si -0x74(%rbp),%eax
cltq
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1258 <start_logger+0xc8>
cvtsi2ssq -0x68(%rbp),%xmm0
cvtsi2ssq -0x10(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x70(%rbp)
cvtsi2ssq -0x60(%rbp),%xmm0
cvtsi2ssq -0x10(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x6c(%rbp)
mov    -0x20(%rbp),%rdi
movss  -0x70(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x6c(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
lea    0xd01(%rip),%rsi
mov    $0x2,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rdx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_logger>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rax
mov    %rdi,%r13
lea    0xe45(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     130d <start_logger+0x16d>
mov    %rax,%rbp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <time@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
lea    0xe18(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x19,%esi
mov    %rax,%rcx
call   1070 <strftime@plt>
lea    0xe07(%rip),%rdi
mov    $0x20,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10c0 <fwrite@plt>
lea    0xe0f(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r13,%r13
jle    1314 <start_logger+0x174>
mov    %r13,0x10(%rsp)
xor    %r12d,%r12d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <time@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x19,%esi
lea    0xd8a(%rip),%rdx
mov    %rax,%rcx
call   1070 <strftime@plt>
xor    %eax,%eax
call   1090 <mem_usage_percent@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10b0 <cpu_load_percent@plt>
movss  %xmm0,0x8(%rsp)
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,0x18(%rsp)
xor    %eax,%eax
call   10a0 <swap_active@plt>
cmp    $0x1,%eax
lea    0xdd5(%rip),%r8
lea    0xdc9(%rip),%rax
cmove  %rax,%r8
mov    %rbp,%rdi
lea    0xd8c(%rip),%rsi
mov    %rbx,%rdx
movsd  0x18(%rsp),%xmm0
mov    %r14d,%ecx
mov    $0x1,%al
call   1050 <fprintf@plt>
movslq %r14d,%rax
cvttss2si 0x8(%rsp),%ecx
add    %rax,%r12
movslq %ecx,%rax
add    %rax,%r15
add    $0xffffffffffffffff,%r13
jne    1250 <start_logger+0xb0>
xorps  %xmm0,%xmm0
cvtsi2ss %r12,%xmm0
cvtsi2ss %r15,%xmm1
mov    0x10(%rsp),%r13
xor    %ebx,%ebx
jmp    131a <start_logger+0x17a>
mov    $0x1,%ebx
jmp    135e <start_logger+0x1be>
xorps  %xmm1,%xmm1
xorps  %xmm0,%xmm0
cvtsi2ss %r13,%xmm2
divss  %xmm2,%xmm0
divss  %xmm2,%xmm1
cvtss2sd %xmm0,%xmm0
cvtss2sd %xmm1,%xmm1
lea    0xd58(%rip),%rsi
mov    %rbp,%rdi
mov    $0x2,%al
call   1050 <fprintf@plt>
lea    0xda9(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [3 x i8] c"a+\00"
@.str.1 = [11 x i8] c"%a. %x %X\00"
@.str.2 = [33 x i8] c"<Systemmonitor Resource Logger>\0A\00"
@.str.3 = [37 x i8] c"[%s] logger Started for %ld seconds\0A\00"
@.str.4 = [47 x i8] c"[%s] CPU: %7.4f %%\09\09|\09MEM: %3d %%\09\09|\09SWAP: %s\0A\00"
@.str.5 = [5 x i8] c"TRUE\00"
@.str.6 = [6 x i8] c"FALSE\00"
@.str.7 = [98 x i8] c"\0A\0A---Log Results---\0A\0Aaverage memory usage: %.2f %%\0Aaverage CPU usage: %.2f %%\0A\0A-----------------\0A\00"
@.str.8 = [23 x i8] c"[%s], logger stopped.\0A\00"
define i32 @start_logger(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [25 x i8]
%8 = alloca i32
%9 = alloca %struct.tm*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca float
%14 = alloca float
%15 = alloca float
%16 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32* null, i32** %6
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 1, i32* %3
br label %90
22: ; preds = %2
%23 = call i32 @time(i32* %8)
%24 = call %struct.tm* @localtime(i32* %8)
store %struct.tm* %24, %struct.tm** %9
%25 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%26 = load %struct.tm*, %struct.tm** %9
%27 = call i32 @strftime(i8* %25, i32 25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %26)
%28 = load i32*, i32** %6
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32*, i32** %6
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%32 = load i64, i64* %4
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %31, i64 %32)
store i32 0, i32* %10
store i64 0, i64* %11
store i64 0, i64* %12
store float 0.000000e+00, float* %13
store float 0.000000e+00, float* %14
br label %34
34: ; preds = %39, %22
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = load i64, i64* %4
%38 = icmp slt i64 %36, %37
br i1 %38, label %39, label %68
39: ; preds = %34
%40 = call i32 @time(i32* %8)
%41 = call %struct.tm* @localtime(i32* %8)
store %struct.tm* %41, %struct.tm** %9
%42 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%43 = load %struct.tm*, %struct.tm** %9
%44 = call i32 @strftime(i8* %42, i32 25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %43)
%45 = call i32 (...) @mem_usage_percent()
store i32 %45, i32* %16
%46 = call float (...) @cpu_load_percent()
store float %46, float* %15
%47 = load i32*, i32** %6
%48 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%49 = load float, float* %15
%50 = fpext float %49 to double
%51 = load i32, i32* %16
%52 = call i32 (...) @swap_active()
%53 = icmp eq i32 %52, 1
%54 = zext i1 %53 to i64
%55 = select i1 %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0)
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %48, double %50, i32 %51, i8* %55)
%57 = load i32, i32* %16
%58 = sext i32 %57 to i64
%59 = load i64, i64* %12
%60 = add nsw i64 %59, %58
store i64 %60, i64* %12
%61 = load float, float* %15
%62 = fptosi float %61 to i32
%63 = sext i32 %62 to i64
%64 = load i64, i64* %11
%65 = add nsw i64 %64, %63
store i64 %65, i64* %11
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %34
68: ; preds = %34
%69 = load i64, i64* %12
%70 = sitofp i64 %69 to float
%71 = load i64, i64* %4
%72 = sitofp i64 %71 to float
%73 = fdiv float %70, %72
store float %73, float* %14
%74 = load i64, i64* %11
%75 = sitofp i64 %74 to float
%76 = load i64, i64* %4
%77 = sitofp i64 %76 to float
%78 = fdiv float %75, %77
store float %78, float* %13
%79 = load i32*, i32** %6
%80 = load float, float* %14
%81 = fpext float %80 to double
%82 = load float, float* %13
%83 = fpext float %82 to double
%84 = call i32 (i32*, i8*, ...) @fprintf(i32* %79, i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str.7, i64 0, i64 0), double %81, double %83)
%85 = load i32*, i32** %6
%86 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %85, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %86)
%88 = load i32*, i32** %6
%89 = call i32 @fclose(i32* %88)
store i32 0, i32* %3
br label %90
90: ; preds = %68, %21
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [3 x i8] c"a+\00"
@.str.1 = [11 x i8] c"%a. %x %X\00"
@.str.2 = [33 x i8] c"<Systemmonitor Resource Logger>\0A\00"
@.str.3 = [37 x i8] c"[%s] logger Started for %ld seconds\0A\00"
@.str.4 = [47 x i8] c"[%s] CPU: %7.4f %%\09\09|\09MEM: %3d %%\09\09|\09SWAP: %s\0A\00"
@.str.5 = [5 x i8] c"TRUE\00"
@.str.6 = [6 x i8] c"FALSE\00"
@.str.7 = [98 x i8] c"\0A\0A---Log Results---\0A\0Aaverage memory usage: %.2f %%\0Aaverage CPU usage: %.2f %%\0A\0A-----------------\0A\00"
@.str.8 = [23 x i8] c"[%s], logger stopped.\0A\00"
define i32 @start_logger(i64 %0, i8* nocapture %1) {
%3 = alloca [25 x i8]
%4 = alloca i32
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %51, label %7
7: ; preds = %2
%8 = getelementptr inbounds [25 x i8], [25 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = call i32 @time(i32* %4)
%11 = call %struct.tm* @localtime(i32* %4)
%12 = call i32 @strftime(i8* %8, i32 25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %11)
%13 = call i64 @fwrite(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 32, i64 1, i32* %5)
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %8, i64 %0)
%15 = icmp sgt i64 %0, 0
br i1 %15, label %16, label %40
16: ; preds = %7, %16
%17 = phi i64 [ %35, %16 ], [ 0, %7 ]
%18 = phi i64 [ %31, %16 ], [ 0, %7 ]
%19 = phi i64 [ %34, %16 ], [ 0, %7 ]
%20 = call i32 @time(i32* %4)
%21 = call %struct.tm* @localtime(i32* %4)
%22 = call i32 @strftime(i8* %8, i32 25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %21)
%23 = call i32 (...) @mem_usage_percent()
%24 = call float (...) @cpu_load_percent()
%25 = fpext float %24 to double
%26 = call i32 (...) @swap_active()
%27 = icmp eq i32 %26, 1
%28 = select i1 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0)
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %8, double %25, i32 %23, i8* %28)
%30 = sext i32 %23 to i64
%31 = add nsw i64 %18, %30
%32 = fptosi float %24 to i32
%33 = sext i32 %32 to i64
%34 = add nsw i64 %19, %33
%35 = add nuw nsw i64 %17, 1
%36 = icmp eq i64 %35, %0
br i1 %36, label %37, label %16
37: ; preds = %16
%38 = sitofp i64 %31 to float
%39 = sitofp i64 %34 to float
br label %40
40: ; preds = %37, %7
%41 = phi float [ 0.000000e+00, %7 ], [ %39, %37 ]
%42 = phi float [ 0.000000e+00, %7 ], [ %38, %37 ]
%43 = sitofp i64 %0 to float
%44 = fdiv float %42, %43
%45 = fdiv float %41, %43
%46 = fpext float %44 to double
%47 = fpext float %45 to double
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str.7, i64 0, i64 0), double %46, double %47)
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %8)
%50 = call i32 @fclose(i32* %5)
br label %51
51: ; preds = %2, %40
%52 = phi i32 [ 0, %40 ], [ 1, %2 ]
ret i32 %52
}