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

/* Variables and functions */
int get_stored_topology_info(int, int *, int *, int *);
int parse_int_file(int /*<<< orphan*/, char *, int);
int get_physical_package_id(int cpu) {
  int ret;
  ret = parse_int_file(
      0, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
  if (ret < 0) {
    int core_id, pkg_id, die_id;
    ret = get_stored_topology_info(cpu, &core_id, &pkg_id, &die_id);
    if (!ret)
      return pkg_id;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_physical_package_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edx
xor    %edi,%edi
lea    0xec9(%rip),%rsi
call   1030 <parse_int_file@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    117a <get_physical_package_id+0x5a>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1040 <get_stored_topology_info@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1175 <get_physical_package_id+0x55>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1180 <get_physical_package_id+0x60>
jmp    117a <get_physical_package_id+0x5a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_physical_package_id>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xed2(%rip),%rsi
xor    %edi,%edi
mov    %ebx,%edx
call   1030 <parse_int_file@plt>
test   %eax,%eax
jns    1159 <get_physical_package_id+0x39>
lea    0xc(%rsp),%rsi
lea    0x4(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
call   1040 <get_stored_topology_info@plt>
test   %eax,%eax
jne    1159 <get_physical_package_id+0x39>
mov    0x4(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [59 x i8] c"/sys/devices/system/cpu/cpu%d/topology/physical_package_id\00"
define i32 @get_physical_package_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @parse_int_file(i32 0, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i32 @get_stored_topology_info(i32 %13, i32* %5, i32* %6, i32* %7)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = load i32, i32* %6
store i32 %18, i32* %2
br label %22
19: ; preds = %12
br label %20
20: ; preds = %19, %1
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [59 x i8] c"/sys/devices/system/cpu/cpu%d/topology/physical_package_id\00"
define i32 @get_physical_package_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @parse_int_file(i32 0, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = bitcast i32* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = call i32 @get_stored_topology_info(i32 %0, i32* %2, i32* %3, i32* %4)
%12 = icmp eq i32 %11, 0
%13 = load i32, i32* %3
br i1 %12, label %16, label %14
14: ; preds = %7, %1
%15 = phi i32 [ %11, %7 ], [ %5, %1 ]
br label %16
16: ; preds = %7, %14
%17 = phi i32 [ %15, %14 ], [ %13, %7 ]
ret i32 %17
}