#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__ {
  int width;
  int height;
};
typedef TYPE_1__ XWindowAttributes;
typedef scalar_t__ Window;
typedef scalar_t__ Status;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
scalar_t__ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ StructureNotifyMask;
scalar_t__ XGetWindowAttributes(int /*<<< orphan*/ *, scalar_t__, TYPE_1__ *);
int /*<<< orphan*/ XMapWindow(int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XSelectInput(int /*<<< orphan*/ *, scalar_t__,
                                int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
Display *prepare_display(int *width, int *height) {
  Display *display = XOpenDisplay(NULL);
  if (!display) {
    fprintf(stderr, "Could not open display.\n");
    exit(1);
  }
  Window root = DefaultRootWindow(display);
  if (root < 0) {
    fprintf(stderr, "Unable to find default root window.\n");
    exit(2);
  }
  XSelectInput(display, root, StructureNotifyMask);
  XMapWindow(display, root);
  XWindowAttributes xwAttr;
  Status ret = XGetWindowAttributes(display, root, &xwAttr);
  if (ret == 0) {
    fprintf(stderr, "Unable to query current screen resolution.\n");
    exit(3);
  }
  *width = xwAttr.width;
  *height = xwAttr.height;
  return display;
}


x86 assembly (clang, -O0):
<prepare_display>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <XOpenDisplay@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <prepare_display+0x47>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <DefaultRootWindow@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ee <prepare_display+0x7e>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
call   1060 <XSelectInput@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <XMapWindow@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <XGetWindowAttributes@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1250 <prepare_display+0xe0>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_display>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %edi,%edi
call   1040 <XOpenDisplay@plt>
test   %rax,%rax
je     11eb <prepare_display+0x7b>
mov    %rax,%r12
mov    %rax,%rdi
call   1030 <DefaultRootWindow@plt>
test   %rax,%rax
js     120a <prepare_display+0x9a>
mov    %rax,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <XSelectInput@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <XMapWindow@plt>
mov    %rsp,%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <XGetWindowAttributes@plt>
test   %rax,%rax
je     1229 <prepare_display+0xb9>
mov    (%rsp),%eax
mov    %eax,(%r15)
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Could not open display.\0A\00"
@.str.1 = [37 x i8] c"Unable to find default root window.\0A\00"
@StructureNotifyMask = global i32 0
@.str.2 = [44 x i8] c"Unable to query current screen resolution.\0A\00"
define i32* @prepare_display(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca %struct.TYPE_3__
%8 = alloca i64
store i32* %0, i32** %3
store i32* %1, i32** %4
%9 = call i32* @XOpenDisplay(i32* null)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32*, i32** %5
%18 = call i64 @DefaultRootWindow(i32* %17)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 2)
unreachable
25: ; preds = %16
%26 = load i32*, i32** %5
%27 = load i64, i64* %6
%28 = load i32, i32* @StructureNotifyMask
%29 = call i32 @XSelectInput(i32* %26, i64 %27, i32 %28)
%30 = load i32*, i32** %5
%31 = load i64, i64* %6
%32 = call i32 @XMapWindow(i32* %30, i64 %31)
%33 = load i32*, i32** %5
%34 = load i64, i64* %6
%35 = call i64 @XGetWindowAttributes(i32* %33, i64 %34, %struct.TYPE_3__* %7)
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %25
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 3)
unreachable
42: ; preds = %25
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i32*, i32** %3
store i32 %44, i32* %45
%46 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load i32*, i32** %4
store i32 %47, i32* %48
%49 = load i32*, i32** %5
ret i32* %49
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Could not open display.\0A\00"
@.str.1 = [37 x i8] c"Unable to find default root window.\0A\00"
@StructureNotifyMask = global i32 0
@.str.2 = [44 x i8] c"Unable to query current screen resolution.\0A\00"
define i32* @prepare_display(i32* nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.TYPE_3__
%4 = tail call i32* @XOpenDisplay(i32* null)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i64 @DefaultRootWindow(i32* %4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %10
%18 = load i32, i32* @StructureNotifyMask
%19 = tail call i32 @XSelectInput(i32* %4, i64 %11, i32 %18)
%20 = tail call i32 @XMapWindow(i32* %4, i64 %11)
%21 = bitcast %struct.TYPE_3__* %3 to i8*
%22 = call i64 @XGetWindowAttributes(i32* %4, i64 %11, %struct.TYPE_3__* %3)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 3)
unreachable
28: ; preds = %17
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %0
%31 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 1
%32 = load i32, i32* %31
store i32 %32, i32* %1
ret i32* %4
}