#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_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ subwindow;
  unsigned int button;
};
struct TYPE_5__ {
  scalar_t__ type;
  TYPE_1__ xbutton;
};
typedef TYPE_2__ XEvent;
typedef scalar_t__ Window;
typedef int /*<<< orphan*/ Display;
typedef int /*<<< orphan*/ Cursor;

/* Variables and functions */
scalar_t__ ButtonPress;
int /*<<< orphan*/ ButtonPressMask;
int /*<<< orphan*/ CurrentTime;
int /*<<< orphan*/ False;
int /*<<< orphan*/ GrabModeAsync;
int /*<<< orphan*/ GrabModeSync;
scalar_t__ GrabSuccess;
scalar_t__ None;
scalar_t__ RootWindow(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ SyncPointer;
int /*<<< orphan*/ XAllowEvents(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ XC_bogosity;
int /*<<< orphan*/ XCreateFontCursor(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ XDefaultScreen(int /*<<< orphan*/ *);
char *XDisplayName(int /*<<< orphan*/);
scalar_t__ XGrabPointer(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
                        int /*<<< orphan*/, int /*<<< orphan*/,
                        int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/,
                        int /*<<< orphan*/);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/);
int /*<<< orphan*/ XSync(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ XWindowEvent(int /*<<< orphan*/ *, scalar_t__,
                                int /*<<< orphan*/, TYPE_2__ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
Window findVictim(unsigned int *button) {
  Display *display = XOpenDisplay(0);
  if (!display) {
    fprintf(stderr, "Could not open display %s\n", XDisplayName(0));
    exit(1);
  }
  Window root = RootWindow(display, XDefaultScreen(display));
  Cursor cursor = XCreateFontCursor(display, XC_bogosity);
  XSync(display, 1);
  if (XGrabPointer(display, root, False, ButtonPressMask, GrabModeSync,
                   GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
    fprintf(stderr, "Cursor-grabbing failed!\n");
    exit(1);
  }
  Window victim = None;
  while (victim == None) {
    XEvent event;
    XAllowEvents(display, SyncPointer, CurrentTime);
    XWindowEvent(display, root, ButtonPressMask, &event);
    if (event.type == ButtonPress) {
      victim = event.xbutton.subwindow;
      *button = event.xbutton.button;
    }
  }
  return victim;
}


x86 assembly (clang, -O0):
<findVictim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1203 <findVictim+0x53>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
xor    %edi,%edi
call   1090 <XDisplayName@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <XDefaultScreen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
call   10b0 <RootWindow@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
call   1040 <XCreateFontCursor@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   10a0 <XSync@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
mov    0x2d72(%rip),%rax
mov    (%rax),%ecx
mov    0x2d71(%rip),%rax
mov    (%rax),%r8d
mov    0x2d47(%rip),%rax
mov    (%rax),%r9d
mov    0x2d4d(%rip),%rax
mov    (%rax),%r11
mov    -0x1c(%rbp),%r10d
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %r11,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1050 <XGrabPointer@plt>
mov    0x2cec(%rip),%rcx
cmp    (%rcx),%rax
je     12ce <findVictim+0x11e>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
jne    134f <findVictim+0x19f>
mov    -0x10(%rbp),%rdi
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
mov    0x2ce4(%rip),%rax
mov    (%rax),%edx
call   1030 <XAllowEvents@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2cb6(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
call   10c0 <XWindowEvent@plt>
mov    -0x40(%rbp),%rax
mov    0x2c78(%rip),%rcx
cmp    (%rcx),%rax
jne    134a <findVictim+0x19a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
jmp    12dc <findVictim+0x12c>
mov    -0x28(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findVictim>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x8(%rsp)
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
test   %rax,%rax
je     12f5 <findVictim+0x145>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <XDefaultScreen@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   10b0 <RootWindow@plt>
mov    %rax,%r15
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1040 <XCreateFontCursor@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
call   10a0 <XSync@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
mov    (%rax),%ecx
mov    0x2db4(%rip),%rax
mov    (%rax),%r8d
mov    0x2d8a(%rip),%rax
mov    (%rax),%r9d
mov    0x2d90(%rip),%rbp
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r12,%rdi
mov    %r15,%rsi
push   %rax
push   %rbx
push   0x0(%rbp)
call   1050 <XGrabPointer@plt>
add    $0x20,%rsp
mov    0x2d30(%rip),%rcx
cmp    (%rcx),%rax
jne    1322 <findVictim+0x172>
mov    0x0(%rbp),%rbx
mov    0x2d2c(%rip),%rbp
lea    0x10(%rsp),%r13
mov    0x2d28(%rip),%r14
jmp    129c <findVictim+0xec>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2d31(%rip),%rax
cmp    (%rax),%rbx
jne    12e3 <findVictim+0x133>
mov    0x0(%rbp),%esi
mov    0x2d42(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
call   1030 <XAllowEvents@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rcx
call   10c0 <XWindowEvent@plt>
mov    0x10(%rsp),%rax
cmp    (%r14),%rax
jne    1290 <findVictim+0xe0>
mov    0x18(%rsp),%rbx
mov    0x20(%rsp),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
jmp    1290 <findVictim+0xe0>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb4(%rip),%rax
mov    (%rax),%ebx
xor    %edi,%edi
call   1090 <XDisplayName@plt>
lea    0xcf4(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
lea    0xce9(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Could not open display %s\0A\00"
@XC_bogosity = global i32 0
@False = global i32 0
@ButtonPressMask = global i32 0
@GrabModeSync = global i32 0
@GrabModeAsync = global i32 0
@None = global i64 0
@CurrentTime = global i32 0
@GrabSuccess = global i64 0
@.str.1 = [25 x i8] c"Cursor-grabbing failed!\0A\00"
@SyncPointer = global i32 0
@ButtonPress = global i64 0
define i64 @findVictim(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.TYPE_5__
store i32* %0, i32** %2
%8 = call i32* @XOpenDisplay(i32 0)
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i8* @XDisplayName(i32 0)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = load i32*, i32** %3
%19 = call i32 @XDefaultScreen(i32* %18)
%20 = call i64 @RootWindow(i32* %17, i32 %19)
store i64 %20, i64* %4
%21 = load i32*, i32** %3
%22 = load i32, i32* @XC_bogosity
%23 = call i32 @XCreateFontCursor(i32* %21, i32 %22)
store i32 %23, i32* %5
%24 = load i32*, i32** %3
%25 = call i32 @XSync(i32* %24, i32 1)
%26 = load i32*, i32** %3
%27 = load i64, i64* %4
%28 = load i32, i32* @False
%29 = load i32, i32* @ButtonPressMask
%30 = load i32, i32* @GrabModeSync
%31 = load i32, i32* @GrabModeAsync
%32 = load i64, i64* @None
%33 = load i32, i32* %5
%34 = load i32, i32* @CurrentTime
%35 = call i64 @XGrabPointer(i32* %26, i64 %27, i32 %28, i32 %29, i32 %30, i32 %31, i64 %32, i32 %33, i32 %34)
%36 = load i64, i64* @GrabSuccess
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %16
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %16
%43 = load i64, i64* @None
store i64 %43, i64* %6
br label %44
44: ; preds = %69, %42
%45 = load i64, i64* %6
%46 = load i64, i64* @None
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %70
48: ; preds = %44
%49 = load i32*, i32** %3
%50 = load i32, i32* @SyncPointer
%51 = load i32, i32* @CurrentTime
%52 = call i32 @XAllowEvents(i32* %49, i32 %50, i32 %51)
%53 = load i32*, i32** %3
%54 = load i64, i64* %4
%55 = load i32, i32* @ButtonPressMask
%56 = call i32 @XWindowEvent(i32* %53, i64 %54, i32 %55, %struct.TYPE_5__* %7)
%57 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* @ButtonPress
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %69
61: ; preds = %48
%62 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i32 0, i32 1
%63 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %62, i32 0, i32 0
%64 = load i64, i64* %63
store i64 %64, i64* %6
%65 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i32 0, i32 1
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %65, i32 0, i32 1
%67 = load i32, i32* %66
%68 = load i32*, i32** %2
store i32 %67, i32* %68
br label %69
69: ; preds = %61, %48
br label %44
70: ; preds = %44
%71 = load i64, i64* %6
ret i64 %71
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64, i32 }
@stderr = global i32 0
@.str = [27 x i8] c"Could not open display %s\0A\00"
@XC_bogosity = global i32 0
@False = global i32 0
@ButtonPressMask = global i32 0
@GrabModeSync = global i32 0
@GrabModeAsync = global i32 0
@None = global i64 0
@CurrentTime = global i32 0
@GrabSuccess = global i64 0
@.str.1 = [25 x i8] c"Cursor-grabbing failed!\0A\00"
@SyncPointer = global i32 0
@ButtonPress = global i64 0
define i64 @findVictim(i32* nocapture writeonly %0) {
%2 = alloca %struct.TYPE_5__
%3 = tail call i32* @XOpenDisplay(i32 0)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i8* @XDisplayName(i32 0)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 @XDefaultScreen(i32* %3)
%12 = tail call i64 @RootWindow(i32* %3, i32 %11)
%13 = load i32, i32* @XC_bogosity
%14 = tail call i32 @XCreateFontCursor(i32* %3, i32 %13)
%15 = tail call i32 @XSync(i32* %3, i32 1)
%16 = load i32, i32* @False
%17 = load i32, i32* @ButtonPressMask
%18 = load i32, i32* @GrabModeSync
%19 = load i32, i32* @GrabModeAsync
%20 = load i64, i64* @None
%21 = load i32, i32* @CurrentTime
%22 = tail call i64 @XGrabPointer(i32* %3, i64 %12, i32 %16, i32 %17, i32 %18, i32 %19, i64 %20, i32 %14, i32 %21)
%23 = load i64, i64* @GrabSuccess
%24 = icmp eq i64 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %10
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %10
%30 = load i64, i64* @None
%31 = bitcast %struct.TYPE_5__* %2 to i8*
%32 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 1, i32 0
%34 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %2, i64 0, i32 1, i32 1
br label %35
35: ; preds = %29, %48
%36 = phi i64 [ %30, %29 ], [ %49, %48 ]
%37 = load i32, i32* @SyncPointer
%38 = load i32, i32* @CurrentTime
%39 = call i32 @XAllowEvents(i32* %3, i32 %37, i32 %38)
%40 = load i32, i32* @ButtonPressMask
%41 = call i32 @XWindowEvent(i32* %3, i64 %12, i32 %40, %struct.TYPE_5__* %2)
%42 = load i64, i64* %32
%43 = load i64, i64* @ButtonPress
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i64, i64* %33
%47 = load i32, i32* %34
store i32 %47, i32* %0
br label %48
48: ; preds = %45, %35
%49 = phi i64 [ %46, %45 ], [ %36, %35 ]
%50 = load i64, i64* @None
%51 = icmp eq i64 %49, %50
br i1 %51, label %35, label %52
52: ; preds = %48
ret i64 %49
}