#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_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ ioctl(int, int, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_usage(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  int fd;
  int fd_data;
  int buf[100];
  int len;
  if (argc != 2 && argc != 3) {
    print_usage(argv[0]);
    return -1;
  }
  fd = open("/dev/input_emu", O_RDWR);
  if (fd < 0) {
    printf("can't open /dev/input_emu \n");
    return -1;
  }
  if (strcmp(argv[1], "replay") == 0) {
    ioctl(fd, 0);
  } else if (strcmp(argv[1], "write") == 0) {
    if (argc != 3) {
      print_usage(argv[0]);
      return -1;
    }
    fd_data = open(argv[2], O_RDONLY);
    if (fd_data < 0) {
      printf("can't open %s \n", argv[2]);
      return -1;
    }
    while (1) {
      len = read(fd_data, buf, 100);
      if (len == 0) {
        printf("write ok\n");
        break;
      } else {
        write(fd, buf, len);
      }
    }
  } else if (strcmp(argv[1], "tag") == 0) {
    if (argc != 3) {
      print_usage(argv[0]);
      return -1;
    }
    ioctl(fd, 1, argv[2]);
  } else {
    print_usage(argv[0]);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b5 <main+0x45>
cmpl   $0x3,-0x8(%rbp)
je     11b5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f1 <main+0x81>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2b(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1220 <main+0xb0>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
mov    $0x0,%al
call   1050 <ioctl@plt>
jmp    1381 <main+0x211>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe03(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1303 <main+0x193>
cmpl   $0x3,-0x8(%rbp)
je     1260 <main+0xf0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a5 <main+0x135>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
jmp    12aa <main+0x13a>
mov    -0x18(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x1b4(%rbp)
cmpl   $0x0,-0x1b4(%rbp)
jne    12e4 <main+0x174>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fe <main+0x18e>
mov    -0x14(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
mov    -0x1b4(%rbp),%edx
call   1030 <write@plt>
jmp    12aa <main+0x13a>
jmp    137c <main+0x20c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd40(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    135f <main+0x1ef>
cmpl   $0x3,-0x8(%rbp)
je     1343 <main+0x1d3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
mov    $0x1,%esi
mov    $0x0,%al
call   1050 <ioctl@plt>
jmp    1377 <main+0x207>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1388 <main+0x218>
jmp    137c <main+0x20c>
jmp    1381 <main+0x211>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x190,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    12b0 <main+0x130>
mov    %edi,%ebp
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1220 <main+0xa0>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1231 <main+0xb1>
mov    0x8(%rbx),%rdi
lea    0xe3c(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1241 <main+0xc1>
mov    0x8(%rbx),%rdi
lea    0xe3d(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12b0 <main+0x130>
cmp    $0x3,%ebp
jne    12b0 <main+0x130>
mov    0x10(%rbx),%rdx
xor    %ebx,%ebx
mov    %r14d,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <ioctl@plt>
jmp    12bd <main+0x13d>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    12b8 <main+0x138>
xor    %ebx,%ebx
mov    %r14d,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <ioctl@plt>
jmp    12bd <main+0x13d>
cmp    $0x3,%ebp
jne    12b0 <main+0x130>
mov    0x10(%rbx),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     12cb <main+0x14b>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
je     12a0 <main+0x120>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1280 <main+0x100>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    12bd <main+0x13d>
mov    (%rbx),%rdi
call   10a0 <print_usage@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x190,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x10(%rbx),%rsi
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b8 <main+0x138>

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/input_emu\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"can't open /dev/input_emu \0A\00"
@.str.2 = [7 x i8] c"replay\00"
@.str.3 = [6 x i8] c"write\00"
@O_RDONLY = global i32 0
@.str.4 = [16 x i8] c"can't open %s \0A\00"
@.str.5 = [10 x i8] c"write ok\0A\00"
@.str.6 = [4 x i8] c"tag\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 [100 x i32]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @print_usage(i8* %18)
store i32 -1, i32* %3
br label %107
20: ; preds = %12, %2
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %107
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32, i32* %6
%35 = call i32 (i32, i32, ...) @ioctl(i32 %34, i32 0)
br label %106
36: ; preds = %27
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %79
42: ; preds = %36
%43 = load i32, i32* %4
%44 = icmp ne i32 %43, 3
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @print_usage(i8* %48)
store i32 -1, i32* %3
br label %107
50: ; preds = %42
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* %53, i32 %54)
store i32 %55, i32* %7
%56 = load i32, i32* %7
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %50
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %61)
store i32 -1, i32* %3
br label %107
63: ; preds = %50
br label %64
64: ; preds = %63, %77
%65 = load i32, i32* %7
%66 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 0
%67 = call i32 @read(i32 %65, i32* %66, i32 100)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %78
72: ; preds = %64
%73 = load i32, i32* %6
%74 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 0
%75 = load i32, i32* %9
%76 = call i32 @write(i32 %73, i32* %74, i32 %75)
br label %77
77: ; preds = %72
br label %64
78: ; preds = %70
br label %105
79: ; preds = %36
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i64 1
%82 = load i8*, i8** %81
%83 = call i64 @strcmp(i8* %82, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %99
85: ; preds = %79
%86 = load i32, i32* %4
%87 = icmp ne i32 %86, 3
br i1 %87, label %88, label %93
88: ; preds = %85
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 0
%91 = load i8*, i8** %90
%92 = call i32 @print_usage(i8* %91)
store i32 -1, i32* %3
br label %107
93: ; preds = %85
%94 = load i32, i32* %6
%95 = load i8**, i8*** %5
%96 = getelementptr inbounds i8*, i8** %95, i64 2
%97 = load i8*, i8** %96
%98 = call i32 (i32, i32, ...) @ioctl(i32 %94, i32 1, i8* %97)
br label %104
99: ; preds = %79
%100 = load i8**, i8*** %5
%101 = getelementptr inbounds i8*, i8** %100, i64 0
%102 = load i8*, i8** %101
%103 = call i32 @print_usage(i8* %102)
store i32 -1, i32* %3
br label %107
104: ; preds = %93
br label %105
105: ; preds = %104, %78
br label %106
106: ; preds = %105, %33
store i32 0, i32* %3
br label %107
107: ; preds = %106, %99, %88, %58, %45, %25, %15
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/input_emu\00"
@O_RDWR = global i32 0
@.str.2 = [7 x i8] c"replay\00"
@.str.3 = [6 x i8] c"write\00"
@O_RDONLY = global i32 0
@.str.4 = [16 x i8] c"can't open %s \0A\00"
@.str.6 = [4 x i8] c"tag\00"
@str = [9 x i8] c"write ok\00"
@str.7 = [27 x i8] c"can't open /dev/input_emu \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i32]
%4 = bitcast [100 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 3
%6 = and i32 %0, -2
%7 = icmp eq i32 %6, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @print_usage(i8* %9)
br label %67
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %67
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 (i32, i32, ...) @ioctl(i32 %13, i32 0)
br label %67
24: ; preds = %17
%25 = load i8*, i8** %18
%26 = tail call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %52
28: ; preds = %24
br i1 %5, label %32, label %29
29: ; preds = %28
%30 = load i8*, i8** %1
%31 = tail call i32 @print_usage(i8* %30)
br label %67
32: ; preds = %28
%33 = getelementptr inbounds i8*, i8** %1, i64 2
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDONLY
%36 = tail call i32 @open(i8* %34, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %32
%39 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 0
%40 = call i32 @read(i32 %36, i32* %39, i32 100)
%41 = icmp eq i32 %40, 0
br i1 %41, label %45, label %47
42: ; preds = %32
%43 = load i8*, i8** %33
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %67
45: ; preds = %47, %38
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %67
47: ; preds = %38, %47
%48 = phi i32 [ %50, %47 ], [ %40, %38 ]
%49 = call i32 @write(i32 %13, i32* %39, i32 %48)
%50 = call i32 @read(i32 %36, i32* %39, i32 100)
%51 = icmp eq i32 %50, 0
br i1 %51, label %45, label %47
52: ; preds = %24
%53 = load i8*, i8** %18
%54 = tail call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %52
br i1 %5, label %60, label %57
57: ; preds = %56
%58 = load i8*, i8** %1
%59 = tail call i32 @print_usage(i8* %58)
br label %67
60: ; preds = %56
%61 = getelementptr inbounds i8*, i8** %1, i64 2
%62 = load i8*, i8** %61
%63 = tail call i32 (i32, i32, ...) @ioctl(i32 %13, i32 1, i8* %62)
br label %67
64: ; preds = %52
%65 = load i8*, i8** %1
%66 = tail call i32 @print_usage(i8* %65)
br label %67
67: ; preds = %22, %60, %45, %64, %57, %42, %29, %15, %8
%68 = phi i32 [ -1, %8 ], [ -1, %15 ], [ -1, %29 ], [ -1, %42 ], [ -1, %57 ], [ -1, %64 ], [ 0, %45 ], [ 0, %60 ], [ 0, %22 ]
ret i32 %68
}