#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 maxcpus;
typedef char cpu_set_t;
typedef int /*<<< orphan*/ __cpu_mask;

/* Variables and functions */
int __NCPUBITS;
unsigned int cpumask_parse(char *, char *, size_t);
int /*<<< orphan*/ free(char *);
size_t get_processor_number_kernel_max();
int get_processor_number_online();
char *malloc(int);
char *sysfsparser_getline(char *, unsigned long);
void get_cputopology_read(unsigned int *nsockets, unsigned int *ncores,
                          unsigned int *nthreads) {
  cpu_set_t *set;
  size_t cpu, maxcpus, setsize = 0;
  *nsockets = *ncores = *nthreads = 1;
  maxcpus = get_processor_number_kernel_max();
  if (!(set = (malloc(((((maxcpus) + __NCPUBITS - 1) / __NCPUBITS) *
                       sizeof(__cpu_mask))))))
    return;
  setsize = ((((maxcpus) + __NCPUBITS - 1) / __NCPUBITS) * sizeof(__cpu_mask));
  for (cpu = 0; cpu < maxcpus; cpu++) {
    char *thread_siblings =
        sysfsparser_getline("/sys/devices/system"
                            "/cpu"
                            "/cpu%lu/topology/thread_siblings",
                            (unsigned long)cpu);
    if (!thread_siblings)
      continue;
    *nthreads = cpumask_parse(thread_siblings, set, setsize);
    if (*nthreads <= 0)
      *nthreads = 1;
    char *core_siblings = sysfsparser_getline("/sys/devices/system"
                                              "/cpu"
                                              "/cpu%lu/topology/core_siblings",
                                              (unsigned long)cpu);
    *ncores = cpumask_parse(core_siblings, set, setsize) / *nthreads;
    if (*ncores <= 0)
      *ncores = 1;
    int ncpus = get_processor_number_online();
    *nsockets = ncpus / *nthreads / *ncores;
    if (!*nsockets)
      *nsockets = 1;
    free(core_siblings);
    free(thread_siblings);
  }
  (free(set));
}


x86 assembly (clang, -O0):
<get_cputopology_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x8(%rbp),%rax
movl   $0x1,(%rax)
mov    $0x0,%al
call   1050 <get_processor_number_kernel_max@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e38(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    0x2e27(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e7 <get_cputopology_read+0x87>
jmp    1331 <get_cputopology_read+0x1d1>
mov    -0x30(%rbp),%rax
mov    0x2df6(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    0x2de5(%rip),%rcx
movslq (%rcx),%rcx
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1328 <get_cputopology_read+0x1c8>
mov    -0x28(%rbp),%rsi
lea    0xdcc(%rip),%rdi
call   1060 <sysfsparser_getline@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    124d <get_cputopology_read+0xed>
jmp    1317 <get_cputopology_read+0x1b7>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <cpumask_parse@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
ja     127d <get_cputopology_read+0x11d>
mov    -0x18(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x28(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1060 <sysfsparser_getline@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <cpumask_parse@plt>
mov    -0x18(%rbp),%rcx
xor    %edx,%edx
divl   (%rcx)
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
ja     12c9 <get_cputopology_read+0x169>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    $0x0,%al
call   1030 <get_processor_number_online@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0x18(%rbp),%rcx
xor    %edx,%edx
divl   (%rcx)
mov    -0x10(%rbp),%rcx
xor    %edx,%edx
divl   (%rcx)
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1305 <get_cputopology_read+0x1a5>
mov    -0x8(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x48(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    121b <get_cputopology_read+0xbb>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cputopology_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x10(%rsp)
movl   $0x1,(%rdx)
mov    %rsi,0x20(%rsp)
movl   $0x1,(%rsi)
mov    %rdi,0x18(%rsp)
movl   $0x1,(%rdi)
xor    %eax,%eax
call   1050 <get_processor_number_kernel_max@plt>
mov    0x2e4b(%rip),%rcx
movslq (%rcx),%rcx
mov    %rax,0x8(%rsp)
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     11c2 <get_cputopology_read+0x62>
xor    %edx,%edx
div    %rcx
mov    %rax,%r13
jmp    11c9 <get_cputopology_read+0x69>
xor    %edx,%edx
div    %ecx
mov    %eax,%r13d
lea    0x0(,%r13,4),%edi
call   1070 <malloc@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     12da <get_cputopology_read+0x17a>
cmpq   $0x0,0x8(%rsp)
je     12c3 <get_cputopology_read+0x163>
shl    $0x2,%r13
xor    %r15d,%r15d
mov    0x10(%rsp),%rbp
jmp    120f <get_cputopology_read+0xaf>
nopl   (%rax)
add    $0x1,%r15
cmp    %r15,0x8(%rsp)
je     12c3 <get_cputopology_read+0x163>
lea    0xdea(%rip),%rdi
mov    %r15,%rsi
call   1060 <sysfsparser_getline@plt>
test   %rax,%rax
je     1200 <get_cputopology_read+0xa0>
mov    %rax,%r12
mov    %rax,%rdi
mov    (%rsp),%rbx
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <cpumask_parse@plt>
test   %eax,%eax
mov    $0x1,%ecx
cmove  %ecx,%eax
mov    %eax,0x0(%rbp)
lea    0xdec(%rip),%rdi
mov    %r15,%rsi
call   1060 <sysfsparser_getline@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <cpumask_parse@plt>
mov    %eax,%ecx
mov    0x0(%rbp),%esi
xor    %edx,%edx
div    %esi
cmp    %ecx,%esi
mov    $0x1,%ecx
cmova  %ecx,%eax
mov    %r13,%rbx
mov    0x20(%rsp),%r13
mov    %eax,0x0(%r13)
xor    %eax,%eax
call   1030 <get_processor_number_online@plt>
xor    %edx,%edx
divl   0x0(%rbp)
mov    %eax,%ecx
mov    0x0(%r13),%esi
mov    %rbx,%r13
xor    %edx,%edx
div    %esi
cmp    %ecx,%esi
mov    $0x1,%ecx
cmova  %ecx,%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r12,%rdi
call   1040 <free@plt>
jmp    1200 <get_cputopology_read+0xa0>
mov    (%rsp),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__NCPUBITS = global i32 0
@.str = [56 x i8] c"/sys/devices/system/cpu/cpu%lu/topology/thread_siblings\00"
@.str.1 = [54 x i8] c"/sys/devices/system/cpu/cpu%lu/topology/core_siblings\00"
define void @get_cputopology_read(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i64 0, i64* %10
%14 = load i32*, i32** %6
store i32 1, i32* %14
%15 = load i32*, i32** %5
store i32 1, i32* %15
%16 = load i32*, i32** %4
store i32 1, i32* %16
%17 = call i64 (...) @get_processor_number_kernel_max()
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = load i32, i32* @__NCPUBITS
%20 = sext i32 %19 to i64
%21 = add i64 %18, %20
%22 = sub i64 %21, 1
%23 = load i32, i32* @__NCPUBITS
%24 = sext i32 %23 to i64
%25 = udiv i64 %22, %24
%26 = mul i64 %25, 4
%27 = trunc i64 %26 to i32
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %3
br label %104
31: ; preds = %3
%32 = load i64, i64* %9
%33 = load i32, i32* @__NCPUBITS
%34 = sext i32 %33 to i64
%35 = add i64 %32, %34
%36 = sub i64 %35, 1
%37 = load i32, i32* @__NCPUBITS
%38 = sext i32 %37 to i64
%39 = udiv i64 %36, %38
%40 = mul i64 %39, 4
store i64 %40, i64* %10
store i64 0, i64* %8
br label %41
41: ; preds = %98, %31
%42 = load i64, i64* %8
%43 = load i64, i64* %9
%44 = icmp ult i64 %42, %43
br i1 %44, label %45, label %101
45: ; preds = %41
%46 = load i64, i64* %8
%47 = call i8* @sysfsparser_getline(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i64 %46)
store i8* %47, i8** %11
%48 = load i8*, i8** %11
%49 = icmp ne i8* %48, null
br i1 %49, label %51, label %50
50: ; preds = %45
br label %98
51: ; preds = %45
%52 = load i8*, i8** %11
%53 = load i8*, i8** %7
%54 = load i64, i64* %10
%55 = call i32 @cpumask_parse(i8* %52, i8* %53, i64 %54)
%56 = load i32*, i32** %6
store i32 %55, i32* %56
%57 = load i32*, i32** %6
%58 = load i32, i32* %57
%59 = icmp ule i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %51
%61 = load i32*, i32** %6
store i32 1, i32* %61
br label %62
62: ; preds = %60, %51
%63 = load i64, i64* %8
%64 = call i8* @sysfsparser_getline(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i64 %63)
store i8* %64, i8** %12
%65 = load i8*, i8** %12
%66 = load i8*, i8** %7
%67 = load i64, i64* %10
%68 = call i32 @cpumask_parse(i8* %65, i8* %66, i64 %67)
%69 = load i32*, i32** %6
%70 = load i32, i32* %69
%71 = udiv i32 %68, %70
%72 = load i32*, i32** %5
store i32 %71, i32* %72
%73 = load i32*, i32** %5
%74 = load i32, i32* %73
%75 = icmp ule i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %62
%77 = load i32*, i32** %5
store i32 1, i32* %77
br label %78
78: ; preds = %76, %62
%79 = call i32 (...) @get_processor_number_online()
store i32 %79, i32* %13
%80 = load i32, i32* %13
%81 = load i32*, i32** %6
%82 = load i32, i32* %81
%83 = udiv i32 %80, %82
%84 = load i32*, i32** %5
%85 = load i32, i32* %84
%86 = udiv i32 %83, %85
%87 = load i32*, i32** %4
store i32 %86, i32* %87
%88 = load i32*, i32** %4
%89 = load i32, i32* %88
%90 = icmp ne i32 %89, 0
br i1 %90, label %93, label %91
91: ; preds = %78
%92 = load i32*, i32** %4
store i32 1, i32* %92
br label %93
93: ; preds = %91, %78
%94 = load i8*, i8** %12
%95 = call i32 @free(i8* %94)
%96 = load i8*, i8** %11
%97 = call i32 @free(i8* %96)
br label %98
98: ; preds = %93, %50
%99 = load i64, i64* %8
%100 = add i64 %99, 1
store i64 %100, i64* %8
br label %41
101: ; preds = %41
%102 = load i8*, i8** %7
%103 = call i32 @free(i8* %102)
br label %104
104: ; preds = %101, %30
ret void
}

LLVM IR (-O2):
@__NCPUBITS = global i32 0
@.str = [56 x i8] c"/sys/devices/system/cpu/cpu%lu/topology/thread_siblings\00"
@.str.1 = [54 x i8] c"/sys/devices/system/cpu/cpu%lu/topology/core_siblings\00"
define void @get_cputopology_read(i32* nocapture writeonly %0, i32* nocapture %1, i32* nocapture %2) {
store i32 1, i32* %2
store i32 1, i32* %1
store i32 1, i32* %0
%4 = tail call i64 (...) @get_processor_number_kernel_max()
%5 = load i32, i32* @__NCPUBITS
%6 = sext i32 %5 to i64
%7 = add i64 %4, -1
%8 = add i64 %7, %6
%9 = udiv i64 %8, %6
%10 = trunc i64 %9 to i32
%11 = shl i32 %10, 2
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %45, label %14
14: ; preds = %3
%15 = shl i64 %9, 2
%16 = icmp eq i64 %4, 0
br i1 %16, label %43, label %17
17: ; preds = %14, %40
%18 = phi i64 [ %41, %40 ], [ 0, %14 ]
%19 = tail call i8* @sysfsparser_getline(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i64 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %40, label %21
21: ; preds = %17
%22 = tail call i32 @cpumask_parse(i8* %19, i8* %12, i64 %15)
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i32 1, i32 %22
store i32 %24, i32* %2
%25 = tail call i8* @sysfsparser_getline(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i64 %18)
%26 = tail call i32 @cpumask_parse(i8* %25, i8* %12, i64 %15)
%27 = load i32, i32* %2
%28 = udiv i32 %26, %27
%29 = icmp ugt i32 %27, %26
%30 = select i1 %29, i32 1, i32 %28
store i32 %30, i32* %1
%31 = tail call i32 (...) @get_processor_number_online()
%32 = load i32, i32* %2
%33 = udiv i32 %31, %32
%34 = load i32, i32* %1
%35 = udiv i32 %33, %34
%36 = icmp ugt i32 %34, %33
%37 = select i1 %36, i32 1, i32 %35
store i32 %37, i32* %0
%38 = tail call i32 @free(i8* %25)
%39 = tail call i32 @free(i8* %19)
br label %40
40: ; preds = %17, %21
%41 = add nuw i64 %18, 1
%42 = icmp eq i64 %41, %4
br i1 %42, label %43, label %17
43: ; preds = %40, %14
%44 = tail call i32 @free(i8* %12)
br label %45
45: ; preds = %3, %43
ret void
}