#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, char *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int get_num_ht_siblings(int cpu) {
  char path[80];
  FILE *filep;
  int sib1, sib2;
  int matches;
  char character;
  sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
          cpu);
  filep = fopen(path, "r");
  if (filep == NULL) {
    perror(path);
    exit(1);
  }
  matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
  fclose(filep);
  if (matches == 3)
    return 2;
  else
    return 1;
}


x86 assembly (clang, -O0):
<get_num_ht_siblings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe84(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xeb0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11b3 <get_num_ht_siblings+0x53>
lea    -0x60(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x68(%rbp),%rdi
lea    0xe80(%rip),%rsi
lea    -0x6c(%rbp),%rdx
lea    -0x75(%rbp),%rcx
lea    -0x70(%rbp),%r8
call   1040 <fscanf@plt>
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x3,-0x74(%rbp)
jne    11f1 <get_num_ht_siblings+0x91>
movl   $0x2,-0x4(%rbp)
jmp    11f8 <get_num_ht_siblings+0x98>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num_ht_siblings>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %edi,%edx
lea    0xe91(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xeb9(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11cd <get_num_ht_siblings+0x6d>
mov    %rax,%rbx
lea    0xea4(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x7(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0x3,%ebp
sete   %al
add    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0x10(%rsp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [60 x i8] c"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%d%c%d\0A\00"
define i32 @get_num_ht_siblings(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %3
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%19 = call i32 @perror(i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = load i32*, i32** %5
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %6, i8* %9, i32* %7)
store i32 %23, i32* %8
%24 = load i32*, i32** %5
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 2, i32* %2
br label %30
29: ; preds = %21
store i32 1, i32* %2
br label %30
30: ; preds = %29, %28
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [60 x i8] c"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%d%c%d\0A\00"
define i32 @get_num_ht_siblings(i32 %0) {
%2 = alloca [80 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* %6)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %3, i8* %5, i32* %4)
%17 = call i32 @fclose(i32* %10)
%18 = icmp eq i32 %16, 3
%19 = select i1 %18, i32 2, i32 1
ret i32 %19
}