#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_online = get_processor_number_online();
    unsigned int ncpus = ncpus_online > 0 ? ncpus_online : 0;
    *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    $0x60,%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    1356 <get_cputopology_read+0x1f6>
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    134d <get_cputopology_read+0x1ed>
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    133c <get_cputopology_read+0x1dc>
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)
jne    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)
jne    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)
cmpl   $0x0,-0x4c(%rbp)
jle    12e8 <get_cputopology_read+0x188>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x54(%rbp)
jmp    12f2 <get_cputopology_read+0x192>
xor    %eax,%eax
mov    %eax,-0x54(%rbp)
jmp    12f2 <get_cputopology_read+0x192>
mov    -0x54(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%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    132a <get_cputopology_read+0x1ca>
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    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cputopology_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,0x18(%rsp)
movl   $0x1,(%rdx)
mov    %rsi,0x20(%rsp)
movl   $0x1,(%rsi)
mov    %rdi,0x30(%rsp)
movl   $0x1,(%rdi)
xor    %eax,%eax
call   1050 <get_processor_number_kernel_max@plt>
mov    0x2e4b(%rip),%rcx
movslq (%rcx),%rcx
mov    %rax,0x10(%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,%rbx
jmp    11c8 <get_cputopology_read+0x68>
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
lea    0x0(,%rbx,4),%edi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     12f9 <get_cputopology_read+0x199>
cmpq   $0x0,0x10(%rsp)
je     12e1 <get_cputopology_read+0x181>
shl    $0x2,%rbx
xor    %r15d,%r15d
mov    %rbx,0x28(%rsp)
mov    0x8(%rsp),%rbp
mov    0x18(%rsp),%r13
jmp    1258 <get_cputopology_read+0xf8>
cs nopw 0x0(%rax,%rax,1)
xor    %edx,%edx
mov    0x18(%rsp),%rcx
divl   (%rcx)
mov    %eax,%ecx
mov    0x20(%rsp),%rax
mov    (%rax),%esi
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
cmp    %ecx,%esi
cmova  %ebx,%eax
mov    0x30(%rsp),%rcx
mov    %eax,(%rcx)
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r12,%rdi
call   1040 <free@plt>
mov    0x28(%rsp),%rbx
add    $0x1,%r15
cmp    %r15,0x10(%rsp)
je     12e1 <get_cputopology_read+0x181>
lea    0xda1(%rip),%rdi
mov    %r15,%rsi
call   1060 <sysfsparser_getline@plt>
test   %rax,%rax
je     1249 <get_cputopology_read+0xe9>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <cpumask_parse@plt>
test   %eax,%eax
mov    $0x1,%ecx
cmove  %ecx,%eax
mov    %eax,0x0(%r13)
lea    0xda6(%rip),%rdi
mov    %r15,%rsi
call   1060 <sysfsparser_getline@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1080 <cpumask_parse@plt>
mov    %eax,%ecx
mov    0x0(%r13),%esi
xor    %edx,%edx
div    %esi
cmp    %ecx,%esi
mov    $0x1,%ebx
mov    $0x1,%ecx
cmova  %ecx,%eax
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
call   1030 <get_processor_number_online@plt>
test   %eax,%eax
jns    1210 <get_cputopology_read+0xb0>
xor    %eax,%eax
jmp    1210 <get_cputopology_read+0xb0>
mov    0x8(%rsp),%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
add    $0x38,%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
%14 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i64 0, i64* %10
%15 = load i32*, i32** %6
store i32 1, i32* %15
%16 = load i32*, i32** %5
store i32 1, i32* %16
%17 = load i32*, i32** %4
store i32 1, i32* %17
%18 = call i64 (...) @get_processor_number_kernel_max()
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = load i32, i32* @__NCPUBITS
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = sub i64 %22, 1
%24 = load i32, i32* @__NCPUBITS
%25 = sext i32 %24 to i64
%26 = udiv i64 %23, %25
%27 = mul i64 %26, 4
%28 = trunc i64 %27 to i32
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %3
br label %112
32: ; preds = %3
%33 = load i64, i64* %9
%34 = load i32, i32* @__NCPUBITS
%35 = sext i32 %34 to i64
%36 = add i64 %33, %35
%37 = sub i64 %36, 1
%38 = load i32, i32* @__NCPUBITS
%39 = sext i32 %38 to i64
%40 = udiv i64 %37, %39
%41 = mul i64 %40, 4
store i64 %41, i64* %10
store i64 0, i64* %8
br label %42
42: ; preds = %106, %32
%43 = load i64, i64* %8
%44 = load i64, i64* %9
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %109
46: ; preds = %42
%47 = load i64, i64* %8
%48 = call i8* @sysfsparser_getline(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i64 %47)
store i8* %48, i8** %11
%49 = load i8*, i8** %11
%50 = icmp ne i8* %49, null
br i1 %50, label %52, label %51
51: ; preds = %46
br label %106
52: ; preds = %46
%53 = load i8*, i8** %11
%54 = load i8*, i8** %7
%55 = load i64, i64* %10
%56 = call i32 @cpumask_parse(i8* %53, i8* %54, i64 %55)
%57 = load i32*, i32** %6
store i32 %56, i32* %57
%58 = load i32*, i32** %6
%59 = load i32, i32* %58
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %52
%62 = load i32*, i32** %6
store i32 1, i32* %62
br label %63
63: ; preds = %61, %52
%64 = load i64, i64* %8
%65 = call i8* @sysfsparser_getline(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i64 %64)
store i8* %65, i8** %12
%66 = load i8*, i8** %12
%67 = load i8*, i8** %7
%68 = load i64, i64* %10
%69 = call i32 @cpumask_parse(i8* %66, i8* %67, i64 %68)
%70 = load i32*, i32** %6
%71 = load i32, i32* %70
%72 = udiv i32 %69, %71
%73 = load i32*, i32** %5
store i32 %72, i32* %73
%74 = load i32*, i32** %5
%75 = load i32, i32* %74
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %63
%78 = load i32*, i32** %5
store i32 1, i32* %78
br label %79
79: ; preds = %77, %63
%80 = call i32 (...) @get_processor_number_online()
store i32 %80, i32* %13
%81 = load i32, i32* %13
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = load i32, i32* %13
br label %86
85: ; preds = %79
br label %86
86: ; preds = %85, %83
%87 = phi i32 [ %84, %83 ], [ 0, %85 ]
store i32 %87, i32* %14
%88 = load i32, i32* %14
%89 = load i32*, i32** %6
%90 = load i32, i32* %89
%91 = udiv i32 %88, %90
%92 = load i32*, i32** %5
%93 = load i32, i32* %92
%94 = udiv i32 %91, %93
%95 = load i32*, i32** %4
store i32 %94, i32* %95
%96 = load i32*, i32** %4
%97 = load i32, i32* %96
%98 = icmp ne i32 %97, 0
br i1 %98, label %101, label %99
99: ; preds = %86
%100 = load i32*, i32** %4
store i32 1, i32* %100
br label %101
101: ; preds = %99, %86
%102 = load i8*, i8** %12
%103 = call i32 @free(i8* %102)
%104 = load i8*, i8** %11
%105 = call i32 @free(i8* %104)
br label %106
106: ; preds = %101, %51
%107 = load i64, i64* %8
%108 = add i64 %107, 1
store i64 %108, i64* %8
br label %42
109: ; preds = %42
%110 = load i8*, i8** %7
%111 = call i32 @free(i8* %110)
br label %112
112: ; preds = %109, %31
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 %47, label %14
14: ; preds = %3
%15 = shl i64 %9, 2
%16 = icmp eq i64 %4, 0
br i1 %16, label %45, label %17
17: ; preds = %14, %42
%18 = phi i64 [ %43, %42 ], [ 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 %42, 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 = icmp sgt i32 %31, 0
%33 = select i1 %32, i32 %31, i32 0
%34 = load i32, i32* %2
%35 = udiv i32 %33, %34
%36 = load i32, i32* %1
%37 = udiv i32 %35, %36
%38 = icmp ugt i32 %36, %35
%39 = select i1 %38, i32 1, i32 %37
store i32 %39, i32* %0
%40 = tail call i32 @free(i8* %25)
%41 = tail call i32 @free(i8* %19)
br label %42
42: ; preds = %17, %21
%43 = add nuw i64 %18, 1
%44 = icmp eq i64 %43, %4
br i1 %44, label %45, label %17
45: ; preds = %42, %14
%46 = tail call i32 @free(i8* %12)
br label %47
47: ; preds = %3, %45
ret void
}