#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  char type;
  int /*<<< orphan*/ xkey;
};
typedef TYPE_1__ XEvent;
typedef int /*<<< orphan*/ Window;
typedef int /*<<< orphan*/ KeySym;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ False;
char KeyPress;
int KeyPressMask;
int KeyReleaseMask;
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int XLookupString(int /*<<< orphan*/ *, char *, int, int /*<<< orphan*/ *,
                  int /*<<< orphan*/);
int /*<<< orphan*/ XNextEvent(int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XSelectInput(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ XSync(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ stderr;
int main(void) {
  Display *d;
  Window r;
  XEvent e;
  KeySym key;
  char text[256];
  d = XOpenDisplay(NULL);
  if (d == NULL) {
    fprintf(stderr, "Cannot open display\n");
    exit(1);
  }
  r = DefaultRootWindow(d);
  XSelectInput(d, r, KeyPressMask | KeyReleaseMask);
  XSync(d, False);
  while (!XNextEvent(d, &e)) {
    if (e.type == KeyPress && XLookupString(&e.xkey, text, 255, &key, 0) == 1) {
      if (text[0] == 'q') {
        break;
      }
      printf("'%c' key!\n", text[0]);
    } else {
      printf("event '%d'\n", e.type);
    }
  }
  XCloseDisplay(d);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e9 <main+0x49>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <DefaultRootWindow@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    0x2db4(%rip),%rax
or     (%rax),%edx
call   10a0 <XSelectInput@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1090 <XSync@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <XNextEvent@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1247 <main+0xa7>
jmp    12c8 <main+0x128>
movsbl -0x20(%rbp),%eax
mov    0x2d86(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    12b3 <main+0x113>
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
lea    -0x24(%rbp),%rcx
xor    %r8d,%r8d
call   1030 <XLookupString@plt>
cmp    $0x1,%eax
jne    12b3 <main+0x113>
movsbl -0x130(%rbp),%eax
cmp    $0x71,%eax
jne    129b <main+0xfb>
jmp    12c8 <main+0x128>
lea    0xd73(%rip),%rdi
movsbl -0x130(%rbp),%esi
call   1050 <printf@plt>
jmp    12c3 <main+0x123>
lea    0xd66(%rip),%rdi
movsbl -0x20(%rbp),%esi
call   1050 <printf@plt>
jmp    1225 <main+0x85>
mov    -0x10(%rbp),%rdi
call   10c0 <XCloseDisplay@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
xor    %edi,%edi
call   1060 <XOpenDisplay@plt>
test   %rax,%rax
je     12ad <main+0x10d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <DefaultRootWindow@plt>
mov    0x2e0d(%rip),%rcx
mov    0x2de6(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <XSelectInput@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <XSync@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <XNextEvent@plt>
test   %eax,%eax
je     1224 <main+0x84>
mov    %rbx,%rdi
call   10c0 <XCloseDisplay@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r12
mov    %rsp,%rbp
lea    0xde1(%rip),%r13
jmp    126e <main+0xce>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rsp),%eax
movsbl %al,%esi
mov    %r13,%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <XNextEvent@plt>
test   %eax,%eax
jne    1208 <main+0x68>
movzbl (%rsp),%eax
cmp    (%r14),%al
jne    1254 <main+0xb4>
lea    0x4(%rsp),%rdi
mov    %r15,%rsi
mov    $0xff,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1030 <XLookupString@plt>
cmp    $0x1,%eax
jne    1250 <main+0xb0>
movzbl 0x10(%rsp),%eax
cmp    $0x71,%al
je     1208 <main+0x68>
movsbl %al,%esi
lea    0xd6a(%rip),%rdi
jmp    125a <main+0xba>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd43(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i8, i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open display\0A\00"
@KeyPressMask = global i32 0
@KeyReleaseMask = global i32 0
@False = global i32 0
@KeyPress = global i8 0
@.str.1 = [11 x i8] c"'%c' key!\0A\00"
@.str.2 = [12 x i8] c"event '%d'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.TYPE_3__
%5 = alloca i32
%6 = alloca [256 x i8]
store i32 0, i32* %1
%7 = call i32* @XOpenDisplay(i32* null)
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32*, i32** %2
%16 = call i32 @DefaultRootWindow(i32* %15)
store i32 %16, i32* %3
%17 = load i32*, i32** %2
%18 = load i32, i32* %3
%19 = load i32, i32* @KeyPressMask
%20 = load i32, i32* @KeyReleaseMask
%21 = or i32 %19, %20
%22 = call i32 @XSelectInput(i32* %17, i32 %18, i32 %21)
%23 = load i32*, i32** %2
%24 = load i32, i32* @False
%25 = call i32 @XSync(i32* %23, i32 %24)
br label %26
26: ; preds = %57, %14
%27 = load i32*, i32** %2
%28 = call i32 @XNextEvent(i32* %27, %struct.TYPE_3__* %4)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %58
31: ; preds = %26
%32 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i8, i8* @KeyPress
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %34, %36
br i1 %37, label %38, label %53
38: ; preds = %31
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 1
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%41 = call i32 @XLookupString(i32* %39, i8* %40, i32 255, i32* %5, i32 0)
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 113
br i1 %47, label %48, label %49
48: ; preds = %43
br label %58
49: ; preds = %43
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%51 = load i8, i8* %50
%52 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 signext %51)
br label %57
53: ; preds = %38, %31
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %4, i32 0, i32 0
%55 = load i8, i8* %54
%56 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %55)
br label %57
57: ; preds = %53, %49
br label %26
58: ; preds = %48, %26
%59 = load i32*, i32** %2
%60 = call i32 @XCloseDisplay(i32* %59)
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i8, i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open display\0A\00"
@KeyPressMask = global i32 0
@KeyReleaseMask = global i32 0
@False = global i32 0
@KeyPress = global i8 0
@.str.1 = [11 x i8] c"'%c' key!\0A\00"
@.str.2 = [12 x i8] c"event '%d'\0A\00"
define i32 @main() {
%1 = alloca %struct.TYPE_3__
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @XOpenDisplay(i32* null)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = tail call i32 @DefaultRootWindow(i32* %7)
%15 = load i32, i32* @KeyPressMask
%16 = load i32, i32* @KeyReleaseMask
%17 = or i32 %16, %15
%18 = tail call i32 @XSelectInput(i32* %7, i32 %14, i32 %17)
%19 = load i32, i32* @False
%20 = tail call i32 @XSync(i32* %7, i32 %19)
%21 = call i32 @XNextEvent(i32* %7, %struct.TYPE_3__* %1)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %45
23: ; preds = %13
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
br label %25
25: ; preds = %23, %42
%26 = load i8, i8* %4
%27 = load i8, i8* @KeyPress
%28 = icmp eq i8 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %25
%30 = call i32 @XLookupString(i32* %24, i8* %6, i32 255, i32* %2, i32 0)
%31 = icmp eq i32 %30, 1
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = load i8, i8* %4
br label %39
34: ; preds = %29
%35 = load i8, i8* %6
%36 = icmp eq i8 %35, 113
br i1 %36, label %45, label %37
37: ; preds = %34
%38 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 signext %35)
br label %42
39: ; preds = %32, %25
%40 = phi i8 [ %33, %32 ], [ %26, %25 ]
%41 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8 signext %40)
br label %42
42: ; preds = %39, %37
%43 = call i32 @XNextEvent(i32* %7, %struct.TYPE_3__* %1)
%44 = icmp eq i32 %43, 0
br i1 %44, label %25, label %45
45: ; preds = %42, %34, %13
%46 = call i32 @XCloseDisplay(i32* %7)
ret i32 0
}