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

/* Variables and functions */
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int EXIT_FAILURE;
int /*<<< orphan*/ XA_CARDINAL;
int client_msg(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, unsigned long,
               int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
scalar_t__ get_property(int /*<<< orphan*/ *, int /*<<< orphan*/,
                        int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int window_to_desktop(Display *disp, Window win, int desktop) {
  CARD32 *cur_desktop = NULL;
  Window root = DefaultRootWindow(disp);
  if (desktop == -1) {
    if (!(cur_desktop = (CARD32 *)get_property(disp, root, XA_CARDINAL,
                                               "_NET_CURRENT_DESKTOP", NULL))) {
      if (!(cur_desktop = (CARD32 *)get_property(disp, root, XA_CARDINAL,
                                                 "_WIN_WORKSPACE", NULL))) {
        fputs("Cannot get current desktop properties. "
              "(_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)"
              "\n",
              stderr);
        return EXIT_FAILURE;
      }
    }
    desktop = *cur_desktop;
  }
  free(cur_desktop);
  return client_msg(disp, win, "_NET_WM_DESKTOP", (unsigned long)desktop, 0, 0,
                    0, 0);
}


x86 assembly (clang, -O0):
<window_to_desktop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <DefaultRootWindow@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1212 <window_to_desktop+0xc2>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
lea    0xe69(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <get_property@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1209 <window_to_desktop+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    0xe4f(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <get_property@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1204 <window_to_desktop+0xb4>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <window_to_desktop+0xfa>
jmp    1209 <window_to_desktop+0xb9>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movslq -0x18(%rbp),%rcx
lea    0xe51(%rip),%rdx
xor    %r9d,%r9d
mov    %r9d,%r8d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1040 <client_msg@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<window_to_desktop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1060 <DefaultRootWindow@plt>
cmp    $0xffffffff,%ebp
je     116d <window_to_desktop+0x1d>
xor    %edi,%edi
jmp    11b8 <window_to_desktop+0x68>
mov    %eax,%r15d
mov    0x2e59(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xe7f(%rip),%rcx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %r8d,%r8d
call   1050 <get_property@plt>
mov    %rax,%rdi
test   %rax,%rax
jne    11b6 <window_to_desktop+0x66>
mov    0x0(%rbp),%edx
lea    0xe75(%rip),%rcx
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %r8d,%r8d
call   1050 <get_property@plt>
mov    %rax,%rdi
test   %rax,%rax
je     11e7 <window_to_desktop+0x97>
mov    (%rdi),%ebp
call   1030 <free@plt>
movslq %ebp,%rcx
lea    0xeb7(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
call   1040 <client_msg@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
jmp    11e0 <window_to_desktop+0x90>

LLVM IR (-O0):
@XA_CARDINAL = global i32 0
@.str = [21 x i8] c"_NET_CURRENT_DESKTOP\00"
@.str.1 = [15 x i8] c"_WIN_WORKSPACE\00"
@.str.2 = [90 x i8] c"Cannot get current desktop properties. (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"_NET_WM_DESKTOP\00"
define i32 @window_to_desktop(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* null, i32** %8
%10 = load i32*, i32** %5
%11 = call i32 @DefaultRootWindow(i32* %10)
store i32 %11, i32* %9
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %36
14: ; preds = %3
%15 = load i32*, i32** %5
%16 = load i32, i32* %9
%17 = load i32, i32* @XA_CARDINAL
%18 = call i64 @get_property(i32* %15, i32 %16, i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32* null)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %33, label %21
21: ; preds = %14
%22 = load i32*, i32** %5
%23 = load i32, i32* %9
%24 = load i32, i32* @XA_CARDINAL
%25 = call i64 @get_property(i32* %22, i32 %23, i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32* null)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %8
%27 = icmp ne i32* %26, null
br i1 %27, label %32, label %28
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = call i32 @fputs(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %4
br label %44
32: ; preds = %21
br label %33
33: ; preds = %32, %14
%34 = load i32*, i32** %8
%35 = load i32, i32* %34
store i32 %35, i32* %7
br label %36
36: ; preds = %33, %3
%37 = load i32*, i32** %8
%38 = call i32 @free(i32* %37)
%39 = load i32*, i32** %5
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = call i32 @client_msg(i32* %39, i32 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %42, i32 0, i32 0, i32 0, i32 0)
store i32 %43, i32* %4
br label %44
44: ; preds = %36, %28
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@XA_CARDINAL = global i32 0
@.str = [21 x i8] c"_NET_CURRENT_DESKTOP\00"
@.str.1 = [15 x i8] c"_WIN_WORKSPACE\00"
@.str.2 = [90 x i8] c"Cannot get current desktop properties. (_NET_CURRENT_DESKTOP or _WIN_WORKSPACE property)\0A\00"
@stderr = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"_NET_WM_DESKTOP\00"
define i32 @window_to_desktop(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @DefaultRootWindow(i32* %0)
%5 = icmp eq i32 %2, -1
br i1 %5, label %6, label %22
6: ; preds = %3
%7 = load i32, i32* @XA_CARDINAL
%8 = tail call i64 @get_property(i32* %0, i32 %4, i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32* null)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %6
%11 = load i32, i32* @XA_CARDINAL
%12 = tail call i64 @get_property(i32* %0, i32 %4, i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32* null)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fputs(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
br label %28
18: ; preds = %10, %6
%19 = phi i64 [ %8, %6 ], [ %12, %10 ]
%20 = inttoptr i64 %19 to i32*
%21 = load i32, i32* %20
br label %22
22: ; preds = %18, %3
%23 = phi i32 [ %21, %18 ], [ %2, %3 ]
%24 = phi i32* [ %20, %18 ], [ null, %3 ]
%25 = tail call i32 @free(i32* %24)
%26 = sext i32 %23 to i64
%27 = tail call i32 @client_msg(i32* %0, i32 %1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i64 %26, i32 0, i32 0, i32 0, i32 0)
br label %28
28: ; preds = %22, %14
%29 = phi i32 [ %27, %22 ], [ %17, %14 ]
ret i32 %29
}