#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_WRONLY;
int /*<<< orphan*/ USBDEVFS_RESET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
void main(int argc, char **argv) {
  const char *filename;
  int fd;
  if (argc < 2) {
    printf("Give USB device name as parameter\n");
    exit(1);
  }
  filename = argv[1];
  fd = open(filename, O_WRONLY);
  if (fd == -1) {
    perror("USB device open failed");
    exit(2);
  }
  if (ioctl(fd, USBDEVFS_RESET, 0) == -1) {
    perror("USBDEVFS_RESET device ioctl failed");
    exit(3);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
jge    118f <main+0x2f>
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d0 <main+0x70>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1202 <main+0xa2>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jle    119e <main+0x3e>
mov    0x8(%rsi),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b4 <main+0x54>
mov    %eax,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11ca <main+0x6a>
mov    %ebx,%edi
pop    %rbx
jmp    1050 <close@plt>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xe46(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Give USB device name as parameter\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"USB device open failed\00"
@USBDEVFS_RESET = global i32 0
@.str.2 = [35 x i8] c"USBDEVFS_RESET device ioctl failed\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %4
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %12
%25 = load i32, i32* %6
%26 = load i32, i32* @USBDEVFS_RESET
%27 = call i32 @ioctl(i32 %25, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %24
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"USB device open failed\00"
@USBDEVFS_RESET = global i32 0
@.str.2 = [35 x i8] c"USBDEVFS_RESET device ioctl failed\00"
@str = [34 x i8] c"Give USB device name as parameter\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 2)
unreachable
16: ; preds = %7
%17 = load i32, i32* @USBDEVFS_RESET
%18 = tail call i32 @ioctl(i32 %11, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 @exit(i32 3)
unreachable
23: ; preds = %16
%24 = tail call i32 @close(i32 %11)
ret void
}