#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
struct TYPE_4__ {
  scalar_t__ window;
};
struct TYPE_5__ {
  scalar_t__ type;
  TYPE_1__ xany;
};
typedef TYPE_2__ XEvent;
typedef scalar_t__ Window;
typedef int /*<<< orphan*/ Time;
typedef int /*<<< orphan*/ Display;
typedef scalar_t__ Atom;

/* Variables and functions */
scalar_t__ ConnectionNumber(int /*<<< orphan*/ *);
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ False;
scalar_t__ SelectionNotify;
scalar_t__ XA_STRING;
int /*<<< orphan*/ XConvertSelection(int /*<<< orphan*/ *, scalar_t__,
                                     scalar_t__, scalar_t__, scalar_t__,
                                     int /*<<< orphan*/);
scalar_t__ XCreateSimpleWindow(int /*<<< orphan*/ *, int /*<<< orphan*/,
                               int /*<<< orphan*/, int /*<<< orphan*/, int, int,
                               int /*<<< orphan*/, int /*<<< orphan*/,
                               int /*<<< orphan*/);
scalar_t__ XGetWindowProperty(int /*<<< orphan*/ *, scalar_t__, scalar_t__,
                              int /*<<< orphan*/, int, int /*<<< orphan*/,
                              scalar_t__, scalar_t__ *, int *, unsigned long *,
                              unsigned long *, unsigned char **);
scalar_t__ XInternAtom(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ XNextEvent(int /*<<< orphan*/ *, TYPE_2__ *);
scalar_t__ XPending(int /*<<< orphan*/ *);
int /*<<< orphan*/ getTimestamp(int /*<<< orphan*/ *, scalar_t__);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *fetchSelection(Display *dpy, const char *selection) {
  Atom selatom = XInternAtom(dpy, selection, False);
  Atom clipatom = XInternAtom(dpy, "CLIPBOARD", False);
  Time now;
  XEvent ev;
  Window win;
  int ok = 0;
  struct timeval timeout;
  fd_set fdset;
  win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0);
  now = getTimestamp(dpy, win);
  XConvertSelection(dpy, selatom, XA_STRING, clipatom, win, now);
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  while (0) {
    int res;
    if (XPending(dpy) == 0) {
      FD_ZERO(&fdset);
      FD_SET(ConnectionNumber(dpy), &fdset);
      res = select(ConnectionNumber(dpy) + 1, &fdset, NULL, NULL, &timeout);
      if (res <= 0) {
        ok = 0;
        break;
      }
    }
    if (res > 0 || XPending(dpy) > 0) {
      XNextEvent(dpy, &ev);
      if (ev.type == SelectionNotify && ev.xany.window == win) {
        ok = 1;
        break;
      }
    }
  }
  if (ok) {
    Atom rtype;
    int bits;
    unsigned long len, bytes;
    unsigned char *data;
    if (XGetWindowProperty(dpy, win, clipatom, 0, (4 * 1024 * 1024) / 4, False,
                           XA_STRING, &rtype, &bits, &len, &bytes, &data) != 0)
      return NULL;
    if ((rtype != XA_STRING) || (bits != 8)) {
      return NULL;
    } else {
      return (char *)data;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<fetchSelection>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc8,%rsp
mov    %rdi,-0x28(%rbp)
mov    %rsi,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   10a0 <XInternAtom@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xdfa(%rip),%rsi
call   10a0 <XInternAtom@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <DefaultRootWindow@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,%esi
xor    %ecx,%ecx
mov    $0x1,%r9d
mov    %ecx,%edx
mov    %r9d,%r8d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x10(%rsp)
call   10b0 <XCreateSimpleWindow@plt>
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   1030 <getTimestamp@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d61(%rip),%rax
mov    (%rax),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x60(%rbp),%r8
mov    -0x44(%rbp),%r9d
call   10e0 <XConvertSelection@plt>
movl   $0x1,-0x78(%rbp)
movq   $0x0,-0x70(%rbp)
xor    %eax,%eax
test   $0x1,%al
jne    12b1 <fetchSelection+0xf1>
jmp    1386 <fetchSelection+0x1c6>
mov    -0x28(%rbp),%rdi
call   1040 <XPending@plt>
cmp    $0x0,%rax
jne    1324 <fetchSelection+0x164>
lea    -0x7c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ConnectionNumber@plt>
mov    %rax,%rdi
lea    -0x7c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ConnectionNumber@plt>
mov    %rax,%rdi
add    $0x1,%rdi
lea    -0x7c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x78(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x80(%rbp)
cmpl   $0x0,-0x80(%rbp)
jg     131f <fetchSelection+0x15f>
movl   $0x0,-0x64(%rbp)
jmp    1386 <fetchSelection+0x1c6>
jmp    1324 <fetchSelection+0x164>
cmpl   $0x0,-0x80(%rbp)
jg     1341 <fetchSelection+0x181>
mov    -0x28(%rbp),%rdi
call   1040 <XPending@plt>
cmp    $0x0,%rax
jle    1381 <fetchSelection+0x1c1>
mov    -0x28(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1090 <XNextEvent@plt>
mov    -0x58(%rbp),%rax
mov    0x2c9f(%rip),%rcx
cmp    (%rcx),%rax
jne    137c <fetchSelection+0x1bc>
mov    -0x50(%rbp),%rax
cmp    -0x60(%rbp),%rax
jne    137c <fetchSelection+0x1bc>
movl   $0x1,-0x64(%rbp)
jmp    1386 <fetchSelection+0x1c6>
jmp    1381 <fetchSelection+0x1c1>
jmp    12a2 <fetchSelection+0xe2>
cmpl   $0x0,-0x64(%rbp)
je     1455 <fetchSelection+0x295>
mov    -0x28(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    0x2c35(%rip),%rax
mov    (%rax),%r9d
mov    0x2c33(%rip),%rax
mov    (%rax),%r15
xor    %ecx,%ecx
mov    $0x100000,%r8d
lea    -0x88(%rbp),%r14
lea    -0x8c(%rbp),%rbx
lea    -0x98(%rbp),%r11
lea    -0xa0(%rbp),%r10
lea    -0xa8(%rbp),%rax
mov    %r15,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   10c0 <XGetWindowProperty@plt>
cmp    $0x0,%rax
je     1414 <fetchSelection+0x254>
movq   $0x0,-0x20(%rbp)
jmp    145d <fetchSelection+0x29d>
mov    -0x88(%rbp),%rax
mov    0x2bbe(%rip),%rcx
cmp    (%rcx),%rax
jne    1438 <fetchSelection+0x278>
cmpl   $0x8,-0x8c(%rbp)
je     1445 <fetchSelection+0x285>
movq   $0x0,-0x20(%rbp)
jmp    145d <fetchSelection+0x29d>
mov    -0xa8(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    145d <fetchSelection+0x29d>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0xc8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetchSelection>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7e(%rip),%r15
mov    (%r15),%edx
call   1050 <XInternAtom@plt>
mov    %rax,%r14
mov    (%r15),%edx
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1050 <XInternAtom@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <DefaultRootWindow@plt>
sub    $0x8,%rsp
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    $0x1,%r9d
push   $0x0
push   $0x0
push   $0x0
call   1060 <XCreateSimpleWindow@plt>
add    $0x20,%rsp
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <getTimestamp@plt>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rcx
mov    %r12,%r8
mov    %eax,%r9d
call   1070 <XConvertSelection@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
%struct.timeval = type { i32, i64 }
@False = global i32 0
@.str = [10 x i8] c"CLIPBOARD\00"
@XA_STRING = global i64 0
@SelectionNotify = global i64 0
define i8* @fetchSelection(i32* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.TYPE_5__
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
%20 = load i32*, i32** %4
%21 = load i8*, i8** %5
%22 = load i32, i32* @False
%23 = call i64 @XInternAtom(i32* %20, i8* %21, i32 %22)
store i64 %23, i64* %6
%24 = load i32*, i32** %4
%25 = load i32, i32* @False
%26 = call i64 @XInternAtom(i32* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
store i64 %26, i64* %7
store i32 0, i32* %11
%27 = load i32*, i32** %4
%28 = load i32*, i32** %4
%29 = call i32 @DefaultRootWindow(i32* %28)
%30 = call i64 @XCreateSimpleWindow(i32* %27, i32 %29, i32 0, i32 0, i32 1, i32 1, i32 0, i32 0, i32 0)
store i64 %30, i64* %10
%31 = load i32*, i32** %4
%32 = load i64, i64* %10
%33 = call i32 @getTimestamp(i32* %31, i64 %32)
store i32 %33, i32* %8
%34 = load i32*, i32** %4
%35 = load i64, i64* %6
%36 = load i64, i64* @XA_STRING
%37 = load i64, i64* %7
%38 = load i64, i64* %10
%39 = load i32, i32* %8
%40 = call i32 @XConvertSelection(i32* %34, i64 %35, i64 %36, i64 %37, i64 %38, i32 %39)
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 1, i32* %41
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %42
br label %43
43: ; preds = %83, %2
br i1 false, label %44, label %84
44: ; preds = %43
%45 = load i32*, i32** %4
%46 = call i64 @XPending(i32* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %61
48: ; preds = %44
%49 = call i32 @FD_ZERO(i32* %13)
%50 = load i32*, i32** %4
%51 = call i64 @ConnectionNumber(i32* %50)
%52 = call i32 @FD_SET(i64 %51, i32* %13)
%53 = load i32*, i32** %4
%54 = call i64 @ConnectionNumber(i32* %53)
%55 = add nsw i64 %54, 1
%56 = call i32 @select(i64 %55, i32* %13, i32* null, i32* null, %struct.timeval* %12)
store i32 %56, i32* %14
%57 = load i32, i32* %14
%58 = icmp sle i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %48
store i32 0, i32* %11
br label %84
60: ; preds = %48
br label %61
61: ; preds = %60, %44
%62 = load i32, i32* %14
%63 = icmp sgt i32 %62, 0
br i1 %63, label %68, label %64
64: ; preds = %61
%65 = load i32*, i32** %4
%66 = call i64 @XPending(i32* %65)
%67 = icmp sgt i64 %66, 0
br i1 %67, label %68, label %83
68: ; preds = %64, %61
%69 = load i32*, i32** %4
%70 = call i32 @XNextEvent(i32* %69, %struct.TYPE_5__* %9)
%71 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %9, i32 0, i32 0
%72 = load i64, i64* %71
%73 = load i64, i64* @SelectionNotify
%74 = icmp eq i64 %72, %73
br i1 %74, label %75, label %82
75: ; preds = %68
%76 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %9, i32 0, i32 1
%77 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %76, i32 0, i32 0
%78 = load i64, i64* %77
%79 = load i64, i64* %10
%80 = icmp eq i64 %78, %79
br i1 %80, label %81, label %82
81: ; preds = %75
store i32 1, i32* %11
br label %84
82: ; preds = %75, %68
br label %83
83: ; preds = %82, %64
br label %43
84: ; preds = %81, %59, %43
%85 = load i32, i32* %11
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %106
87: ; preds = %84
%88 = load i32*, i32** %4
%89 = load i64, i64* %10
%90 = load i64, i64* %7
%91 = load i32, i32* @False
%92 = load i64, i64* @XA_STRING
%93 = call i64 @XGetWindowProperty(i32* %88, i64 %89, i64 %90, i32 0, i32 1048576, i32 %91, i64 %92, i64* %15, i32* %16, i64* %17, i64* %18, i8** %19)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %87
store i8* null, i8** %3
br label %107
96: ; preds = %87
%97 = load i64, i64* %15
%98 = load i64, i64* @XA_STRING
%99 = icmp ne i64 %97, %98
br i1 %99, label %103, label %100
100: ; preds = %96
%101 = load i32, i32* %16
%102 = icmp ne i32 %101, 8
br i1 %102, label %103, label %104
103: ; preds = %100, %96
store i8* null, i8** %3
br label %107
104: ; preds = %100
%105 = load i8*, i8** %19
store i8* %105, i8** %3
br label %107
106: ; preds = %84
store i8* null, i8** %3
br label %107
107: ; preds = %106, %104, %103, %95
%108 = load i8*, i8** %3
ret i8* %108
}

LLVM IR (-O2):
%struct.TYPE_5__ = type { i64, %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i64 }
@False = global i32 0
@.str = [10 x i8] c"CLIPBOARD\00"
@XA_STRING = global i64 0
@SelectionNotify = global i64 0
define noalias i8* @fetchSelection(i32* %0, i8* %1) {
%3 = load i32, i32* @False
%4 = tail call i64 @XInternAtom(i32* %0, i8* %1, i32 %3)
%5 = load i32, i32* @False
%6 = tail call i64 @XInternAtom(i32* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @DefaultRootWindow(i32* %0)
%8 = tail call i64 @XCreateSimpleWindow(i32* %0, i32 %7, i32 0, i32 0, i32 1, i32 1, i32 0, i32 0, i32 0)
%9 = tail call i32 @getTimestamp(i32* %0, i64 %8)
%10 = load i64, i64* @XA_STRING
%11 = tail call i32 @XConvertSelection(i32* %0, i64 %4, i64 %10, i64 %6, i64 %8, i32 %9)
ret i8* null
}