#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 /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int cpu_is_first_core_in_package(int cpu) {
  char path[64];
  FILE *filep;
  int first_cpu;
  sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list",
          cpu);
  filep = fopen(path, "r");
  if (filep == NULL) {
    perror(path);
    exit(1);
  }
  fscanf(filep, "%d", &first_cpu);
  fclose(filep);
  return (cpu == first_cpu);
}


x86 assembly (clang, -O0):
<cpu_is_first_core_in_package>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe87(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11b0 <cpu_is_first_core_in_package+0x50>
lea    -0x50(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x58(%rbp),%rdi
lea    0xe81(%rip),%rsi
lea    -0x5c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
cmp    -0x5c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_is_first_core_in_package>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %edi,%ebp
lea    0xe91(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xeb5(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c1 <cpu_is_first_core_in_package+0x61>
mov    %rax,%rbx
lea    0xea0(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    %ebp,0xc(%rsp)
sete   %al
add    $0x58,%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 = [58 x i8] c"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @cpu_is_first_core_in_package(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x i8]
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %2
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%15 = call i32 @perror(i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
%22 = load i32, i32* %2
%23 = load i32, i32* %5
%24 = icmp eq i32 %22, %23
%25 = zext i1 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
@.str = [58 x i8] c"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @cpu_is_first_core_in_package(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, 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 %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* %4)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%14 = call i32 @fclose(i32* %7)
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, %0
%17 = zext i1 %16 to i32
ret i32 %17
}