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

/* Variables and functions */
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ USBDEVFS_RESET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  const char *devpath;
  int fd;
  int rc;
  if (argc != 2) {
    fprintf(stderr, "Usage: ./usbreset /dev/device-filename\n");
    return 1;
  }
  devpath = argv[1];
  fd = open(devpath, O_NONBLOCK);
  if (fd < 0) {
    perror("Error while opening device file");
  }
  rc = ioctl(fd, USBDEVFS_RESET, 0);
  if (rc < 0) {
    perror("Error in ioctl");
  }
  printf("Device %s reset successful\n", devpath);
  close(fd);
  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)
je     11a1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1223 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d8 <main+0x78>
lea    0xe55(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1204 <main+0xa4>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    11b4 <main+0x54>
mov    0x8(%rsi),%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11d5 <main+0x75>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
test   %eax,%eax
js     11e3 <main+0x83>
lea    0xeb4(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11ce <main+0x6e>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
jmp    1184 <main+0x24>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
jmp    119c <main+0x3c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: ./usbreset /dev/device-filename\0A\00"
@O_NONBLOCK = global i32 0
@.str.1 = [32 x i8] c"Error while opening device file\00"
@USBDEVFS_RESET = global i32 0
@.str.2 = [15 x i8] c"Error in ioctl\00"
@.str.3 = [28 x i8] c"Device %s reset successful\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %38
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_NONBLOCK
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
%26 = load i32, i32* %7
%27 = load i32, i32* @USBDEVFS_RESET
%28 = call i32 @ioctl(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i8*, i8** %6
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %33, %11
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: ./usbreset /dev/device-filename\0A\00"
@O_NONBLOCK = global i32 0
@.str.1 = [32 x i8] c"Error while opening device file\00"
@USBDEVFS_RESET = global i32 0
@.str.2 = [15 x i8] c"Error in ioctl\00"
@.str.3 = [28 x i8] c"Device %s reset successful\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %24
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_NONBLOCK
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %7
%16 = load i32, i32* @USBDEVFS_RESET
%17 = tail call i32 @ioctl(i32 %11, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%23 = tail call i32 @close(i32 %11)
br label %24
24: ; preds = %21, %4
%25 = phi i32 [ 1, %4 ], [ 0, %21 ]
ret i32 %25
}