#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 pci_device {
  int domain;
  int bus;
  int dev;
  int func;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ dirname;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int pci_device_has_uio_driver(struct pci_device *dev) {
  struct dirent *e;
  DIR *dir;
  char dirname[PATH_MAX];
  snprintf(dirname, sizeof(dirname),
           "/sys/bus/pci/devices"
           "/"
           "%04x:%02x:%02x.%1u"
           "/uio",
           dev->domain, dev->bus, dev->dev, dev->func);
  dir = opendir(dirname);
  if (!dir) {
    snprintf(dirname, sizeof(dirname),
             "/sys/bus/pci/devices"
             "/"
             "%04x:%02x:%02x.%1u",
             dev->domain, dev->bus, dev->dev, dev->func);
    dir = opendir(dirname);
    if (!dir)
      return 0;
  }
  while ((e = readdir(dir)) != NULL) {
    if (strncmp(e->d_name, "uio", 3) == 0) {
      break;
    }
  }
  closedir(dir);
  if (!e)
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<pci_device_has_uio_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
lea    0xe4e(%rip),%rdx
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
add    $0x10,%rsp
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1248 <pci_device_has_uio_driver+0xf8>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%r8d
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r9d
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%eax
lea    0xe24(%rip),%rdx
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x40(%rbp),%rdi
add    $0x10,%rsp
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1243 <pci_device_has_uio_driver+0xf3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12c4 <pci_device_has_uio_driver+0x174>
jmp    1248 <pci_device_has_uio_driver+0xf8>
jmp    124d <pci_device_has_uio_driver+0xfd>
mov    -0x20(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     128f <pci_device_has_uio_driver+0x13f>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    128a <pci_device_has_uio_driver+0x13a>
jmp    128f <pci_device_has_uio_driver+0x13f>
jmp    124d <pci_device_has_uio_driver+0xfd>
mov    -0x20(%rbp),%rdi
call   1060 <closedir@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12b6 <pci_device_has_uio_driver+0x166>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12c4 <pci_device_has_uio_driver+0x174>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_device_has_uio_driver>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    0x2e88(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    (%rdi),%ecx
mov    0x4(%rdi),%r8d
mov    0x8(%rdi),%r9d
mov    0xc(%rdi),%eax
sub    $0x8,%rsp
lea    0xe69(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
push   %rax
call   1050 <snprintf@plt>
add    $0x10,%rsp
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11f5 <pci_device_has_uio_driver+0xa5>
mov    (%r12),%ecx
mov    0x4(%r12),%r8d
mov    0x8(%r12),%r9d
mov    0xc(%r12),%eax
sub    $0x8,%rsp
lea    0xe57(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
push   %rax
call   1050 <snprintf@plt>
add    $0x10,%rsp
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1248 <pci_device_has_uio_driver+0xf8>
lea    0xe2c(%rip),%r14
nopl   0x0(%rax)
mov    %r13,%rdi
call   1070 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1224 <pci_device_has_uio_driver+0xd4>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1200 <pci_device_has_uio_driver+0xb0>
xor    %r14d,%r14d
test   %rbx,%rbx
setne  %r14b
mov    %r13,%rdi
call   1060 <closedir@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
jmp    1236 <pci_device_has_uio_driver+0xe6>

LLVM IR (-O0):
%struct.pci_device = type { i32, i32, i32, i32 }
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [44 x i8] c"/sys/bus/pci/devices/%04x:%02x:%02x.%1u/uio\00"
@.str.1 = [40 x i8] c"/sys/bus/pci/devices/%04x:%02x:%02x.%1u\00"
@.str.2 = [4 x i8] c"uio\00"
define i32 @pci_device_has_uio_driver(%struct.pci_device* %0) {
%2 = alloca i32
%3 = alloca %struct.pci_device*
%4 = alloca %struct.dirent*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store %struct.pci_device* %0, %struct.pci_device** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
%13 = trunc i64 %10 to i32
%14 = load %struct.pci_device*, %struct.pci_device** %3
%15 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load %struct.pci_device*, %struct.pci_device** %3
%18 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %17, i32 0, i32 1
%19 = load i32, i32* %18
%20 = load %struct.pci_device*, %struct.pci_device** %3
%21 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %20, i32 0, i32 2
%22 = load i32, i32* %21
%23 = load %struct.pci_device*, %struct.pci_device** %3
%24 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %23, i32 0, i32 3
%25 = load i32, i32* %24
%26 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %19, i32 %22, i32 %25)
%27 = call i32* @opendir(i8* %12)
store i32* %27, i32** %5
%28 = load i32*, i32** %5
%29 = icmp ne i32* %28, null
br i1 %29, label %50, label %30
30: ; preds = %1
%31 = trunc i64 %10 to i32
%32 = load %struct.pci_device*, %struct.pci_device** %3
%33 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load %struct.pci_device*, %struct.pci_device** %3
%36 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %35, i32 0, i32 1
%37 = load i32, i32* %36
%38 = load %struct.pci_device*, %struct.pci_device** %3
%39 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load %struct.pci_device*, %struct.pci_device** %3
%42 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = call i32 @snprintf(i8* %12, i32 %31, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %37, i32 %40, i32 %43)
%45 = call i32* @opendir(i8* %12)
store i32* %45, i32** %5
%46 = load i32*, i32** %5
%47 = icmp ne i32* %46, null
br i1 %47, label %49, label %48
48: ; preds = %30
store i32 0, i32* %2
store i32 1, i32* %8
br label %70
49: ; preds = %30
br label %50
50: ; preds = %49, %1
br label %51
51: ; preds = %62, %50
%52 = load i32*, i32** %5
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %4
%54 = icmp ne %struct.dirent* %53, null
br i1 %54, label %55, label %63
55: ; preds = %51
%56 = load %struct.dirent*, %struct.dirent** %4
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @strncmp(i32 %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %63
62: ; preds = %55
br label %51
63: ; preds = %61, %51
%64 = load i32*, i32** %5
%65 = call i32 @closedir(i32* %64)
%66 = load %struct.dirent*, %struct.dirent** %4
%67 = icmp ne %struct.dirent* %66, null
br i1 %67, label %69, label %68
68: ; preds = %63
store i32 0, i32* %2
store i32 1, i32* %8
br label %70
69: ; preds = %63
store i32 1, i32* %2
store i32 1, i32* %8
br label %70
70: ; preds = %69, %68, %48
%71 = load i8*, i8** %6
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
%struct.pci_device = type { i32, i32, i32, i32 }
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [44 x i8] c"/sys/bus/pci/devices/%04x:%02x:%02x.%1u/uio\00"
@.str.1 = [40 x i8] c"/sys/bus/pci/devices/%04x:%02x:%02x.%1u\00"
@.str.2 = [4 x i8] c"uio\00"
define i32 @pci_device_has_uio_driver(%struct.pci_device* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 0
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 2
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 3
%12 = load i32, i32* %11
%13 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %8, i32 %10, i32 %12)
%14 = call i32* @opendir(i8* %4)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %24
16: ; preds = %1
%17 = load i32, i32* %5
%18 = load i32, i32* %7
%19 = load i32, i32* %9
%20 = load i32, i32* %11
%21 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18, i32 %19, i32 %20)
%22 = call i32* @opendir(i8* %4)
%23 = icmp eq i32* %22, null
br i1 %23, label %38, label %24
24: ; preds = %16, %1
%25 = phi i32* [ %14, %1 ], [ %22, %16 ]
br label %26
26: ; preds = %29, %24
%27 = call %struct.dirent* @readdir(i32* %25)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @strncmp(i32 %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %26
34: ; preds = %29, %26
%35 = call i32 @closedir(i32* %25)
%36 = xor i1 %28, true
%37 = zext i1 %36 to i32
br label %38
38: ; preds = %34, %16
%39 = phi i32 [ 0, %16 ], [ %37, %34 ]
ret i32 %39
}