#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ perror(char *);
int strlen(char *);
int cache_size() {
  const char *cache_size_path =
      "/sys/devices/system/cpu/cpu0/cache/index3/size";
  FILE *cache_size_fd;
  if (!(cache_size_fd = fopen(cache_size_path, "r"))) {
    perror("could not open cache size file");
    return -1;
  }
  char line[512];
  if (!fgets(line, 512, cache_size_fd)) {
    fclose(cache_size_fd);
    perror("could not read from cache size file");
    return -1;
  }
  const int newline_pos = strlen(line) - 1;
  if (line[newline_pos] == '\n') {
    line[newline_pos] = '\0';
  }
  int multiplier = 1;
  const int multiplier_pos = newline_pos - 1;
  switch (line[multiplier_pos]) {
  case 'K':
    multiplier = 1024;
    break;
  case 'M':
    multiplier = 1024 * 1024;
    break;
  case 'G':
    multiplier = 1024 * 1024 * 1024;
    break;
  }
  if (multiplier != 1) {
    line[multiplier_pos] = '\0';
  }
  int value = atoi(line);
  int cache_size = value * multiplier;
  fclose(cache_size_fd);
  return cache_size;
}


x86 assembly (clang, -O0):
<cache_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
lea    0xe8e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ac <cache_size+0x4c>
lea    0xe96(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <cache_size+0x1a6>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    11eb <cache_size+0x8b>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1306 <cache_size+0x1a6>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x224(%rbp)
movslq -0x224(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1227 <cache_size+0xc7>
movslq -0x224(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
movl   $0x1,-0x228(%rbp)
mov    -0x224(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x22c(%rbp)
movslq -0x22c(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
mov    %eax,-0x238(%rbp)
sub    $0x47,%eax
je     12a9 <cache_size+0x149>
jmp    1263 <cache_size+0x103>
mov    -0x238(%rbp),%eax
sub    $0x4b,%eax
je     128b <cache_size+0x12b>
jmp    1277 <cache_size+0x117>
mov    -0x238(%rbp),%eax
sub    $0x4d,%eax
je     129a <cache_size+0x13a>
jmp    12b3 <cache_size+0x153>
movl   $0x400,-0x228(%rbp)
jmp    12b3 <cache_size+0x153>
movl   $0x100000,-0x228(%rbp)
jmp    12b3 <cache_size+0x153>
movl   $0x40000000,-0x228(%rbp)
cmpl   $0x1,-0x228(%rbp)
je     12cf <cache_size+0x16f>
movslq -0x22c(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x230(%rbp)
mov    -0x230(%rbp),%eax
imul   -0x228(%rbp),%eax
mov    %eax,-0x234(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
lea    0xe8e(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     120b <cache_size+0xab>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     1214 <cache_size+0xb4>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11b7 <cache_size+0x57>
movb   $0x0,-0x1(%rsp,%rax,1)
movsbl -0x2(%rax,%rbx,1),%ecx
cmp    $0x47,%ecx
je     11d7 <cache_size+0x77>
cmp    $0x4b,%ecx
je     11de <cache_size+0x7e>
mov    $0x1,%ebp
cmp    $0x4d,%ecx
jne    11e8 <cache_size+0x88>
mov    $0x100000,%ebp
jmp    11e3 <cache_size+0x83>
mov    $0x40000000,%ebp
jmp    11e3 <cache_size+0x83>
mov    $0x400,%ebp
movb   $0x0,-0x2(%rsp,%rax,1)
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
imul   %ebp,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
jmp    1223 <cache_size+0xc3>
mov    %r14,%rdi
call   1030 <fclose@plt>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11fd <cache_size+0x9d>

LLVM IR (-O0):
@.str = [47 x i8] c"/sys/devices/system/cpu/cpu0/cache/index3/size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"could not open cache size file\00"
@.str.3 = [36 x i8] c"could not read from cache size file\00"
define i32 @cache_size() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [512 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8** %2
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %3
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
15: ; preds = %0
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%17 = load i32*, i32** %3
%18 = call i32 @fgets(i8* %16, i32 512, i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32*, i32** %3
%22 = call i32 @fclose(i32* %21)
%23 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %65
24: ; preds = %15
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = sub nsw i32 %26, 1
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %34, %24
store i32 1, i32* %6
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 1
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
switch i32 %45, label %49 [
i32 75, label %46
i32 77, label %47
i32 71, label %48
]
46: ; preds = %38
store i32 1024, i32* %6
br label %49
47: ; preds = %38
store i32 1048576, i32* %6
br label %49
48: ; preds = %38
store i32 1073741824, i32* %6
br label %49
49: ; preds = %38, %48, %47, %46
%50 = load i32, i32* %6
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %54
store i8 0, i8* %55
br label %56
56: ; preds = %52, %49
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%58 = call i32 @atoi(i8* %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = load i32, i32* %6
%61 = mul nsw i32 %59, %60
store i32 %61, i32* %9
%62 = load i32*, i32** %3
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %9
store i32 %64, i32* %1
br label %65
65: ; preds = %56, %20, %13
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [47 x i8] c"/sys/devices/system/cpu/cpu0/cache/index3/size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"could not open cache size file\00"
@.str.3 = [36 x i8] c"could not read from cache size file\00"
define i32 @cache_size() {
%1 = alloca [512 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([47 x i8], [47 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 %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %38
6: ; preds = %0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%8 = call i32 @fgets(i8* %7, i32 512, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = call i32 @fclose(i32* %2)
%12 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %36
13: ; preds = %6
%14 = call i32 @strlen(i8* %7)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %21
20: ; preds = %13
store i8 0, i8* %17
br label %21
21: ; preds = %20, %13
%22 = add nsw i32 %14, -2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
switch i32 %26, label %31 [
i32 75, label %29
i32 77, label %27
i32 71, label %28
]
27: ; preds = %21
br label %29
28: ; preds = %21
br label %29
29: ; preds = %21, %28, %27
%30 = phi i32 [ 1048576, %27 ], [ 1073741824, %28 ], [ 1024, %21 ]
store i8 0, i8* %24
br label %31
31: ; preds = %21, %29
%32 = phi i32 [ %30, %29 ], [ 1, %21 ]
%33 = call i32 @atoi(i8* %7)
%34 = mul nsw i32 %33, %32
%35 = call i32 @fclose(i32* %2)
br label %36
36: ; preds = %31, %10
%37 = phi i32 [ %34, %31 ], [ -1, %10 ]
br label %38
38: ; preds = %36, %4
%39 = phi i32 [ %37, %36 ], [ -1, %4 ]
ret i32 %39
}