#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 /*<<< orphan*/ generic_cache_info(int, int, char *, int);
char *strstr(char *, char *);
int generic_cache_shared(int cpu, int id) {
  char tmp[512];
  char *beg, *end;
  int num = 0;
  generic_cache_info(cpu, id, tmp, sizeof(tmp));
  beg = strstr(tmp, ",") + 1;
  if (beg == NULL)
    return -1;
  else
    beg++;
  end = strstr(beg, ",") + 1;
  if (end == NULL)
    return -1;
  else
    end++;
  beg = end;
  end = strstr(beg, ",") + 1;
  if (end == NULL)
    return -1;
  else
    end++;
  beg = end;
  end = strstr(beg, ",") + 1;
  if (end == NULL)
    return -1;
  else
    end++;
  beg = end;
  while (strstr(beg, "cpu") != NULL) {
    end = strstr(beg, "cpu");
    beg = end + 1;
    num++;
  }
  if (num != 0)
    return num;
  else
    return -1;
}


x86 assembly (clang, -O0):
<generic_cache_shared>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x224(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x210(%rbp),%rdx
mov    $0x200,%ecx
call   1030 <generic_cache_info@plt>
lea    -0x210(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1040 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    118a <generic_cache_shared+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <generic_cache_shared+0x203>
mov    -0x218(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1040 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    11d4 <generic_cache_shared+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <generic_cache_shared+0x203>
mov    -0x220(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1040 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    122c <generic_cache_shared+0x10c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <generic_cache_shared+0x203>
mov    -0x220(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   1040 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
jne    1284 <generic_cache_shared+0x164>
movl   $0xffffffff,-0x4(%rbp)
jmp    1323 <generic_cache_shared+0x203>
mov    -0x220(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    0xd50(%rip),%rsi
call   1040 <strstr@plt>
cmp    $0x0,%rax
je     1301 <generic_cache_shared+0x1e1>
mov    -0x218(%rbp),%rdi
lea    0xd33(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x220(%rbp)
mov    -0x220(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x218(%rbp)
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    12a4 <generic_cache_shared+0x184>
cmpl   $0x0,-0x224(%rbp)
je     131c <generic_cache_shared+0x1fc>
mov    -0x224(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1323 <generic_cache_shared+0x203>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generic_cache_shared>:
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdx
mov    $0x200,%ecx
call   1040 <generic_cache_info@plt>
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
lea    0x2(%rax),%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
lea    0x2(%rax),%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
lea    0x2(%rax),%rdi
mov    $0x2c,%esi
call   1030 <strchr@plt>
lea    0x2(%rax),%rdi
lea    0xe74(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11b9 <generic_cache_shared+0x89>
xor    %ebx,%ebx
lea    0xe61(%rip),%r14
nop
add    $0x1,%rax
add    $0x1,%ebx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    11a0 <generic_cache_shared+0x70>
jmp    11be <generic_cache_shared+0x8e>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
@.str.1 = [4 x i8] c"cpu\00"
define i32 @generic_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*
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = call i32 @generic_cache_info(i32 %10, i32 %11, i8* %12, i32 512)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%15 = call i8* @strstr(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %74
20: ; preds = %2
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %7
br label %23
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = getelementptr inbounds i8, i8* %25, i64 1
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 -1, i32* %3
br label %74
30: ; preds = %23
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %8
br label %33
33: ; preds = %30
%34 = load i8*, i8** %8
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = call i8* @strstr(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -1, i32* %3
br label %74
41: ; preds = %33
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %8
br label %44
44: ; preds = %41
%45 = load i8*, i8** %8
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = call i8* @strstr(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%48 = getelementptr inbounds i8, i8* %47, i64 1
store i8* %48, i8** %8
%49 = load i8*, i8** %8
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %52
51: ; preds = %44
store i32 -1, i32* %3
br label %74
52: ; preds = %44
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %8
br label %55
55: ; preds = %52
%56 = load i8*, i8** %8
store i8* %56, i8** %7
br label %57
57: ; preds = %61, %55
%58 = load i8*, i8** %7
%59 = call i8* @strstr(i8* %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i8*, i8** %7
%63 = call i8* @strstr(i8* %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %63, i8** %8
%64 = load i8*, i8** %8
%65 = getelementptr inbounds i8, i8* %64, i64 1
store i8* %65, i8** %7
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1
store i32 %67, i32* %9
br label %57
68: ; preds = %57
%69 = load i32, i32* %9
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = load i32, i32* %9
store i32 %72, i32* %3
br label %74
73: ; preds = %68
store i32 -1, i32* %3
br label %74
74: ; preds = %73, %71, %51, %40, %29, %19
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"cpu\00"
define i32 @generic_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 @generic_cache_info(i32 %0, i32 %1, i8* %4, i32 512)
%6 = call i8* @strchr(i8* %4, i32 44)
%7 = getelementptr inbounds i8, i8* %6, i64 2
%8 = call i8* @strchr(i8* %7, i32 44)
%9 = getelementptr inbounds i8, i8* %8, i64 2
%10 = call i8* @strchr(i8* %9, i32 44)
%11 = getelementptr inbounds i8, i8* %10, i64 2
%12 = call i8* @strchr(i8* %11, i32 44)
%13 = getelementptr inbounds i8, i8* %12, i64 2
%14 = call i8* @strstr(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %2, %16
%17 = phi i8* [ %21, %16 ], [ %14, %2 ]
%18 = phi i32 [ %20, %16 ], [ 0, %2 ]
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = add nuw nsw i32 %18, 1
%21 = call i8* @strstr(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %16
23: ; preds = %16, %2
%24 = phi i32 [ -1, %2 ], [ %20, %16 ]
ret i32 %24
}