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

/* Variables and functions */
int /*<<< orphan*/ AnyPropertyType;
int /*<<< orphan*/ False;
scalar_t__ Success;
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XDefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ XFree(unsigned char *);
scalar_t__ XGetWindowProperty(int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/ *, int *,
                              unsigned long *, unsigned long *,
                              unsigned char **);
int /*<<< orphan*/ XInternAtom(int /*<<< orphan*/ *, char *,
                               int /*<<< orphan*/);
int /*<<< orphan*/ *XOpenDisplay(char *);
int /*<<< orphan*/ XSync(int /*<<< orphan*/ *, int /*<<< orphan*/);
int cur_desk(char *display) {
  Display *dsp;
  Atom actual_type;
  int fmt;
  long unsigned bytes, nitems;
  unsigned char *data;
  if (!(dsp = XOpenDisplay(display)))
    return 127;
  if (XGetWindowProperty(dsp, XDefaultRootWindow(dsp),
                         XInternAtom(dsp, "_NET_CURRENT_DESKTOP", False), 0, ~0,
                         False, AnyPropertyType, &actual_type, &fmt, &nitems,
                         &bytes, &data) != Success) {
    XSync(dsp, False);
    XCloseDisplay(dsp);
    return 127;
  }
  int current_desktop = *(int *)data;
  XFree(data);
  XSync(dsp, False);
  XCloseDisplay(dsp);
  return current_desktop;
}


x86 assembly (clang, -O0):
<cur_desk>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdi,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <XOpenDisplay@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11a4 <cur_desk+0x34>
movl   $0x7f,-0x1c(%rbp)
jmp    1296 <cur_desk+0x126>
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <XDefaultRootWindow@plt>
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    0xe34(%rip),%rsi
call   1050 <XInternAtom@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%esi
mov    %eax,%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%r9d
mov    0x2dfd(%rip),%rax
mov    (%rax),%r15d
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
lea    -0x34(%rbp),%r14
lea    -0x38(%rbp),%rbx
lea    -0x48(%rbp),%r11
lea    -0x40(%rbp),%r10
lea    -0x50(%rbp),%rax
mov    %r15d,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   1080 <XGetWindowProperty@plt>
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
je     1263 <cur_desk+0xf3>
mov    -0x30(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1060 <XSync@plt>
mov    -0x30(%rbp),%rdi
call   1090 <XCloseDisplay@plt>
movl   $0x7f,-0x1c(%rbp)
jmp    1296 <cur_desk+0x126>
mov    -0x50(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <XFree@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
call   1060 <XSync@plt>
mov    -0x30(%rbp),%rdi
call   1090 <XCloseDisplay@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<cur_desk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
call   1040 <XOpenDisplay@plt>
test   %rax,%rax
je     122d <cur_desk+0xbd>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <XDefaultRootWindow@plt>
mov    %eax,%ebp
mov    0x2e38(%rip),%r14
mov    (%r14),%edx
lea    0xe56(%rip),%rsi
mov    %rbx,%rdi
call   1050 <XInternAtom@plt>
mov    (%r14),%r9d
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%r10d
lea    0x8(%rsp),%r11
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0x10(%rsp),%r13
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
push   %r11
push   %r15
push   %r12
push   %r13
lea    0x34(%rsp),%rax
push   %rax
push   %r10
call   1080 <XGetWindowProperty@plt>
add    $0x30,%rsp
mov    0x2df6(%rip),%rcx
mov    $0x7f,%ebp
cmp    (%rcx),%rax
jne    1218 <cur_desk+0xa8>
mov    0x8(%rsp),%rdi
mov    (%rdi),%ebp
call   1030 <XFree@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1060 <XSync@plt>
mov    %rbx,%rdi
call   1090 <XCloseDisplay@plt>
jmp    1232 <cur_desk+0xc2>
mov    $0x7f,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"_NET_CURRENT_DESKTOP\00"
@False = global i32 0
@AnyPropertyType = global i32 0
@Success = global i64 0
define i32 @cur_desk(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32* @XOpenDisplay(i8* %11)
store i32* %12, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store i32 127, i32* %2
br label %45
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32*, i32** %4
%18 = call i32 @XDefaultRootWindow(i32* %17)
%19 = load i32*, i32** %4
%20 = load i32, i32* @False
%21 = call i32 @XInternAtom(i32* %19, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @False
%23 = load i32, i32* @AnyPropertyType
%24 = call i64 @XGetWindowProperty(i32* %16, i32 %18, i32 %21, i32 0, i32 -1, i32 %22, i32 %23, i32* %5, i32* %6, i64* %8, i64* %7, i8** %9)
%25 = load i64, i64* @Success
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %15
%28 = load i32*, i32** %4
%29 = load i32, i32* @False
%30 = call i32 @XSync(i32* %28, i32 %29)
%31 = load i32*, i32** %4
%32 = call i32 @XCloseDisplay(i32* %31)
store i32 127, i32* %2
br label %45
33: ; preds = %15
%34 = load i8*, i8** %9
%35 = bitcast i8* %34 to i32*
%36 = load i32, i32* %35
store i32 %36, i32* %10
%37 = load i8*, i8** %9
%38 = call i32 @XFree(i8* %37)
%39 = load i32*, i32** %4
%40 = load i32, i32* @False
%41 = call i32 @XSync(i32* %39, i32 %40)
%42 = load i32*, i32** %4
%43 = call i32 @XCloseDisplay(i32* %42)
%44 = load i32, i32* %10
store i32 %44, i32* %2
br label %45
45: ; preds = %33, %27, %14
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [21 x i8] c"_NET_CURRENT_DESKTOP\00"
@False = global i32 0
@AnyPropertyType = global i32 0
@Success = global i64 0
define i32 @cur_desk(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i64* %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = bitcast i8** %6 to i8*
%12 = tail call i32* @XOpenDisplay(i8* %0)
%13 = icmp eq i32* %12, null
br i1 %13, label %33, label %14
14: ; preds = %1
%15 = tail call i32 @XDefaultRootWindow(i32* %12)
%16 = load i32, i32* @False
%17 = tail call i32 @XInternAtom(i32* %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @False
%19 = load i32, i32* @AnyPropertyType
%20 = call i64 @XGetWindowProperty(i32* %12, i32 %15, i32 %17, i32 0, i32 -1, i32 %18, i32 %19, i32* %2, i32* %3, i64* %5, i64* %4, i8** %6)
%21 = load i64, i64* @Success
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %28
23: ; preds = %14
%24 = load i8*, i8** %6
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = call i32 @XFree(i8* %24)
br label %28
28: ; preds = %14, %23
%29 = phi i32 [ %26, %23 ], [ 127, %14 ]
%30 = load i32, i32* @False
%31 = call i32 @XSync(i32* %12, i32 %30)
%32 = call i32 @XCloseDisplay(i32* %12)
br label %33
33: ; preds = %28, %1
%34 = phi i32 [ 127, %1 ], [ %29, %28 ]
ret i32 %34
}