#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;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(char *, int, int, int /*<<< orphan*/ *);
int snprintf(char *, int, char *, int, int, int, int);
int pci_device_unbind_kernel_driver(struct pci_device *dev) {
  int n;
  FILE *fd;
  char filename[PATH_MAX];
  char buf[256];
  snprintf(filename, sizeof(filename),
           "/sys/bus/pci/devices"
           "/"
           "%04x:%02x:%02x.%1u"
           "/driver/unbind",
           dev->domain, dev->bus, dev->dev, dev->func);
  fd = fopen(filename, "w");
  if (!fd)
    return 0;
  n = snprintf(buf, sizeof(buf), "%04x:%02x:%02x.%1u", dev->domain, dev->bus,
               dev->dev, dev->func);
  if (fwrite(buf, n, 1, fd) == 0)
    goto error;
  fclose(fd);
  return 0;
error:
  fclose(fd);
  return -1;
}


x86 assembly (clang, -O0):
<pci_device_unbind_kernel_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x140(%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    0xe5c(%rip),%rdx
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
mov    -0x140(%rbp),%rdi
add    $0x10,%rsp
lea    0xe74(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ec <pci_device_unbind_kernel_driver+0xac>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x134(%rbp)
jmp    128f <pci_device_unbind_kernel_driver+0x14f>
lea    -0x130(%rbp),%rdi
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
mov    $0x100,%esi
lea    0xe1c(%rip),%rdx
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
add    $0x10,%rsp
mov    %eax,-0x14(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
cmp    $0x0,%rax
jne    1256 <pci_device_unbind_kernel_driver+0x116>
jmp    1275 <pci_device_unbind_kernel_driver+0x135>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x134(%rbp)
jmp    128f <pci_device_unbind_kernel_driver+0x14f>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x134(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_device_unbind_kernel_driver>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r15
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%rdi),%ecx
mov    0x4(%rdi),%r8d
mov    0x8(%rdi),%r9d
mov    0xc(%rdi),%eax
sub    $0x8,%rsp
lea    0xe78(%rip),%rdx
mov    %rbx,%rdi
push   %rax
call   1040 <snprintf@plt>
add    $0x10,%rsp
lea    0xe9a(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1204 <pci_device_unbind_kernel_driver+0xc4>
mov    %rax,%r14
mov    (%r15),%ecx
mov    0x4(%r15),%r8d
mov    0x8(%r15),%r9d
mov    0xc(%r15),%eax
sub    $0x8,%rsp
lea    0xe72(%rip),%rdx
lea    -0x120(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
push   %rax
call   1040 <snprintf@plt>
add    $0x10,%rsp
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%rbx
sbb    %eax,%eax
jmp    1206 <pci_device_unbind_kernel_driver+0xc6>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.pci_device = type { i32, i32, i32, i32 }
@PATH_MAX = global i32 0
@.str = [54 x i8] c"/sys/bus/pci/devices/%04x:%02x:%02x.%1u/driver/unbind\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"%04x:%02x:%02x.%1u\00"
define i32 @pci_device_unbind_kernel_driver(%struct.pci_device* nocapture %0) {
%2 = alloca [256 x i8]
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 0
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 2
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 3
%14 = load i32, i32* %13
%15 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %10, i32 %12, i32 %14)
%16 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %28, label %18
18: ; preds = %1
%19 = load i32, i32* %7
%20 = load i32, i32* %9
%21 = load i32, i32* %11
%22 = load i32, i32* %13
%23 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20, i32 %21, i32 %22)
%24 = call i64 @fwrite(i8* %6, i32 %23, i32 1, i32* %16)
%25 = icmp eq i64 %24, 0
%26 = call i32 @fclose(i32* %16)
%27 = sext i1 %25 to i32
br label %28
28: ; preds = %18, %1
%29 = phi i32 [ 0, %1 ], [ %27, %18 ]
ret i32 %29
}