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

/* Variables and functions */
int /*<<< orphan*/ *DisplayOfScreen(int /*<<< orphan*/ *);
int /*<<< orphan*/ False;
int /*<<< orphan*/ None;
scalar_t__ RootWindowOfScreen(int /*<<< orphan*/ *);
scalar_t__ Success;
int /*<<< orphan*/ XA_WINDOW;
int /*<<< orphan*/ XFree(char *);
scalar_t__ XGetWindowProperty(int /*<<< orphan*/ *, scalar_t__,
                              int /*<<< orphan*/, int /*<<< orphan*/, int,
                              int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/ *, int *, unsigned long *,
                              unsigned long *, unsigned char **);
int /*<<< orphan*/ XInternAtom(int /*<<< orphan*/ *, char *,
                               int /*<<< orphan*/);
scalar_t__ XQueryTree(int /*<<< orphan*/ *, scalar_t__, scalar_t__ *,
                      scalar_t__ *, scalar_t__ **, unsigned int *);
char *getenv(char *);
int sscanf(char const *, char *, unsigned long *, char *);
Window VirtualRootWindowOfScreen(Screen *screen) {
  static Screen *save_screen = (Screen *)0;
  static Window root = (Window)0;
  if (screen != save_screen) {
    Display *dpy = DisplayOfScreen(screen);
    Atom __SWM_VROOT = None;
    int i;
    Window rootReturn, parentReturn, *children;
    unsigned int numChildren;
    const char *xss_id = getenv("XSCREENSAVER_WINDOW");
    if (xss_id && *xss_id) {
      unsigned long id = 0;
      char c;
      if (1 == sscanf(xss_id, " 0x%lx %c", &id, &c) ||
          1 == sscanf(xss_id, " %lu %c", &id, &c)) {
        root = (Window)id;
        save_screen = screen;
        return root;
      }
    }
    root = RootWindowOfScreen(screen);
    __SWM_VROOT = XInternAtom(dpy, "__SWM_VROOT", False);
    if (XQueryTree(dpy, root, &rootReturn, &parentReturn, &children,
                   &numChildren)) {
      for (i = 0; i < numChildren; i++) {
        Atom actual_type;
        int actual_format;
        unsigned long nitems, bytesafter;
        Window *newRoot = (Window *)0;
        if (XGetWindowProperty(dpy, children[i], __SWM_VROOT, 0, 1, False,
                               XA_WINDOW, &actual_type, &actual_format, &nitems,
                               &bytesafter,
                               (unsigned char **)&newRoot) == Success &&
            newRoot) {
          root = *newRoot;
          break;
        }
      }
      if (children)
        XFree((char *)children);
    }
    save_screen = screen;
  }
  return root;
}


x86 assembly (clang, -O0):
<VirtualRootWindowOfScreen>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    %rdi,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    0x2ec9(%rip),%rax
je     13b4 <VirtualRootWindowOfScreen+0x234>
mov    -0x28(%rbp),%rdi
call   1060 <DisplayOfScreen@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     126a <VirtualRootWindowOfScreen+0xea>
mov    -0x60(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     126a <VirtualRootWindowOfScreen+0xea>
movq   $0x0,-0x68(%rbp)
mov    -0x60(%rbp),%rdi
lea    0xe18(%rip),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x69(%rbp),%rcx
call   1080 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     123f <VirtualRootWindowOfScreen+0xbf>
mov    -0x60(%rbp),%rdi
lea    0xdfb(%rip),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x69(%rbp),%rcx
call   1080 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1265 <VirtualRootWindowOfScreen+0xe5>
mov    -0x68(%rbp),%rax
mov    %rax,0x2e26(%rip)
mov    -0x28(%rbp),%rax
mov    %rax,0x2e13(%rip)
mov    0x2e14(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    13bf <VirtualRootWindowOfScreen+0x23f>
jmp    126a <VirtualRootWindowOfScreen+0xea>
mov    -0x28(%rbp),%rdi
call   1090 <RootWindowOfScreen@plt>
mov    %rax,0x2df6(%rip)
mov    -0x30(%rbp),%rdi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
lea    0xd98(%rip),%rsi
call   1070 <XInternAtom@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dcf(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
lea    -0x50(%rbp),%r8
lea    -0x54(%rbp),%r9
call   1040 <XQueryTree@plt>
cmp    $0x0,%rax
je     13a9 <VirtualRootWindowOfScreen+0x229>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x54(%rbp),%eax
jae    1390 <VirtualRootWindowOfScreen+0x210>
movq   $0x0,-0x90(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x50(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x34(%rbp),%edx
mov    0x2ce8(%rip),%rax
mov    (%rax),%r9d
mov    0x2cc6(%rip),%rax
mov    (%rax),%r15d
lea    -0x90(%rbp),%rax
xor    %ecx,%ecx
mov    $0x1,%r8d
lea    -0x70(%rbp),%r14
lea    -0x74(%rbp),%rbx
lea    -0x80(%rbp),%r11
lea    -0x88(%rbp),%r10
mov    %r15d,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   10a0 <XGetWindowProperty@plt>
mov    0x2ca8(%rip),%rcx
cmp    (%rcx),%rax
jne    137d <VirtualRootWindowOfScreen+0x1fd>
cmpq   $0x0,-0x90(%rbp)
je     137d <VirtualRootWindowOfScreen+0x1fd>
mov    -0x90(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2cf8(%rip)
jmp    1390 <VirtualRootWindowOfScreen+0x210>
jmp    1382 <VirtualRootWindowOfScreen+0x202>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12c7 <VirtualRootWindowOfScreen+0x147>
cmpq   $0x0,-0x50(%rbp)
je     13a4 <VirtualRootWindowOfScreen+0x224>
mov    -0x50(%rbp),%rdi
call   1050 <XFree@plt>
jmp    13a9 <VirtualRootWindowOfScreen+0x229>
mov    -0x28(%rbp),%rax
mov    %rax,0x2cb4(%rip)
mov    0x2cb5(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<VirtualRootWindowOfScreen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
cmp    %rdi,0x2ed3(%rip)
je     1338 <VirtualRootWindowOfScreen+0x1b8>
mov    %rdi,%rbx
call   1060 <DisplayOfScreen@plt>
mov    %rax,%r12
lea    0xe53(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121c <VirtualRootWindowOfScreen+0x9c>
mov    %rax,%rbp
cmpb   $0x0,(%rax)
je     121c <VirtualRootWindowOfScreen+0x9c>
movq   $0x0,0x8(%rsp)
lea    0xe45(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
mov    %rbp,%rdi
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     1204 <VirtualRootWindowOfScreen+0x84>
lea    0xe31(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
mov    %rbp,%rdi
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    121c <VirtualRootWindowOfScreen+0x9c>
mov    0x8(%rsp),%rax
mov    %rax,0x2e60(%rip)
mov    %rbx,0x2e51(%rip)
jmp    133f <VirtualRootWindowOfScreen+0x1bf>
mov    %rbx,%rdi
call   1090 <RootWindowOfScreen@plt>
mov    %rax,0x2e45(%rip)
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
lea    0xdeb(%rip),%rsi
mov    %r12,%rdi
call   1070 <XInternAtom@plt>
mov    %eax,%r14d
mov    0x2e23(%rip),%rsi
lea    0x40(%rsp),%rdx
lea    0x38(%rsp),%rcx
lea    0x20(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %r12,%rdi
call   1040 <XQueryTree@plt>
test   %rax,%rax
je     1331 <VirtualRootWindowOfScreen+0x1b1>
mov    %rbx,0x28(%rsp)
cmpl   $0x0,0x4(%rsp)
je     131d <VirtualRootWindowOfScreen+0x19d>
xor    %ebx,%ebx
lea    0x34(%rsp),%r13
mov    0x2d68(%rip),%r15
jmp    12ad <VirtualRootWindowOfScreen+0x12d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbx
mov    0x4(%rsp),%eax
cmp    %rax,%rbx
jae    131d <VirtualRootWindowOfScreen+0x19d>
movq   $0x0,0x10(%rsp)
mov    0x20(%rsp),%rax
mov    (%rax,%rbx,8),%rsi
mov    0x2d1a(%rip),%rax
mov    (%rax),%r9d
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %r12,%rdi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0x1,%r8d
lea    0x10(%rsp),%rbp
push   %rbp
lea    0x20(%rsp),%rbp
push   %rbp
lea    0x18(%rsp),%rbp
push   %rbp
lea    0x48(%rsp),%rbp
push   %rbp
push   %r13
push   %rax
call   10a0 <XGetWindowProperty@plt>
add    $0x30,%rsp
cmp    (%r15),%rax
jne    12a0 <VirtualRootWindowOfScreen+0x120>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     12a0 <VirtualRootWindowOfScreen+0x120>
mov    (%rax),%rax
mov    %rax,0x2d53(%rip)
mov    0x20(%rsp),%rdi
test   %rdi,%rdi
mov    0x28(%rsp),%rbx
je     1331 <VirtualRootWindowOfScreen+0x1b1>
call   1050 <XFree@plt>
mov    %rbx,0x2d30(%rip)
mov    0x2d31(%rip),%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@VirtualRootWindowOfScreen.save_screen = internal global i32* null
@VirtualRootWindowOfScreen.root = internal global i64 0
@None = global i32 0
@.str = [20 x i8] c"XSCREENSAVER_WINDOW\00"
@.str.1 = [10 x i8] c" 0x%lx %c\00"
@.str.2 = [8 x i8] c" %lu %c\00"
@.str.3 = [12 x i8] c"__SWM_VROOT\00"
@False = global i32 0
@XA_WINDOW = global i32 0
@Success = global i64 0
define i64 @VirtualRootWindowOfScreen(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
%17 = alloca i64
%18 = alloca i64*
store i32* %0, i32** %3
%19 = load i32*, i32** %3
%20 = load i32*, i32** @VirtualRootWindowOfScreen.save_screen
%21 = icmp ne i32* %19, %20
br i1 %21, label %22, label %96
22: ; preds = %1
%23 = load i32*, i32** %3
%24 = call i32* @DisplayOfScreen(i32* %23)
store i32* %24, i32** %4
%25 = load i32, i32* @None
store i32 %25, i32* %5
%26 = call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %47
29: ; preds = %22
%30 = load i8*, i8** %11
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %47
34: ; preds = %29
store i64 0, i64* %12
%35 = load i8*, i8** %11
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %12, i8* %13)
%37 = icmp eq i32 1, %36
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i8*, i8** %11
%40 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64* %12, i8* %13)
%41 = icmp eq i32 1, %40
br i1 %41, label %42, label %46
42: ; preds = %38, %34
%43 = load i64, i64* %12
store i64 %43, i64* @VirtualRootWindowOfScreen.root
%44 = load i32*, i32** %3
store i32* %44, i32** @VirtualRootWindowOfScreen.save_screen
%45 = load i64, i64* @VirtualRootWindowOfScreen.root
store i64 %45, i64* %2
br label %98
46: ; preds = %38
br label %47
47: ; preds = %46, %29, %22
%48 = load i32*, i32** %3
%49 = call i64 @RootWindowOfScreen(i32* %48)
store i64 %49, i64* @VirtualRootWindowOfScreen.root
%50 = load i32*, i32** %4
%51 = load i32, i32* @False
%52 = call i32 @XInternAtom(i32* %50, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %51)
store i32 %52, i32* %5
%53 = load i32*, i32** %4
%54 = load i64, i64* @VirtualRootWindowOfScreen.root
%55 = call i64 @XQueryTree(i32* %53, i64 %54, i64* %7, i64* %8, i64** %9, i32* %10)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %94
57: ; preds = %47
store i32 0, i32* %6
br label %58
58: ; preds = %83, %57
%59 = load i32, i32* %6
%60 = load i32, i32* %10
%61 = icmp ult i32 %59, %60
br i1 %61, label %62, label %86
62: ; preds = %58
store i64* null, i64** %18
%63 = load i32*, i32** %4
%64 = load i64*, i64** %9
%65 = load i32, i32* %6
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i64, i64* %64, i64 %66
%68 = load i64, i64* %67
%69 = load i32, i32* %5
%70 = load i32, i32* @False
%71 = load i32, i32* @XA_WINDOW
%72 = bitcast i64** %18 to i8**
%73 = call i64 @XGetWindowProperty(i32* %63, i64 %68, i32 %69, i32 0, i32 1, i32 %70, i32 %71, i32* %14, i32* %15, i64* %16, i64* %17, i8** %72)
%74 = load i64, i64* @Success
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %82
76: ; preds = %62
%77 = load i64*, i64** %18
%78 = icmp ne i64* %77, null
br i1 %78, label %79, label %82
79: ; preds = %76
%80 = load i64*, i64** %18
%81 = load i64, i64* %80
store i64 %81, i64* @VirtualRootWindowOfScreen.root
br label %86
82: ; preds = %76, %62
br label %83
83: ; preds = %82
%84 = load i32, i32* %6
%85 = add nsw i32 %84, 1
store i32 %85, i32* %6
br label %58
86: ; preds = %79, %58
%87 = load i64*, i64** %9
%88 = icmp ne i64* %87, null
br i1 %88, label %89, label %93
89: ; preds = %86
%90 = load i64*, i64** %9
%91 = bitcast i64* %90 to i8*
%92 = call i32 @XFree(i8* %91)
br label %93
93: ; preds = %89, %86
br label %94
94: ; preds = %93, %47
%95 = load i32*, i32** %3
store i32* %95, i32** @VirtualRootWindowOfScreen.save_screen
br label %96
96: ; preds = %94, %1
%97 = load i64, i64* @VirtualRootWindowOfScreen.root
store i64 %97, i64* %2
br label %98
98: ; preds = %96, %42
%99 = load i64, i64* %2
ret i64 %99
}

LLVM IR (-O2):
@VirtualRootWindowOfScreen.save_screen = internal unnamed_addr global i32* null
@VirtualRootWindowOfScreen.root = internal unnamed_addr global i64 0
@None = global i32 0
@.str = [20 x i8] c"XSCREENSAVER_WINDOW\00"
@.str.1 = [10 x i8] c" 0x%lx %c\00"
@.str.2 = [8 x i8] c" %lu %c\00"
@.str.3 = [12 x i8] c"__SWM_VROOT\00"
@False = global i32 0
@XA_WINDOW = global i32 0
@Success = global i64 0
define i64 @VirtualRootWindowOfScreen(i32* %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64*
%13 = load i32*, i32** @VirtualRootWindowOfScreen.save_screen
%14 = icmp eq i32* %13, %0
br i1 %14, label %80, label %15
15: ; preds = %1
%16 = tail call i32* @DisplayOfScreen(i32* %0)
%17 = bitcast i64* %2 to i8*
%18 = bitcast i64* %3 to i8*
%19 = bitcast i64** %4 to i8*
%20 = bitcast i32* %5 to i8*
%21 = tail call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %34, label %23
23: ; preds = %15
%24 = load i8, i8* %21
%25 = icmp eq i8 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%28 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64* %6, i8* %7)
%29 = icmp eq i32 %28, 1
br i1 %29, label %78, label %30
30: ; preds = %26
%31 = call i32 @sscanf(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64* %6, i8* %7)
%32 = icmp eq i32 %31, 1
br i1 %32, label %78, label %33
33: ; preds = %30
br label %34
34: ; preds = %33, %23, %15
%35 = call i64 @RootWindowOfScreen(i32* %0)
store i64 %35, i64* @VirtualRootWindowOfScreen.root
%36 = load i32, i32* @False
%37 = call i32 @XInternAtom(i32* %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i64, i64* @VirtualRootWindowOfScreen.root
%39 = call i64 @XQueryTree(i32* %16, i64 %38, i64* %2, i64* %3, i64** %4, i32* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %77, label %41
41: ; preds = %34
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, 0
br i1 %43, label %71, label %44
44: ; preds = %41
%45 = bitcast i32* %8 to i8*
%46 = bitcast i32* %9 to i8*
%47 = bitcast i64* %10 to i8*
%48 = bitcast i64* %11 to i8*
%49 = bitcast i64** %12 to i8*
%50 = bitcast i64** %12 to i8**
br label %51
51: ; preds = %44, %66
%52 = phi i64 [ 0, %44 ], [ %67, %66 ]
store i64* null, i64** %12
%53 = load i64*, i64** %4
%54 = getelementptr inbounds i64, i64* %53, i64 %52
%55 = load i64, i64* %54
%56 = load i32, i32* @False
%57 = load i32, i32* @XA_WINDOW
%58 = call i64 @XGetWindowProperty(i32* %16, i64 %55, i32 %37, i32 0, i32 1, i32 %56, i32 %57, i32* %8, i32* %9, i64* %10, i64* %11, i8** %50)
%59 = load i64, i64* @Success
%60 = icmp eq i64 %58, %59
%61 = load i64*, i64** %12
%62 = icmp ne i64* %61, null
%63 = select i1 %60, i1 %62, i1 false
br i1 %63, label %64, label %66
64: ; preds = %51
%65 = load i64, i64* %61
store i64 %65, i64* @VirtualRootWindowOfScreen.root
br label %71
66: ; preds = %51
%67 = add nuw nsw i64 %52, 1
%68 = load i32, i32* %5
%69 = zext i32 %68 to i64
%70 = icmp ult i64 %67, %69
br i1 %70, label %51, label %71
71: ; preds = %66, %41, %64
%72 = load i64*, i64** %4
%73 = icmp eq i64* %72, null
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = bitcast i64* %72 to i8*
%76 = call i32 @XFree(i8* %75)
br label %77
77: ; preds = %34, %74, %71
store i32* %0, i32** @VirtualRootWindowOfScreen.save_screen
br label %80
78: ; preds = %30, %26
%79 = load i64, i64* %6
store i64 %79, i64* @VirtualRootWindowOfScreen.root
store i32* %0, i32** @VirtualRootWindowOfScreen.save_screen
br label %82
80: ; preds = %77, %1
%81 = load i64, i64* @VirtualRootWindowOfScreen.root
br label %82
82: ; preds = %78, %80
%83 = phi i64 [ %79, %78 ], [ %81, %80 ]
ret i64 %83
}