#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 */
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ find_interface_in_dir(char *, char const *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int sscanf(char *, char *, int *, int *);
int find_interface(const char *ifname, char lspci[13]) {
  DIR *devices_dir;
  struct dirent *devices_dp;
  DIR *pci_dir;
  struct dirent *pci_dp;
  char dirname[PATH_MAX];
  devices_dir = opendir("/sys/devices");
  if (!devices_dir) {
    return -1;
  }
  while ((devices_dp = readdir(devices_dir)) != NULL) {
    int domain, bus;
    if (sscanf(devices_dp->d_name, "pci%d:%x", &domain, &bus) == 2) {
      snprintf(dirname, PATH_MAX - 1, "%s/%s", "/sys/devices",
               devices_dp->d_name);
      pci_dir = opendir(dirname);
      if (pci_dir == NULL)
        continue;
      while ((pci_dp = readdir(pci_dir)) != NULL) {
        char pciname[PATH_MAX];
        snprintf(pciname, PATH_MAX - 1, "%s/%s", dirname, pci_dp->d_name);
        if (find_interface_in_dir(pciname, ifname)) {
          closedir(pci_dir);
          closedir(devices_dir);
          sprintf(lspci, "%04x:%02x:%02x,%01x", domain, bus, 0, 0);
          return 1;
        }
      }
      closedir(pci_dir);
    }
  }
  closedir(devices_dir);
  return -1;
}


x86 assembly (clang, -O0):
<find_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
lea    0xe4a(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dd <find_interface+0x6d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    136f <find_interface+0x1ff>
jmp    11e2 <find_interface+0x72>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1358 <find_interface+0x1e8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe06(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x54(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jne    1353 <find_interface+0x1e3>
mov    -0x70(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%r8
lea    0xddb(%rip),%rdx
lea    0xdbe(%rip),%rcx
call   1050 <snprintf@plt>
mov    -0x70(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1264 <find_interface+0xf4>
jmp    11e2 <find_interface+0x72>
jmp    1269 <find_interface+0xf9>
mov    -0x30(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     134a <find_interface+0x1da>
mov    -0x70(%rbp),%rcx
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rdx
mov    %rdx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x68(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xd4c(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x78(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <find_interface_in_dir@plt>
cmp    $0x0,%rax
je     1327 <find_interface+0x1b7>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x18(%rbp),%rdi
mov    -0x50(%rbp),%edx
mov    -0x54(%rbp),%ecx
lea    0xd13(%rip),%rsi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1090 <sprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    132e <find_interface+0x1be>
movl   $0x0,-0x4c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x4c(%rbp),%eax
test   %eax,%eax
jne    136f <find_interface+0x1ff>
jmp    1345 <find_interface+0x1d5>
jmp    1269 <find_interface+0xf9>
mov    -0x30(%rbp),%rdi
call   1060 <closedir@plt>
jmp    11e2 <find_interface+0x72>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_interface>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,-0x48(%rbp)
mov    0x2e61(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe56(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,%r12
mov    $0xffffffff,%eax
test   %r12,%r12
je     12e9 <find_interface+0x179>
mov    %rbx,-0x40(%rbp)
mov    %r12,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     12a5 <find_interface+0x135>
mov    %rax,%rbx
mov    %r12,-0x38(%rbp)
jmp    1200 <find_interface+0x90>
xchg   %ax,%ax
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    -0x38(%rbp),%r12
mov    %r12,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12a5 <find_interface+0x135>
mov    (%rbx),%rdi
lea    0xe03(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jne    11ec <find_interface+0x7c>
mov    (%r14),%esi
add    $0xffffffff,%esi
mov    (%rbx),%r8
mov    %r13,%rdi
lea    0xde7(%rip),%rdx
lea    0xdca(%rip),%rcx
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     11ec <find_interface+0x7c>
mov    %rax,%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     11e0 <find_interface+0x70>
mov    %rsp,%r12
mov    (%r14),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%r14),%esi
add    $0xffffffff,%esi
mov    (%rax),%r8
mov    %rbx,%rdi
lea    0xd8f(%rip),%rdx
mov    %r13,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x48(%rbp),%rsi
call   1040 <find_interface_in_dir@plt>
test   %rax,%rax
jne    12b4 <find_interface+0x144>
mov    %r12,%rsp
jmp    1250 <find_interface+0xe0>
mov    %r12,%rdi
call   1060 <closedir@plt>
mov    $0xffffffff,%eax
jmp    12e9 <find_interface+0x179>
mov    %r15,%rdi
call   1060 <closedir@plt>
mov    -0x38(%rbp),%rdi
call   1060 <closedir@plt>
mov    -0x30(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xd4a(%rip),%rsi
mov    -0x40(%rbp),%rdi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <sprintf@plt>
mov    %r12,%rsp
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/sys/devices\00"
@.str.1 = [9 x i8] c"pci%d:%x\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [20 x i8] c"%04x:%02x:%02x,%01x\00"
define i32 @find_interface(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i8, i64 %18
store i64 %18, i64* %11
%21 = call i32* @opendir(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
25: ; preds = %2
br label %26
26: ; preds = %83, %46, %25
%27 = load i32*, i32** %6
%28 = call %struct.dirent* @readdir(i32* %27)
store %struct.dirent* %28, %struct.dirent** %7
%29 = icmp ne %struct.dirent* %28, null
br i1 %29, label %30, label %84
30: ; preds = %26
%31 = load %struct.dirent*, %struct.dirent** %7
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i32 0, i32 0
%33 = load i8*, i8** %32
%34 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %13, i32* %14)
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %83
36: ; preds = %30
%37 = load i32, i32* @PATH_MAX
%38 = sub nsw i32 %37, 1
%39 = load %struct.dirent*, %struct.dirent** %7
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %20, i32 %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %41)
%43 = call i32* @opendir(i8* %20)
store i32* %43, i32** %8
%44 = load i32*, i32** %8
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %47
46: ; preds = %36
br label %26
47: ; preds = %36
br label %48
48: ; preds = %79, %47
%49 = load i32*, i32** %8
%50 = call %struct.dirent* @readdir(i32* %49)
store %struct.dirent* %50, %struct.dirent** %9
%51 = icmp ne %struct.dirent* %50, null
br i1 %51, label %52, label %80
52: ; preds = %48
%53 = load i32, i32* @PATH_MAX
%54 = zext i32 %53 to i64
%55 = call i8* @llvm.stacksave()
store i8* %55, i8** %15
%56 = alloca i8, i64 %54
store i64 %54, i64* %16
%57 = load i32, i32* @PATH_MAX
%58 = sub nsw i32 %57, 1
%59 = load %struct.dirent*, %struct.dirent** %9
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 @snprintf(i8* %56, i32 %58, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %61)
%63 = load i8*, i8** %4
%64 = call i64 @find_interface_in_dir(i8* %56, i8* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %75
66: ; preds = %52
%67 = load i32*, i32** %8
%68 = call i32 @closedir(i32* %67)
%69 = load i32*, i32** %6
%70 = call i32 @closedir(i32* %69)
%71 = load i8*, i8** %5
%72 = load i32, i32* %13
%73 = load i32, i32* %14
%74 = call i32 @sprintf(i8* %71, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %73, i32 0, i32 0)
store i32 1, i32* %3
store i32 1, i32* %12
br label %76
75: ; preds = %52
store i32 0, i32* %12
br label %76
76: ; preds = %75, %66
%77 = load i8*, i8** %15
%78 = load i32, i32* %12
switch i32 %78, label %87 [
i32 0, label %79
]
79: ; preds = %76
br label %48
80: ; preds = %48
%81 = load i32*, i32** %8
%82 = call i32 @closedir(i32* %81)
br label %83
83: ; preds = %80, %30
br label %26
84: ; preds = %26
%85 = load i32*, i32** %6
%86 = call i32 @closedir(i32* %85)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %87
87: ; preds = %84, %76, %24
%88 = load i8*, i8** %10
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/sys/devices\00"
@.str.1 = [9 x i8] c"pci%d:%x\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [20 x i8] c"%04x:%02x:%02x,%01x\00"
define i32 @find_interface(i8* %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %61, label %10
10: ; preds = %2
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %59, label %13
13: ; preds = %10
%14 = bitcast i32* %3 to i8*
%15 = bitcast i32* %4 to i8*
br label %16
16: ; preds = %13, %56
%17 = phi %struct.dirent* [ %11, %13 ], [ %57, %56 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %56
22: ; preds = %16
%23 = load i32, i32* @PATH_MAX
%24 = add nsw i32 %23, -1
%25 = load i8*, i8** %18
%26 = call i32 @snprintf(i8* %7, i32 %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32* @opendir(i8* %7)
%28 = icmp eq i32* %27, null
br i1 %28, label %56, label %29
29: ; preds = %22
%30 = call %struct.dirent* @readdir(i32* %27)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %54, label %35
32: ; preds = %35
%33 = call %struct.dirent* @readdir(i32* %27)
%34 = icmp eq %struct.dirent* %33, null
br i1 %34, label %54, label %35
35: ; preds = %29, %32
%36 = phi %struct.dirent* [ %33, %32 ], [ %30, %29 ]
%37 = load i32, i32* @PATH_MAX
%38 = zext i32 %37 to i64
%39 = call i8* @llvm.stacksave()
%40 = alloca i8, i64 %38
%41 = load i32, i32* @PATH_MAX
%42 = add nsw i32 %41, -1
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i64 0, i32 0
%44 = load i8*, i8** %43
%45 = call i32 @snprintf(i8* %40, i32 %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %44)
%46 = call i64 @find_interface_in_dir(i8* %40, i8* %0)
%47 = icmp eq i64 %46, 0
br i1 %47, label %32, label %48
48: ; preds = %35
%49 = call i32 @closedir(i32* %27)
%50 = call i32 @closedir(i32* %8)
%51 = load i32, i32* %3
%52 = load i32, i32* %4
%53 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %52, i32 0, i32 0)
br label %61
54: ; preds = %32, %29
%55 = call i32 @closedir(i32* %27)
br label %56
56: ; preds = %16, %54, %22
%57 = call %struct.dirent* @readdir(i32* %8)
%58 = icmp eq %struct.dirent* %57, null
br i1 %58, label %59, label %16
59: ; preds = %56, %10
%60 = call i32 @closedir(i32* %8)
br label %61
61: ; preds = %48, %2, %59
%62 = phi i32 [ -1, %59 ], [ -1, %2 ], [ 1, %48 ]
ret i32 %62
}