#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 input_event {
  int type;
  int code;
  int value;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, struct input_event *, int);
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  int fd;
  int mode = 0;
  if (argc < 2) {
    printf("usage: %s <device>\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  struct input_event ev;
  while (1) {
    read(fd, &ev, sizeof(struct input_event));
    if ((ev.type == 1) && (ev.code == 29)) {
      if (((ev.value == 2) || (ev.value == 1)) && (mode == 0)) {
        system("xdotool keydown Mode_switch; xdotool keydown SHIFT");
        mode = 2;
      } else if ((ev.value == 0) && (mode == 2)) {
        system("xdotool keyup Mode_switch; xdotool keyup SHIFT");
        mode = 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1185 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cmpl   $0x1,-0x28(%rbp)
jne    122a <main+0xea>
cmpl   $0x1d,-0x24(%rbp)
jne    122a <main+0xea>
cmpl   $0x2,-0x20(%rbp)
je     11d7 <main+0x97>
cmpl   $0x1,-0x20(%rbp)
jne    11f9 <main+0xb9>
cmpl   $0x0,-0x18(%rbp)
jne    11f9 <main+0xb9>
lea    0xe2c(%rip),%rdi
call   1030 <system@plt>
movl   $0x2,-0x18(%rbp)
jmp    1225 <main+0xe5>
cmpl   $0x0,-0x20(%rbp)
jne    1220 <main+0xe0>
cmpl   $0x2,-0x18(%rbp)
jne    1220 <main+0xe0>
lea    0xe33(%rip),%rdi
call   1030 <system@plt>
movl   $0x0,-0x18(%rbp)
jmp    1225 <main+0xe5>
jmp    122a <main+0xea>
jmp    119e <main+0x5e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     1172 <main+0x32>
mov    (%rsi),%rsi
lea    0xea5(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsi),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
xor    %ebp,%ebp
mov    %rsp,%r14
lea    0xe82(%rip),%r15
lea    0xeae(%rip),%r12
jmp    11b0 <main+0x70>
mov    $0x2,%ebp
mov    %r15,%rdi
call   1030 <system@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <read@plt>
cmpl   $0x1,(%rsp)
jne    11b0 <main+0x70>
cmpl   $0x1d,0x4(%rsp)
jne    11b0 <main+0x70>
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
cmp    $0x1,%ecx
ja     11dc <main+0x9c>
test   %ebp,%ebp
je     119b <main+0x5b>
test   %eax,%eax
jne    11b0 <main+0x70>
cmp    $0x2,%ebp
jne    11b0 <main+0x70>
xor    %ebp,%ebp
mov    %r12,%rdi
call   1030 <system@plt>
jmp    11b0 <main+0x70>

LLVM IR (-O0):
%struct.input_event = type { i32, i32, i32 }
@.str = [20 x i8] c"usage: %s <device>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [51 x i8] c"xdotool keydown Mode_switch; xdotool keydown SHIFT\00"
@.str.2 = [47 x i8] c"xdotool keyup Mode_switch; xdotool keyup SHIFT\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.input_event
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14)
ret i32 1
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_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %16, %56
%23 = load i32, i32* %6
%24 = call i32 @read(i32 %23, %struct.input_event* %8, i32 12)
%25 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 0
%26 = load i32, i32* %25
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %56
28: ; preds = %22
%29 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 1
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 29
br i1 %31, label %32, label %56
32: ; preds = %28
%33 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 2
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, 2
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 2
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %45
40: ; preds = %36, %32
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @system(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %7
br label %55
45: ; preds = %40, %36
%46 = getelementptr inbounds %struct.input_event, %struct.input_event* %8, i32 0, i32 2
%47 = load i32, i32* %46
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, 2
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @system(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %7
br label %54
54: ; preds = %52, %49, %45
br label %55
55: ; preds = %54, %43
br label %56
56: ; preds = %55, %28, %22
br label %22
}

LLVM IR (-O2):
%struct.input_event = type { i32, i32, i32 }
@.str = [20 x i8] c"usage: %s <device>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [51 x i8] c"xdotool keydown Mode_switch; xdotool keydown SHIFT\00"
@.str.2 = [47 x i8] c"xdotool keyup Mode_switch; xdotool keyup SHIFT\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.input_event
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %6)
ret i32 1
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = bitcast %struct.input_event* %3 to i8*
%14 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 0
%15 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 1
%16 = getelementptr inbounds %struct.input_event, %struct.input_event* %3, i64 0, i32 2
br label %17
17: ; preds = %37, %8
%18 = phi i32 [ %39, %37 ], [ 0, %8 ]
%19 = icmp eq i32 %18, 0
%20 = icmp eq i32 %18, 2
br label %21
21: ; preds = %36, %17
%22 = call i32 @read(i32 %12, %struct.input_event* %3, i32 12)
%23 = load i32, i32* %14
%24 = icmp eq i32 %23, 1
%25 = load i32, i32* %15
%26 = icmp eq i32 %25, 29
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %28, label %36
28: ; preds = %21
%29 = load i32, i32* %16
%30 = add i32 %29, -1
%31 = icmp ult i32 %30, 2
%32 = and i1 %31, %19
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = icmp eq i32 %29, 0
%35 = and i1 %34, %20
br i1 %35, label %37, label %36
36: ; preds = %33, %21
br label %21
37: ; preds = %33, %28
%38 = phi i8* [ getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), %28 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), %33 ]
%39 = phi i32 [ 2, %28 ], [ 0, %33 ]
%40 = tail call i32 @system(i8* %38)
br label %17
}