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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
int busid2node(int slot, int bus, int device, int function) {
  char path[256];
  FILE *fd;
  int bus_id = -1;
  if (!slot && !bus && !device && !function)
    return bus_id;
  snprintf(path, sizeof(path),
           "/sys/bus/pci/devices/%04X:%02X:%02X.%X/numa_node", slot, bus,
           device, function);
  if ((fd = fopen(path, "r")) != NULL) {
    char data[32] = {0};
    if (fgets(data, sizeof(data), fd) != NULL)
      bus_id = atoi(data);
    fclose(fd);
  }
  return bus_id;
}


x86 assembly (clang, -O0):
<busid2node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0xffffffff,-0x12c(%rbp)
ff ff ff
cmpl   $0x0,-0x8(%rbp)
jne    11b7 <busid2node+0x57>
cmpl   $0x0,-0xc(%rbp)
jne    11b7 <busid2node+0x57>
cmpl   $0x0,-0x10(%rbp)
jne    11b7 <busid2node+0x57>
cmpl   $0x0,-0x14(%rbp)
jne    11b7 <busid2node+0x57>
mov    -0x12c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1260 <busid2node+0x100>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    -0x14(%rbp),%eax
mov    $0x100,%esi
lea    0xe28(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     1257 <busid2node+0xf7>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x150(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0x20,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     124b <busid2node+0xeb>
lea    -0x150(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x12c(%rbp)
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x12c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busid2node>:
push   %rbp
push   %rbx
sub    $0x138,%rsp
mov    %edx,%r9d
mov    %esi,%edx
or     %edi,%edx
or     %r9d,%edx
mov    $0xffffffff,%ebp
or     %ecx,%edx
je     11e5 <busid2node+0x95>
mov    %esi,%r8d
mov    %edi,%eax
mov    %ecx,(%rsp)
lea    0xe85(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
lea    0xe9b(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e5 <busid2node+0x95>
mov    %rax,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d8 <busid2node+0x88>
lea    0x10(%rsp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
jmp    11dd <busid2node+0x8d>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x138,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"/sys/bus/pci/devices/%04X:%02X:%02X.%X/numa_node\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @busid2node(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [32 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 -1, i32* %12
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %4
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %19
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = load i32, i32* %12
store i32 %26, i32* %5
br label %51
27: ; preds = %22, %19, %16, %4
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = call i32 @snprintf(i8* %28, i32 256, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30, i32 %31, i32 %32)
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %11
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %49
37: ; preds = %27
%38 = bitcast [32 x i8]* %13 to i8*
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%40 = load i32*, i32** %11
%41 = call i32* @fgets(i8* %39, i32 32, i32* %40)
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%45 = call i32 @atoi(i8* %44)
store i32 %45, i32* %12
br label %46
46: ; preds = %43, %37
%47 = load i32*, i32** %11
%48 = call i32 @fclose(i32* %47)
br label %49
49: ; preds = %46, %27
%50 = load i32, i32* %12
store i32 %50, i32* %5
br label %51
51: ; preds = %49, %25
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@.str = [49 x i8] c"/sys/bus/pci/devices/%04X:%02X:%02X.%X/numa_node\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @busid2node(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = or i32 %1, %0
%9 = or i32 %8, %2
%10 = or i32 %9, %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %4
%13 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i32 %2, i32 %3)
%14 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %25, label %16
16: ; preds = %12
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = call i32* @fgets(i8* %17, i32 32, i32* %14)
%19 = icmp eq i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @atoi(i8* %17)
br label %22
22: ; preds = %20, %16
%23 = phi i32 [ %21, %20 ], [ -1, %16 ]
%24 = call i32 @fclose(i32* %14)
br label %25
25: ; preds = %12, %22, %4
%26 = phi i32 [ -1, %4 ], [ %23, %22 ], [ -1, %12 ]
ret i32 %26
}