#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 uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int FILENAME_MAX;
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
bool cpu_physical(uint32_t cpu) {
  char file[FILENAME_MAX];
  snprintf(file, FILENAME_MAX,
           "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
  FILE *fp = fopen(file, "r");
  if (fp != NULL) {
    char name[16];
    size_t len = fread(name, 1, 15, fp);
    name[len] = '\0';
    fclose(fp);
    if (cpu != (uint32_t)atoi(name))
      return false;
  }
  return true;
}


x86 assembly (clang, -O0):
<cpu_physical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%ecx
lea    0xe66(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1220 <cpu_physical+0xd0>
lea    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xf,%edx
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %rax,%rcx
mov    -0x4c(%rbp),%eax
cmp    %ecx,%eax
je     121b <cpu_physical+0xcb>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    122e <cpu_physical+0xde>
jmp    1220 <cpu_physical+0xd0>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_physical>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe7c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xea6(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11de <cpu_physical+0x8e>
mov    %rax,%rbx
lea    -0x30(%rbp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xf,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
movb   $0x0,-0x30(%rbp,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1070 <atoi@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14d,%ecx
jne    11e3 <cpu_physical+0x93>
mov    $0x1,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [60 x i8] c"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @cpu_physical(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [16 x i8]
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %3
%10 = load i32, i32* @FILENAME_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i32, i32* @FILENAME_MAX
%15 = load i32, i32* %3
%16 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %35
20: ; preds = %1
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i64 @fread(i8* %21, i32 1, i32 15, i32* %22)
store i64 %23, i64* %8
%24 = load i64, i64* %8
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %3
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%30 = call i64 @atoi(i8* %29)
%31 = trunc i64 %30 to i32
%32 = icmp ne i32 %28, %31
br i1 %32, label %33, label %34
33: ; preds = %20
store i32 0, i32* %2
store i32 1, i32* %9
br label %36
34: ; preds = %20
br label %35
35: ; preds = %34, %1
store i32 1, i32* %2
store i32 1, i32* %9
br label %36
36: ; preds = %35, %33
%37 = load i8*, i8** %4
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [60 x i8] c"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @cpu_physical(i32 %0) {
%2 = alloca [16 x i8]
%3 = load i32, i32* @FILENAME_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%11 = call i64 @fread(i8* %10, i32 1, i32 15, i32* %7)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 @fclose(i32* %7)
%14 = call i64 @atoi(i8* %10)
%15 = trunc i64 %14 to i32
%16 = icmp eq i32 %15, %0
br i1 %16, label %17, label %18
17: ; preds = %9, %1
br label %18
18: ; preds = %9, %17
%19 = phi i32 [ 1, %17 ], [ 0, %9 ]
ret i32 %19
}