#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ clr_vars(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ get_vars(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ set_vars(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int fd;
  enum { e_get, e_clr, e_set } option;
  if (argc == 1) {
    option = e_get;
  } else if (argc == 2) {
    if (strcmp(argv[1], "-g") == 0) {
      option = e_get;
    } else if (strcmp(argv[1], "-c") == 0) {
      option = e_clr;
    } else if (strcmp(argv[1], "-s") == 0) {
      option = e_set;
    } else {
      fprintf(stderr, "Usage: %s [-g | -c | -s]\n", argv[0]);
      return 1;
    }
  } else {
    fprintf(stderr, "Usage: %s [-g | -c | -s]\n", argv[0]);
    return 1;
  }
  fd = open("/dev/query", O_RDWR);
  if (fd == -1) {
    perror("query_apps open");
    return 2;
  }
  switch (option) {
  case e_get:
    get_vars(fd);
    break;
  case e_clr:
    clr_vars(fd);
    break;
  case e_set:
    set_vars(fd);
    break;
  default:
    break;
  }
  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   $0x1,-0x8(%rbp)
jne    11ac <main+0x2c>
movl   $0x0,-0x18(%rbp)
jmp    1298 <main+0x118>
cmpl   $0x2,-0x8(%rbp)
jne    126b <main+0xeb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11e0 <main+0x60>
movl   $0x0,-0x18(%rbp)
jmp    1266 <main+0xe6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe14(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    120a <main+0x8a>
movl   $0x1,-0x18(%rbp)
jmp    1261 <main+0xe1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xded(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1234 <main+0xb4>
movl   $0x2,-0x18(%rbp)
jmp    125c <main+0xdc>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdbe(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <main+0x1c2>
jmp    1261 <main+0xe1>
jmp    1266 <main+0xe6>
jmp    1293 <main+0x113>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd87(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1342 <main+0x1c2>
jmp    1298 <main+0x118>
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    0xd7b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12d2 <main+0x152>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1342 <main+0x1c2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     1307 <main+0x187>
jmp    12e5 <main+0x165>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
je     1314 <main+0x194>
jmp    12f6 <main+0x176>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     1321 <main+0x1a1>
jmp    132e <main+0x1ae>
mov    -0x14(%rbp),%edi
call   1070 <get_vars@plt>
jmp    1333 <main+0x1b3>
mov    -0x14(%rbp),%edi
call   1060 <clr_vars@plt>
jmp    1333 <main+0x1b3>
mov    -0x14(%rbp),%edi
call   10a0 <set_vars@plt>
jmp    1333 <main+0x1b3>
jmp    1333 <main+0x1b3>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
cmp    $0x1,%edi
je     11fc <main+0x7c>
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11d1 <main+0x51>
mov    0x8(%rbx),%rdi
lea    0xe63(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11fc <main+0x7c>
mov    0x8(%rbx),%rdi
lea    0xe51(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11f0 <main+0x70>
mov    0x8(%rbx),%rdi
lea    0xe3f(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11f7 <main+0x77>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe25(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1248 <main+0xc8>
mov    $0x1,%ebp
jmp    11fc <main+0x7c>
mov    $0x2,%ebp
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xcf>
mov    %eax,%ebx
cmp    $0x2,%ebp
je     122f <main+0xaf>
cmp    $0x1,%ebp
je     1238 <main+0xb8>
test   %ebp,%ebp
jne    123f <main+0xbf>
mov    %ebx,%edi
call   1070 <get_vars@plt>
jmp    123f <main+0xbf>
mov    %ebx,%edi
call   10a0 <set_vars@plt>
jmp    123f <main+0xbf>
mov    %ebx,%edi
call   1060 <clr_vars@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    1248 <main+0xc8>

LLVM IR (-O0):
@.str = [3 x i8] c"-g\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [3 x i8] c"-s\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"Usage: %s [-g | -c | -s]\0A\00"
@.str.4 = [11 x i8] c"/dev/query\00"
@O_RDWR = global i32 0
@.str.5 = [16 x i8] c"query_apps open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %7
br label %51
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %44
14: ; preds = %11
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 0, i32* %7
br label %43
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 1, i32* %7
br label %42
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 2, i32* %7
br label %41
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %39)
store i32 1, i32* %3
br label %73
41: ; preds = %34
br label %42
42: ; preds = %41, %27
br label %43
43: ; preds = %42, %20
br label %50
44: ; preds = %11
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %48)
store i32 1, i32* %3
br label %73
50: ; preds = %43
br label %51
51: ; preds = %50, %10
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %3
br label %73
58: ; preds = %51
%59 = load i32, i32* %7
switch i32 %59, label %69 [
i32 0, label %60
i32 1, label %63
i32 2, label %66
]
60: ; preds = %58
%61 = load i32, i32* %6
%62 = call i32 @get_vars(i32 %61)
br label %70
63: ; preds = %58
%64 = load i32, i32* %6
%65 = call i32 @clr_vars(i32 %64)
br label %70
66: ; preds = %58
%67 = load i32, i32* %6
%68 = call i32 @set_vars(i32 %67)
br label %70
69: ; preds = %58
br label %70
70: ; preds = %69, %66, %63, %60
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %70, %56, %44, %35
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [3 x i8] c"-g\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [3 x i8] c"-s\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"Usage: %s [-g | -c | -s]\0A\00"
@.str.4 = [11 x i8] c"/dev/query\00"
@O_RDWR = global i32 0
@.str.5 = [16 x i8] c"query_apps open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
switch i32 %0, label %20 [
i32 1, label %24
i32 2, label %3
]
3: ; preds = %2
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %1
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %18)
br label %40
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %1
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %40
24: ; preds = %12, %8, %3, %2
%25 = phi i32 [ 0, %2 ], [ 0, %3 ], [ 1, %8 ], [ 2, %12 ]
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %40
31: ; preds = %24
switch i32 %25, label %38 [
i32 0, label %32
i32 1, label %34
i32 2, label %36
]
32: ; preds = %31
%33 = tail call i32 @get_vars(i32 %27)
br label %38
34: ; preds = %31
%35 = tail call i32 @clr_vars(i32 %27)
br label %38
36: ; preds = %31
%37 = tail call i32 @set_vars(i32 %27)
br label %38
38: ; preds = %31, %36, %34, %32
%39 = tail call i32 @close(i32 %27)
br label %40
40: ; preds = %38, %29, %20, %16
%41 = phi i32 [ 2, %29 ], [ 0, %38 ], [ 1, %16 ], [ 1, %20 ]
ret i32 %41
}