#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 {
  scalar_t__ tm_hour;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ check_for_new_date();
scalar_t__ 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*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  check_for_new_date();
  bool quiet = false;
  if (argc > 1)
    if (strcmp(argv[1], "-q") == 0)
      quiet = true;
  if (quiet == false)
    printf("This tool is designed to grab data for the graphs in systemmonitor "
           "every hour. Normaly it is started by cron.\n");
  int cpu = (int)(cpu_load_percent() + 0.5);
  int mem = mem_usage_percent();
  time_t rawtime;
  time(&rawtime);
  struct tm *timen;
  timen = localtime(&rawtime);
  int hour = (int)timen->tm_hour;
  char path[100];
  sprintf(path, "%s%d", "/var/info/systemmonitor/stats/", hour);
  FILE *output = fopen(path, "w");
  if (output == NULL) {
    fprintf(stderr,
            "Systemmonitor_Data_Grabber: FATAL ERROR: FILE_NOT_FOUND\n");
    return -1;
  } else {
    fprintf(output, "%d\n%d", cpu, mem);
    fclose(output);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <check_for_new_date@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    120b <main+0x5b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe18(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1206 <main+0x56>
movl   $0x1,-0x14(%rbp)
jmp    120b <main+0x5b>
cmpl   $0x0,-0x14(%rbp)
jne    1221 <main+0x71>
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <cpu_load_percent@plt>
cvtsi2sd %rax,%xmm0
movsd  0xdcb(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <mem_usage_percent@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <time@plt>
lea    -0x20(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x2c(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
lea    0xdff(%rip),%rsi
lea    0xdfd(%rip),%rdx
call   10d0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    12d3 <main+0x123>
mov    0x2d2a(%rip),%rax
movslq (%rax),%rdi
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <main+0x151>
mov    -0x98(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x98(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10b0 <check_for_new_date@plt>
cmp    $0x2,%ebp
jl     11f0 <main+0x30>
mov    0x8(%rbx),%rdi
lea    0xe22(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11fc <main+0x3c>
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <cpu_load_percent@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10a0 <mem_usage_percent@plt>
mov    %eax,%r14d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <time@plt>
mov    %rbp,%rdi
call   1030 <localtime@plt>
mov    (%rax),%ecx
lea    0xddb(%rip),%rsi
lea    0xdd9(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10d0 <sprintf@plt>
lea    0xde4(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     128a <main+0xca>
mov    %rax,%rbp
cvtsi2sd %r15,%xmm0
addsd  0xd98(%rip),%xmm0
cvttsd2si %xmm0,%edx
lea    0xdf7(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    12af <main+0xef>
mov    0x2d4f(%rip),%rax
movslq (%rax),%rcx
lea    0xd96(%rip),%rdi
mov    $0x38,%esi
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i64 }
@.str = [3 x i8] c"-q\00"
@.str.1 = [111 x i8] c"This tool is designed to grab data for the graphs in systemmonitor every hour. Normaly it is started by cron.\0A\00"
@.str.2 = [5 x i8] c"%s%d\00"
@.str.3 = [31 x i8] c"/var/info/systemmonitor/stats/\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [57 x i8] c"Systemmonitor_Data_Grabber: FATAL ERROR: FILE_NOT_FOUND\0A\00"
@.str.6 = [6 x i8] c"%d\0A%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.tm*
%11 = alloca i32
%12 = alloca [100 x i8]
%13 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 (...) @check_for_new_date()
store i32 0, i32* %6
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %25
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 1, i32* %6
br label %24
24: ; preds = %23, %17
br label %25
25: ; preds = %24, %2
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([111 x i8], [111 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i64 (...) @cpu_load_percent()
%32 = sitofp i64 %31 to double
%33 = fadd double %32, 5.000000e-01
%34 = fptosi double %33 to i32
store i32 %34, i32* %7
%35 = call i32 (...) @mem_usage_percent()
store i32 %35, i32* %8
%36 = call i32 @time(i32* %9)
%37 = call %struct.tm* @localtime(i32* %9)
store %struct.tm* %37, %struct.tm** %10
%38 = load %struct.tm*, %struct.tm** %10
%39 = getelementptr inbounds %struct.tm, %struct.tm* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %11
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%43 = load i32, i32* %11
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %46, i32** %13
%47 = load i32*, i32** %13
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %54
49: ; preds = %30
%50 = load i32, i32* @stderr
%51 = sext i32 %50 to i64
%52 = inttoptr i64 %51 to i32*
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
54: ; preds = %30
%55 = load i32*, i32** %13
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %56, i32 %57)
%59 = load i32*, i32** %13
%60 = call i32 @fclose(i32* %59)
store i32 0, i32* %3
br label %61
61: ; preds = %54, %49
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.tm = type { i64 }
@.str = [3 x i8] c"-q\00"
@.str.2 = [5 x i8] c"%s%d\00"
@.str.3 = [31 x i8] c"/var/info/systemmonitor/stats/\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [57 x i8] c"Systemmonitor_Data_Grabber: FATAL ERROR: FILE_NOT_FOUND\0A\00"
@.str.6 = [6 x i8] c"%d\0A%d\00"
@str = [110 x i8] c"This tool is designed to grab data for the graphs in systemmonitor every hour. Normaly it is started by cron.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = tail call i32 (...) @check_for_new_date()
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2, %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([110 x i8], [110 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = tail call i64 (...) @cpu_load_percent()
%16 = tail call i32 (...) @mem_usage_percent()
%17 = bitcast i32* %3 to i8*
%18 = call i32 @time(i32* %3)
%19 = call %struct.tm* @localtime(i32* %3)
%20 = getelementptr inbounds %struct.tm, %struct.tm* %19, i64 0, i32 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%24 = call i32 @sprintf(i8* %23, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%25 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %14
%28 = load i32, i32* @stderr
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i32*
%31 = call i64 @fwrite(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i64 56, i64 1, i32* %30)
br label %38
32: ; preds = %14
%33 = sitofp i64 %15 to double
%34 = fadd double %33, 5.000000e-01
%35 = fptosi double %34 to i32
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %35, i32 %16)
%37 = call i32 @fclose(i32* %25)
br label %38
38: ; preds = %32, %27
%39 = phi i32 [ -1, %27 ], [ 0, %32 ]
ret i32 %39
}