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

/* Variables and functions */
int PATH_MAX;
int check_modules(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fwrite(char *, int, int, int /*<<< orphan*/ *);
int snprintf(char *, int, char *, ...);
int /*<<< orphan*/ stderr;
int pci_device_bind_uio_driver(struct pci_device *dev, char *driver_name) {
  int err, n;
  FILE *fd;
  char filename[PATH_MAX];
  char buf[256];
  err = check_modules(driver_name);
  if (err < 0) {
    fprintf(stderr, "No %s module loaded\n", driver_name);
    return err;
  }
  snprintf(filename, sizeof(filename),
           "/sys/bus/pci/drivers"
           "/"
           "%s"
           "/new_id",
           driver_name);
  fd = fopen(filename, "w");
  if (!fd) {
    return -1;
  }
  n = snprintf(buf, sizeof(buf), "%04x %04x", dev->vendor_id, dev->device_id);
  if (fwrite(buf, n, 1, fd) == 0)
    goto error;
  fclose(fd);
  return 0;
error:
  fclose(fd);
  return -1;
}


x86 assembly (clang, -O0):
<pci_device_bind_uio_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x158(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x150(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <check_modules@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ed <pci_device_bind_uio_driver+0x8d>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x144(%rbp)
jmp    12d2 <pci_device_bind_uio_driver+0x172>
mov    -0x150(%rbp),%rdi
mov    -0x158(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
lea    0xe0d(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x150(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1247 <pci_device_bind_uio_driver+0xe7>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x144(%rbp)
jmp    12d2 <pci_device_bind_uio_driver+0x172>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%r8d
mov    $0x100,%esi
lea    0xdce(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x20(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x0,%rax
jne    1299 <pci_device_bind_uio_driver+0x139>
jmp    12b8 <pci_device_bind_uio_driver+0x158>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x144(%rbp)
jmp    12d2 <pci_device_bind_uio_driver+0x172>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x144(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_device_bind_uio_driver>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e6f(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsi,%rdi
call   1050 <check_modules@plt>
test   %eax,%eax
js     121a <pci_device_bind_uio_driver+0xba>
lea    0xe6c(%rip),%rdx
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0xe74(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1237 <pci_device_bind_uio_driver+0xd7>
mov    %rax,%rbx
mov    (%r14),%ecx
mov    0x4(%r14),%r8d
lea    0xe58(%rip),%rdx
lea    -0x130(%rbp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%r14
sbb    %r13d,%r13d
jmp    123d <pci_device_bind_uio_driver+0xdd>
mov    %eax,%r13d
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
jmp    123d <pci_device_bind_uio_driver+0xdd>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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