#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) {
      if (die_id < 0)
        die_id = 0;
      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    118b <get_physical_die_id+0x6b>
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    1186 <get_physical_die_id+0x66>
cmpl   $0x0,-0x18(%rbp)
jge    117b <get_physical_die_id+0x5b>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <get_physical_die_id+0x82>
jmp    118b <get_physical_die_id+0x6b>
cmpl   $0x0,-0xc(%rbp)
jge    119c <get_physical_die_id+0x7c>
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    115e <get_physical_die_id+0x3e>
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
jne    115e <get_physical_die_id+0x3e>
mov    0xc(%rsp),%eax
xor    %ebx,%ebx
test   %eax,%eax
cmovns %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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 %24
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 %23, label %17
17: ; preds = %12
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 0, i32* %7
br label %21
21: ; preds = %20, %17
%22 = load i32, i32* %7
store i32 %22, i32* %2
br label %30
23: ; preds = %12
br label %24
24: ; preds = %23, %1
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 0, i32* %4
br label %28
28: ; preds = %27, %24
%29 = load i32, i32* %4
store i32 %29, i32* %2
br label %30
30: ; preds = %28, %21
%31 = load i32, i32* %2
ret i32 %31
}

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 %18
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
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
%16 = select i1 %15, i32 %14, i32 0
br label %22
17: ; preds = %7
br label %18
18: ; preds = %17, %1
%19 = phi i32 [ %11, %17 ], [ %5, %1 ]
%20 = icmp sgt i32 %19, 0
%21 = select i1 %20, i32 %19, i32 0
br label %22
22: ; preds = %13, %18
%23 = phi i32 [ %21, %18 ], [ %16, %13 ]
ret i32 %23
}