#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*/ 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 ((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)
cmpl   $0x2,-0x8(%rbp)
jne    11d6 <main+0x66>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe61(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    11d6 <main+0x66>
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    1294 <main+0x124>
cmpl   $0x2,-0x8(%rbp)
jne    1226 <main+0xb6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe19(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1226 <main+0xb6>
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    128f <main+0x11f>
cmpl   $0x2,-0x8(%rbp)
jne    126b <main+0xfb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x80,%eax
jge    126b <main+0xfb>
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    128a <main+0x11a>
lea    0xd9e(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <list_devices@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
jmp    128f <main+0x11f>
jmp    1294 <main+0x124>
cmpq   $0x0,-0x20(%rbp)
jne    12c0 <main+0x150>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
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   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1215 <main+0xa5>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe74(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    11b3 <main+0x43>
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
jmp    11ff <main+0x8f>
mov    0x8(%rbx),%rdi
lea    0xe4a(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    11e5 <main+0x75>
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %edi,%edi
xor    %esi,%esi
xor    %r8d,%r8d
jmp    11ff <main+0x8f>
mov    0x8(%rbx),%rdi
call   1040 <strlen@plt>
cmp    $0x7f,%eax
jg     1215 <main+0xa5>
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     122a <main+0xba>
mov    %rax,%rdi
call   1050 <reset_device@plt>
xor    %eax,%eax
jmp    1244 <main+0xd4>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <list_devices@plt>
jmp    123f <main+0xcf>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.usbentry = type { i32 }
@.str = [8 x i8] c"%3d/%3d\00"
@.str.1 = [8 x i8] c"%4x:%4x\00"
@.str.2 = [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"
@stderr = global i32 0
@.str.3 = [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 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %7)
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call %struct.usbentry* @find_device(i32* %6, i32* %7, i32* null, i32* null, i8* null)
store %struct.usbentry* %18, %struct.usbentry** %8
br label %49
19: ; preds = %11, %2
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7)
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* %6, i32* %7, i8* null)
store %struct.usbentry* %29, %struct.usbentry** %8
br label %48
30: ; preds = %22, %19
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 2
br i1 %32, label %33, label %44
33: ; preds = %30
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @strlen(i8* %36)
%38 = icmp slt i32 %37, 128
br i1 %38, label %39, label %44
39: ; preds = %33
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* null, i32* null, i8* %42)
store %struct.usbentry* %43, %struct.usbentry** %8
br label %47
44: ; preds = %33, %30
%45 = call i32 @printf(i8* getelementptr inbounds ([171 x i8], [171 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 (...) @list_devices()
store i32 1, i32* %3
br label %58
47: ; preds = %39
br label %48
48: ; preds = %47, %28
br label %49
49: ; preds = %48, %17
%50 = load %struct.usbentry*, %struct.usbentry** %8
%51 = icmp ne %struct.usbentry* %50, null
br i1 %51, label %55, label %52
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %58
55: ; preds = %49
%56 = load %struct.usbentry*, %struct.usbentry** %8
%57 = call i32 @reset_device(%struct.usbentry* %56)
store i32 0, i32* %3
br label %58
58: ; preds = %55, %52, %44
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.usbentry = type { i32 }
@.str = [8 x i8] c"%3d/%3d\00"
@.str.1 = [8 x i8] c"%4x:%4x\00"
@stderr = global i32 0
@.str.3 = [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 = icmp eq i32 %0, 2
br i1 %7, label %8, label %28
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4)
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call %struct.usbentry* @find_device(i32* %3, i32* %4, i32* null, i32* null, i8* null)
br label %31
15: ; preds = %8
%16 = load i8*, i8** %9
%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 = %15
%20 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* %3, i32* %4, i8* null)
br label %31
21: ; preds = %15
%22 = load i8*, i8** %9
%23 = call i32 @strlen(i8* %22)
%24 = icmp slt i32 %23, 128
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %9
%27 = call %struct.usbentry* @find_device(i32* null, i32* null, i32* null, i32* null, i8* %26)
br label %31
28: ; preds = %2, %21
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([170 x i8], [170 x i8]* @str, i64 0, i64 0))
%30 = call i32 (...) @list_devices()
br label %39
31: ; preds = %19, %25, %13
%32 = phi %struct.usbentry* [ %14, %13 ], [ %20, %19 ], [ %27, %25 ]
%33 = icmp eq %struct.usbentry* %32, null
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %39
37: ; preds = %31
%38 = call i32 @reset_device(%struct.usbentry* %32)
br label %39
39: ; preds = %37, %34, %28
%40 = phi i32 [ 0, %37 ], [ 1, %34 ], [ 1, %28 ]
ret i32 %40
}