#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 */
typedef int /*<<< orphan*/ command;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int setgid(int /*<<< orphan*/);
int setuid(int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int system(char *);
int main(int argc, char *argv[]) {
  if (argc != 2 ||
      (strcmp(argv[1], "start") != 0 && strcmp(argv[1], "stop") != 0 &&
       strcmp(argv[1], "status") != 0)) {
    fprintf(stderr, "Usage: %s <start|stop|status>\n", argv[0]);
    return EXIT_FAILURE;
  }
  if (setuid(0) == -1) {
    perror("setuid");
    return EXIT_FAILURE;
  }
  if (setgid(0) == -1) {
    perror("setgid");
    return EXIT_FAILURE;
  }
  int status = EXIT_FAILURE;
  const char *display = getenv("DISPLAY");
  const char *debug = getenv("XKEYSNAIL_DEBUG") ? "/dev/stderr" : "/dev/null";
  if (strcmp(argv[1], "start") == 0) {
    if (display == NULL || display[0] == '\0') {
      setenv("DISPLAY", ":0", 1);
    }
    char command[4096] = {'\0'};
    snprintf(command, sizeof(command),
             "xhost +SI:localuser:root >>%s 2>&1; "
             "xkeysnail ~/.config/xkeysnail/config.py --quiet --watch >>%s "
             "2>&1 &",
             debug, debug);
    status = system(command);
    status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                   : WEXITSTATUS(status));
    if (status == EXIT_SUCCESS) {
      for (int i = 0; i < 3; ++i) {
        status = system("pgrep -x xkeysnail >/dev/null 2>&1");
        status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                       : WEXITSTATUS(status));
        if (status == EXIT_SUCCESS) {
          break;
        }
        sleep(1);
      }
    }
    return status;
  } else if (strcmp(argv[1], "stop") == 0) {
    status = system("killall xkeysnail");
    return ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                 : WEXITSTATUS(status));
  } else if (strcmp(argv[1], "status") == 0) {
    status = system("pgrep -x xkeysnail >/dev/null 2>&1");
    status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                   : WEXITSTATUS(status));
    printf("%s\n", (status == 0 ? "started" : "stoped"));
    return status;
  }
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    125d <main+0x7d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdee(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     128a <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     128a <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdbd(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     128a <main+0xaa>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9e(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
xor    %edi,%edi
call   10f0 <setuid@plt>
cmp    $0xffffffff,%eax
jne    12b7 <main+0xd7>
lea    0xd90(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
xor    %edi,%edi
call   10c0 <setgid@plt>
cmp    $0xffffffff,%eax
jne    12e4 <main+0x104>
lea    0xd6a(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xd48(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
lea    0xd40(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdx
lea    0xd4d(%rip),%rax
lea    0xd3a(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xcc8(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    14c5 <main+0x2e5>
cmpq   $0x0,-0x20(%rbp)
je     1362 <main+0x182>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    137a <main+0x19a>
lea    0xcd6(%rip),%rdi
lea    0xcfd(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1000,%esi
lea    0xcc8(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x1030(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     13d7 <main+0x1f7>
mov    -0x14(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    13eb <main+0x20b>
mov    0x2bfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1038(%rbp)
jmp    13f9 <main+0x219>
mov    -0x14(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,-0x1038(%rbp)
mov    -0x1038(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2bc4(%rip),%rcx
cmp    (%rcx),%eax
jne    14ba <main+0x2da>
movl   $0x0,-0x1034(%rbp)
cmpl   $0x3,-0x1034(%rbp)
jge    14b5 <main+0x2d5>
lea    0xca6(%rip),%rdi
call   1050 <system@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1455 <main+0x275>
mov    -0x14(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1469 <main+0x289>
mov    0x2b7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x103c(%rbp)
jmp    1477 <main+0x297>
mov    -0x14(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,-0x103c(%rbp)
mov    -0x103c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2b46(%rip),%rcx
cmp    (%rcx),%eax
jne    1497 <main+0x2b7>
jmp    14b5 <main+0x2d5>
mov    $0x1,%edi
call   1100 <sleep@plt>
mov    -0x1034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1034(%rbp)
jmp    141e <main+0x23e>
jmp    14ba <main+0x2da>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xb32(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    153d <main+0x35d>
lea    0xc11(%rip),%rdi
call   1050 <system@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     150d <main+0x32d>
mov    -0x14(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1521 <main+0x341>
mov    0x2ac4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1040(%rbp)
jmp    152f <main+0x34f>
mov    -0x14(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,-0x1040(%rbp)
mov    -0x1040(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xabf(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    15df <main+0x3ff>
lea    0xb76(%rip),%rdi
call   1050 <system@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1585 <main+0x3a5>
mov    -0x14(%rbp),%edi
call   10e0 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1599 <main+0x3b9>
mov    0x2a4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1044(%rbp)
jmp    15a7 <main+0x3c7>
mov    -0x14(%rbp),%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,-0x1044(%rbp)
mov    -0x1044(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
lea    0xb5f(%rip),%rsi
lea    0xb50(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
lea    0xb3e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    15f5 <main+0x415>
jmp    15e4 <main+0x404>
jmp    15e9 <main+0x409>
mov    0x29e8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1233 <main+0x53>
mov    0x8(%rbx),%rdi
lea    0xe01(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1263 <main+0x83>
mov    0x8(%rbx),%rdi
lea    0xdf2(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1263 <main+0x83>
mov    0x8(%rbx),%rdi
lea    0xde2(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1263 <main+0x83>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdcc(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   10f0 <setuid@plt>
cmp    $0xffffffff,%eax
je     1490 <main+0x2b0>
xor    %edi,%edi
call   10c0 <setgid@plt>
cmp    $0xffffffff,%eax
je     1499 <main+0x2b9>
lea    0xdb5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
lea    0xdae(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xdbb(%rip),%rax
lea    0xda8(%rip),%r14
cmove  %rax,%r14
mov    0x8(%rbx),%rdi
lea    0xd42(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     132e <main+0x14e>
mov    0x8(%rbx),%rdi
lea    0xd33(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     13a2 <main+0x1c2>
mov    0x8(%rbx),%rdi
lea    0xd1f(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    124b <main+0x6b>
lea    0xdd7(%rip),%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     1462 <main+0x282>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     1462 <main+0x282>
mov    %ebx,%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    146b <main+0x28b>
test   %r15,%r15
je     1339 <main+0x159>
cmpb   $0x0,(%r15)
jne    1351 <main+0x171>
lea    0xcff(%rip),%rdi
lea    0xd26(%rip),%rsi
mov    $0x1,%edx
call   1040 <setenv@plt>
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0xd06(%rip),%rdx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14,%rcx
mov    %r14,%r8
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     13db <main+0x1fb>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     13db <main+0x1fb>
mov    %ebx,%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    13e4 <main+0x204>
lea    0xd52(%rip),%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     124b <main+0x6b>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     124b <main+0x6b>
mov    %ebx,%edi
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    10b0 <WEXITSTATUS@plt>
mov    0x2bf6(%rip),%rax
mov    (%rax),%ebx
mov    0x2be5(%rip),%r14
cmp    (%r14),%ebx
jne    1254 <main+0x74>
lea    0xcdd(%rip),%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     141d <main+0x23d>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     141d <main+0x23d>
mov    %ebx,%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    1426 <main+0x246>
mov    0x2bb4(%rip),%rax
mov    (%rax),%ebx
cmp    (%r14),%ebx
je     1254 <main+0x74>
mov    $0x1,%edi
call   1100 <sleep@plt>
lea    0xc98(%rip),%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     14aa <main+0x2ca>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     14aa <main+0x2ca>
mov    %ebx,%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    14b3 <main+0x2d3>
mov    0x2b6f(%rip),%rax
mov    (%rax),%ebx
test   %ebx,%ebx
lea    0xc9d(%rip),%rax
lea    0xc9e(%rip),%rsi
cmove  %rax,%rsi
lea    0xc87(%rip),%rdi
call   1060 <printf@plt>
jmp    1254 <main+0x74>
lea    0xb9a(%rip),%rdi
jmp    14a0 <main+0x2c0>
lea    0xb98(%rip),%rdi
call   10d0 <perror@plt>
jmp    124b <main+0x6b>
mov    0x2b27(%rip),%rax
mov    (%rax),%ebx
cmp    (%r14),%ebx
je     1254 <main+0x74>
mov    $0x1,%edi
call   1100 <sleep@plt>
lea    0xc0b(%rip),%rdi
call   1050 <system@plt>
cmp    $0xffffffff,%eax
je     14ef <main+0x30f>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <WIFEXITED@plt>
test   %eax,%eax
je     14ef <main+0x30f>
mov    %ebx,%edi
call   10b0 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    14f8 <main+0x318>
mov    0x2ae2(%rip),%rax
mov    (%rax),%ebx
cmp    (%r14),%ebx
je     1254 <main+0x74>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    1254 <main+0x74>

LLVM IR (-O0):
@.str = [6 x i8] c"start\00"
@.str.1 = [5 x i8] c"stop\00"
@.str.2 = [7 x i8] c"status\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"Usage: %s <start|stop|status>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [7 x i8] c"setuid\00"
@.str.5 = [7 x i8] c"setgid\00"
@.str.6 = [8 x i8] c"DISPLAY\00"
@.str.7 = [16 x i8] c"XKEYSNAIL_DEBUG\00"
@.str.8 = [12 x i8] c"/dev/stderr\00"
@.str.9 = [10 x i8] c"/dev/null\00"
@.str.10 = [3 x i8] c":0\00"
@.str.11 = [104 x i8] c"xhost +SI:localuser:root >>%s 2>&1; xkeysnail ~/.config/xkeysnail/config.py --quiet --watch >>%s 2>&1 &\00"
@EXIT_SUCCESS = global i32 0
@.str.12 = [35 x i8] c"pgrep -x xkeysnail >/dev/null 2>&1\00"
@.str.13 = [18 x i8] c"killall xkeysnail\00"
@.str.14 = [4 x i8] c"%s\0A\00"
@.str.15 = [8 x i8] c"started\00"
@.str.16 = [7 x i8] c"stoped\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %31, label %13
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %13
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %19
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %25, %2
%32 = load i32, i32* @stderr
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %3
br label %180
38: ; preds = %25, %19, %13
%39 = call i32 @setuid(i32 0)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
store i32 %43, i32* %3
br label %180
44: ; preds = %38
%45 = call i32 @setgid(i32 0)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
store i32 %49, i32* %3
br label %180
50: ; preds = %44
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %6
%52 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i8* %52, i8** %7
%53 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%54 = icmp ne i8* %53, null
%55 = zext i1 %54 to i64
%56 = select i1 %54, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0)
store i8* %56, i8** %8
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i64 @strcmp(i8* %59, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %128
62: ; preds = %50
%63 = load i8*, i8** %7
%64 = icmp eq i8* %63, null
br i1 %64, label %71, label %65
65: ; preds = %62
%66 = load i8*, i8** %7
%67 = getelementptr inbounds i8, i8* %66, i64 0
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %65, %62
%72 = call i32 @setenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 1)
br label %73
73: ; preds = %71, %65
%74 = bitcast [4096 x i8]* %9 to i8*
%75 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%76 = load i8*, i8** %8
%77 = load i8*, i8** %8
%78 = call i32 @snprintf(i8* %75, i32 4096, i8* getelementptr inbounds ([104 x i8], [104 x i8]* @.str.11, i64 0, i64 0), i8* %76, i8* %77)
%79 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%80 = call i32 @system(i8* %79)
store i32 %80, i32* %6
%81 = load i32, i32* %6
%82 = icmp eq i32 %81, -1
br i1 %82, label %87, label %83
83: ; preds = %73
%84 = load i32, i32* %6
%85 = call i32 @WIFEXITED(i32 %84)
%86 = icmp ne i32 %85, 0
br i1 %86, label %89, label %87
87: ; preds = %83, %73
%88 = load i32, i32* @EXIT_FAILURE
br label %92
89: ; preds = %83
%90 = load i32, i32* %6
%91 = call i32 @WEXITSTATUS(i32 %90)
br label %92
92: ; preds = %89, %87
%93 = phi i32 [ %88, %87 ], [ %91, %89 ]
store i32 %93, i32* %6
%94 = load i32, i32* %6
%95 = load i32, i32* @EXIT_SUCCESS
%96 = icmp eq i32 %94, %95
br i1 %96, label %97, label %126
97: ; preds = %92
store i32 0, i32* %10
br label %98
98: ; preds = %122, %97
%99 = load i32, i32* %10
%100 = icmp slt i32 %99, 3
br i1 %100, label %101, label %125
101: ; preds = %98
%102 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
store i32 %102, i32* %6
%103 = load i32, i32* %6
%104 = icmp eq i32 %103, -1
br i1 %104, label %109, label %105
105: ; preds = %101
%106 = load i32, i32* %6
%107 = call i32 @WIFEXITED(i32 %106)
%108 = icmp ne i32 %107, 0
br i1 %108, label %111, label %109
109: ; preds = %105, %101
%110 = load i32, i32* @EXIT_FAILURE
br label %114
111: ; preds = %105
%112 = load i32, i32* %6
%113 = call i32 @WEXITSTATUS(i32 %112)
br label %114
114: ; preds = %111, %109
%115 = phi i32 [ %110, %109 ], [ %113, %111 ]
store i32 %115, i32* %6
%116 = load i32, i32* %6
%117 = load i32, i32* @EXIT_SUCCESS
%118 = icmp eq i32 %116, %117
br i1 %118, label %119, label %120
119: ; preds = %114
br label %125
120: ; preds = %114
%121 = call i32 @sleep(i32 1)
br label %122
122: ; preds = %120
%123 = load i32, i32* %10
%124 = add nsw i32 %123, 1
store i32 %124, i32* %10
br label %98
125: ; preds = %119, %98
br label %126
126: ; preds = %125, %92
%127 = load i32, i32* %6
store i32 %127, i32* %3
br label %180
128: ; preds = %50
%129 = load i8**, i8*** %5
%130 = getelementptr inbounds i8*, i8** %129, i64 1
%131 = load i8*, i8** %130
%132 = call i64 @strcmp(i8* %131, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%133 = icmp eq i64 %132, 0
br i1 %133, label %134, label %149
134: ; preds = %128
%135 = call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0))
store i32 %135, i32* %6
%136 = load i32, i32* %6
%137 = icmp eq i32 %136, -1
br i1 %137, label %142, label %138
138: ; preds = %134
%139 = load i32, i32* %6
%140 = call i32 @WIFEXITED(i32 %139)
%141 = icmp ne i32 %140, 0
br i1 %141, label %144, label %142
142: ; preds = %138, %134
%143 = load i32, i32* @EXIT_FAILURE
br label %147
144: ; preds = %138
%145 = load i32, i32* %6
%146 = call i32 @WEXITSTATUS(i32 %145)
br label %147
147: ; preds = %144, %142
%148 = phi i32 [ %143, %142 ], [ %146, %144 ]
store i32 %148, i32* %3
br label %180
149: ; preds = %128
%150 = load i8**, i8*** %5
%151 = getelementptr inbounds i8*, i8** %150, i64 1
%152 = load i8*, i8** %151
%153 = call i64 @strcmp(i8* %152, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%154 = icmp eq i64 %153, 0
br i1 %154, label %155, label %176
155: ; preds = %149
%156 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
store i32 %156, i32* %6
%157 = load i32, i32* %6
%158 = icmp eq i32 %157, -1
br i1 %158, label %163, label %159
159: ; preds = %155
%160 = load i32, i32* %6
%161 = call i32 @WIFEXITED(i32 %160)
%162 = icmp ne i32 %161, 0
br i1 %162, label %165, label %163
163: ; preds = %159, %155
%164 = load i32, i32* @EXIT_FAILURE
br label %168
165: ; preds = %159
%166 = load i32, i32* %6
%167 = call i32 @WEXITSTATUS(i32 %166)
br label %168
168: ; preds = %165, %163
%169 = phi i32 [ %164, %163 ], [ %167, %165 ]
store i32 %169, i32* %6
%170 = load i32, i32* %6
%171 = icmp eq i32 %170, 0
%172 = zext i1 %171 to i64
%173 = select i1 %171, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.15, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.16, i64 0, i64 0)
%174 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0), i8* %173)
%175 = load i32, i32* %6
store i32 %175, i32* %3
br label %180
176: ; preds = %149
br label %177
177: ; preds = %176
br label %178
178: ; preds = %177
%179 = load i32, i32* @EXIT_FAILURE
store i32 %179, i32* %3
br label %180
180: ; preds = %178, %168, %147, %126, %47, %41, %31
%181 = load i32, i32* %3
ret i32 %181
}

LLVM IR (-O2):
@.str = [6 x i8] c"start\00"
@.str.1 = [5 x i8] c"stop\00"
@.str.2 = [7 x i8] c"status\00"
@stderr = global i32 0
@.str.3 = [31 x i8] c"Usage: %s <start|stop|status>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [7 x i8] c"setuid\00"
@.str.5 = [7 x i8] c"setgid\00"
@.str.6 = [8 x i8] c"DISPLAY\00"
@.str.7 = [16 x i8] c"XKEYSNAIL_DEBUG\00"
@.str.8 = [12 x i8] c"/dev/stderr\00"
@.str.9 = [10 x i8] c"/dev/null\00"
@.str.10 = [3 x i8] c":0\00"
@.str.11 = [104 x i8] c"xhost +SI:localuser:root >>%s 2>&1; xkeysnail ~/.config/xkeysnail/config.py --quiet --watch >>%s 2>&1 &\00"
@EXIT_SUCCESS = global i32 0
@.str.12 = [35 x i8] c"pgrep -x xkeysnail >/dev/null 2>&1\00"
@.str.13 = [18 x i8] c"killall xkeysnail\00"
@.str.14 = [4 x i8] c"%s\0A\00"
@.str.15 = [8 x i8] c"started\00"
@.str.16 = [7 x i8] c"stoped\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %5
%11 = load i8*, i8** %6
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %14, %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
br label %149
23: ; preds = %14, %10, %5
%24 = tail call i32 @setuid(i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
br label %149
29: ; preds = %23
%30 = tail call i32 @setgid(i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
br label %149
35: ; preds = %29
%36 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%38 = icmp eq i8* %37, null
%39 = select i1 %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0)
%40 = load i8*, i8** %6
%41 = tail call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %114
43: ; preds = %35
%44 = icmp eq i8* %36, null
br i1 %44, label %48, label %45
45: ; preds = %43
%46 = load i8, i8* %36
%47 = icmp eq i8 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45, %43
%49 = tail call i32 @setenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 1)
br label %50
50: ; preds = %48, %45
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%52 = call i32 @snprintf(i8* %51, i32 4096, i8* getelementptr inbounds ([104 x i8], [104 x i8]* @.str.11, i64 0, i64 0), i8* %39, i8* %39)
%53 = call i32 @system(i8* %51)
%54 = icmp eq i32 %53, -1
br i1 %54, label %58, label %55
55: ; preds = %50
%56 = call i32 @WIFEXITED(i32 %53)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55, %50
%59 = load i32, i32* @EXIT_FAILURE
br label %62
60: ; preds = %55
%61 = call i32 @WEXITSTATUS(i32 %53)
br label %62
62: ; preds = %60, %58
%63 = phi i32 [ %59, %58 ], [ %61, %60 ]
%64 = load i32, i32* @EXIT_SUCCESS
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %112
66: ; preds = %62
%67 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
%68 = icmp eq i32 %67, -1
br i1 %68, label %72, label %69
69: ; preds = %66
%70 = call i32 @WIFEXITED(i32 %67)
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %69, %66
%73 = load i32, i32* @EXIT_FAILURE
br label %76
74: ; preds = %69
%75 = call i32 @WEXITSTATUS(i32 %67)
br label %76
76: ; preds = %74, %72
%77 = phi i32 [ %73, %72 ], [ %75, %74 ]
%78 = load i32, i32* @EXIT_SUCCESS
%79 = icmp eq i32 %77, %78
br i1 %79, label %112, label %80
80: ; preds = %76
%81 = call i32 @sleep(i32 1)
%82 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
%83 = icmp eq i32 %82, -1
br i1 %83, label %89, label %84
84: ; preds = %80
%85 = call i32 @WIFEXITED(i32 %82)
%86 = icmp eq i32 %85, 0
br i1 %86, label %89, label %87
87: ; preds = %84
%88 = call i32 @WEXITSTATUS(i32 %82)
br label %91
89: ; preds = %84, %80
%90 = load i32, i32* @EXIT_FAILURE
br label %91
91: ; preds = %89, %87
%92 = phi i32 [ %90, %89 ], [ %88, %87 ]
%93 = load i32, i32* @EXIT_SUCCESS
%94 = icmp eq i32 %92, %93
br i1 %94, label %112, label %95
95: ; preds = %91
%96 = call i32 @sleep(i32 1)
%97 = call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
%98 = icmp eq i32 %97, -1
br i1 %98, label %104, label %99
99: ; preds = %95
%100 = call i32 @WIFEXITED(i32 %97)
%101 = icmp eq i32 %100, 0
br i1 %101, label %104, label %102
102: ; preds = %99
%103 = call i32 @WEXITSTATUS(i32 %97)
br label %106
104: ; preds = %99, %95
%105 = load i32, i32* @EXIT_FAILURE
br label %106
106: ; preds = %104, %102
%107 = phi i32 [ %105, %104 ], [ %103, %102 ]
%108 = load i32, i32* @EXIT_SUCCESS
%109 = icmp eq i32 %107, %108
br i1 %109, label %112, label %110
110: ; preds = %106
%111 = call i32 @sleep(i32 1)
br label %112
112: ; preds = %76, %91, %106, %110, %62
%113 = phi i32 [ %63, %62 ], [ %77, %76 ], [ %92, %91 ], [ %107, %106 ], [ %107, %110 ]
br label %149
114: ; preds = %35
%115 = load i8*, i8** %6
%116 = tail call i64 @strcmp(i8* %115, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%117 = icmp eq i64 %116, 0
br i1 %117, label %118, label %128
118: ; preds = %114
%119 = tail call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0))
%120 = icmp eq i32 %119, -1
br i1 %120, label %124, label %121
121: ; preds = %118
%122 = tail call i32 @WIFEXITED(i32 %119)
%123 = icmp eq i32 %122, 0
br i1 %123, label %124, label %126
124: ; preds = %121, %118
%125 = load i32, i32* @EXIT_FAILURE
br label %149
126: ; preds = %121
%127 = tail call i32 @WEXITSTATUS(i32 %119)
br label %149
128: ; preds = %114
%129 = load i8*, i8** %6
%130 = tail call i64 @strcmp(i8* %129, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%131 = icmp eq i64 %130, 0
br i1 %131, label %132, label %147
132: ; preds = %128
%133 = tail call i32 @system(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.12, i64 0, i64 0))
%134 = icmp eq i32 %133, -1
br i1 %134, label %138, label %135
135: ; preds = %132
%136 = tail call i32 @WIFEXITED(i32 %133)
%137 = icmp eq i32 %136, 0
br i1 %137, label %138, label %140
138: ; preds = %135, %132
%139 = load i32, i32* @EXIT_FAILURE
br label %142
140: ; preds = %135
%141 = tail call i32 @WEXITSTATUS(i32 %133)
br label %142
142: ; preds = %140, %138
%143 = phi i32 [ %139, %138 ], [ %141, %140 ]
%144 = icmp eq i32 %143, 0
%145 = select i1 %144, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.15, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.16, i64 0, i64 0)
%146 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.14, i64 0, i64 0), i8* %145)
br label %149
147: ; preds = %128
%148 = load i32, i32* @EXIT_FAILURE
br label %149
149: ; preds = %112, %142, %147, %126, %124, %32, %26, %18
%150 = phi i32 [ %22, %18 ], [ %28, %26 ], [ %34, %32 ], [ %113, %112 ], [ %143, %142 ], [ %148, %147 ], [ %125, %124 ], [ %127, %126 ]
ret i32 %150
}