#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ remove_all_chars(char *, char);
int cache_size_kb(void) {
  char line[512], buffer[32];
  int column;
  FILE *cpuinfo;
  if (!(cpuinfo =
            fopen("/sys/devices/system/cpu/cpu0/cache/index2/size", "r"))) {
    perror("/sys/devices/system/cpu/cpu0/cache/index2/size: fopen");
    return -1;
  }
  while (fgets(line, sizeof(line), cpuinfo)) {
    char *colStr;
    remove_all_chars(line, 'K');
    remove_all_chars(line, 'B');
    column = atoi(line);
    fclose(cpuinfo);
    return (int)column;
  }
  fclose(cpuinfo);
  return -1;
}


x86 assembly (clang, -O0):
<cache_size_kb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
lea    0xe8e(%rip),%rdi
lea    0xeb6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x240(%rbp)
cmp    $0x0,%rax
jne    11a7 <cache_size_kb+0x47>
lea    0xe9b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <cache_size_kb+0xcf>
jmp    11ac <cache_size_kb+0x4c>
lea    -0x210(%rbp),%rdi
mov    -0x240(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     121c <cache_size_kb+0xbc>
lea    -0x210(%rbp),%rdi
mov    $0x4b,%esi
call   1080 <remove_all_chars@plt>
lea    -0x210(%rbp),%rdi
mov    $0x42,%esi
call   1080 <remove_all_chars@plt>
lea    -0x210(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x234(%rbp)
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x234(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <cache_size_kb+0xcf>
mov    -0x240(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_size_kb>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
lea    0xe90(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e3 <cache_size_kb+0x83>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11ca <cache_size_kb+0x6a>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x4b,%esi
call   1080 <remove_all_chars@plt>
mov    %rbp,%rdi
mov    $0x42,%esi
call   1080 <remove_all_chars@plt>
mov    %rbp,%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11d7 <cache_size_kb+0x77>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe47(%rip),%rdi
call   1060 <perror@plt>
jmp    11d2 <cache_size_kb+0x72>

LLVM IR (-O0):
@.str = [47 x i8] c"/sys/devices/system/cpu/cpu0/cache/index2/size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [54 x i8] c"/sys/devices/system/cpu/cpu0/cache/index2/size: fopen\00"
define i32 @cache_size_kb() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca [32 x i8]
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = 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))
store i32* %7, i32** %5
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
11: ; preds = %0
br label %12
12: ; preds = %11
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %5
%15 = call i64 @fgets(i8* %13, i32 512, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%19 = call i32 @remove_all_chars(i8* %18, i8 signext 75)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%21 = call i32 @remove_all_chars(i8* %20, i8 signext 66)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %4
%24 = load i32*, i32** %5
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %4
store i32 %26, i32* %1
br label %30
27: ; preds = %12
%28 = load i32*, i32** %5
%29 = call i32 @fclose(i32* %28)
store i32 -1, i32* %1
br label %30
30: ; preds = %27, %17, %9
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [47 x i8] c"/sys/devices/system/cpu/cpu0/cache/index2/size\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [54 x i8] c"/sys/devices/system/cpu/cpu0/cache/index2/size: fopen\00"
define i32 @cache_size_kb() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = 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))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
br label %17
7: ; preds = %0
%8 = call i64 @fgets(i8* %2, i32 512, i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = call i32 @remove_all_chars(i8* %2, i8 signext 75)
%12 = call i32 @remove_all_chars(i8* %2, i8 signext 66)
%13 = call i32 @atoi(i8* %2)
%14 = call i32 @fclose(i32* %3)
br label %17
15: ; preds = %7
%16 = call i32 @fclose(i32* %3)
br label %17
17: ; preds = %15, %10, %5
%18 = phi i32 [ %13, %10 ], [ -1, %15 ], [ -1, %5 ]
ret i32 %18
}