#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*/ tmp;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ cache_info(int, int, char *, int);
unsigned long long generic_cache_size(int, int);
char *strstr(char *, char *);
unsigned long long cache_size(int cpu, int id) {
  char tmp[512];
  char *beg, *end;
  cache_info(cpu, id, tmp, sizeof(tmp));
  beg = strstr(tmp, ",");
  if (beg == NULL)
    return generic_cache_size(cpu, id);
  else
    beg += 2;
  end = strstr(beg, ",");
  if (end != NULL)
    *end = '\0';
  end = strstr(beg, "KiB");
  if (end != NULL) {
    end--;
    *end = '\0';
    return atoi(beg) * 1024;
  }
  end = strstr(beg, "MiB");
  if (end != NULL) {
    end--;
    *end = '\0';
    return atoi(beg) * 1024 * 1024;
  }
  return generic_cache_size(cpu, id);
}


x86 assembly (clang, -O0):
<cache_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
lea    -0x210(%rbp),%rdx
mov    $0x200,%ecx
call   1030 <cache_info@plt>
lea    -0x210(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    11a4 <cache_size+0x64>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <generic_cache_size@plt>
mov    %rax,-0x8(%rbp)
jmp    12b6 <cache_size+0x176>
mov    -0x218(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     11e8 <cache_size+0xa8>
mov    -0x220(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x218(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     1246 <cache_size+0x106>
mov    -0x220(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x218(%rbp),%rdi
call   1050 <atoi@plt>
shl    $0xa,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    12b6 <cache_size+0x176>
mov    -0x218(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     12a7 <cache_size+0x167>
mov    -0x220(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x218(%rbp),%rdi
call   1050 <atoi@plt>
shl    $0xa,%eax
shl    $0xa,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    12b6 <cache_size+0x176>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <generic_cache_size@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdx
mov    $0x200,%ecx
call   1030 <cache_info@plt>
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11ec <cache_size+0x9c>
mov    %rax,%rbx
add    $0x2,%rbx
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119e <cache_size+0x4e>
movb   $0x0,(%rax)
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c5 <cache_size+0x75>
movb   $0x0,-0x1(%rax)
mov    %rbx,%rdi
call   1060 <atoi@plt>
shl    $0xa,%eax
cltq
jmp    11f6 <cache_size+0xa6>
lea    0xe38(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11ec <cache_size+0x9c>
movb   $0x0,-0x1(%rax)
mov    %rbx,%rdi
call   1060 <atoi@plt>
shl    $0x14,%eax
cltq
jmp    11f6 <cache_size+0xa6>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <generic_cache_size@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
define i64 @cache_size(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%12 = call i32 @cache_info(i32 %9, i32 %10, i8* %11, i32 512)
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%14 = call i8* @strstr(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i64 @generic_cache_size(i32 %18, i32 %19)
store i64 %20, i64* %3
br label %62
21: ; preds = %2
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8* %23, i8** %7
br label %24
24: ; preds = %21
%25 = load i8*, i8** %7
%26 = call i8* @strstr(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i8*, i8** %8
store i8 0, i8* %30
br label %31
31: ; preds = %29, %24
%32 = load i8*, i8** %7
%33 = call i8* @strstr(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i32 -1
store i8* %38, i8** %8
%39 = load i8*, i8** %8
store i8 0, i8* %39
%40 = load i8*, i8** %7
%41 = call i32 @atoi(i8* %40)
%42 = mul nsw i32 %41, 1024
%43 = sext i32 %42 to i64
store i64 %43, i64* %3
br label %62
44: ; preds = %31
%45 = load i8*, i8** %7
%46 = call i8* @strstr(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %58
49: ; preds = %44
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i32 -1
store i8* %51, i8** %8
%52 = load i8*, i8** %8
store i8 0, i8* %52
%53 = load i8*, i8** %7
%54 = call i32 @atoi(i8* %53)
%55 = mul nsw i32 %54, 1024
%56 = mul nsw i32 %55, 1024
%57 = sext i32 %56 to i64
store i64 %57, i64* %3
br label %62
58: ; preds = %44
%59 = load i32, i32* %4
%60 = load i32, i32* %5
%61 = call i64 @generic_cache_size(i32 %59, i32 %60)
store i64 %61, i64* %3
br label %62
62: ; preds = %58, %49, %36, %17
%63 = load i64, i64* %3
ret i64 %63
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"KiB\00"
@.str.2 = [4 x i8] c"MiB\00"
define i64 @cache_size(i32 %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32 @cache_info(i32 %0, i32 %1, i8* %4, i32 512)
%6 = call i8* @strchr(i8* %4, i32 44)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i64 @generic_cache_size(i32 %0, i32 %1)
br label %33
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %6, i64 2
%12 = call i8* @strchr(i8* %11, i32 44)
%13 = icmp eq i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %10
store i8 0, i8* %12
br label %15
15: ; preds = %14, %10
%16 = call i8* @strstr(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %16, i64 -1
store i8 0, i8* %19
%20 = call i32 @atoi(i8* %11)
%21 = shl nsw i32 %20, 10
%22 = sext i32 %21 to i64
br label %33
23: ; preds = %15
%24 = call i8* @strstr(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %24, i64 -1
store i8 0, i8* %27
%28 = call i32 @atoi(i8* %11)
%29 = shl nsw i32 %28, 20
%30 = sext i32 %29 to i64
br label %33
31: ; preds = %23
%32 = call i64 @generic_cache_size(i32 %0, i32 %1)
br label %33
33: ; preds = %31, %26, %18, %8
%34 = phi i64 [ %9, %8 ], [ %22, %18 ], [ %30, %26 ], [ %32, %31 ]
ret i64 %34
}