#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);
int /*<<< orphan*/ close(int);
scalar_t__ is_audio(unsigned char *, 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 usb_checkdevice(char *fname) {
  int fd, l;
  unsigned char buf[4096];
  char tmp[64];
  int vendor, device;
  if ((fd = open(fname, O_RDONLY, 0)) == -1) {
    perror(fname);
    return;
  }
  if ((l = read(fd, buf, sizeof(buf))) == -1) {
    perror(fname);
    return;
  }
  close(fd);
  if (l < 12 || buf[1] != 1)
    return;
  vendor = buf[8] | (buf[9] << 8);
  device = buf[10] | (buf[11] << 8);
  sprintf(tmp, "usb%x,%x", vendor, device);
  if (add_drv(tmp, 1))
    return;
  sprintf(tmp, "usbif%x,%x", vendor, device);
  if (add_drv(tmp, 1))
    return;
  if (is_audio(buf, l)) {
    if (add_drv("usbif,class1", 1))
      return;
  }
}


x86 assembly (clang, -O0):
<usb_checkdevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11ad <usb_checkdevice+0x3d>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
jmp    12f0 <usb_checkdevice+0x180>
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11db <usb_checkdevice+0x6b>
mov    -0x8(%rbp),%rdi
call   1080 <perror@plt>
jmp    12f0 <usb_checkdevice+0x180>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
cmpl   $0xc,-0x10(%rbp)
jl     11fd <usb_checkdevice+0x8d>
movzbl -0x100f(%rbp),%eax
cmp    $0x1,%eax
je     1202 <usb_checkdevice+0x92>
jmp    12f0 <usb_checkdevice+0x180>
movzbl -0x1008(%rbp),%eax
movzbl -0x1007(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x1054(%rbp)
movzbl -0x1006(%rbp),%eax
movzbl -0x1005(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0x1058(%rbp)
lea    -0x1050(%rbp),%rdi
mov    -0x1054(%rbp),%edx
mov    -0x1058(%rbp),%ecx
lea    0xdb2(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x1050(%rbp),%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
cmp    $0x0,%rax
je     1273 <usb_checkdevice+0x103>
jmp    12f0 <usb_checkdevice+0x180>
lea    -0x1050(%rbp),%rdi
mov    -0x1054(%rbp),%edx
mov    -0x1058(%rbp),%ecx
lea    0xd7c(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x1050(%rbp),%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
cmp    $0x0,%rax
je     12b2 <usb_checkdevice+0x142>
jmp    12f0 <usb_checkdevice+0x180>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <is_audio@plt>
cmp    $0x0,%rax
je     12f0 <usb_checkdevice+0x180>
lea    0xd42(%rip),%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
cmp    $0x0,%rax
je     12eb <usb_checkdevice+0x17b>
jmp    12f0 <usb_checkdevice+0x180>
jmp    12f0 <usb_checkdevice+0x180>
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usb_checkdevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1048,%rsp
mov    %rdi,%rbx
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1262 <usb_checkdevice+0xf2>
mov    %eax,%ebp
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1262 <usb_checkdevice+0xf2>
mov    %eax,%r15d
mov    %ebp,%edi
call   1030 <close@plt>
cmp    $0xc,%r15d
jl     1254 <usb_checkdevice+0xe4>
cmpb   $0x1,0x41(%rsp)
jne    1254 <usb_checkdevice+0xe4>
movzwl 0x48(%rsp),%ebx
movzwl 0x4a(%rsp),%r14d
lea    0xe1a(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %r14d,%ecx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
test   %rax,%rax
jne    1254 <usb_checkdevice+0xe4>
lea    0xdfa(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %r14d,%ecx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
test   %rax,%rax
jne    1254 <usb_checkdevice+0xe4>
lea    0x40(%rsp),%rdi
mov    %r15d,%esi
call   1050 <is_audio@plt>
test   %rax,%rax
je     1254 <usb_checkdevice+0xe4>
lea    0xdca(%rip),%rdi
mov    $0x1,%esi
call   1060 <add_drv@plt>
add    $0x1048,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x1048,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>

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

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