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

/* Variables and functions */
int /*<<< orphan*/ XFree(scalar_t__ *);
int XQueryTree(int /*<<< orphan*/ *, scalar_t__, scalar_t__ *, scalar_t__ *,
               scalar_t__ **, unsigned int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void print_xquerytree(FILE *out_file, Window win_handle, Display *dpy) {
  if (win_handle == 0) {
    return;
  }
  Window root_win = 0;
  Window parent_win = 0;
  Window *childs_list = NULL;
  unsigned int num_childs = 0;
  int k = 0;
  int queryRes = XQueryTree(dpy, win_handle, &root_win, &parent_win,
                            &childs_list, &num_childs);
  if (queryRes != 0) {
    fprintf(out_file, "Active window: %#lx, root %#lx, parent %#lx ",
            win_handle, root_win, parent_win);
    if ((num_childs > 0) && (childs_list != NULL)) {
      fprintf(out_file, "Children: ");
      for (k = 0; k < num_childs; k++) {
        fprintf(out_file, "%#lx ", childs_list[k]);
      }
      fprintf(out_file, "\n");
      XFree(childs_list);
      childs_list = NULL;
    }
  }
}


x86 assembly (clang, -O0):
<print_xquerytree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <print_xquerytree+0x24>
jmp    1250 <print_xquerytree+0x120>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x30(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1030 <XQueryTree@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1250 <print_xquerytree+0x120>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x34(%rbp)
jbe    124b <print_xquerytree+0x11b>
cmpq   $0x0,-0x30(%rbp)
je     124b <print_xquerytree+0x11b>
mov    -0x8(%rbp),%rdi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jae    1228 <print_xquerytree+0xf8>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11f0 <print_xquerytree+0xc0>
mov    -0x8(%rbp),%rdi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <XFree@plt>
movq   $0x0,-0x30(%rbp)
jmp    1250 <print_xquerytree+0x120>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_xquerytree>:
test   %rsi,%rsi
je     1253 <print_xquerytree+0x103>
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
lea    0x18(%rsp),%rax
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %rdx,%rdi
mov    %rax,%rdx
call   1030 <XQueryTree@plt>
test   %eax,%eax
je     124a <print_xquerytree+0xfa>
mov    0x18(%rsp),%rcx
mov    0x10(%rsp),%r8
lea    0xe3d(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
cmpl   $0x0,0x4(%rsp)
je     124a <print_xquerytree+0xfa>
cmpq   $0x0,0x8(%rsp)
je     124a <print_xquerytree+0xfa>
lea    0xe47(%rip),%rdi
mov    $0xa,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmpl   $0x0,0x4(%rsp)
je     1233 <print_xquerytree+0xe3>
lea    0xe21(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,8),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbx
mov    0x4(%rsp),%eax
cmp    %rax,%rbx
jb     1210 <print_xquerytree+0xc0>
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    0x8(%rsp),%rdi
call   1040 <XFree@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [45 x i8] c"Active window: %#lx, root %#lx, parent %#lx \00"
@.str.1 = [11 x i8] c"Children: \00"
@.str.2 = [6 x i8] c"%#lx \00"
@.str.3 = [2 x i8] c"\0A\00"
define void @print_xquerytree(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
br label %57
16: ; preds = %3
store i64 0, i64* %7
store i64 0, i64* %8
store i64* null, i64** %9
store i32 0, i32* %10
store i32 0, i32* %11
%17 = load i32*, i32** %6
%18 = load i64, i64* %5
%19 = call i32 @XQueryTree(i32* %17, i64 %18, i64* %7, i64* %8, i64** %9, i32* %10)
store i32 %19, i32* %12
%20 = load i32, i32* %12
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %57
22: ; preds = %16
%23 = load i32*, i32** %4
%24 = load i64, i64* %5
%25 = load i64, i64* %7
%26 = load i64, i64* %8
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %24, i64 %25, i64 %26)
%28 = load i32, i32* %10
%29 = icmp ugt i32 %28, 0
br i1 %29, label %30, label %56
30: ; preds = %22
%31 = load i64*, i64** %9
%32 = icmp ne i64* %31, null
br i1 %32, label %33, label %56
33: ; preds = %30
%34 = load i32*, i32** %4
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %11
br label %36
36: ; preds = %48, %33
%37 = load i32, i32* %11
%38 = load i32, i32* %10
%39 = icmp ult i32 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %36
%41 = load i32*, i32** %4
%42 = load i64*, i64** %9
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i64, i64* %42, i64 %44
%46 = load i64, i64* %45
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %46)
br label %48
48: ; preds = %40
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %36
51: ; preds = %36
%52 = load i32*, i32** %4
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%54 = load i64*, i64** %9
%55 = call i32 @XFree(i64* %54)
store i64* null, i64** %9
br label %56
56: ; preds = %51, %30, %22
br label %57
57: ; preds = %15, %56, %16
ret void
}

LLVM IR (-O2):
@.str = [45 x i8] c"Active window: %#lx, root %#lx, parent %#lx \00"
@.str.1 = [11 x i8] c"Children: \00"
@.str.2 = [6 x i8] c"%#lx \00"
define void @print_xquerytree(i32* nocapture %0, i64 %1, i32* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i32
%8 = icmp eq i64 %1, 0
br i1 %8, label %44, label %9
9: ; preds = %3
%10 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%11 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%12 = bitcast i64** %6 to i8*
store i64* null, i64** %6
%13 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%14 = call i32 @XQueryTree(i32* %2, i64 %1, i64* %4, i64* %5, i64** %6, i32* %7)
%15 = icmp eq i32 %14, 0
br i1 %15, label %43, label %16
16: ; preds = %9
%17 = load i64, i64* %4
%18 = load i64, i64* %5
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %1, i64 %17, i64 %18)
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
%22 = load i64*, i64** %6
%23 = icmp ne i64* %22, null
%24 = select i1 %21, i1 %23, i1 false
br i1 %24, label %25, label %43
25: ; preds = %16
%26 = call i64 @fwrite(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i64 10, i64 1, i32* %0)
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %25, %29
%30 = phi i64 [ %35, %29 ], [ 0, %25 ]
%31 = load i64*, i64** %6
%32 = getelementptr inbounds i64, i64* %31, i64 %30
%33 = load i64, i64* %32
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %33)
%35 = add nuw nsw i64 %30, 1
%36 = load i32, i32* %7
%37 = zext i32 %36 to i64
%38 = icmp ult i64 %35, %37
br i1 %38, label %29, label %39
39: ; preds = %29, %25
%40 = call i32 @fputc(i32 10, i32* %0)
%41 = load i64*, i64** %6
%42 = call i32 @XFree(i64* %41)
br label %43
43: ; preds = %16, %39, %9
br label %44
44: ; preds = %3, %43
ret void
}