#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_die_id(int cpu) {
  int ret;
  ret = parse_int_file(0, "/sys/devices/system/cpu/cpu%d/topology/die_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 die_id;
  }
  if (ret < 0)
    ret = 0;
  return ret;
}


x86 assembly (clang, -O0):
<get_physical_die_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_die_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_die_id+0x55>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1191 <get_physical_die_id+0x71>
jmp    117a <get_physical_die_id+0x5a>
cmpl   $0x0,-0xc(%rbp)
jge    118b <get_physical_die_id+0x6b>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_physical_die_id>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0xed1(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%edx
call   1030 <parse_int_file@plt>
test   %eax,%eax
jns    1158 <get_physical_die_id+0x38>
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
call   1040 <get_stored_topology_info@plt>
test   %eax,%eax
je     1166 <get_physical_die_id+0x46>
test   %eax,%eax
cmovns %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0xc(%rsp),%ebx
jmp    115d <get_physical_die_id+0x3d>

LLVM IR (-O0):
@.str = [46 x i8] c"/sys/devices/system/cpu/cpu%d/topology/die_id\00"
define i32 @get_physical_die_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 ([46 x i8], [46 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* %7
store i32 %18, i32* %2
br label %26
19: ; preds = %12
br label %20
20: ; preds = %19, %1
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %4
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [46 x i8] c"/sys/devices/system/cpu/cpu%d/topology/die_id\00"
define i32 @get_physical_die_id(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = tail call i32 @parse_int_file(i32 0, i8* getelementptr inbounds ([46 x i8], [46 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* %4
br i1 %12, label %18, label %14
14: ; preds = %7, %1
%15 = phi i32 [ %11, %7 ], [ %5, %1 ]
%16 = icmp sgt i32 %15, 0
%17 = select i1 %16, i32 %15, i32 0
br label %18
18: ; preds = %7, %14
%19 = phi i32 [ %17, %14 ], [ %13, %7 ]
ret i32 %19
}