#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ add_drv(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
void pci_checkdevice(char *path) {
  unsigned char buf[256];
  char id[32];
  int fd;
  int vendor, device;
  int subvendor, subdevice;
  if ((fd = open(path, O_RDONLY, 0)) == -1) {
    perror(path);
    return;
  }
  if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
    perror(path);
    close(fd);
    return;
  }
  close(fd);
  vendor = buf[0] | (buf[1] << 8);
  device = buf[2] | (buf[3] << 8);
  subvendor = buf[0x2c] | (buf[0x2d] << 8);
  subdevice = buf[0x2e] | (buf[0x2f] << 8);
  sprintf(id, "pcs%x,%x", subvendor, subdevice);
  if (add_drv(id, 0))
    return;
  sprintf(id, "pci%x,%x", vendor, device);
  add_drv(id, 0);
}


x86 assembly (clang, -O0):
<pci_checkdevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x134(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <pci_checkdevice+0x40>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
jmp    12b6 <pci_checkdevice+0x156>
mov    -0x134(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
cltq
cmp    $0x100,%rax
je     11de <pci_checkdevice+0x7e>
mov    -0x8(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x134(%rbp),%edi
call   1030 <close@plt>
jmp    12b6 <pci_checkdevice+0x156>
mov    -0x134(%rbp),%edi
call   1030 <close@plt>
movzbl -0x110(%rbp),%eax
movzbl -0x10f(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x138(%rbp)
movzbl -0x10e(%rbp),%eax
movzbl -0x10d(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x13c(%rbp)
movzbl -0xe4(%rbp),%eax
movzbl -0xe3(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x140(%rbp)
movzbl -0xe2(%rbp),%eax
movzbl -0xe1(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x144(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x140(%rbp),%edx
mov    -0x144(%rbp),%ecx
lea    0xd99(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
call   1050 <add_drv@plt>
cmp    $0x0,%rax
je     1289 <pci_checkdevice+0x129>
jmp    12b6 <pci_checkdevice+0x156>
lea    -0x130(%rbp),%rdi
mov    -0x138(%rbp),%edx
mov    -0x13c(%rbp),%ecx
lea    0xd66(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
call   1050 <add_drv@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_checkdevice>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%rbx
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     122d <pci_checkdevice+0xcd>
mov    %eax,%ebp
lea    0x20(%rsp),%rsi
mov    %eax,%edi
mov    $0x100,%edx
call   1040 <read@plt>
cmp    $0x100,%eax
jne    1244 <pci_checkdevice+0xe4>
mov    %ebp,%edi
call   1030 <close@plt>
movzbl 0x20(%rsp),%r15d
movzbl 0x21(%rsp),%ebx
movzbl 0x22(%rsp),%r12d
movzbl 0x23(%rsp),%ebp
movzwl 0x4c(%rsp),%edx
movzwl 0x4e(%rsp),%ecx
lea    0xe29(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1080 <sprintf@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <add_drv@plt>
test   %rax,%rax
jne    121d <pci_checkdevice+0xbd>
shl    $0x8,%ebp
or     %r12d,%ebp
shl    $0x8,%ebx
or     %r15d,%ebx
lea    0xe05(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1080 <sprintf@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <add_drv@plt>
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror@plt>
mov    %rbx,%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"pcs%x,%x\00"
@.str.1 = [9 x i8] c"pci%x,%x\00"
define void @pci_checkdevice(i8* %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 @perror(i8* %15)
br label %78
17: ; preds = %1
%18 = load i32, i32* %5
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 256)
%21 = sext i32 %20 to i64
%22 = icmp ne i64 %21, 256
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8*, i8** %2
%25 = call i32 @perror(i8* %24)
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
br label %78
28: ; preds = %17
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 1
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = shl i32 %36, 8
%38 = or i32 %33, %37
store i32 %38, i32* %6
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 2
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 3
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = shl i32 %44, 8
%46 = or i32 %41, %45
store i32 %46, i32* %7
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 44
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 45
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = shl i32 %52, 8
%54 = or i32 %49, %53
store i32 %54, i32* %8
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 46
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 47
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = shl i32 %60, 8
%62 = or i32 %57, %61
store i32 %62, i32* %9
%63 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%64 = load i32, i32* %8
%65 = load i32, i32* %9
%66 = call i32 @sprintf(i8* %63, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %64, i32 %65)
%67 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%68 = call i64 @add_drv(i8* %67, i32 0)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %28
br label %78
71: ; preds = %28
%72 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%73 = load i32, i32* %6
%74 = load i32, i32* %7
%75 = call i32 @sprintf(i8* %72, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %73, i32 %74)
%76 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%77 = call i64 @add_drv(i8* %76, i32 0)
br label %78
78: ; preds = %71, %70, %23, %14
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [9 x i8] c"pcs%x,%x\00"
@.str.1 = [9 x i8] c"pci%x,%x\00"
define void @pci_checkdevice(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* %0)
br label %56
11: ; preds = %1
%12 = call i32 @read(i32 %7, i8* %4, i32 256)
%13 = icmp eq i32 %12, 256
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* %0)
%16 = tail call i32 @close(i32 %7)
br label %56
17: ; preds = %11
%18 = tail call i32 @close(i32 %7)
%19 = load i8, i8* %4
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 1
%21 = load i8, i8* %20
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 2
%23 = load i8, i8* %22
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 3
%25 = load i8, i8* %24
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 44
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 45
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = shl nuw nsw i32 %31, 8
%33 = or i32 %32, %28
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 46
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 47
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = shl nuw nsw i32 %39, 8
%41 = or i32 %40, %36
%42 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %41)
%43 = call i64 @add_drv(i8* %5, i32 0)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %17
%46 = zext i8 %23 to i32
%47 = zext i8 %25 to i32
%48 = shl nuw nsw i32 %47, 8
%49 = or i32 %48, %46
%50 = zext i8 %19 to i32
%51 = zext i8 %21 to i32
%52 = shl nuw nsw i32 %51, 8
%53 = or i32 %52, %50
%54 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %53, i32 %49)
%55 = call i64 @add_drv(i8* %5, i32 0)
br label %56
56: ; preds = %17, %45, %14, %9
ret void
}