#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);
int generic_cache_level(int, int);
char *strstr(char *, char *);
int cache_level(int cpu, int id) {
  char tmp[512];
  char *beg, *end;
  cache_info(cpu, id, tmp, sizeof(tmp));
  beg = strstr(tmp, "Level");
  if (beg == NULL)
    return generic_cache_level(cpu, id);
  else
    beg += 6;
  end = strstr(beg, " ");
  if (end != NULL)
    *end = '\0';
  return atoi(beg);
}


x86 assembly (clang, -O0):
<cache_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%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    11a3 <cache_level+0x63>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <generic_cache_level@plt>
mov    %eax,-0x4(%rbp)
jmp    11f6 <cache_level+0xb6>
mov    -0x218(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     11e7 <cache_level+0xa7>
mov    -0x220(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x218(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cache_level>:
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>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strstr@plt>
test   %rax,%rax
je     11aa <cache_level+0x5a>
mov    %rax,%rbx
add    $0x6,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a0 <cache_level+0x50>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1060 <atoi@plt>
jmp    11b4 <cache_level+0x64>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <generic_cache_level@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"Level\00"
@.str.1 = [2 x i8] c" \00"
define i32 @cache_level(i32 %0, i32 %1) {
%3 = alloca i32
%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 ([6 x i8], [6 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 i32 @generic_cache_level(i32 %18, i32 %19)
store i32 %20, i32* %3
br label %34
21: ; preds = %2
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 6
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.1, 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 i32 @atoi(i8* %32)
store i32 %33, i32* %3
br label %34
34: ; preds = %31, %17
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [6 x i8] c"Level\00"
define i32 @cache_level(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* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @generic_cache_level(i32 %0, i32 %1)
br label %17
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %6, i64 6
%12 = call i8* @strchr(i8* %11, i32 32)
%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 i32 @atoi(i8* %11)
br label %17
17: ; preds = %15, %8
%18 = phi i32 [ %9, %8 ], [ %16, %15 ]
ret i32 %18
}