#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;
};
struct TYPE_5__ {
  int type;
  TYPE_1__ xbutton;
};
typedef TYPE_2__ XEvent;
typedef scalar_t__ Window;
typedef int /*<<< orphan*/ Display;
typedef int /*<<< orphan*/ Cursor;

/* Variables and functions */
#define ButtonPress 129
int ButtonPressMask;
#define ButtonRelease 128
int ButtonReleaseMask;
int /*<<< orphan*/ CurrentTime;
int /*<<< orphan*/ False;
int /*<<< orphan*/ GrabModeAsync;
int /*<<< orphan*/ GrabModeSync;
int GrabSuccess;
scalar_t__ None;
scalar_t__ RootWindow(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ SyncPointer;
int /*<<< orphan*/ XAllowEvents(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ XC_crosshair;
int /*<<< orphan*/ XCreateFontCursor(int /*<<< orphan*/ *, int /*<<< orphan*/);
int XGrabPointer(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/, int,
                 int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__,
                 int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ XUngrabPointer(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ XWindowEvent(int /*<<< orphan*/ *, scalar_t__, int,
                                TYPE_2__ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
Window Select_Window(Display *dpy, int screen) {
  int status;
  Cursor cursor;
  XEvent event;
  Window target_win = None, root = RootWindow(dpy, screen);
  int buttons = 0;
  cursor = XCreateFontCursor(dpy, XC_crosshair);
  status = XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
                        GrabModeSync, GrabModeAsync, root, cursor, CurrentTime);
  if (status != GrabSuccess) {
    fprintf(stderr, "Can't grab the mouse.\n");
    exit(1);
  }
  while ((target_win == None) || (buttons != 0)) {
    XAllowEvents(dpy, SyncPointer, CurrentTime);
    XWindowEvent(dpy, root, ButtonPressMask | ButtonReleaseMask, &event);
    switch (event.type) {
    case ButtonPress:
      if (target_win == None) {
        target_win = event.xbutton.subwindow;
        if (target_win == None)
          target_win = root;
      }
      buttons++;
      break;
    case ButtonRelease:
      if (buttons > 0)
        buttons--;
      break;
    }
  }
  XUngrabPointer(dpy, CurrentTime);
  return target_win;
}


x86 assembly (clang, -O0):
<Select_Window>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1080 <RootWindow@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1040 <XCreateFontCursor@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    0x2db6(%rip),%rax
or     (%rax),%ecx
mov    0x2de5(%rip),%rax
mov    (%rax),%r8d
mov    0x2db3(%rip),%rax
mov    (%rax),%r9d
mov    -0x38(%rbp),%r11
mov    -0x14(%rbp),%r10d
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %r11,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1050 <XGrabPointer@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%eax
je     1257 <Select_Window+0xd7>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    125c <Select_Window+0xdc>
mov    -0x30(%rbp),%rcx
mov    $0x1,%al
mov    0x2d57(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x3d(%rbp)
je     127f <Select_Window+0xff>
cmpl   $0x0,-0x3c(%rbp)
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    128f <Select_Window+0x10f>
jmp    1360 <Select_Window+0x1e0>
mov    -0x8(%rbp),%rdi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
call   1030 <XAllowEvents@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
or     %eax,%edx
lea    -0x28(%rbp),%rcx
call   1090 <XWindowEvent@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x80,%eax
je     1343 <Select_Window+0x1c3>
jmp    12e5 <Select_Window+0x165>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
jne    135b <Select_Window+0x1db>
jmp    12f8 <Select_Window+0x178>
mov    -0x30(%rbp),%rax
mov    0x2cbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1335 <Select_Window+0x1b5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2ca1(%rip),%rcx
cmp    (%rcx),%rax
jne    1330 <Select_Window+0x1b0>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1335 <Select_Window+0x1b5>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    135b <Select_Window+0x1db>
cmpl   $0x0,-0x3c(%rbp)
jle    1356 <Select_Window+0x1d6>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    135b <Select_Window+0x1db>
jmp    125c <Select_Window+0xdc>
mov    -0x8(%rbp),%rdi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
call   1070 <XUngrabPointer@plt>
mov    -0x30(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Select_Window>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e28(%rip),%r13
mov    0x0(%r13),%r14
call   1080 <RootWindow@plt>
mov    %rax,%r15
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <XCreateFontCursor@plt>
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e0b(%rip),%rsi
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
mov    0x2e01(%rip),%rsi
mov    (%rsi),%r8d
mov    0x2dcf(%rip),%rsi
mov    (%rsi),%r9d
mov    0x2dfd(%rip),%rsi
mov    (%rsi),%ebp
sub    $0x8,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
push   %rbp
push   %rax
push   %r15
call   1050 <XGrabPointer@plt>
add    $0x20,%rsp
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%eax
jne    12da <Select_Window+0x15a>
cmp    0x0(%r13),%r14
jne    12b7 <Select_Window+0x137>
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1030 <XAllowEvents@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
mov    0x2d76(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rcx
call   1090 <XWindowEvent@plt>
mov    0x8(%rsp),%eax
cmp    $0x80,%eax
je     12a0 <Select_Window+0x120>
cmp    $0x81,%eax
jne    1296 <Select_Window+0x116>
mov    0x0(%r13),%rax
mov    0x10(%rsp),%rcx
cmp    %rax,%rcx
cmove  %r15,%rcx
cmp    %rax,%r14
cmove  %rcx,%r14
add    $0x1,%ebp
cmp    0x0(%r13),%r14
jne    12af <Select_Window+0x12f>
jmp    1230 <Select_Window+0xb0>
xchg   %ax,%ax
xor    %eax,%eax
test   %ebp,%ebp
setg   %al
sub    %eax,%ebp
cmp    0x0(%r13),%r14
je     1230 <Select_Window+0xb0>
test   %ebp,%ebp
jne    1230 <Select_Window+0xb0>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <XUngrabPointer@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
lea    0xd16(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_5__ = type { i32, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
@None = global i64 0
@XC_crosshair = global i32 0
@False = global i32 0
@ButtonPressMask = global i32 0
@ButtonReleaseMask = global i32 0
@GrabModeSync = global i32 0
@GrabModeAsync = global i32 0
@CurrentTime = global i32 0
@GrabSuccess = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Can't grab the mouse.\0A\00"
@SyncPointer = global i32 0
define i64 @Select_Window(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.TYPE_5__
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%11 = load i64, i64* @None
store i64 %11, i64* %8
%12 = load i32*, i32** %3
%13 = load i32, i32* %4
%14 = call i64 @RootWindow(i32* %12, i32 %13)
store i64 %14, i64* %9
store i32 0, i32* %10
%15 = load i32*, i32** %3
%16 = load i32, i32* @XC_crosshair
%17 = call i32 @XCreateFontCursor(i32* %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32*, i32** %3
%19 = load i64, i64* %9
%20 = load i32, i32* @False
%21 = load i32, i32* @ButtonPressMask
%22 = load i32, i32* @ButtonReleaseMask
%23 = or i32 %21, %22
%24 = load i32, i32* @GrabModeSync
%25 = load i32, i32* @GrabModeAsync
%26 = load i64, i64* %9
%27 = load i32, i32* %6
%28 = load i32, i32* @CurrentTime
%29 = call i32 @XGrabPointer(i32* %18, i64 %19, i32 %20, i32 %23, i32 %24, i32 %25, i64 %26, i32 %27, i32 %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = load i32, i32* @GrabSuccess
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %2
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %2
br label %38
38: ; preds = %84, %37
%39 = load i64, i64* %8
%40 = load i64, i64* @None
%41 = icmp eq i64 %39, %40
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = load i32, i32* %10
%44 = icmp ne i32 %43, 0
br label %45
45: ; preds = %42, %38
%46 = phi i1 [ true, %38 ], [ %44, %42 ]
br i1 %46, label %47, label %85
47: ; preds = %45
%48 = load i32*, i32** %3
%49 = load i32, i32* @SyncPointer
%50 = load i32, i32* @CurrentTime
%51 = call i32 @XAllowEvents(i32* %48, i32 %49, i32 %50)
%52 = load i32*, i32** %3
%53 = load i64, i64* %9
%54 = load i32, i32* @ButtonPressMask
%55 = load i32, i32* @ButtonReleaseMask
%56 = or i32 %54, %55
%57 = call i32 @XWindowEvent(i32* %52, i64 %53, i32 %56, %struct.TYPE_5__* %7)
%58 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %7, i32 0, i32 0
%59 = load i32, i32* %58
switch i32 %59, label %84 [
i32 129, label %60
i32 128, label %77
]
60: ; preds = %47
%61 = load i64, i64* %8
%62 = load i64, i64* @None
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %74
64: ; preds = %60
%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 0
%67 = load i64, i64* %66
store i64 %67, i64* %8
%68 = load i64, i64* %8
%69 = load i64, i64* @None
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = load i64, i64* %9
store i64 %72, i64* %8
br label %73
73: ; preds = %71, %64
br label %74
74: ; preds = %73, %60
%75 = load i32, i32* %10
%76 = add nsw i32 %75, 1
store i32 %76, i32* %10
br label %84
77: ; preds = %47
%78 = load i32, i32* %10
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i32, i32* %10
%82 = add nsw i32 %81, -1
store i32 %82, i32* %10
br label %83
83: ; preds = %80, %77
br label %84
84: ; preds = %47, %83, %74
br label %38
85: ; preds = %45
%86 = load i32*, i32** %3
%87 = load i32, i32* @CurrentTime
%88 = call i32 @XUngrabPointer(i32* %86, i32 %87)
%89 = load i64, i64* %8
ret i64 %89
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i32, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
@None = global i64 0
@XC_crosshair = global i32 0
@False = global i32 0
@ButtonPressMask = global i32 0
@ButtonReleaseMask = global i32 0
@GrabModeSync = global i32 0
@GrabModeAsync = global i32 0
@CurrentTime = global i32 0
@GrabSuccess = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Can't grab the mouse.\0A\00"
@SyncPointer = global i32 0
define i64 @Select_Window(i32* %0, i32 %1) {
%3 = alloca %struct.TYPE_5__
%4 = bitcast %struct.TYPE_5__* %3 to i8*
%5 = load i64, i64* @None
%6 = tail call i64 @RootWindow(i32* %0, i32 %1)
%7 = load i32, i32* @XC_crosshair
%8 = tail call i32 @XCreateFontCursor(i32* %0, i32 %7)
%9 = load i32, i32* @False
%10 = load i32, i32* @ButtonPressMask
%11 = load i32, i32* @ButtonReleaseMask
%12 = or i32 %11, %10
%13 = load i32, i32* @GrabModeSync
%14 = load i32, i32* @GrabModeAsync
%15 = load i32, i32* @CurrentTime
%16 = tail call i32 @XGrabPointer(i32* %0, i64 %6, i32 %9, i32 %12, i32 %13, i32 %14, i64 %6, i32 %8, i32 %15)
%17 = load i32, i32* @GrabSuccess
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i64, i64* @None
%21 = icmp eq i64 %5, %20
br i1 %21, label %22, label %59
22: ; preds = %19
%23 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %3, i64 0, i32 1, i32 0
br label %29
25: ; preds = %2
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %22, %52
%30 = phi i32 [ 0, %22 ], [ %54, %52 ]
%31 = phi i64 [ %5, %22 ], [ %53, %52 ]
%32 = load i32, i32* @SyncPointer
%33 = load i32, i32* @CurrentTime
%34 = call i32 @XAllowEvents(i32* %0, i32 %32, i32 %33)
%35 = load i32, i32* @ButtonPressMask
%36 = load i32, i32* @ButtonReleaseMask
%37 = or i32 %36, %35
%38 = call i32 @XWindowEvent(i32* %0, i64 %6, i32 %37, %struct.TYPE_5__* %3)
%39 = load i32, i32* %23
switch i32 %39, label %52 [
i32 129, label %40
i32 128, label %48
]
40: ; preds = %29
%41 = load i64, i64* @None
%42 = icmp eq i64 %31, %41
%43 = load i64, i64* %24
%44 = icmp eq i64 %43, %41
%45 = select i1 %44, i64 %6, i64 %43
%46 = select i1 %42, i64 %45, i64 %31
%47 = add nsw i32 %30, 1
br label %52
48: ; preds = %29
%49 = icmp sgt i32 %30, 0
%50 = sext i1 %49 to i32
%51 = add nsw i32 %30, %50
br label %52
52: ; preds = %48, %29, %40
%53 = phi i64 [ %31, %29 ], [ %46, %40 ], [ %31, %48 ]
%54 = phi i32 [ %30, %29 ], [ %47, %40 ], [ %51, %48 ]
%55 = load i64, i64* @None
%56 = icmp eq i64 %53, %55
%57 = icmp ne i32 %54, 0
%58 = select i1 %56, i1 true, i1 %57
br i1 %58, label %29, label %59
59: ; preds = %52, %19
%60 = phi i64 [ %5, %19 ], [ %53, %52 ]
%61 = load i32, i32* @CurrentTime
%62 = call i32 @XUngrabPointer(i32* %0, i32 %61)
ret i64 %60
}