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


x86 assembly (clang, -O0):
<cache_shared>:
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    11d7 <cache_shared+0x97>
lea    -0x210(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     11c4 <cache_shared+0x84>
movl   $0x1,-0x4(%rbp)
jmp    1238 <cache_shared+0xf8>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <generic_cache_shared@plt>
mov    %eax,-0x4(%rbp)
jmp    1238 <cache_shared+0xf8>
mov    -0x218(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     1229 <cache_shared+0xe9>
mov    -0x220(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x220(%rbp)
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_shared>:
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    0xe99(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     119c <cache_shared+0x5c>
mov    %rax,%rbx
add    $0x6,%rbx
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     1192 <cache_shared+0x52>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1050 <atoi@plt>
jmp    11c2 <cache_shared+0x82>
lea    0xe63(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strstr@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11c2 <cache_shared+0x82>
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <generic_cache_shared@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"among\00"
@.str.1 = [8 x i8] c"per cpu\00"
@.str.2 = [5 x i8] c"cpus\00"
define i32 @cache_shared(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 %27
17: ; preds = %2
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 1, i32* %3
br label %40
23: ; preds = %17
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 @generic_cache_shared(i32 %24, i32 %25)
store i32 %26, i32* %3
br label %40
27: ; preds = %2
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i64 6
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = call i8* @strstr(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %8
store i8 0, i8* %35
br label %37
37: ; preds = %34, %27
%38 = load i8*, i8** %7
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %37, %23, %22
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [6 x i8] c"among\00"
@.str.1 = [8 x i8] c"per cpu\00"
@.str.2 = [5 x i8] c"cpus\00"
define i32 @cache_shared(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 %13
8: ; preds = %2
%9 = call i8* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = call i32 @generic_cache_shared(i32 %0, i32 %1)
br label %20
13: ; preds = %2
%14 = getelementptr inbounds i8, i8* %6, i64 6
%15 = call i8* @strstr(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %13
store i8 0, i8* %15
br label %18
18: ; preds = %17, %13
%19 = call i32 @atoi(i8* %14)
br label %20
20: ; preds = %8, %18, %11
%21 = phi i32 [ %12, %11 ], [ %19, %18 ], [ 1, %8 ]
ret i32 %21
}