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

/* Variables and functions */
int PATH_MAX;
int agetline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ eprintf(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, unsigned int, unsigned int, unsigned int,
                          unsigned int);
scalar_t__ sscanf(char *, char *, unsigned int *, ...);
int strlcat(char *, char *, int);
int strlcpy(char *, char const *, int);
void lsusb(const char *file) {
  FILE *fp;
  char path[PATH_MAX];
  char *buf = NULL;
  size_t size = 0;
  unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0;
  if (strlcpy(path, file, sizeof(path)) >= sizeof(path))
    eprintf("path too long\n");
  if (strlcat(path, "/uevent", sizeof(path)) >= sizeof(path))
    eprintf("path too long\n");
  if (!(fp = fopen(path, "r")))
    return;
  while (agetline(&buf, &size, fp) != -1) {
    if (sscanf(buf, "BUSNUM=%u\n", &busnum) ||
        sscanf(buf, "DEVNUM=%u\n", &devnum) ||
        sscanf(buf, "PRODUCT=%x/%x/", &pid, &vid))
      i++;
    if (i == 3) {
      printf("Bus %03d Device %03d: ID %04x:%04x\n", busnum, devnum, pid, vid);
      break;
    }
  }
  if (ferror(fp))
    eprintf("%s: read error:", path);
  free(buf);
  fclose(fp);
}


x86 assembly (clang, -O0):
<lsusb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   10a0 <strlcpy@plt>
mov    -0x50(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1237 <lsusb+0x97>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   10b0 <eprintf@plt>
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xdc7(%rip),%rsi
call   1050 <strlcat@plt>
mov    -0x50(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     126a <lsusb+0xca>
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   10b0 <eprintf@plt>
mov    -0x58(%rbp),%rdi
lea    0xda2(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1294 <lsusb+0xf4>
movl   $0x1,-0x48(%rbp)
jmp    138b <lsusb+0x1eb>
jmp    1299 <lsusb+0xf9>
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <agetline@plt>
cmp    $0xffffffff,%eax
je     134d <lsusb+0x1ad>
mov    -0x28(%rbp),%rdi
lea    0xd5b(%rip),%rsi
lea    -0x38(%rbp),%rdx
mov    $0x0,%al
call   1090 <sscanf@plt>
cmp    $0x0,%rax
jne    1317 <lsusb+0x177>
mov    -0x28(%rbp),%rdi
lea    0xd46(%rip),%rsi
lea    -0x3c(%rbp),%rdx
mov    $0x0,%al
call   1090 <sscanf@plt>
cmp    $0x0,%rax
jne    1317 <lsusb+0x177>
mov    -0x28(%rbp),%rdi
lea    0xd31(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x44(%rbp),%rcx
mov    $0x0,%al
call   1090 <sscanf@plt>
cmp    $0x0,%rax
je     1320 <lsusb+0x180>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x3,-0x34(%rbp)
jne    1348 <lsusb+0x1a8>
mov    -0x38(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x40(%rbp),%ecx
mov    -0x44(%rbp),%r8d
lea    0xd00(%rip),%rdi
call   1070 <printf@plt>
jmp    134d <lsusb+0x1ad>
jmp    1299 <lsusb+0xf9>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1372 <lsusb+0x1d2>
mov    -0x58(%rbp),%rsi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   10b0 <eprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsusb>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rsi
mov    0x2e35(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r14
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    %r14,%rdi
mov    %ebx,%edx
call   10a0 <strlcpy@plt>
cltq
cmp    %rbx,%rax
jb     1219 <lsusb+0x79>
lea    0xdee(%rip),%rdi
xor    %eax,%eax
call   10b0 <eprintf@plt>
lea    0xdef(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1050 <strlcat@plt>
cltq
cmp    %rbx,%rax
jb     123f <lsusb+0x9f>
lea    0xdc8(%rip),%rdi
xor    %eax,%eax
call   10b0 <eprintf@plt>
lea    0xdd1(%rip),%rsi
mov    %r14,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1333 <lsusb+0x193>
mov    %rax,%rbx
mov    %r14,-0x48(%rbp)
lea    -0x30(%rbp),%r15
lea    -0x50(%rbp),%r12
lea    -0x40(%rbp),%r14
xor    %r13d,%r13d
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <agetline@plt>
cmp    $0xffffffff,%eax
je     1303 <lsusb+0x163>
mov    -0x30(%rbp),%rdi
lea    0xd87(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <sscanf@plt>
test   %rax,%rax
jne    12e0 <lsusb+0x140>
mov    -0x30(%rbp),%rdi
lea    0xd78(%rip),%rsi
lea    -0x3c(%rbp),%rdx
xor    %eax,%eax
call   1090 <sscanf@plt>
test   %rax,%rax
jne    12e0 <lsusb+0x140>
mov    -0x30(%rbp),%rdi
lea    0xd68(%rip),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x34(%rbp),%rcx
xor    %eax,%eax
call   1090 <sscanf@plt>
test   %rax,%rax
je     12e4 <lsusb+0x144>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
cmp    $0x3,%r13d
jne    1270 <lsusb+0xd0>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xd40(%rip),%rdi
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1322 <lsusb+0x182>
lea    0xd4b(%rip),%rdi
mov    -0x48(%rbp),%rsi
xor    %eax,%eax
call   10b0 <eprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"path too long\0A\00"
@.str.1 = [8 x i8] c"/uevent\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"BUSNUM=%u\0A\00"
@.str.4 = [11 x i8] c"DEVNUM=%u\0A\00"
@.str.5 = [15 x i8] c"PRODUCT=%x/%x/\00"
@.str.6 = [36 x i8] c"Bus %03d Device %03d: ID %04x:%04x\0A\00"
@.str.7 = [16 x i8] c"%s: read error:\00"
define void @lsusb(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %2
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %4
%17 = alloca i8, i64 %15
store i64 %15, i64* %5
store i8* null, i8** %6
store i64 0, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%18 = load i8*, i8** %2
%19 = trunc i64 %15 to i32
%20 = call i32 @strlcpy(i8* %17, i8* %18, i32 %19)
%21 = sext i32 %20 to i64
%22 = icmp uge i64 %21, %15
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %1
%26 = trunc i64 %15 to i32
%27 = call i32 @strlcat(i8* %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = sext i32 %27 to i64
%29 = icmp uge i64 %28, %15
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
%33 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %3
%34 = icmp ne i32* %33, null
br i1 %34, label %36, label %35
35: ; preds = %32
store i32 1, i32* %13
br label %77
36: ; preds = %32
br label %37
37: ; preds = %65, %36
%38 = load i32*, i32** %3
%39 = call i32 @agetline(i8** %6, i64* %7, i32* %38)
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %66
41: ; preds = %37
%42 = load i8*, i8** %6
%43 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %9)
%44 = icmp ne i64 %43, 0
br i1 %44, label %53, label %45
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32* %10)
%48 = icmp ne i64 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %45
%50 = load i8*, i8** %6
%51 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32* %11, i32* %12)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49, %45, %41
%54 = load i32, i32* %8
%55 = add i32 %54, 1
store i32 %55, i32* %8
br label %56
56: ; preds = %53, %49
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, 3
br i1 %58, label %59, label %65
59: ; preds = %56
%60 = load i32, i32* %9
%61 = load i32, i32* %10
%62 = load i32, i32* %11
%63 = load i32, i32* %12
%64 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %60, i32 %61, i32 %62, i32 %63)
br label %66
65: ; preds = %56
br label %37
66: ; preds = %59, %37
%67 = load i32*, i32** %3
%68 = call i64 @ferror(i32* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %17)
br label %72
72: ; preds = %70, %66
%73 = load i8*, i8** %6
%74 = call i32 @free(i8* %73)
%75 = load i32*, i32** %3
%76 = call i32 @fclose(i32* %75)
store i32 0, i32* %13
br label %77
77: ; preds = %72, %35
%78 = load i8*, i8** %4
%79 = load i32, i32* %13
switch i32 %79, label %81 [
i32 0, label %80
i32 1, label %80
]
80: ; preds = %77, %77
ret void
81: ; preds = %77
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"path too long\0A\00"
@.str.1 = [8 x i8] c"/uevent\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"BUSNUM=%u\0A\00"
@.str.4 = [11 x i8] c"DEVNUM=%u\0A\00"
@.str.5 = [15 x i8] c"PRODUCT=%x/%x/\00"
@.str.6 = [36 x i8] c"Bus %03d Device %03d: ID %04x:%04x\0A\00"
@.str.7 = [16 x i8] c"%s: read error:\00"
define void @lsusb(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%12 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%13 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%14 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%15 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%16 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%17 = call i32 @strlcpy(i8* %10, i8* %0, i32 %8)
%18 = sext i32 %17 to i64
%19 = icmp ult i64 %18, %9
br i1 %19, label %22, label %20
20: ; preds = %1
%21 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %1
%23 = call i32 @strlcat(i8* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%24 = sext i32 %23 to i64
%25 = icmp ult i64 %24, %9
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %67, label %31
31: ; preds = %28, %49
%32 = phi i32 [ %50, %49 ], [ 0, %28 ]
%33 = call i32 @agetline(i8** %2, i64* %3, i32* %29)
%34 = icmp eq i32 %33, -1
br i1 %34, label %58, label %35
35: ; preds = %31
%36 = load i8*, i8** %2
%37 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %35
%40 = load i8*, i8** %2
%41 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %2
%45 = call i64 (i8*, i8*, i32*, ...) @sscanf(i8* %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32* %6, i32* %7)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %43, %39, %35
%48 = add i32 %32, 1
br label %49
49: ; preds = %47, %43
%50 = phi i32 [ %48, %47 ], [ %32, %43 ]
%51 = icmp eq i32 %50, 3
br i1 %51, label %52, label %31
52: ; preds = %49
%53 = load i32, i32* %4
%54 = load i32, i32* %5
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %53, i32 %54, i32 %55, i32 %56)
br label %58
58: ; preds = %31, %52
%59 = call i64 @ferror(i32* %29)
%60 = icmp eq i64 %59, 0
br i1 %60, label %63, label %61
61: ; preds = %58
%62 = call i32 (i8*, ...) @eprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %10)
br label %63
63: ; preds = %61, %58
%64 = load i8*, i8** %2
%65 = call i32 @free(i8* %64)
%66 = call i32 @fclose(i32* %29)
br label %67
67: ; preds = %28, %63
ret void
}