#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 usbentry {
  int dummy;
};

/* Variables and functions */
struct usbentry *find_device(int *, int *, int *, int *, char *);
int /*<<< orphan*/ find_usbfs();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ list_devices();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ reset_device(struct usbentry *);
int sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int main(int argc, char **argv) {
  int id1, id2;
  struct usbentry *dev;
  if (!find_usbfs()) {
    fprintf(stderr, "Unable to find usbfs, is it mounted?\n");
    return 1;
  }
  if ((argc == 2) && (sscanf(argv[1], "%3d/%3d", &id1, &id2) == 2)) {
    dev = find_device(&id1, &id2, NULL, NULL, NULL);
  } else if ((argc == 2) && (sscanf(argv[1], "%4x:%4x", &id1, &id2) == 2)) {
    dev = find_device(NULL, NULL, &id1, &id2, NULL);
  } else if ((argc == 2) && strlen(argv[1]) < 128) {
    dev = find_device(NULL, NULL, NULL, NULL, argv[1]);
  } else {
    printf("Usage:\n"
           "  usbreset PPPP:VVVV - reset by product and vendor id\n"
           "  usbreset BBB/DDD   - reset by bus and device number\n"
           "  usbreset \"Product\" - reset by product name\n\n"
           "Devices:\n");
    list_devices();
    return 1;
  }
  if (!dev) {
    fprintf(stderr, "No such device found\n");
    return 1;
  }
  reset_device(dev);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10a0 <find_usbfs@plt>
cmp    $0x0,%eax
jne    11c7 <main+0x47>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1311 <main+0x191>
cmpl   $0x2,-0x8(%rbp)
jne    1217 <main+0x97>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe46(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1217 <main+0x97>
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <find_device@plt>
mov    %rax,-0x20(%rbp)
jmp    12d5 <main+0x155>
cmpl   $0x2,-0x8(%rbp)
jne    1267 <main+0xe7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfe(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1267 <main+0xe7>
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
mov    %r8,%rdi
mov    %r8,%rsi
call   1080 <find_device@plt>
mov    %rax,-0x20(%rbp)
jmp    12d0 <main+0x150>
cmpl   $0x2,-0x8(%rbp)
jne    12ac <main+0x12c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x80,%eax
jge    12ac <main+0x12c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdi
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1080 <find_device@plt>
mov    %rax,-0x20(%rbp)
jmp    12cb <main+0x14b>
lea    0xd83(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <list_devices@plt>
movl   $0x1,-0x4(%rbp)
jmp    1311 <main+0x191>
jmp    12d0 <main+0x150>
jmp    12d5 <main+0x155>
cmpq   $0x0,-0x20(%rbp)
jne    1301 <main+0x181>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1311 <main+0x191>
mov    -0x20(%rbp),%rdi
call   1040 <reset_device@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <find_usbfs@plt>
test   %eax,%eax
je     11ca <main+0x4a>
cmp    $0x2,%ebp
jne    123d <main+0xbd>
mov    0x8(%rbx),%rdi
lea    0xe7f(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    11df <main+0x5f>
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
jmp    1227 <main+0xa7>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
jmp    1262 <main+0xe2>
mov    0x8(%rbx),%rdi
lea    0xe44(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    120d <main+0x8d>
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
xor    %edi,%edi
xor    %esi,%esi
xor    %r8d,%r8d
jmp    1227 <main+0xa7>
mov    0x8(%rbx),%rdi
call   1040 <strlen@plt>
cmp    $0x7f,%eax
jg     123d <main+0xbd>
mov    0x8(%rbx),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <find_device@plt>
test   %rax,%rax
je     1252 <main+0xd2>
mov    %rax,%rdi
call   1050 <reset_device@plt>
xor    %eax,%eax
jmp    126c <main+0xec>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <list_devices@plt>
jmp    1267 <main+0xe7>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.usbentry = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Unable to find usbfs, is it mounted?\0A\00"
@.str.1 = [8 x i8] c"%3d/%3d\00"
@.str.2 = [8 x i8] c"%4x:%4x\00"
@.str.3 = [171 x i8] c"Usage:\0A usbreset PPPP:VVVV - reset by product and vendor id\0A usbreset BBB/DDD - reset by bus and device number\0A usbreset \22Product\22 - reset by product name\0A\0ADevices:\0A\00"
@.str.4 = [22 x i8] c"No such device found\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.usbentry*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @find_usbfs()
%10 = icmp ne i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %64
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7)
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call %struct.usbentry* @find_device(i32* %6, i32* %7, i32* null, i32* null, i8* null)
store %struct.usbentry* %24, %struct.usbentry** %8
br label %55
25: ; preds = %17, %14
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %6, i32* %7)
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* %6, i32* %7, i8* null)
store %struct.usbentry* %35, %struct.usbentry** %8
br label %54
36: ; preds = %28, %25
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 2
br i1 %38, label %39, label %50
39: ; preds = %36
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @strlen(i8* %42)
%44 = icmp slt i32 %43, 128
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* null, i32* null, i8* %48)
store %struct.usbentry* %49, %struct.usbentry** %8
br label %53
50: ; preds = %39, %36
%51 = call i32 @printf(i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 (...) @list_devices()
store i32 1, i32* %3
br label %64
53: ; preds = %45
br label %54
54: ; preds = %53, %34
br label %55
55: ; preds = %54, %23
%56 = load %struct.usbentry*, %struct.usbentry** %8
%57 = icmp ne %struct.usbentry* %56, null
br i1 %57, label %61, label %58
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %64
61: ; preds = %55
%62 = load %struct.usbentry*, %struct.usbentry** %8
%63 = call i32 @reset_device(%struct.usbentry* %62)
store i32 0, i32* %3
br label %64
64: ; preds = %61, %58, %50, %11
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.usbentry = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Unable to find usbfs, is it mounted?\0A\00"
@.str.1 = [8 x i8] c"%3d/%3d\00"
@.str.2 = [8 x i8] c"%4x:%4x\00"
@.str.4 = [22 x i8] c"No such device found\0A\00"
@str = [170 x i8] c"Usage:\0A usbreset PPPP:VVVV - reset by product and vendor id\0A usbreset BBB/DDD - reset by bus and device number\0A usbreset \22Product\22 - reset by product name\0A\0ADevices:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 (...) @find_usbfs()
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %45
12: ; preds = %2
%13 = icmp eq i32 %0, 2
br i1 %13, label %14, label %34
14: ; preds = %12
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call %struct.usbentry* @find_device(i32* %3, i32* %4, i32* null, i32* null, i8* null)
br label %37
21: ; preds = %14
%22 = load i8*, i8** %15
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* %3, i32* %4, i8* null)
br label %37
27: ; preds = %21
%28 = load i8*, i8** %15
%29 = call i32 @strlen(i8* %28)
%30 = icmp slt i32 %29, 128
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %15
%33 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* null, i32* null, i8* %32)
br label %37
34: ; preds = %12, %27
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([170 x i8], [170 x i8]* @str, i64 0, i64 0))
%36 = call i32 (...) @list_devices()
br label %45
37: ; preds = %25, %31, %19
%38 = phi %struct.usbentry* [ %20, %19 ], [ %26, %25 ], [ %33, %31 ]
%39 = icmp eq %struct.usbentry* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %45
43: ; preds = %37
%44 = call i32 @reset_device(%struct.usbentry* %38)
br label %45
45: ; preds = %43, %40, %34, %9
%46 = phi i32 [ 0, %43 ], [ 1, %40 ], [ 1, %34 ], [ 1, %9 ]
ret i32 %46
}