#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 */
struct stat {
  int st_size;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ path;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ is_subcore_capable();
int num_subcores();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int get_cpu_info(int *_threads_per_cpu, int *_cpus_in_system,
                 int *_threads_in_system) {
  DIR *d;
  struct dirent *de;
  int first_cpu = 1;
  int rc;
  int subcores;
  int threads_in_system;
  int threads_per_cpu = 0;
  int cpus_in_system = 0;
  d = opendir("/proc/device-tree/cpus");
  if (!d)
    return -1;
  while ((de = readdir(d)) != NULL) {
    if (!strncmp(de->d_name, "PowerPC", 7)) {
      if (first_cpu) {
        struct stat sbuf;
        char path[PATH_MAX];
        snprintf(path, sizeof(path),
                 "/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s",
                 de->d_name);
        rc = stat(path, &sbuf);
        if (!rc)
          threads_per_cpu = sbuf.st_size / 4;
        first_cpu = 0;
      }
      cpus_in_system++;
    }
  }
  closedir(d);
  threads_in_system = cpus_in_system * threads_per_cpu;
  subcores = num_subcores();
  if (is_subcore_capable() && subcores > 0) {
    threads_per_cpu /= subcores;
    cpus_in_system *= subcores;
  }
  *_threads_per_cpu = threads_per_cpu;
  *_threads_in_system = threads_in_system;
  *_cpus_in_system = cpus_in_system;
  return 0;
}


x86 assembly (clang, -O0):
<get_cpu_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
lea    0xe50(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d0 <get_cpu_info+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    130c <get_cpu_info+0x18c>
jmp    11d5 <get_cpu_info+0x55>
mov    -0x28(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     129c <get_cpu_info+0x11c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xe1d(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1297 <get_cpu_info+0x117>
cmpl   $0x0,-0x34(%rbp)
je     128e <get_cpu_info+0x10e>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x60(%rbp)
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xdcc(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x68(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1280 <get_cpu_info+0x100>
mov    -0x50(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11d5 <get_cpu_info+0x55>
mov    -0x28(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x48(%rbp),%eax
imul   -0x44(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1070 <num_subcores@plt>
mov    %eax,-0x3c(%rbp)
mov    $0x0,%al
call   10a0 <is_subcore_capable@plt>
cmp    $0x0,%rax
je     12ea <get_cpu_info+0x16a>
cmpl   $0x0,-0x3c(%rbp)
jle    12ea <get_cpu_info+0x16a>
mov    -0x3c(%rbp),%ecx
mov    -0x44(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
imul   -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_info>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe5f(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1291 <get_cpu_info+0x111>
mov    %rax,%r14
mov    %rbx,-0x50(%rbp)
mov    %r15,-0x58(%rbp)
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
mov    %r12,-0x38(%rbp)
je     1299 <get_cpu_info+0x119>
mov    %rax,%r15
xor    %r13d,%r13d
mov    $0x1,%r12d
lea    0xe35(%rip),%rbx
movl   $0x0,-0x2c(%rbp)
jmp    1215 <get_cpu_info+0x95>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rsp
mov    %r13,%rbx
mov    -0x44(%rbp),%r13d
add    $0x1,%r13d
xor    %r12d,%r12d
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12a3 <get_cpu_info+0x123>
mov    (%r15),%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1201 <get_cpu_info+0x81>
test   %r12d,%r12d
je     11fa <get_cpu_info+0x7a>
mov    %r13d,-0x44(%rbp)
mov    %rbx,%r13
mov    %rsp,%rbx
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r12
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%r15),%rcx
mov    %r12,%rdi
lea    0xdbd(%rip),%rdx
call   1050 <snprintf@plt>
mov    %r12,%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
test   %eax,%eax
jne    11f0 <get_cpu_info+0x70>
mov    -0x40(%rbp),%eax
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x2,%ecx
mov    %ecx,-0x2c(%rbp)
jmp    11f0 <get_cpu_info+0x70>
mov    $0xffffffff,%r14d
jmp    12f6 <get_cpu_info+0x176>
movl   $0x0,-0x2c(%rbp)
xor    %r13d,%r13d
mov    %r14,%rdi
call   1060 <closedir@plt>
mov    %r13d,%r15d
imul   -0x2c(%rbp),%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <num_subcores@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <is_subcore_capable@plt>
test   %rax,%rax
je     12df <get_cpu_info+0x15f>
test   %ebx,%ebx
jle    12df <get_cpu_info+0x15f>
mov    -0x2c(%rbp),%eax
cltd
idiv   %ebx
imul   %ebx,%r13d
mov    -0x38(%rbp),%rdx
jmp    12e6 <get_cpu_info+0x166>
mov    -0x38(%rbp),%rdx
mov    -0x2c(%rbp),%eax
mov    -0x50(%rbp),%rcx
mov    %eax,(%rcx)
mov    -0x58(%rbp),%rcx
mov    %r15d,(%rcx)
mov    %r13d,(%rdx)
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [23 x i8] c"/proc/device-tree/cpus\00"
@.str.1 = [8 x i8] c"PowerPC\00"
@PATH_MAX = global i32 0
@.str.2 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
define i32 @get_cpu_info(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.stat
%17 = alloca i8*
%18 = alloca i64
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 1, i32* %10
store i32 0, i32* %14
store i32 0, i32* %15
%19 = call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %3
store i32 -1, i32* %4
br label %86
23: ; preds = %3
br label %24
24: ; preds = %59, %23
%25 = load i32*, i32** %8
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %9
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %60
28: ; preds = %24
%29 = load %struct.dirent*, %struct.dirent** %9
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @strncmp(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%33 = icmp ne i32 %32, 0
br i1 %33, label %59, label %34
34: ; preds = %28
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %56
37: ; preds = %34
%38 = load i32, i32* @PATH_MAX
%39 = zext i32 %38 to i64
%40 = call i8* @llvm.stacksave()
store i8* %40, i8** %17
%41 = alloca i8, i64 %39
store i64 %39, i64* %18
%42 = trunc i64 %39 to i32
%43 = load %struct.dirent*, %struct.dirent** %9
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 @snprintf(i8* %41, i32 %42, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @stat(i8* %41, %struct.stat* %16)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %37
%51 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 0
%52 = load i32, i32* %51
%53 = sdiv i32 %52, 4
store i32 %53, i32* %14
br label %54
54: ; preds = %50, %37
store i32 0, i32* %10
%55 = load i8*, i8** %17
br label %56
56: ; preds = %54, %34
%57 = load i32, i32* %15
%58 = add nsw i32 %57, 1
store i32 %58, i32* %15
br label %59
59: ; preds = %56, %28
br label %24
60: ; preds = %24
%61 = load i32*, i32** %8
%62 = call i32 @closedir(i32* %61)
%63 = load i32, i32* %15
%64 = load i32, i32* %14
%65 = mul nsw i32 %63, %64
store i32 %65, i32* %13
%66 = call i32 (...) @num_subcores()
store i32 %66, i32* %12
%67 = call i64 (...) @is_subcore_capable()
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %79
69: ; preds = %60
%70 = load i32, i32* %12
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %69
%73 = load i32, i32* %12
%74 = load i32, i32* %14
%75 = sdiv i32 %74, %73
store i32 %75, i32* %14
%76 = load i32, i32* %12
%77 = load i32, i32* %15
%78 = mul nsw i32 %77, %76
store i32 %78, i32* %15
br label %79
79: ; preds = %72, %69, %60
%80 = load i32, i32* %14
%81 = load i32*, i32** %5
store i32 %80, i32* %81
%82 = load i32, i32* %13
%83 = load i32*, i32** %7
store i32 %82, i32* %83
%84 = load i32, i32* %15
%85 = load i32*, i32** %6
store i32 %84, i32* %85
store i32 0, i32* %4
br label %86
86: ; preds = %79, %22
%87 = load i32, i32* %4
ret i32 %87
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [23 x i8] c"/proc/device-tree/cpus\00"
@.str.1 = [8 x i8] c"PowerPC\00"
@PATH_MAX = global i32 0
@.str.2 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
define i32 @get_cpu_info(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %63, label %7
7: ; preds = %3
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %47, label %10
10: ; preds = %7
%11 = bitcast %struct.stat* %4 to i8*
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %13
13: ; preds = %10, %41
%14 = phi %struct.dirent* [ %8, %10 ], [ %45, %41 ]
%15 = phi i32 [ 1, %10 ], [ %44, %41 ]
%16 = phi i32 [ 0, %10 ], [ %43, %41 ]
%17 = phi i32 [ 0, %10 ], [ %42, %41 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %13
%23 = icmp eq i32 %15, 0
br i1 %23, label %38, label %24
24: ; preds = %22
%25 = load i32, i32* @PATH_MAX
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
%28 = alloca i8, i64 %26
%29 = load i8*, i8** %18
%30 = call i32 @snprintf(i8* %28, i32 %25, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @stat(i8* %28, %struct.stat* %4)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = load i32, i32* %12
%35 = sdiv i32 %34, 4
br label %36
36: ; preds = %33, %24
%37 = phi i32 [ %17, %24 ], [ %35, %33 ]
br label %38
38: ; preds = %36, %22
%39 = phi i32 [ %37, %36 ], [ %17, %22 ]
%40 = add nsw i32 %16, 1
br label %41
41: ; preds = %38, %13
%42 = phi i32 [ %17, %13 ], [ %39, %38 ]
%43 = phi i32 [ %16, %13 ], [ %40, %38 ]
%44 = phi i32 [ %15, %13 ], [ 0, %38 ]
%45 = call %struct.dirent* @readdir(i32* %5)
%46 = icmp eq %struct.dirent* %45, null
br i1 %46, label %47, label %13
47: ; preds = %41, %7
%48 = phi i32 [ 0, %7 ], [ %42, %41 ]
%49 = phi i32 [ 0, %7 ], [ %43, %41 ]
%50 = call i32 @closedir(i32* %5)
%51 = mul nsw i32 %49, %48
%52 = call i32 (...) @num_subcores()
%53 = call i64 (...) @is_subcore_capable()
%54 = icmp ne i64 %53, 0
%55 = icmp sgt i32 %52, 0
%56 = select i1 %54, i1 %55, i1 false
br i1 %56, label %57, label %60
57: ; preds = %47
%58 = sdiv i32 %48, %52
%59 = mul nsw i32 %52, %49
br label %60
60: ; preds = %57, %47
%61 = phi i32 [ %58, %57 ], [ %48, %47 ]
%62 = phi i32 [ %59, %57 ], [ %49, %47 ]
store i32 %61, i32* %0
store i32 %51, i32* %2
store i32 %62, i32* %1
br label %63
63: ; preds = %3, %60
%64 = phi i32 [ 0, %60 ], [ -1, %3 ]
ret i32 %64
}