#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 hidraw_devinfo {
  scalar_t__ bustype;
  scalar_t__ vendor;
  scalar_t__ product;
};
typedef int /*<<< orphan*/ magic_sequence;
typedef scalar_t__ __u32;
typedef scalar_t__ __s16;

/* Variables and functions */
scalar_t__ BUS_USB;
int EINVAL;
int ENOMEM;
int EPERM;
int /*<<< orphan*/ HIDIOCGRAWINFO;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, struct hidraw_devinfo *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int res;
  struct hidraw_devinfo info;
  char magic_sequence[] = {0x10, 0xFF, 0x80, 0xB2, 0x01, 0x00, 0x00};
  if (argc == 1) {
    errno = EINVAL;
    perror("No hidraw device given");
    return 1;
  }
  fd = open(argv[1], O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    perror("Unable to open device");
    return 1;
  }
  res = ioctl(fd, HIDIOCGRAWINFO, &info);
  if (res < 0) {
    perror("error while getting info from device");
  } else {
    if (info.bustype != BUS_USB || info.vendor != (__u32)0x046d ||
        (info.product != (__s16)0xc52b && info.product != (__s16)0xc532)) {
      errno = EPERM;
      perror("The given device is not a Logitech "
             "Unifying Receiver");
      return 1;
    }
  }
  res = write(fd, magic_sequence, sizeof(magic_sequence));
  if (res < 0) {
    printf("Error: %d\n", errno);
    perror("write");
  } else if (res == sizeof(magic_sequence)) {
    printf("The receiver is ready to pair a new device.\n"
           "Switch your device on to pair it.\n");
  } else {
    errno = ENOMEM;
    printf("write: %d were written instead of %ld.\n", res,
           sizeof(magic_sequence));
    perror("write");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe84(%rip),%eax
mov    %eax,-0x37(%rbp)
mov    0xe7e(%rip),%ax
mov    %ax,-0x33(%rbp)
mov    0xe76(%rip),%al
mov    %al,-0x31(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11c7 <main+0x67>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1fa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120b <main+0xab>
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1fa>
mov    -0x14(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    123e <main+0xde>
lea    0xe00(%rip),%rdi
call   1080 <perror@plt>
jmp    12ab <main+0x14b>
mov    -0x30(%rbp),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0x11c>
cmpq   $0x46d,-0x28(%rbp)
jne    127c <main+0x11c>
cmpq   $0xc52b,-0x20(%rbp)
je     12a6 <main+0x146>
cmpq   $0xc532,-0x20(%rbp)
je     12a6 <main+0x146>
mov    0x2d1d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2c(%rip),%rax
mov    %ecx,(%rax)
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <main+0x1fa>
jmp    12ab <main+0x14b>
mov    -0x14(%rbp),%edi
lea    -0x37(%rbp),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12f1 <main+0x191>
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
jmp    134b <main+0x1eb>
movslq -0x18(%rbp),%rax
cmp    $0x7,%rax
jne    1312 <main+0x1b2>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1346 <main+0x1e6>
mov    0x2ca7(%rip),%rax
mov    (%rax),%ecx
mov    0x2c96(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%esi
lea    0xdc0(%rip),%rdi
mov    $0x7,%edx
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd58(%rip),%rdi
call   1080 <perror@plt>
jmp    134b <main+0x1eb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movl   $0x1b2,0x3(%rsp)
movl   $0xb280ff10,(%rsp)
cmp    $0x1,%edi
je     1252 <main+0xe2>
mov    0x8(%rsi),%rdi
mov    0x2e40(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     126d <main+0xfd>
mov    %eax,%ebx
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1280 <main+0x110>
mov    0x2e12(%rip),%rax
mov    0x8(%rsp),%rcx
cmp    (%rax),%rcx
jne    1237 <main+0xc7>
cmpq   $0x46d,0x10(%rsp)
jne    1237 <main+0xc7>
mov    0x18(%rsp),%rax
cmp    $0xc52b,%rax
je     1200 <main+0x90>
cmp    $0xc532,%rax
jne    1237 <main+0xc7>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x7,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1291 <main+0x121>
cmp    $0x7,%eax
jne    12aa <main+0x13a>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    0x2d71(%rip),%rcx
mov    %eax,(%rcx)
lea    0xe09(%rip),%rdi
jmp    1274 <main+0x104>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    0x2d56(%rip),%rcx
mov    %eax,(%rcx)
lea    0xd9c(%rip),%rdi
jmp    1274 <main+0x104>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1231 <main+0xc1>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
jmp    1200 <main+0x90>
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d1 <main+0x161>
mov    0x2d0f(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cfe(%rip),%rdx
mov    %ecx,(%rdx)
lea    0xddc(%rip),%rdi
mov    $0x7,%edx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc1(%rip),%rdi
call   1090 <perror@plt>
jmp    1228 <main+0xb8>

LLVM IR (-O0):
%struct.hidraw_devinfo = type { i64, i64, i64 }
@__const.main.magic_sequence = [7 x i8] c"\10\FF\80\B2\01\00\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"No hidraw device given\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Unable to open device\00"
@HIDIOCGRAWINFO = global i32 0
@.str.2 = [37 x i8] c"error while getting info from device\00"
@BUS_USB = global i64 0
@EPERM = global i32 0
@.str.3 = [53 x i8] c"The given device is not a Logitech Unifying Receiver\00"
@.str.4 = [11 x i8] c"Error: %d\0A\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [79 x i8] c"The receiver is ready to pair a new device.\0ASwitch your device on to pair it.\0A\00"
@ENOMEM = global i32 0
@.str.7 = [40 x i8] c"write: %d were written instead of %ld.\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.hidraw_devinfo
%9 = alloca [7 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [7 x i8]* %9 to i8*
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %82
28: ; preds = %16
%29 = load i32, i32* %6
%30 = load i32, i32* @HIDIOCGRAWINFO
%31 = call i32 @ioctl(i32 %29, i32 %30, %struct.hidraw_devinfo* %8)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %57
36: ; preds = %28
%37 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %8, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load i64, i64* @BUS_USB
%40 = icmp ne i64 %38, %39
br i1 %40, label %53, label %41
41: ; preds = %36
%42 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %8, i32 0, i32 1
%43 = load i64, i64* %42
%44 = icmp ne i64 %43, 1133
br i1 %44, label %53, label %45
45: ; preds = %41
%46 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %8, i32 0, i32 2
%47 = load i64, i64* %46
%48 = icmp ne i64 %47, 50475
br i1 %48, label %49, label %56
49: ; preds = %45
%50 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %8, i32 0, i32 2
%51 = load i64, i64* %50
%52 = icmp ne i64 %51, 50482
br i1 %52, label %53, label %56
53: ; preds = %49, %41, %36
%54 = load i32, i32* @EPERM
store i32 %54, i32* @errno
%55 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %82
56: ; preds = %49, %45
br label %57
57: ; preds = %56, %34
%58 = load i32, i32* %6
%59 = getelementptr inbounds [7 x i8], [7 x i8]* %9, i64 0, i64 0
%60 = call i32 @write(i32 %58, i8* %59, i32 7)
store i32 %60, i32* %7
%61 = load i32, i32* %7
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = load i32, i32* @errno
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %79
67: ; preds = %57
%68 = load i32, i32* %7
%69 = sext i32 %68 to i64
%70 = icmp eq i64 %69, 7
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.6, i64 0, i64 0))
br label %78
73: ; preds = %67
%74 = load i32, i32* @ENOMEM
store i32 %74, i32* @errno
%75 = load i32, i32* %7
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %75, i64 7)
%77 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %78
78: ; preds = %73, %71
br label %79
79: ; preds = %78, %63
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
store i32 0, i32* %3
br label %82
82: ; preds = %79, %53, %26, %13
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.hidraw_devinfo = type { i64, i64, i64 }
@__const.main.magic_sequence = [7 x i8] c"\10\FF\80\B2\01\00\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"No hidraw device given\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"Unable to open device\00"
@HIDIOCGRAWINFO = global i32 0
@.str.2 = [37 x i8] c"error while getting info from device\00"
@BUS_USB = global i64 0
@EPERM = global i32 0
@.str.3 = [53 x i8] c"The given device is not a Logitech Unifying Receiver\00"
@.str.4 = [11 x i8] c"Error: %d\0A\00"
@.str.5 = [6 x i8] c"write\00"
@ENOMEM = global i32 0
@.str.7 = [40 x i8] c"write: %d were written instead of %ld.\0A\00"
@str = [78 x i8] c"The receiver is ready to pair a new device.\0ASwitch your device on to pair it.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.hidraw_devinfo
%4 = alloca [7 x i8]
%5 = bitcast %struct.hidraw_devinfo* %3 to i8*
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %59
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %13, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %59
21: ; preds = %11
%22 = load i32, i32* @HIDIOCGRAWINFO
%23 = call i32 @ioctl(i32 %17, i32 %22, %struct.hidraw_devinfo* %3)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %42
27: ; preds = %21
%28 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %3, i64 0, i32 0
%29 = load i64, i64* %28
%30 = load i64, i64* @BUS_USB
%31 = icmp ne i64 %29, %30
%32 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %3, i64 0, i32 1
%33 = load i64, i64* %32
%34 = icmp ne i64 %33, 1133
%35 = select i1 %31, i1 true, i1 %34
br i1 %35, label %39, label %36
36: ; preds = %27
%37 = getelementptr inbounds %struct.hidraw_devinfo, %struct.hidraw_devinfo* %3, i64 0, i32 2
%38 = load i64, i64* %37
switch i64 %38, label %39 [
i64 50482, label %42
i64 50475, label %42
]
39: ; preds = %36, %27
%40 = load i32, i32* @EPERM
store i32 %40, i32* @errno
%41 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %59
42: ; preds = %36, %36, %25
%43 = call i32 @write(i32 %17, i8* %6, i32 7)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %57
49: ; preds = %42
%50 = icmp eq i32 %43, 7
br i1 %50, label %51, label %53
51: ; preds = %49
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @str, i64 0, i64 0))
br label %57
53: ; preds = %49
%54 = load i32, i32* @ENOMEM
store i32 %54, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %43, i64 7)
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %51, %53, %45
%58 = call i32 @close(i32 %17)
br label %59
59: ; preds = %57, %39, %19, %8
%60 = phi i32 [ 1, %8 ], [ 1, %19 ], [ 0, %57 ], [ 1, %39 ]
ret i32 %60
}