#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  scalar_t__ root;
  int x;
  int y;
};
typedef TYPE_1__ XWindowAttributes;
typedef scalar_t__ Window;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ XFree(scalar_t__ *);
scalar_t__ XGetWindowAttributes(int /*<<< orphan*/ *, scalar_t__, TYPE_1__ *);
int /*<<< orphan*/ XQueryTree(int /*<<< orphan*/ *, scalar_t__, scalar_t__ *,
                              scalar_t__ *, scalar_t__ **, unsigned int *);
int QueryRootPosition(Display *dpy, Window win, int *x, int *y) {
  int X, Y;
  unsigned int nchildren;
  XWindowAttributes win_att;
  Window root, parent, *children;
  if (XGetWindowAttributes(dpy, win, &win_att) == 0)
    return (1);
  if (win == win_att.root) {
    *x = 0;
    *y = 0;
    return (0);
  }
  *x = win_att.x;
  *y = win_att.y;
  XQueryTree(dpy, win, &root, &parent, &children, &nchildren);
  XFree(children);
  QueryRootPosition(dpy, parent, &X, &Y);
  *x += X;
  *y += Y;
  return (0);
}


x86 assembly (clang, -O0):
<QueryRootPosition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1060 <XGetWindowAttributes@plt>
cmp    $0x0,%rax
jne    117f <QueryRootPosition+0x3f>
movl   $0x1,-0x4(%rbp)
jmp    1217 <QueryRootPosition+0xd7>
mov    -0x18(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    11ad <QueryRootPosition+0x6d>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1217 <QueryRootPosition+0xd7>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
lea    -0x60(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1030 <XQueryTree@plt>
mov    -0x60(%rbp),%rdi
call   1040 <XFree@plt>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <QueryRootPosition@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<QueryRootPosition>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0x18(%rsp),%rdx
call   1060 <XGetWindowAttributes@plt>
test   %rax,%rax
je     1178 <QueryRootPosition+0x38>
cmp    %rbx,0x18(%rsp)
jne    117f <QueryRootPosition+0x3f>
movl   $0x0,(%r14)
xor    %eax,%eax
jmp    11d8 <QueryRootPosition+0x98>
mov    $0x1,%eax
jmp    11dd <QueryRootPosition+0x9d>
mov    0x20(%rsp),%eax
mov    %eax,(%r14)
mov    0x24(%rsp),%eax
mov    %eax,(%r15)
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x2c(%rsp),%r9
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <XQueryTree@plt>
mov    0x8(%rsp),%rdi
call   1040 <XFree@plt>
mov    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %rsp,%rcx
mov    %r12,%rdi
call   1050 <QueryRootPosition@plt>
mov    0x4(%rsp),%eax
add    %eax,(%r14)
mov    (%r15),%eax
add    (%rsp),%eax
mov    %eax,(%r15)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i32, i32 }
define i32 @QueryRootPosition(i32* %0, i64 %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.TYPE_3__
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64*
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%17 = load i32*, i32** %6
%18 = load i64, i64* %7
%19 = call i64 @XGetWindowAttributes(i32* %17, i64 %18, %struct.TYPE_3__* %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 1, i32* %5
br label %53
22: ; preds = %4
%23 = load i64, i64* %7
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%25 = load i64, i64* %24
%26 = icmp eq i64 %23, %25
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32*, i32** %8
store i32 0, i32* %28
%29 = load i32*, i32** %9
store i32 0, i32* %29
store i32 0, i32* %5
br label %53
30: ; preds = %22
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load i32*, i32** %8
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 2
%35 = load i32, i32* %34
%36 = load i32*, i32** %9
store i32 %35, i32* %36
%37 = load i32*, i32** %6
%38 = load i64, i64* %7
%39 = call i32 @XQueryTree(i32* %37, i64 %38, i64* %14, i64* %15, i64** %16, i32* %12)
%40 = load i64*, i64** %16
%41 = call i32 @XFree(i64* %40)
%42 = load i32*, i32** %6
%43 = load i64, i64* %15
%44 = call i32 @QueryRootPosition(i32* %42, i64 %43, i32* %10, i32* %11)
%45 = load i32, i32* %10
%46 = load i32*, i32** %8
%47 = load i32, i32* %46
%48 = add nsw i32 %47, %45
store i32 %48, i32* %46
%49 = load i32, i32* %11
%50 = load i32*, i32** %9
%51 = load i32, i32* %50
%52 = add nsw i32 %51, %49
store i32 %52, i32* %50
store i32 0, i32* %5
br label %53
53: ; preds = %30, %27, %21
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i32, i32 }
define i32 @QueryRootPosition(i32* %0, i64 %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.TYPE_3__
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = bitcast i32* %7 to i8*
%15 = bitcast %struct.TYPE_3__* %8 to i8*
%16 = bitcast i64* %9 to i8*
%17 = bitcast i64* %10 to i8*
%18 = bitcast i64** %11 to i8*
%19 = call i64 @XGetWindowAttributes(i32* %0, i64 %1, %struct.TYPE_3__* %8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %44, label %21
21: ; preds = %4
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, %1
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
br label %42
26: ; preds = %21
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 1
%28 = load i32, i32* %27
store i32 %28, i32* %2
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %8, i64 0, i32 2
%30 = load i32, i32* %29
store i32 %30, i32* %3
%31 = call i32 @XQueryTree(i32* %0, i64 %1, i64* %9, i64* %10, i64** %11, i32* %7)
%32 = load i64*, i64** %11
%33 = call i32 @XFree(i64* %32)
%34 = load i64, i64* %10
%35 = call i32 @QueryRootPosition(i32* %0, i64 %34, i32* %5, i32* %6)
%36 = load i32, i32* %5
%37 = load i32, i32* %2
%38 = add nsw i32 %37, %36
store i32 %38, i32* %2
%39 = load i32, i32* %6
%40 = load i32, i32* %3
%41 = add nsw i32 %40, %39
br label %42
42: ; preds = %25, %26
%43 = phi i32 [ %41, %26 ], [ 0, %25 ]
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %4
%45 = phi i32 [ 1, %4 ], [ 0, %42 ]
ret i32 %45
}